src/share/vm/jfr/leakprofiler/leakProfiler.cpp

changeset 9858
b985cbb00e68
child 9885
8e875c964f41
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/jfr/leakprofiler/leakProfiler.cpp	Mon Aug 12 18:30:40 2019 +0300
     1.3 @@ -0,0 +1,132 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "jfr/leakprofiler/emitEventOperation.hpp"
    1.30 +#include "jfr/leakprofiler/leakProfiler.hpp"
    1.31 +#include "jfr/leakprofiler/startOperation.hpp"
    1.32 +#include "jfr/leakprofiler/stopOperation.hpp"
    1.33 +#include "jfr/leakprofiler/sampling/objectSampler.hpp"
    1.34 +#include "jfr/recorder/service/jfrOptionSet.hpp"
    1.35 +#include "memory/iterator.hpp"
    1.36 +#include "oops/oop.hpp"
    1.37 +#include "runtime/atomic.hpp"
    1.38 +#include "runtime/orderAccess.hpp"
    1.39 +#include "runtime/thread.inline.hpp"
    1.40 +#include "runtime/vmThread.hpp"
    1.41 +#include "utilities/ostream.hpp"
    1.42 +
    1.43 +// Only to be updated during safepoint
    1.44 +ObjectSampler* LeakProfiler::_object_sampler = NULL;
    1.45 +
    1.46 +static volatile jbyte suspended = 0;
    1.47 +bool LeakProfiler::start(jint sample_count) {
    1.48 +  if (_object_sampler != NULL) {
    1.49 +    // already started
    1.50 +    return true;
    1.51 +  }
    1.52 +  // Allows user to disable leak profiler on command line by setting queue size to zero.
    1.53 +  if (sample_count > 0) {
    1.54 +    StartOperation op(sample_count);
    1.55 +    VMThread::execute(&op);
    1.56 +    return _object_sampler != NULL;
    1.57 +  }
    1.58 +  return false;
    1.59 +}
    1.60 +
    1.61 +bool LeakProfiler::stop() {
    1.62 +  if (_object_sampler == NULL) {
    1.63 +    // already stopped/not started
    1.64 +    return true;
    1.65 +  }
    1.66 +  StopOperation op;
    1.67 +  VMThread::execute(&op);
    1.68 +  return _object_sampler == NULL;
    1.69 +}
    1.70 +
    1.71 +void LeakProfiler::emit_events(jlong cutoff_ticks, bool emit_all) {
    1.72 +  if (!is_running()) {
    1.73 +    return;
    1.74 +  }
    1.75 +  EmitEventOperation op(cutoff_ticks, emit_all);
    1.76 +  VMThread::execute(&op);
    1.77 +}
    1.78 +
    1.79 +void LeakProfiler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
    1.80 +  assert(SafepointSynchronize::is_at_safepoint(),
    1.81 +    "Leak Profiler::oops_do(...) may only be called during safepoint");
    1.82 +
    1.83 +  if (_object_sampler != NULL) {
    1.84 +    _object_sampler->oops_do(is_alive, f);
    1.85 +  }
    1.86 +}
    1.87 +
    1.88 +void LeakProfiler::sample(HeapWord* object,
    1.89 +                          size_t size,
    1.90 +                          JavaThread* thread) {
    1.91 +  assert(is_running(), "invariant");
    1.92 +  assert(thread != NULL, "invariant");
    1.93 +  assert(thread->thread_state() == _thread_in_vm, "invariant");
    1.94 +
    1.95 +  // exclude compiler threads and code sweeper thread
    1.96 +  if (thread->is_hidden_from_external_view()) {
    1.97 +    return;
    1.98 +  }
    1.99 +
   1.100 +  _object_sampler->add(object, size, thread);
   1.101 +}
   1.102 +
   1.103 +ObjectSampler* LeakProfiler::object_sampler() {
   1.104 +  assert(is_suspended() || SafepointSynchronize::is_at_safepoint(),
   1.105 +    "Leak Profiler::object_sampler() may only be called during safepoint");
   1.106 +  return _object_sampler;
   1.107 +}
   1.108 +
   1.109 +void LeakProfiler::set_object_sampler(ObjectSampler* object_sampler) {
   1.110 +  assert(SafepointSynchronize::is_at_safepoint(),
   1.111 +    "Leak Profiler::set_object_sampler() may only be called during safepoint");
   1.112 +  _object_sampler = object_sampler;
   1.113 +}
   1.114 +
   1.115 +bool LeakProfiler::is_running() {
   1.116 +  return _object_sampler != NULL && !suspended;
   1.117 +}
   1.118 +
   1.119 +bool LeakProfiler::is_suspended() {
   1.120 +  return _object_sampler != NULL && suspended;
   1.121 +}
   1.122 +
   1.123 +void LeakProfiler::resume() {
   1.124 +  assert(is_suspended(), "invariant");
   1.125 +  OrderAccess::storestore();
   1.126 +  Atomic::store((jbyte)0, &suspended);
   1.127 +  assert(is_running(), "invariant");
   1.128 +}
   1.129 +
   1.130 +void LeakProfiler::suspend() {
   1.131 +  assert(SafepointSynchronize::is_at_safepoint(), "invariant");
   1.132 +  assert(_object_sampler != NULL, "invariant");
   1.133 +  assert(!is_suspended(), "invariant");
   1.134 +  suspended = (jbyte)1; // safepoint visible
   1.135 +}

mercurial