author | coleenp |
Thu, 10 Jan 2019 15:13:51 -0500 | |
changeset 53244 | 9807daeb47c4 |
parent 52850 | f527b24990d7 |
child 55571 | 49102ba8cf14 |
child 57870 | 00860d9caf4d |
child 58678 | 9cf78a70fa4f |
permissions | -rw-r--r-- |
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" |
|
50880
e1117321adaf
8197425: Liveset information for Old Object sample event
egahlin
parents:
50113
diff
changeset
|
35 |
#include "logging/log.hpp" |
e1117321adaf
8197425: Liveset information for Old Object sample event
egahlin
parents:
50113
diff
changeset
|
36 |
#include "memory/universe.hpp" |
50113 | 37 |
#include "oops/oop.inline.hpp" |
38 |
#include "runtime/thread.hpp" |
|
39 |
||
40 |
ObjectSampler::ObjectSampler(size_t size) : |
|
41 |
_priority_queue(new SamplePriorityQueue(size)), |
|
42 |
_list(new SampleList(size)), |
|
43 |
_last_sweep(JfrTicks::now()), |
|
44 |
_total_allocated(0), |
|
45 |
_threshold(0), |
|
46 |
_size(size), |
|
47 |
_tryLock(0), |
|
48 |
_dead_samples(false) {} |
|
49 |
||
50 |
ObjectSampler::~ObjectSampler() { |
|
51 |
delete _priority_queue; |
|
52 |
_priority_queue = NULL; |
|
53 |
delete _list; |
|
54 |
_list = NULL; |
|
55 |
} |
|
56 |
||
57 |
void ObjectSampler::add(HeapWord* obj, size_t allocated, JavaThread* thread) { |
|
58 |
assert(thread != NULL, "invariant"); |
|
59 |
const traceid thread_id = thread->threadObj() != NULL ? thread->jfr_thread_local()->thread_id() : 0; |
|
60 |
if (thread_id == 0) { |
|
61 |
return; |
|
62 |
} |
|
63 |
assert(thread_id != 0, "invariant"); |
|
64 |
||
65 |
if (!thread->jfr_thread_local()->has_thread_checkpoint()) { |
|
66 |
JfrCheckpointManager::create_thread_checkpoint(thread); |
|
67 |
assert(thread->jfr_thread_local()->has_thread_checkpoint(), "invariant"); |
|
68 |
} |
|
69 |
||
70 |
traceid stack_trace_id = 0; |
|
71 |
unsigned int stack_trace_hash = 0; |
|
72 |
if (JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) { |
|
73 |
stack_trace_id = JfrStackTraceRepository::record(thread, 0, &stack_trace_hash); |
|
74 |
thread->jfr_thread_local()->set_cached_stack_trace_id(stack_trace_id, stack_trace_hash); |
|
75 |
} |
|
76 |
||
77 |
JfrTryLock tryLock(&_tryLock); |
|
78 |
if (!tryLock.has_lock()) { |
|
79 |
log_trace(jfr, oldobject, sampling)("Skipping old object sample due to lock contention"); |
|
80 |
return; |
|
81 |
} |
|
82 |
||
83 |
if (_dead_samples) { |
|
84 |
scavenge(); |
|
85 |
assert(!_dead_samples, "invariant"); |
|
86 |
} |
|
87 |
||
88 |
_total_allocated += allocated; |
|
89 |
const size_t span = _total_allocated - _priority_queue->total(); |
|
90 |
ObjectSample* sample; |
|
91 |
if ((size_t)_priority_queue->count() == _size) { |
|
92 |
assert(_list->count() == _size, "invariant"); |
|
93 |
const ObjectSample* peek = _priority_queue->peek(); |
|
94 |
if (peek->span() > span) { |
|
95 |
// quick reject, will not fit |
|
96 |
return; |
|
97 |
} |
|
98 |
sample = _list->reuse(_priority_queue->pop()); |
|
99 |
} else { |
|
100 |
sample = _list->get(); |
|
101 |
} |
|
102 |
||
103 |
assert(sample != NULL, "invariant"); |
|
104 |
assert(thread_id != 0, "invariant"); |
|
105 |
sample->set_thread_id(thread_id); |
|
106 |
sample->set_thread_checkpoint(thread->jfr_thread_local()->thread_checkpoint()); |
|
107 |
||
108 |
if (stack_trace_id != 0) { |
|
109 |
sample->set_stack_trace_id(stack_trace_id); |
|
110 |
sample->set_stack_trace_hash(stack_trace_hash); |
|
111 |
} |
|
112 |
||
113 |
sample->set_span(allocated); |
|
114 |
sample->set_object((oop)obj); |
|
115 |
sample->set_allocated(allocated); |
|
50880
e1117321adaf
8197425: Liveset information for Old Object sample event
egahlin
parents:
50113
diff
changeset
|
116 |
sample->set_allocation_time(JfrTicks::now()); |
e1117321adaf
8197425: Liveset information for Old Object sample event
egahlin
parents:
50113
diff
changeset
|
117 |
sample->set_heap_used_at_last_gc(Universe::get_heap_used_at_last_gc()); |
50113 | 118 |
_priority_queue->push(sample); |
119 |
} |
|
120 |
||
121 |
const ObjectSample* ObjectSampler::last() const { |
|
122 |
return _list->last(); |
|
123 |
} |
|
124 |
||
52850 | 125 |
const ObjectSample* ObjectSampler::first() const { |
126 |
return _list->first(); |
|
127 |
} |
|
128 |
||
50113 | 129 |
const ObjectSample* ObjectSampler::last_resolved() const { |
130 |
return _list->last_resolved(); |
|
131 |
} |
|
132 |
||
133 |
void ObjectSampler::set_last_resolved(const ObjectSample* sample) { |
|
134 |
_list->set_last_resolved(sample); |
|
135 |
} |
|
136 |
||
137 |
void ObjectSampler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) { |
|
138 |
ObjectSample* current = _list->last(); |
|
139 |
while (current != NULL) { |
|
140 |
ObjectSample* next = current->next(); |
|
141 |
if (!current->is_dead()) { |
|
142 |
if (is_alive->do_object_b(current->object())) { |
|
143 |
// The weakly referenced object is alive, update pointer |
|
144 |
f->do_oop(const_cast<oop*>(current->object_addr())); |
|
145 |
} else { |
|
146 |
current->set_dead(); |
|
147 |
_dead_samples = true; |
|
148 |
} |
|
149 |
} |
|
150 |
current = next; |
|
151 |
} |
|
152 |
_last_sweep = JfrTicks::now(); |
|
153 |
} |
|
154 |
||
155 |
void ObjectSampler::remove_dead(ObjectSample* sample) { |
|
156 |
assert(sample != NULL, "invariant"); |
|
157 |
assert(sample->is_dead(), "invariant"); |
|
158 |
ObjectSample* const previous = sample->prev(); |
|
159 |
// push span on to previous |
|
160 |
if (previous != NULL) { |
|
161 |
_priority_queue->remove(previous); |
|
162 |
previous->add_span(sample->span()); |
|
163 |
_priority_queue->push(previous); |
|
164 |
} |
|
165 |
_priority_queue->remove(sample); |
|
166 |
_list->release(sample); |
|
167 |
} |
|
168 |
||
169 |
void ObjectSampler::scavenge() { |
|
170 |
ObjectSample* current = _list->last(); |
|
171 |
while (current != NULL) { |
|
172 |
ObjectSample* next = current->next(); |
|
173 |
if (current->is_dead()) { |
|
174 |
remove_dead(current); |
|
175 |
} |
|
176 |
current = next; |
|
177 |
} |
|
178 |
_dead_samples = false; |
|
179 |
} |
|
180 |
||
181 |
int ObjectSampler::item_count() const { |
|
182 |
return _priority_queue->count(); |
|
183 |
} |
|
184 |
||
185 |
const ObjectSample* ObjectSampler::item_at(int index) const { |
|
186 |
return _priority_queue->item_at(index); |
|
187 |
} |
|
188 |
||
189 |
ObjectSample* ObjectSampler::item_at(int index) { |
|
190 |
return const_cast<ObjectSample*>( |
|
191 |
const_cast<const ObjectSampler*>(this)->item_at(index) |
|
192 |
); |
|
193 |
} |
|
194 |
||
195 |
const JfrTicks& ObjectSampler::last_sweep() const { |
|
196 |
return _last_sweep; |
|
197 |
} |