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