1 | /* |
2 | * Copyright (c) 2017, 2018, 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 | |
46 | static ObjectSampler& instance() { |
47 | assert(_instance != NULL, "invariant" ); |
48 | return *_instance; |
49 | } |
50 | |
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 | |
60 | ObjectSampler::~ObjectSampler() { |
61 | delete _priority_queue; |
62 | _priority_queue = NULL; |
63 | delete _list; |
64 | _list = NULL; |
65 | } |
66 | |
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 | |
74 | bool ObjectSampler::is_created() { |
75 | return _instance != NULL; |
76 | } |
77 | |
78 | ObjectSampler* ObjectSampler::sampler() { |
79 | assert(is_created(), "invariant" ); |
80 | return _instance; |
81 | } |
82 | |
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 | |
94 | ObjectSampler* ObjectSampler::acquire() { |
95 | assert(is_created(), "invariant" ); |
96 | while (Atomic::cmpxchg(1, &_lock, 0) == 1) {} |
97 | return _instance; |
98 | } |
99 | |
100 | void ObjectSampler::release() { |
101 | assert(is_created(), "invariant" ); |
102 | OrderAccess::fence(); |
103 | _lock = 0; |
104 | } |
105 | |
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_checkpoint()) { |
114 | JfrCheckpointManager::create_thread_checkpoint(thread); |
115 | } |
116 | assert(tl->has_thread_checkpoint(), "invariant" ); |
117 | return tl->thread_id(); |
118 | } |
119 | |
120 | // Populates the thread local stack frames, but does not add them |
121 | // to the stacktrace repository (...yet, see stacktrace_id() below) |
122 | // |
123 | void ObjectSampler::fill_stacktrace(JfrStackTrace* stacktrace, JavaThread* thread) { |
124 | assert(stacktrace != NULL, "invariant" ); |
125 | assert(thread != NULL, "invariant" ); |
126 | if (JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) { |
127 | JfrStackTraceRepository::fill_stacktrace_for(thread, stacktrace, 0); |
128 | } |
129 | } |
130 | |
131 | // We were successful in acquiring the try lock and have been selected for adding a sample. |
132 | // Go ahead with installing our previously taken stacktrace into the stacktrace repository. |
133 | // |
134 | traceid ObjectSampler::stacktrace_id(const JfrStackTrace* stacktrace, JavaThread* thread) { |
135 | assert(stacktrace != NULL, "invariant" ); |
136 | assert(stacktrace->hash() != 0, "invariant" ); |
137 | const traceid stacktrace_id = JfrStackTraceRepository::add(stacktrace, thread); |
138 | thread->jfr_thread_local()->set_cached_stack_trace_id(stacktrace_id, stacktrace->hash()); |
139 | return stacktrace_id; |
140 | } |
141 | |
142 | void ObjectSampler::sample(HeapWord* obj, size_t allocated, JavaThread* thread) { |
143 | assert(thread != NULL, "invariant" ); |
144 | assert(is_created(), "invariant" ); |
145 | |
146 | const traceid thread_id = get_thread_id(thread); |
147 | if (thread_id == 0) { |
148 | return; |
149 | } |
150 | |
151 | const JfrThreadLocal* const tl = thread->jfr_thread_local(); |
152 | JfrStackTrace stacktrace(tl->stackframes(), tl->stackdepth()); |
153 | fill_stacktrace(&stacktrace, thread); |
154 | |
155 | // try enter critical section |
156 | JfrTryLock tryLock(&_lock); |
157 | if (!tryLock.has_lock()) { |
158 | log_trace(jfr, oldobject, sampling)("Skipping old object sample due to lock contention" ); |
159 | return; |
160 | } |
161 | |
162 | instance().add(obj, allocated, thread_id, &stacktrace, thread); |
163 | } |
164 | |
165 | void ObjectSampler::add(HeapWord* obj, size_t allocated, traceid thread_id, JfrStackTrace* stacktrace, JavaThread* thread) { |
166 | assert(stacktrace != NULL, "invariant" ); |
167 | assert(thread_id != 0, "invariant" ); |
168 | assert(thread != NULL, "invariant" ); |
169 | assert(thread->jfr_thread_local()->has_thread_checkpoint(), "invariant" ); |
170 | |
171 | if (_dead_samples) { |
172 | scavenge(); |
173 | assert(!_dead_samples, "invariant" ); |
174 | } |
175 | |
176 | _total_allocated += allocated; |
177 | const size_t span = _total_allocated - _priority_queue->total(); |
178 | ObjectSample* sample; |
179 | if ((size_t)_priority_queue->count() == _size) { |
180 | assert(_list->count() == _size, "invariant" ); |
181 | const ObjectSample* peek = _priority_queue->peek(); |
182 | if (peek->span() > span) { |
183 | // quick reject, will not fit |
184 | return; |
185 | } |
186 | sample = _list->reuse(_priority_queue->pop()); |
187 | } else { |
188 | sample = _list->get(); |
189 | } |
190 | |
191 | assert(sample != NULL, "invariant" ); |
192 | sample->set_thread_id(thread_id); |
193 | sample->set_thread_checkpoint(thread->jfr_thread_local()->thread_checkpoint()); |
194 | |
195 | const unsigned int stacktrace_hash = stacktrace->hash(); |
196 | if (stacktrace_hash != 0) { |
197 | sample->set_stack_trace_id(stacktrace_id(stacktrace, thread)); |
198 | sample->set_stack_trace_hash(stacktrace_hash); |
199 | } |
200 | |
201 | sample->set_span(allocated); |
202 | sample->set_object((oop)obj); |
203 | sample->set_allocated(allocated); |
204 | sample->set_allocation_time(JfrTicks::now()); |
205 | sample->set_heap_used_at_last_gc(Universe::get_heap_used_at_last_gc()); |
206 | _priority_queue->push(sample); |
207 | } |
208 | |
209 | void ObjectSampler::scavenge() { |
210 | ObjectSample* current = _list->last(); |
211 | while (current != NULL) { |
212 | ObjectSample* next = current->next(); |
213 | if (current->is_dead()) { |
214 | remove_dead(current); |
215 | } |
216 | current = next; |
217 | } |
218 | _dead_samples = false; |
219 | } |
220 | |
221 | void ObjectSampler::remove_dead(ObjectSample* sample) { |
222 | assert(sample != NULL, "invariant" ); |
223 | assert(sample->is_dead(), "invariant" ); |
224 | ObjectSample* const previous = sample->prev(); |
225 | // push span on to previous |
226 | if (previous != NULL) { |
227 | _priority_queue->remove(previous); |
228 | previous->add_span(sample->span()); |
229 | _priority_queue->push(previous); |
230 | } |
231 | _priority_queue->remove(sample); |
232 | _list->release(sample); |
233 | } |
234 | |
235 | void ObjectSampler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) { |
236 | assert(is_created(), "invariant" ); |
237 | assert(SafepointSynchronize::is_at_safepoint(), "invariant" ); |
238 | ObjectSampler& sampler = instance(); |
239 | ObjectSample* current = sampler._list->last(); |
240 | while (current != NULL) { |
241 | ObjectSample* next = current->next(); |
242 | if (!current->is_dead()) { |
243 | if (is_alive->do_object_b(current->object())) { |
244 | // The weakly referenced object is alive, update pointer |
245 | f->do_oop(const_cast<oop*>(current->object_addr())); |
246 | } else { |
247 | current->set_dead(); |
248 | sampler._dead_samples = true; |
249 | } |
250 | } |
251 | current = next; |
252 | } |
253 | sampler._last_sweep = JfrTicks::now(); |
254 | } |
255 | |
256 | const ObjectSample* ObjectSampler::last() const { |
257 | return _list->last(); |
258 | } |
259 | |
260 | const ObjectSample* ObjectSampler::first() const { |
261 | return _list->first(); |
262 | } |
263 | |
264 | const ObjectSample* ObjectSampler::last_resolved() const { |
265 | return _list->last_resolved(); |
266 | } |
267 | |
268 | void ObjectSampler::set_last_resolved(const ObjectSample* sample) { |
269 | _list->set_last_resolved(sample); |
270 | } |
271 | |
272 | int ObjectSampler::item_count() const { |
273 | return _priority_queue->count(); |
274 | } |
275 | |
276 | const ObjectSample* ObjectSampler::item_at(int index) const { |
277 | return _priority_queue->item_at(index); |
278 | } |
279 | |
280 | ObjectSample* ObjectSampler::item_at(int index) { |
281 | return const_cast<ObjectSample*>( |
282 | const_cast<const ObjectSampler*>(this)->item_at(index) |
283 | ); |
284 | } |
285 | |
286 | const JfrTicks& ObjectSampler::last_sweep() const { |
287 | return _last_sweep; |
288 | } |
289 | |