50113
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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 |
#include "precompiled.hpp"
|
|
25 |
#include "jfr/jfrEvents.hpp"
|
|
26 |
#include "jfr/leakprofiler/sampling/objectSample.hpp"
|
|
27 |
#include "jfr/leakprofiler/sampling/objectSampler.hpp"
|
|
28 |
#include "jfr/leakprofiler/sampling/sampleList.hpp"
|
|
29 |
#include "jfr/leakprofiler/sampling/samplePriorityQueue.hpp"
|
|
30 |
#include "jfr/recorder/jfrEventSetting.inline.hpp"
|
|
31 |
#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
|
|
32 |
#include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
|
|
33 |
#include "jfr/support/jfrThreadLocal.hpp"
|
|
34 |
#include "jfr/utilities/jfrTryLock.hpp"
|
|
35 |
#include "oops/oop.inline.hpp"
|
|
36 |
#include "runtime/thread.hpp"
|
|
37 |
#include "logging/log.hpp"
|
|
38 |
|
|
39 |
ObjectSampler::ObjectSampler(size_t size) :
|
|
40 |
_priority_queue(new SamplePriorityQueue(size)),
|
|
41 |
_list(new SampleList(size)),
|
|
42 |
_last_sweep(JfrTicks::now()),
|
|
43 |
_total_allocated(0),
|
|
44 |
_threshold(0),
|
|
45 |
_size(size),
|
|
46 |
_tryLock(0),
|
|
47 |
_dead_samples(false) {}
|
|
48 |
|
|
49 |
ObjectSampler::~ObjectSampler() {
|
|
50 |
delete _priority_queue;
|
|
51 |
_priority_queue = NULL;
|
|
52 |
delete _list;
|
|
53 |
_list = NULL;
|
|
54 |
}
|
|
55 |
|
|
56 |
void ObjectSampler::add(HeapWord* obj, size_t allocated, JavaThread* thread) {
|
|
57 |
assert(thread != NULL, "invariant");
|
|
58 |
const traceid thread_id = thread->threadObj() != NULL ? thread->jfr_thread_local()->thread_id() : 0;
|
|
59 |
if (thread_id == 0) {
|
|
60 |
return;
|
|
61 |
}
|
|
62 |
assert(thread_id != 0, "invariant");
|
|
63 |
|
|
64 |
if (!thread->jfr_thread_local()->has_thread_checkpoint()) {
|
|
65 |
JfrCheckpointManager::create_thread_checkpoint(thread);
|
|
66 |
assert(thread->jfr_thread_local()->has_thread_checkpoint(), "invariant");
|
|
67 |
}
|
|
68 |
|
|
69 |
traceid stack_trace_id = 0;
|
|
70 |
unsigned int stack_trace_hash = 0;
|
|
71 |
if (JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) {
|
|
72 |
stack_trace_id = JfrStackTraceRepository::record(thread, 0, &stack_trace_hash);
|
|
73 |
thread->jfr_thread_local()->set_cached_stack_trace_id(stack_trace_id, stack_trace_hash);
|
|
74 |
}
|
|
75 |
|
|
76 |
const JfrTicks allocation_time = JfrTicks::now();
|
|
77 |
|
|
78 |
JfrTryLock tryLock(&_tryLock);
|
|
79 |
if (!tryLock.has_lock()) {
|
|
80 |
log_trace(jfr, oldobject, sampling)("Skipping old object sample due to lock contention");
|
|
81 |
return;
|
|
82 |
}
|
|
83 |
|
|
84 |
if (_dead_samples) {
|
|
85 |
scavenge();
|
|
86 |
assert(!_dead_samples, "invariant");
|
|
87 |
}
|
|
88 |
|
|
89 |
_total_allocated += allocated;
|
|
90 |
const size_t span = _total_allocated - _priority_queue->total();
|
|
91 |
ObjectSample* sample;
|
|
92 |
if ((size_t)_priority_queue->count() == _size) {
|
|
93 |
assert(_list->count() == _size, "invariant");
|
|
94 |
const ObjectSample* peek = _priority_queue->peek();
|
|
95 |
if (peek->span() > span) {
|
|
96 |
// quick reject, will not fit
|
|
97 |
return;
|
|
98 |
}
|
|
99 |
sample = _list->reuse(_priority_queue->pop());
|
|
100 |
} else {
|
|
101 |
sample = _list->get();
|
|
102 |
}
|
|
103 |
|
|
104 |
assert(sample != NULL, "invariant");
|
|
105 |
assert(thread_id != 0, "invariant");
|
|
106 |
sample->set_thread_id(thread_id);
|
|
107 |
sample->set_thread_checkpoint(thread->jfr_thread_local()->thread_checkpoint());
|
|
108 |
|
|
109 |
if (stack_trace_id != 0) {
|
|
110 |
sample->set_stack_trace_id(stack_trace_id);
|
|
111 |
sample->set_stack_trace_hash(stack_trace_hash);
|
|
112 |
}
|
|
113 |
|
|
114 |
sample->set_span(allocated);
|
|
115 |
sample->set_object((oop)obj);
|
|
116 |
sample->set_allocated(allocated);
|
|
117 |
sample->set_allocation_time(allocation_time);
|
|
118 |
_priority_queue->push(sample);
|
|
119 |
}
|
|
120 |
|
|
121 |
const ObjectSample* ObjectSampler::last() const {
|
|
122 |
return _list->last();
|
|
123 |
}
|
|
124 |
|
|
125 |
const ObjectSample* ObjectSampler::last_resolved() const {
|
|
126 |
return _list->last_resolved();
|
|
127 |
}
|
|
128 |
|
|
129 |
void ObjectSampler::set_last_resolved(const ObjectSample* sample) {
|
|
130 |
_list->set_last_resolved(sample);
|
|
131 |
}
|
|
132 |
|
|
133 |
void ObjectSampler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
|
|
134 |
ObjectSample* current = _list->last();
|
|
135 |
while (current != NULL) {
|
|
136 |
ObjectSample* next = current->next();
|
|
137 |
if (!current->is_dead()) {
|
|
138 |
if (is_alive->do_object_b(current->object())) {
|
|
139 |
// The weakly referenced object is alive, update pointer
|
|
140 |
f->do_oop(const_cast<oop*>(current->object_addr()));
|
|
141 |
} else {
|
|
142 |
current->set_dead();
|
|
143 |
_dead_samples = true;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
current = next;
|
|
147 |
}
|
|
148 |
_last_sweep = JfrTicks::now();
|
|
149 |
}
|
|
150 |
|
|
151 |
void ObjectSampler::remove_dead(ObjectSample* sample) {
|
|
152 |
assert(sample != NULL, "invariant");
|
|
153 |
assert(sample->is_dead(), "invariant");
|
|
154 |
ObjectSample* const previous = sample->prev();
|
|
155 |
// push span on to previous
|
|
156 |
if (previous != NULL) {
|
|
157 |
_priority_queue->remove(previous);
|
|
158 |
previous->add_span(sample->span());
|
|
159 |
_priority_queue->push(previous);
|
|
160 |
}
|
|
161 |
_priority_queue->remove(sample);
|
|
162 |
_list->release(sample);
|
|
163 |
}
|
|
164 |
|
|
165 |
void ObjectSampler::scavenge() {
|
|
166 |
ObjectSample* current = _list->last();
|
|
167 |
while (current != NULL) {
|
|
168 |
ObjectSample* next = current->next();
|
|
169 |
if (current->is_dead()) {
|
|
170 |
remove_dead(current);
|
|
171 |
}
|
|
172 |
current = next;
|
|
173 |
}
|
|
174 |
_dead_samples = false;
|
|
175 |
}
|
|
176 |
|
|
177 |
int ObjectSampler::item_count() const {
|
|
178 |
return _priority_queue->count();
|
|
179 |
}
|
|
180 |
|
|
181 |
const ObjectSample* ObjectSampler::item_at(int index) const {
|
|
182 |
return _priority_queue->item_at(index);
|
|
183 |
}
|
|
184 |
|
|
185 |
ObjectSample* ObjectSampler::item_at(int index) {
|
|
186 |
return const_cast<ObjectSample*>(
|
|
187 |
const_cast<const ObjectSampler*>(this)->item_at(index)
|
|
188 |
);
|
|
189 |
}
|
|
190 |
|
|
191 |
const JfrTicks& ObjectSampler::last_sweep() const {
|
|
192 |
return _last_sweep;
|
|
193 |
}
|