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 (UseCompactObjectHeaders || UseShenandoahGC) {
38 // 1. With a 32-bit mark word in Lilliput2, we don't have enough unused
39 // bits to store edge index information in the mark word
40 // 2. Even without compressed object headers, with Shenandoah, we don't
41 // have enough free bits in the mark word because of needing that
42 // space for forwarding pointers in the evacuation & update refs phase.
43 return false;
44 }
45 return true;
46 }
47
48 bool LeakProfiler::is_running() {
49 return ObjectSampler::is_created();
50 }
51
52 bool LeakProfiler::start(int sample_count) {
53 if (is_running()) {
54 return true;
55 }
56
57 // Allows user to disable leak profiler on command line by setting queue size to zero.
58 if (sample_count == 0) {
59 return false;
60 }
61
62 // Exit cleanly if not supported
63 if (!is_supported()) {
64 log_trace(jfr, system)("Object sampling is not supported");
65 return false;
66 }
67
68 assert(!is_running(), "invariant");
69 assert(sample_count > 0, "invariant");
70
71 // schedule the safepoint operation for installing the object sampler
72 StartOperation op(sample_count);
73 VMThread::execute(&op);
74
75 if (!is_running()) {
76 log_trace(jfr, system)("Object sampling could not be started because the sampler could not be allocated");
77 return false;
78 }
79 assert(is_running(), "invariant");
80 log_trace(jfr, system)("Object sampling started");
81 return true;
82 }
83
84 bool LeakProfiler::stop() {
85 if (!is_running()) {
86 return false;
87 }
88
89 // schedule the safepoint operation for uninstalling and destroying the object sampler
90 StopOperation op;
91 VMThread::execute(&op);
92
93 assert(!is_running(), "invariant");
94 log_trace(jfr, system)("Object sampling stopped");
95 return true;
96 }
97
98 void LeakProfiler::emit_events(int64_t cutoff_ticks, bool emit_all, bool skip_bfs) {
99 if (!is_running()) {
100 return;
101 }
102 // exclusive access to object sampler instance
103 ObjectSampler* const sampler = ObjectSampler::acquire();
104 assert(sampler != nullptr, "invariant");
105 EventEmitter::emit(sampler, cutoff_ticks, emit_all, skip_bfs);
106 ObjectSampler::release();
107 }
108
109 void LeakProfiler::sample(HeapWord* object, size_t size, JavaThread* thread) {
110 assert(is_running(), "invariant");
111 assert(thread != nullptr, "invariant");
112 assert(thread->thread_state() == _thread_in_vm, "invariant");
113
114 // exclude compiler threads
115 if (thread->is_hidden_from_external_view()) {
116 return;
117 }
118
119 ObjectSampler::sample(object, size, thread);
120 }