Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/jfr/leakprofiler/sampling/objectSampler.cpp
41155 views
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 "gc/shared/collectedHeap.hpp"
27
#include "gc/shared/oopStorage.hpp"
28
#include "gc/shared/oopStorageSet.hpp"
29
#include "jfr/jfrEvents.hpp"
30
#include "jfr/leakprofiler/sampling/objectSample.hpp"
31
#include "jfr/leakprofiler/sampling/objectSampler.hpp"
32
#include "jfr/leakprofiler/sampling/sampleList.hpp"
33
#include "jfr/leakprofiler/sampling/samplePriorityQueue.hpp"
34
#include "jfr/recorder/jfrEventSetting.inline.hpp"
35
#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
36
#include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
37
#include "jfr/support/jfrThreadLocal.hpp"
38
#include "jfr/utilities/jfrTime.hpp"
39
#include "jfr/utilities/jfrTryLock.hpp"
40
#include "logging/log.hpp"
41
#include "memory/universe.hpp"
42
#include "oops/oop.inline.hpp"
43
#include "runtime/atomic.hpp"
44
#include "runtime/orderAccess.hpp"
45
#include "runtime/safepoint.hpp"
46
#include "runtime/thread.hpp"
47
48
// Timestamp of when the gc last processed the set of sampled objects.
49
// Atomic access to prevent word tearing on 32-bit platforms.
50
static volatile int64_t _last_sweep;
51
52
// Condition variable to communicate that some sampled objects have been cleared by the gc
53
// and can therefore be removed from the sample priority queue.
54
static bool volatile _dead_samples = false;
55
56
// The OopStorage instance is used to hold weak references to sampled objects.
57
// It is constructed and registered during VM initialization. This is a singleton
58
// that persist independent of the state of the ObjectSampler.
59
static OopStorage* _oop_storage = NULL;
60
61
OopStorage* ObjectSampler::oop_storage() { return _oop_storage; }
62
63
// Callback invoked by the GC after an iteration over the oop storage
64
// that may have cleared dead referents. num_dead is the number of entries
65
// already NULL or cleared by the iteration.
66
void ObjectSampler::oop_storage_gc_notification(size_t num_dead) {
67
if (num_dead != 0) {
68
// The ObjectSampler instance may have already been cleaned or a new
69
// instance was created concurrently. This allows for a small race where cleaning
70
// could be done again.
71
Atomic::store(&_dead_samples, true);
72
Atomic::store(&_last_sweep, (int64_t)JfrTicks::now().value());
73
}
74
}
75
76
bool ObjectSampler::create_oop_storage() {
77
_oop_storage = OopStorageSet::create_weak("Weak JFR Old Object Samples", mtTracing);
78
assert(_oop_storage != NULL, "invariant");
79
_oop_storage->register_num_dead_callback(&oop_storage_gc_notification);
80
return true;
81
}
82
83
static ObjectSampler* _instance = NULL;
84
85
static ObjectSampler& instance() {
86
assert(_instance != NULL, "invariant");
87
return *_instance;
88
}
89
90
ObjectSampler::ObjectSampler(size_t size) :
91
_priority_queue(new SamplePriorityQueue(size)),
92
_list(new SampleList(size)),
93
_total_allocated(0),
94
_threshold(0),
95
_size(size) {
96
Atomic::store(&_dead_samples, false);
97
Atomic::store(&_last_sweep, (int64_t)JfrTicks::now().value());
98
}
99
100
ObjectSampler::~ObjectSampler() {
101
delete _priority_queue;
102
_priority_queue = NULL;
103
delete _list;
104
_list = NULL;
105
}
106
107
bool ObjectSampler::create(size_t size) {
108
assert(SafepointSynchronize::is_at_safepoint(), "invariant");
109
assert(_oop_storage != NULL, "should be already created");
110
assert(_instance == NULL, "invariant");
111
_instance = new ObjectSampler(size);
112
return _instance != NULL;
113
}
114
115
bool ObjectSampler::is_created() {
116
return _instance != NULL;
117
}
118
119
ObjectSampler* ObjectSampler::sampler() {
120
assert(is_created(), "invariant");
121
return _instance;
122
}
123
124
void ObjectSampler::destroy() {
125
assert(SafepointSynchronize::is_at_safepoint(), "invariant");
126
if (_instance != NULL) {
127
ObjectSampler* const sampler = _instance;
128
_instance = NULL;
129
delete sampler;
130
}
131
}
132
133
static volatile int _lock = 0;
134
135
ObjectSampler* ObjectSampler::acquire() {
136
while (Atomic::cmpxchg(&_lock, 0, 1) == 1) {}
137
return _instance;
138
}
139
140
void ObjectSampler::release() {
141
OrderAccess::fence();
142
_lock = 0;
143
}
144
145
static traceid get_thread_id(JavaThread* thread) {
146
assert(thread != NULL, "invariant");
147
if (thread->threadObj() == NULL) {
148
return 0;
149
}
150
const JfrThreadLocal* const tl = thread->jfr_thread_local();
151
assert(tl != NULL, "invariant");
152
if (tl->is_excluded()) {
153
return 0;
154
}
155
if (!tl->has_thread_blob()) {
156
JfrCheckpointManager::create_thread_blob(thread);
157
}
158
assert(tl->has_thread_blob(), "invariant");
159
return tl->thread_id();
160
}
161
162
class RecordStackTrace {
163
private:
164
JavaThread* _jt;
165
bool _enabled;
166
public:
167
RecordStackTrace(JavaThread* jt) : _jt(jt),
168
_enabled(JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) {
169
if (_enabled) {
170
JfrStackTraceRepository::record_for_leak_profiler(jt);
171
}
172
}
173
~RecordStackTrace() {
174
if (_enabled) {
175
_jt->jfr_thread_local()->clear_cached_stack_trace();
176
}
177
}
178
};
179
180
void ObjectSampler::sample(HeapWord* obj, size_t allocated, JavaThread* thread) {
181
assert(thread != NULL, "invariant");
182
assert(is_created(), "invariant");
183
const traceid thread_id = get_thread_id(thread);
184
if (thread_id == 0) {
185
return;
186
}
187
RecordStackTrace rst(thread);
188
// try enter critical section
189
JfrTryLock tryLock(&_lock);
190
if (!tryLock.acquired()) {
191
log_trace(jfr, oldobject, sampling)("Skipping old object sample due to lock contention");
192
return;
193
}
194
instance().add(obj, allocated, thread_id, thread);
195
}
196
197
void ObjectSampler::add(HeapWord* obj, size_t allocated, traceid thread_id, JavaThread* thread) {
198
assert(obj != NULL, "invariant");
199
assert(thread_id != 0, "invariant");
200
assert(thread != NULL, "invariant");
201
assert(thread->jfr_thread_local()->has_thread_blob(), "invariant");
202
203
if (Atomic::load(&_dead_samples)) {
204
// There's a small race where a GC scan might reset this to true, potentially
205
// causing a back-to-back scavenge.
206
Atomic::store(&_dead_samples, false);
207
scavenge();
208
}
209
210
_total_allocated += allocated;
211
const size_t span = _total_allocated - _priority_queue->total();
212
ObjectSample* sample;
213
if ((size_t)_priority_queue->count() == _size) {
214
assert(_list->count() == _size, "invariant");
215
const ObjectSample* peek = _priority_queue->peek();
216
if (peek->span() > span) {
217
// quick reject, will not fit
218
return;
219
}
220
sample = _list->reuse(_priority_queue->pop());
221
} else {
222
sample = _list->get();
223
}
224
225
assert(sample != NULL, "invariant");
226
sample->set_thread_id(thread_id);
227
228
const JfrThreadLocal* const tl = thread->jfr_thread_local();
229
sample->set_thread(tl->thread_blob());
230
231
const unsigned int stacktrace_hash = tl->cached_stack_trace_hash();
232
if (stacktrace_hash != 0) {
233
sample->set_stack_trace_id(tl->cached_stack_trace_id());
234
sample->set_stack_trace_hash(stacktrace_hash);
235
}
236
237
sample->set_span(allocated);
238
sample->set_object(cast_to_oop(obj));
239
sample->set_allocated(allocated);
240
sample->set_allocation_time(JfrTicks::now());
241
sample->set_heap_used_at_last_gc(Universe::heap()->used_at_last_gc());
242
_priority_queue->push(sample);
243
}
244
245
void ObjectSampler::scavenge() {
246
ObjectSample* current = _list->last();
247
while (current != NULL) {
248
ObjectSample* next = current->next();
249
if (current->is_dead()) {
250
remove_dead(current);
251
}
252
current = next;
253
}
254
}
255
256
void ObjectSampler::remove_dead(ObjectSample* sample) {
257
assert(sample != NULL, "invariant");
258
assert(sample->is_dead(), "invariant");
259
sample->release();
260
261
ObjectSample* const previous = sample->prev();
262
// push span onto previous
263
if (previous != NULL) {
264
_priority_queue->remove(previous);
265
previous->add_span(sample->span());
266
_priority_queue->push(previous);
267
}
268
_priority_queue->remove(sample);
269
_list->release(sample);
270
}
271
272
ObjectSample* ObjectSampler::last() const {
273
return _list->last();
274
}
275
276
const ObjectSample* ObjectSampler::first() const {
277
return _list->first();
278
}
279
280
const ObjectSample* ObjectSampler::last_resolved() const {
281
return _list->last_resolved();
282
}
283
284
void ObjectSampler::set_last_resolved(const ObjectSample* sample) {
285
_list->set_last_resolved(sample);
286
}
287
288
int ObjectSampler::item_count() const {
289
return _priority_queue->count();
290
}
291
292
const ObjectSample* ObjectSampler::item_at(int index) const {
293
return _priority_queue->item_at(index);
294
}
295
296
ObjectSample* ObjectSampler::item_at(int index) {
297
return const_cast<ObjectSample*>(
298
const_cast<const ObjectSampler*>(this)->item_at(index)
299
);
300
}
301
302
int64_t ObjectSampler::last_sweep() {
303
return Atomic::load(&_last_sweep);
304
}
305
306