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/checkpoint/eventEmitter.hpp"
 26 #include "jfr/leakprofiler/leakProfiler.hpp"
 27 #include "jfr/leakprofiler/sampling/objectSampler.hpp"
 28 #include "jfr/leakprofiler/startOperation.hpp"
 29 #include "jfr/leakprofiler/stopOperation.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 || UseZGC) {
 38     // Leak Profiler uses mark words in the ways that might interfere
 39     // with concurrent GC uses of them. This affects Shenandoah.
 40     //
 41     // Generational ZGC only does weak reference processing in the old generation.
 42     // All objects that would usually die, because we are sampling stuff
 43     // that immediately becomes garbage, will be artificially kept alive
 44     // until an old-generation collection. This incurs a significant
 45     // performance hit by causing allocation stalls.
 46     return false;
 47   }
 48   return true;
 49 }
 50 
 51 bool LeakProfiler::is_running() {
 52   return ObjectSampler::is_created();
 53 }
 54 
 55 bool LeakProfiler::start(int sample_count) {
 56   if (is_running()) {
 57     return true;
 58   }
 59 
 60   // Allows user to disable leak profiler on command line by setting queue size to zero.
 61   if (sample_count == 0) {
 62     return false;
 63   }
 64 
 65   // Exit cleanly if not supported
 66   if (!is_supported()) {
 67     log_info(jfr, system)("jdk.OldObjectSample event is currently not supported for %s.",
 68       UseShenandoahGC ? "ShenandoahGC" : "ZGC");
 69     return false;
 70   }
 71 
 72   assert(!is_running(), "invariant");
 73   assert(sample_count > 0, "invariant");
 74 
 75   // schedule the safepoint operation for installing the object sampler
 76   StartOperation op(sample_count);
 77   VMThread::execute(&op);
 78 
 79   if (!is_running()) {
 80     log_trace(jfr, system)("Object sampling could not be started because the sampler could not be allocated");
 81     return false;
 82   }
 83   assert(is_running(), "invariant");
 84   log_trace(jfr, system)("Object sampling started");
 85   return true;
 86 }
 87 
 88 bool LeakProfiler::stop() {
 89   if (!is_running()) {
 90     return false;
 91   }
 92 
 93   // schedule the safepoint operation for uninstalling and destroying the object sampler
 94   StopOperation op;
 95   VMThread::execute(&op);
 96 
 97   assert(!is_running(), "invariant");
 98   log_trace(jfr, system)("Object sampling stopped");
 99   return true;
100 }
101 
102 void LeakProfiler::emit_events(int64_t cutoff_ticks, bool emit_all, bool skip_bfs) {
103   if (!is_running()) {
104     return;
105   }
106   // exclusive access to object sampler instance
107   ObjectSampler* const sampler = ObjectSampler::acquire();
108   assert(sampler != nullptr, "invariant");
109   EventEmitter::emit(sampler, cutoff_ticks, emit_all, skip_bfs);
110   ObjectSampler::release();
111 }
112 
113 void LeakProfiler::sample(HeapWord* object, size_t size, JavaThread* thread) {
114   assert(is_running(), "invariant");
115   assert(thread != nullptr, "invariant");
116   assert(thread->thread_state() == _thread_in_vm, "invariant");
117 
118   // exclude compiler threads
119   if (thread->is_hidden_from_external_view()) {
120     return;
121   }
122 
123   ObjectSampler::sample(object, size, thread);
124 }