src/hotspot/share/jfr/leakprofiler/leakProfiler.cpp
author dholmes
Sat, 23 Jun 2018 01:32:41 -0400
changeset 50735 2f2af62dfac7
parent 50673 e7da80712449
child 52925 9c18c9d839d3
permissions -rw-r--r--
8010319: Implementation of JEP 181: Nest-Based Access Control Reviewed-by: alanb, psandoz, mchung, coleenp, acorn, mcimadamore, forax, jlahoda, sspitsyn, abuckley Contributed-by: alex.buckley@oracle.com, maurizio.mimadamore@oracle.com, mandy.chung@oracle.com, tobias.hartmann@oracle.com, david.holmes@oracle.com, vladimir.x.ivanov@oracle.com, karen.kinnear@oracle.com, vladimir.kozlov@oracle.com, john.r.rose@oracle.com, daniel.smith@oracle.com, serguei.spitsyn@oracle.com, kumardotsrinivasan@gmail.com, boris.ulasevich@bell-sw.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
 * accompanied this code).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
 * questions.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
#include "precompiled.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
#include "jfr/leakprofiler/emitEventOperation.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
#include "jfr/leakprofiler/leakProfiler.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
#include "jfr/leakprofiler/startOperation.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
#include "jfr/leakprofiler/stopOperation.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
#include "jfr/leakprofiler/sampling/objectSampler.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
#include "jfr/recorder/service/jfrOptionSet.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
#include "memory/iterator.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
#include "oops/oop.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
#include "runtime/atomic.hpp"
50429
83aec1d357d4 8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents: 50122
diff changeset
    35
#include "runtime/orderAccess.hpp"
50122
d2bcbcf0d3d9 8203278: AArch64/PPC64 build failures after JDK-8199712 (Flight Recorder)
shade
parents: 50113
diff changeset
    36
#include "runtime/thread.inline.hpp"
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
#include "runtime/vmThread.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
#include "utilities/ostream.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
// Only to be updated during safepoint
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
ObjectSampler* LeakProfiler::_object_sampler = NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
static volatile jbyte suspended = 0;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
bool LeakProfiler::start(jint sample_count) {
50673
e7da80712449 8205338: ZGC: LeakProfiler not yet supported
pliden
parents: 50429
diff changeset
    45
  if (UseZGC) {
e7da80712449 8205338: ZGC: LeakProfiler not yet supported
pliden
parents: 50429
diff changeset
    46
    log_warning(jfr)("LeakProfiler is currently not supported in combination with ZGC");
e7da80712449 8205338: ZGC: LeakProfiler not yet supported
pliden
parents: 50429
diff changeset
    47
    return false;
e7da80712449 8205338: ZGC: LeakProfiler not yet supported
pliden
parents: 50429
diff changeset
    48
  }
e7da80712449 8205338: ZGC: LeakProfiler not yet supported
pliden
parents: 50429
diff changeset
    49
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
  if (_object_sampler != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
    // already started
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
    return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
  // Allows user to disable leak profiler on command line by setting queue size to zero.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
  if (sample_count > 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
    StartOperation op(sample_count);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
    VMThread::execute(&op);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
    return _object_sampler != NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
  return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
bool LeakProfiler::stop() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
  if (_object_sampler == NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
    // already stopped/not started
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
    return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
  StopOperation op;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
  VMThread::execute(&op);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
  return _object_sampler == NULL;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
void LeakProfiler::emit_events(jlong cutoff_ticks, bool emit_all) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
  if (!is_running()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
  EmitEventOperation op(cutoff_ticks, emit_all);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
  VMThread::execute(&op);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
void LeakProfiler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
  assert(SafepointSynchronize::is_at_safepoint(),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
    "Leak Profiler::oops_do(...) may only be called during safepoint");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
  if (_object_sampler != NULL) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
    _object_sampler->oops_do(is_alive, f);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
void LeakProfiler::sample(HeapWord* object,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
                          size_t size,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
                          JavaThread* thread) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
  assert(is_running(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
  assert(thread != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
  assert(thread->thread_state() == _thread_in_vm, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
  // exclude compiler threads and code sweeper thread
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
  if (thread->is_hidden_from_external_view()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
    return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
  _object_sampler->add(object, size, thread);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
ObjectSampler* LeakProfiler::object_sampler() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
  assert(is_suspended() || SafepointSynchronize::is_at_safepoint(),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
    "Leak Profiler::object_sampler() may only be called during safepoint");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
  return _object_sampler;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
void LeakProfiler::set_object_sampler(ObjectSampler* object_sampler) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
  assert(SafepointSynchronize::is_at_safepoint(),
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
    "Leak Profiler::set_object_sampler() may only be called during safepoint");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
  _object_sampler = object_sampler;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
bool LeakProfiler::is_running() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
  return _object_sampler != NULL && !suspended;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
bool LeakProfiler::is_suspended() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
  return _object_sampler != NULL && suspended;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
void LeakProfiler::resume() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
  assert(is_suspended(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
  OrderAccess::storestore();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
  Atomic::store((jbyte)0, &suspended);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
  assert(is_running(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
void LeakProfiler::suspend() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
  assert(SafepointSynchronize::is_at_safepoint(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
  assert(_object_sampler != NULL, "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
  assert(!is_suspended(), "invariant");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
  suspended = (jbyte)1; // safepoint visible
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
}