1 /*
   2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jfr/leakprofiler/leakProfiler.hpp"
  27 #include "jfr/leakprofiler/startOperation.hpp"
  28 #include "jfr/leakprofiler/stopOperation.hpp"
  29 #include "jfr/leakprofiler/checkpoint/eventEmitter.hpp"
  30 #include "jfr/leakprofiler/sampling/objectSampler.hpp"
  31 #include "jfr/recorder/service/jfrOptionSet.hpp"
  32 #include "logging/log.hpp"
  33 #include "memory/iterator.hpp"
  34 #include "runtime/thread.inline.hpp"
  35 #include "runtime/vmThread.hpp"
  36 
  37 bool LeakProfiler::is_running() {
  38   return ObjectSampler::is_created();
  39 }
  40 
  41 bool LeakProfiler::start(int sample_count) {
  42   if (is_running()) {
  43     return true;
  44   }
  45 
  46   // Allows user to disable leak profiler on command line by setting queue size to zero.
  47   if (sample_count == 0) {
  48     return false;
  49   }
  50 
  51   if (UseZGC) {
  52     log_warning(jfr)("LeakProfiler is currently not supported in combination with ZGC");
  53     return false;
  54   }
  55 
  56   assert(!is_running(), "invariant");
  57   assert(sample_count > 0, "invariant");
  58 
  59   // schedule the safepoint operation for installing the object sampler
  60   StartOperation op(sample_count);
  61   VMThread::execute(&op);
  62 
  63   if (!is_running()) {
  64     log_trace(jfr, system)("Object sampling could not be started because the sampler could not be allocated");
  65     return false;
  66   }
  67   assert(is_running(), "invariant");
  68   log_trace(jfr, system)("Object sampling started");
  69   return true;
  70 }
  71 
  72 bool LeakProfiler::stop() {
  73   if (!is_running()) {
  74     return false;
  75   }
  76 
  77   // schedule the safepoint operation for uninstalling and destroying the object sampler
  78   StopOperation op;
  79   VMThread::execute(&op);
  80 
  81   assert(!is_running(), "invariant");
  82   log_trace(jfr, system)("Object sampling stopped");
  83   return true;
  84 }
  85 
  86 void LeakProfiler::emit_events(int64_t cutoff_ticks, bool emit_all) {
  87   if (!is_running()) {
  88     return;
  89   }
  90   // exclusive access to object sampler instance
  91   ObjectSampler* const sampler = ObjectSampler::acquire();
  92   assert(sampler != NULL, "invariant");
  93   EventEmitter::emit(sampler, cutoff_ticks, emit_all);
  94   ObjectSampler::release();
  95 }
  96 
  97 void LeakProfiler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
  98   assert(SafepointSynchronize::is_at_safepoint(),
  99     "Leak Profiler::oops_do(...) may only be called during safepoint");
 100   if (is_running()) {
 101     ObjectSampler::oops_do(is_alive, f);
 102   }
 103 }
 104 
 105 void LeakProfiler::sample(HeapWord* object, size_t size, JavaThread* thread) {
 106   assert(is_running(), "invariant");
 107   assert(thread != NULL, "invariant");
 108   assert(thread->thread_state() == _thread_in_vm, "invariant");
 109 
 110   // exclude compiler threads and code sweeper thread
 111   if (thread->is_hidden_from_external_view()) {
 112     return;
 113   }
 114 
 115   ObjectSampler::sample(object, size, thread);
 116 }