1 /*
  2  * Copyright (c) 2014, 2025, 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 "jfr/leakprofiler/leakProfiler.hpp"
 26 #include "jfr/leakprofiler/startOperation.hpp"
 27 #include "jfr/leakprofiler/stopOperation.hpp"
 28 #include "jfr/leakprofiler/checkpoint/eventEmitter.hpp"
 29 #include "jfr/leakprofiler/sampling/objectSampler.hpp"
 30 #include "jfr/recorder/service/jfrOptionSet.hpp"
 31 #include "logging/log.hpp"
 32 #include "memory/iterator.hpp"
 33 #include "runtime/javaThread.inline.hpp"
 34 #include "runtime/vmThread.hpp"
 35 
 36 bool LeakProfiler::is_supported() {
 37   if (UseShenandoahGC) {
 38     // Leak Profiler uses mark words in the ways that might interfere
 39     // with concurrent GC uses of them. This affects Shenandoah.
 40     return false;
 41   }
 42   return true;
 43 }
 44 
 45 bool LeakProfiler::is_running() {
 46   return ObjectSampler::is_created();
 47 }
 48 
 49 bool LeakProfiler::start(int sample_count) {
 50   if (is_running()) {
 51     return true;
 52   }
 53 
 54   // Allows user to disable leak profiler on command line by setting queue size to zero.
 55   if (sample_count == 0) {
 56     return false;
 57   }
 58 
 59   // Exit cleanly if not supported
 60   if (!is_supported()) {
 61     log_trace(jfr, system)("Object sampling is not supported");
 62     return false;
 63   }
 64 
 65   assert(!is_running(), "invariant");
 66   assert(sample_count > 0, "invariant");
 67 
 68   // schedule the safepoint operation for installing the object sampler
 69   StartOperation op(sample_count);
 70   VMThread::execute(&op);
 71 
 72   if (!is_running()) {
 73     log_trace(jfr, system)("Object sampling could not be started because the sampler could not be allocated");
 74     return false;
 75   }
 76   assert(is_running(), "invariant");
 77   log_trace(jfr, system)("Object sampling started");
 78   return true;
 79 }
 80 
 81 bool LeakProfiler::stop() {
 82   if (!is_running()) {
 83     return false;
 84   }
 85 
 86   // schedule the safepoint operation for uninstalling and destroying the object sampler
 87   StopOperation op;
 88   VMThread::execute(&op);
 89 
 90   assert(!is_running(), "invariant");
 91   log_trace(jfr, system)("Object sampling stopped");
 92   return true;
 93 }
 94 
 95 void LeakProfiler::emit_events(int64_t cutoff_ticks, bool emit_all, bool skip_bfs) {
 96   if (!is_running()) {
 97     return;
 98   }
 99   // exclusive access to object sampler instance
100   ObjectSampler* const sampler = ObjectSampler::acquire();
101   assert(sampler != nullptr, "invariant");
102   EventEmitter::emit(sampler, cutoff_ticks, emit_all, skip_bfs);
103   ObjectSampler::release();
104 }
105 
106 void LeakProfiler::sample(HeapWord* object, size_t size, JavaThread* thread) {
107   assert(is_running(), "invariant");
108   assert(thread != nullptr, "invariant");
109   assert(thread->thread_state() == _thread_in_vm, "invariant");
110 
111   // exclude compiler threads
112   if (thread->is_hidden_from_external_view()) {
113     return;
114   }
115 
116   ObjectSampler::sample(object, size, thread);
117 }