1 /*
2 * Copyright (c) 2017, 2021, 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/jfrEvents.hpp"
27 #include "jfr/leakprofiler/sampling/objectSample.hpp"
28 #include "jfr/leakprofiler/sampling/objectSampler.hpp"
29 #include "jfr/leakprofiler/sampling/sampleList.hpp"
30 #include "jfr/leakprofiler/sampling/samplePriorityQueue.hpp"
31 #include "jfr/recorder/jfrEventSetting.inline.hpp"
32 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
33 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
34 #include "jfr/support/jfrThreadLocal.hpp"
35 #include "jfr/utilities/jfrTryLock.hpp"
36 #include "logging/log.hpp"
37 #include "memory/universe.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "runtime/atomic.hpp"
40 #include "runtime/orderAccess.hpp"
41 #include "runtime/safepoint.hpp"
42 #include "runtime/thread.hpp"
43
44 static ObjectSampler* _instance = NULL;
45
instance()46 static ObjectSampler& instance() {
47 assert(_instance != NULL, "invariant");
48 return *_instance;
49 }
50
ObjectSampler(size_t size)51 ObjectSampler::ObjectSampler(size_t size) :
52 _priority_queue(new SamplePriorityQueue(size)),
53 _list(new SampleList(size)),
54 _last_sweep(JfrTicks::now()),
55 _total_allocated(0),
56 _threshold(0),
57 _size(size),
58 _dead_samples(false) {}
59
~ObjectSampler()60 ObjectSampler::~ObjectSampler() {
61 delete _priority_queue;
62 _priority_queue = NULL;
63 delete _list;
64 _list = NULL;
65 }
66
create(size_t size)67 bool ObjectSampler::create(size_t size) {
68 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
69 assert(_instance == NULL, "invariant");
70 _instance = new ObjectSampler(size);
71 return _instance != NULL;
72 }
73
is_created()74 bool ObjectSampler::is_created() {
75 return _instance != NULL;
76 }
77
sampler()78 ObjectSampler* ObjectSampler::sampler() {
79 assert(is_created(), "invariant");
80 return _instance;
81 }
82
destroy()83 void ObjectSampler::destroy() {
84 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
85 if (_instance != NULL) {
86 ObjectSampler* const sampler = _instance;
87 _instance = NULL;
88 delete sampler;
89 }
90 }
91
92 static volatile int _lock = 0;
93
acquire()94 ObjectSampler* ObjectSampler::acquire() {
95 assert(is_created(), "invariant");
96 while (Atomic::cmpxchg(1, &_lock, 0) == 1) {}
97 return _instance;
98 }
99
release()100 void ObjectSampler::release() {
101 assert(is_created(), "invariant");
102 OrderAccess::fence();
103 _lock = 0;
104 }
105
get_thread_id(JavaThread * thread)106 static traceid get_thread_id(JavaThread* thread) {
107 assert(thread != NULL, "invariant");
108 if (thread->threadObj() == NULL) {
109 return 0;
110 }
111 const JfrThreadLocal* const tl = thread->jfr_thread_local();
112 assert(tl != NULL, "invariant");
113 if (!tl->has_thread_blob()) {
114 JfrCheckpointManager::create_thread_blob(thread);
115 }
116 assert(tl->has_thread_blob(), "invariant");
117 return tl->thread_id();
118 }
119
120 class RecordStackTrace {
121 private:
122 JavaThread* _jt;
123 bool _enabled;
124 public:
RecordStackTrace(JavaThread * jt)125 RecordStackTrace(JavaThread* jt) : _jt(jt),
126 _enabled(JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) {
127 if (_enabled) {
128 JfrStackTraceRepository::record_for_leak_profiler(jt);
129 }
130 }
~RecordStackTrace()131 ~RecordStackTrace() {
132 if (_enabled) {
133 _jt->jfr_thread_local()->clear_cached_stack_trace();
134 }
135 }
136 };
137
sample(HeapWord * obj,size_t allocated,JavaThread * thread)138 void ObjectSampler::sample(HeapWord* obj, size_t allocated, JavaThread* thread) {
139 assert(thread != NULL, "invariant");
140 assert(is_created(), "invariant");
141 const traceid thread_id = get_thread_id(thread);
142 if (thread_id == 0) {
143 return;
144 }
145 RecordStackTrace rst(thread);
146 // try enter critical section
147 JfrTryLock tryLock(&_lock);
148 if (!tryLock.has_lock()) {
149 log_trace(jfr, oldobject, sampling)("Skipping old object sample due to lock contention");
150 return;
151 }
152 instance().add(obj, allocated, thread_id, thread);
153 }
154
add(HeapWord * obj,size_t allocated,traceid thread_id,JavaThread * thread)155 void ObjectSampler::add(HeapWord* obj, size_t allocated, traceid thread_id, JavaThread* thread) {
156 assert(obj != NULL, "invariant");
157 assert(thread_id != 0, "invariant");
158 assert(thread != NULL, "invariant");
159 assert(thread->jfr_thread_local()->has_thread_blob(), "invariant");
160
161 if (_dead_samples) {
162 scavenge();
163 assert(!_dead_samples, "invariant");
164 }
165
166 _total_allocated += allocated;
167 const size_t span = _total_allocated - _priority_queue->total();
168 ObjectSample* sample;
169 if ((size_t)_priority_queue->count() == _size) {
170 assert(_list->count() == _size, "invariant");
171 const ObjectSample* peek = _priority_queue->peek();
172 if (peek->span() > span) {
173 // quick reject, will not fit
174 return;
175 }
176 sample = _list->reuse(_priority_queue->pop());
177 } else {
178 sample = _list->get();
179 }
180
181 assert(sample != NULL, "invariant");
182 sample->set_thread_id(thread_id);
183
184 const JfrThreadLocal* const tl = thread->jfr_thread_local();
185 sample->set_thread(tl->thread_blob());
186
187 const unsigned int stacktrace_hash = tl->cached_stack_trace_hash();
188 if (stacktrace_hash != 0) {
189 sample->set_stack_trace_id(tl->cached_stack_trace_id());
190 sample->set_stack_trace_hash(stacktrace_hash);
191 }
192
193 sample->set_span(allocated);
194 sample->set_object((oop)obj);
195 sample->set_allocated(allocated);
196 sample->set_allocation_time(JfrTicks::now());
197 sample->set_heap_used_at_last_gc(Universe::get_heap_used_at_last_gc());
198 _priority_queue->push(sample);
199 }
200
scavenge()201 void ObjectSampler::scavenge() {
202 ObjectSample* current = _list->last();
203 while (current != NULL) {
204 ObjectSample* next = current->next();
205 if (current->is_dead()) {
206 remove_dead(current);
207 }
208 current = next;
209 }
210 _dead_samples = false;
211 }
212
remove_dead(ObjectSample * sample)213 void ObjectSampler::remove_dead(ObjectSample* sample) {
214 assert(sample != NULL, "invariant");
215 assert(sample->is_dead(), "invariant");
216 ObjectSample* const previous = sample->prev();
217 // push span on to previous
218 if (previous != NULL) {
219 _priority_queue->remove(previous);
220 previous->add_span(sample->span());
221 _priority_queue->push(previous);
222 }
223 _priority_queue->remove(sample);
224 _list->release(sample);
225 }
226
oops_do(BoolObjectClosure * is_alive,OopClosure * f)227 void ObjectSampler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
228 assert(is_created(), "invariant");
229 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
230 ObjectSampler& sampler = instance();
231 ObjectSample* current = sampler._list->last();
232 while (current != NULL) {
233 ObjectSample* next = current->next();
234 if (!current->is_dead()) {
235 if (is_alive->do_object_b(current->object())) {
236 // The weakly referenced object is alive, update pointer
237 f->do_oop(const_cast<oop*>(current->object_addr()));
238 } else {
239 current->set_dead();
240 sampler._dead_samples = true;
241 }
242 }
243 current = next;
244 }
245 sampler._last_sweep = JfrTicks::now();
246 }
247
last() const248 ObjectSample* ObjectSampler::last() const {
249 return _list->last();
250 }
251
first() const252 const ObjectSample* ObjectSampler::first() const {
253 return _list->first();
254 }
255
last_resolved() const256 const ObjectSample* ObjectSampler::last_resolved() const {
257 return _list->last_resolved();
258 }
259
set_last_resolved(const ObjectSample * sample)260 void ObjectSampler::set_last_resolved(const ObjectSample* sample) {
261 _list->set_last_resolved(sample);
262 }
263
item_count() const264 int ObjectSampler::item_count() const {
265 return _priority_queue->count();
266 }
267
item_at(int index) const268 const ObjectSample* ObjectSampler::item_at(int index) const {
269 return _priority_queue->item_at(index);
270 }
271
item_at(int index)272 ObjectSample* ObjectSampler::item_at(int index) {
273 return const_cast<ObjectSample*>(
274 const_cast<const ObjectSampler*>(this)->item_at(index)
275 );
276 }
277
last_sweep() const278 const JfrTicks& ObjectSampler::last_sweep() const {
279 return _last_sweep;
280 }
281