13195
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 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 |
|
|
26 |
#include "runtime/atomic.hpp"
|
|
27 |
#include "runtime/interfaceSupport.hpp"
|
|
28 |
#include "runtime/mutexLocker.hpp"
|
|
29 |
#include "runtime/safepoint.hpp"
|
|
30 |
#include "runtime/threadCritical.hpp"
|
|
31 |
#include "services/memPtr.hpp"
|
|
32 |
#include "services/memReporter.hpp"
|
|
33 |
#include "services/memTracker.hpp"
|
|
34 |
#include "utilities/decoder.hpp"
|
|
35 |
#include "utilities/globalDefinitions.hpp"
|
|
36 |
|
|
37 |
bool NMT_track_callsite = false;
|
|
38 |
|
|
39 |
// walk all 'known' threads at NMT sync point, and collect their recorders
|
|
40 |
void SyncThreadRecorderClosure::do_thread(Thread* thread) {
|
|
41 |
assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
|
|
42 |
if (thread->is_Java_thread()) {
|
|
43 |
JavaThread* javaThread = (JavaThread*)thread;
|
|
44 |
MemRecorder* recorder = javaThread->get_recorder();
|
|
45 |
if (recorder != NULL) {
|
|
46 |
MemTracker::enqueue_pending_recorder(recorder);
|
|
47 |
javaThread->set_recorder(NULL);
|
|
48 |
}
|
|
49 |
}
|
|
50 |
_thread_count ++;
|
|
51 |
}
|
|
52 |
|
|
53 |
|
|
54 |
MemRecorder* MemTracker::_global_recorder = NULL;
|
|
55 |
MemSnapshot* MemTracker::_snapshot = NULL;
|
|
56 |
MemBaseline MemTracker::_baseline;
|
|
57 |
Mutex MemTracker::_query_lock(Monitor::native, "NMT_queryLock");
|
|
58 |
volatile MemRecorder* MemTracker::_merge_pending_queue = NULL;
|
|
59 |
volatile MemRecorder* MemTracker::_pooled_recorders = NULL;
|
|
60 |
MemTrackWorker* MemTracker::_worker_thread = NULL;
|
|
61 |
int MemTracker::_sync_point_skip_count = 0;
|
|
62 |
MemTracker::NMTLevel MemTracker::_tracking_level = MemTracker::NMT_off;
|
|
63 |
volatile MemTracker::NMTStates MemTracker::_state = NMT_uninited;
|
|
64 |
MemTracker::ShutdownReason MemTracker::_reason = NMT_shutdown_none;
|
|
65 |
int MemTracker::_thread_count = 255;
|
|
66 |
volatile jint MemTracker::_pooled_recorder_count = 0;
|
|
67 |
debug_only(intx MemTracker::_main_thread_tid = 0;)
|
|
68 |
debug_only(volatile jint MemTracker::_pending_recorder_count = 0;)
|
|
69 |
|
|
70 |
void MemTracker::init_tracking_options(const char* option_line) {
|
|
71 |
_tracking_level = NMT_off;
|
|
72 |
if (strncmp(option_line, "=summary", 8) == 0) {
|
|
73 |
_tracking_level = NMT_summary;
|
|
74 |
} else if (strncmp(option_line, "=detail", 8) == 0) {
|
|
75 |
_tracking_level = NMT_detail;
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
// first phase of bootstrapping, when VM is still in single-threaded mode.
|
|
80 |
void MemTracker::bootstrap_single_thread() {
|
|
81 |
if (_tracking_level > NMT_off) {
|
|
82 |
assert(_state == NMT_uninited, "wrong state");
|
|
83 |
|
|
84 |
// NMT is not supported with UseMallocOnly is on. NMT can NOT
|
|
85 |
// handle the amount of malloc data without significantly impacting
|
|
86 |
// runtime performance when this flag is on.
|
|
87 |
if (UseMallocOnly) {
|
|
88 |
shutdown(NMT_use_malloc_only);
|
|
89 |
return;
|
|
90 |
}
|
|
91 |
|
|
92 |
debug_only(_main_thread_tid = os::current_thread_id();)
|
|
93 |
_state = NMT_bootstrapping_single_thread;
|
|
94 |
NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
// second phase of bootstrapping, when VM is about to or already entered multi-theaded mode.
|
|
99 |
void MemTracker::bootstrap_multi_thread() {
|
|
100 |
if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) {
|
|
101 |
// create nmt lock for multi-thread execution
|
|
102 |
assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
|
|
103 |
_state = NMT_bootstrapping_multi_thread;
|
|
104 |
NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
// fully start nmt
|
|
109 |
void MemTracker::start() {
|
|
110 |
// Native memory tracking is off from command line option
|
|
111 |
if (_tracking_level == NMT_off || shutdown_in_progress()) return;
|
|
112 |
|
|
113 |
assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
|
|
114 |
assert(_state == NMT_bootstrapping_multi_thread, "wrong state");
|
|
115 |
|
|
116 |
_snapshot = new (std::nothrow)MemSnapshot();
|
|
117 |
if (_snapshot != NULL && !_snapshot->out_of_memory()) {
|
|
118 |
if (start_worker()) {
|
|
119 |
_state = NMT_started;
|
|
120 |
NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
|
|
121 |
return;
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
// fail to start native memory tracking, shut it down
|
|
126 |
shutdown(NMT_initialization);
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Shutting down native memory tracking.
|
|
131 |
* We can not shutdown native memory tracking immediately, so we just
|
|
132 |
* setup shutdown pending flag, every native memory tracking component
|
|
133 |
* should orderly shut itself down.
|
|
134 |
*
|
|
135 |
* The shutdown sequences:
|
|
136 |
* 1. MemTracker::shutdown() sets MemTracker to shutdown pending state
|
|
137 |
* 2. Worker thread calls MemTracker::final_shutdown(), which transites
|
|
138 |
* MemTracker to final shutdown state.
|
|
139 |
* 3. At sync point, MemTracker does final cleanup, before sets memory
|
|
140 |
* tracking level to off to complete shutdown.
|
|
141 |
*/
|
|
142 |
void MemTracker::shutdown(ShutdownReason reason) {
|
|
143 |
if (_tracking_level == NMT_off) return;
|
|
144 |
|
|
145 |
if (_state <= NMT_bootstrapping_single_thread) {
|
|
146 |
// we still in single thread mode, there is not contention
|
|
147 |
_state = NMT_shutdown_pending;
|
|
148 |
_reason = reason;
|
|
149 |
} else {
|
|
150 |
// we want to know who initialized shutdown
|
|
151 |
if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending,
|
|
152 |
(jint*)&_state, (jint)NMT_started)) {
|
|
153 |
_reason = reason;
|
|
154 |
}
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
// final phase of shutdown
|
|
159 |
void MemTracker::final_shutdown() {
|
|
160 |
// delete all pending recorders and pooled recorders
|
|
161 |
delete_all_pending_recorders();
|
|
162 |
delete_all_pooled_recorders();
|
|
163 |
|
|
164 |
{
|
|
165 |
// shared baseline and snapshot are the only objects needed to
|
|
166 |
// create query results
|
|
167 |
MutexLockerEx locker(&_query_lock, true);
|
|
168 |
// cleanup baseline data and snapshot
|
|
169 |
_baseline.clear();
|
|
170 |
delete _snapshot;
|
|
171 |
_snapshot = NULL;
|
|
172 |
}
|
|
173 |
|
|
174 |
// shutdown shared decoder instance, since it is only
|
|
175 |
// used by native memory tracking so far.
|
|
176 |
Decoder::shutdown();
|
|
177 |
|
|
178 |
MemTrackWorker* worker = NULL;
|
|
179 |
{
|
|
180 |
ThreadCritical tc;
|
|
181 |
// can not delete worker inside the thread critical
|
|
182 |
if (_worker_thread != NULL && Thread::current() == _worker_thread) {
|
|
183 |
worker = _worker_thread;
|
|
184 |
_worker_thread = NULL;
|
|
185 |
}
|
|
186 |
}
|
|
187 |
if (worker != NULL) {
|
|
188 |
delete worker;
|
|
189 |
}
|
|
190 |
_state = NMT_final_shutdown;
|
|
191 |
}
|
|
192 |
|
|
193 |
// delete all pooled recorders
|
|
194 |
void MemTracker::delete_all_pooled_recorders() {
|
|
195 |
// free all pooled recorders
|
|
196 |
volatile MemRecorder* cur_head = _pooled_recorders;
|
|
197 |
if (cur_head != NULL) {
|
|
198 |
MemRecorder* null_ptr = NULL;
|
|
199 |
while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr,
|
|
200 |
(void*)&_pooled_recorders, (void*)cur_head)) {
|
|
201 |
cur_head = _pooled_recorders;
|
|
202 |
}
|
|
203 |
if (cur_head != NULL) {
|
|
204 |
delete cur_head;
|
|
205 |
_pooled_recorder_count = 0;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
// delete all recorders in pending queue
|
|
211 |
void MemTracker::delete_all_pending_recorders() {
|
|
212 |
// free all pending recorders
|
|
213 |
MemRecorder* pending_head = get_pending_recorders();
|
|
214 |
if (pending_head != NULL) {
|
|
215 |
delete pending_head;
|
|
216 |
}
|
|
217 |
}
|
|
218 |
|
|
219 |
/*
|
|
220 |
* retrieve per-thread recorder of specified thread.
|
|
221 |
* if thread == NULL, it means global recorder
|
|
222 |
*/
|
|
223 |
MemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) {
|
|
224 |
if (shutdown_in_progress()) return NULL;
|
|
225 |
|
|
226 |
MemRecorder* rc;
|
|
227 |
if (thread == NULL) {
|
|
228 |
rc = _global_recorder;
|
|
229 |
} else {
|
|
230 |
rc = thread->get_recorder();
|
|
231 |
}
|
|
232 |
|
|
233 |
if (rc != NULL && rc->is_full()) {
|
|
234 |
enqueue_pending_recorder(rc);
|
|
235 |
rc = NULL;
|
|
236 |
}
|
|
237 |
|
|
238 |
if (rc == NULL) {
|
|
239 |
rc = get_new_or_pooled_instance();
|
|
240 |
if (thread == NULL) {
|
|
241 |
_global_recorder = rc;
|
|
242 |
} else {
|
|
243 |
thread->set_recorder(rc);
|
|
244 |
}
|
|
245 |
}
|
|
246 |
return rc;
|
|
247 |
}
|
|
248 |
|
|
249 |
/*
|
|
250 |
* get a per-thread recorder from pool, or create a new one if
|
|
251 |
* there is not one available.
|
|
252 |
*/
|
|
253 |
MemRecorder* MemTracker::get_new_or_pooled_instance() {
|
|
254 |
MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders);
|
|
255 |
if (cur_head == NULL) {
|
|
256 |
MemRecorder* rec = new (std::nothrow)MemRecorder();
|
|
257 |
if (rec == NULL || rec->out_of_memory()) {
|
|
258 |
shutdown(NMT_out_of_memory);
|
|
259 |
if (rec != NULL) {
|
|
260 |
delete rec;
|
|
261 |
rec = NULL;
|
|
262 |
}
|
|
263 |
}
|
|
264 |
return rec;
|
|
265 |
} else {
|
|
266 |
MemRecorder* next_head = cur_head->next();
|
|
267 |
if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders,
|
|
268 |
(void*)cur_head)) {
|
|
269 |
return get_new_or_pooled_instance();
|
|
270 |
}
|
|
271 |
cur_head->set_next(NULL);
|
|
272 |
Atomic::dec(&_pooled_recorder_count);
|
|
273 |
debug_only(cur_head->set_generation();)
|
|
274 |
return cur_head;
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
/*
|
|
279 |
* retrieve all recorders in pending queue, and empty the queue
|
|
280 |
*/
|
|
281 |
MemRecorder* MemTracker::get_pending_recorders() {
|
|
282 |
MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
|
|
283 |
MemRecorder* null_ptr = NULL;
|
|
284 |
while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue,
|
|
285 |
(void*)cur_head)) {
|
|
286 |
cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
|
|
287 |
}
|
|
288 |
debug_only(Atomic::store(0, &_pending_recorder_count));
|
|
289 |
return cur_head;
|
|
290 |
}
|
|
291 |
|
|
292 |
/*
|
|
293 |
* release a recorder to recorder pool.
|
|
294 |
*/
|
|
295 |
void MemTracker::release_thread_recorder(MemRecorder* rec) {
|
|
296 |
assert(rec != NULL, "null recorder");
|
|
297 |
// we don't want to pool too many recorders
|
|
298 |
rec->set_next(NULL);
|
|
299 |
if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) {
|
|
300 |
delete rec;
|
|
301 |
return;
|
|
302 |
}
|
|
303 |
|
|
304 |
rec->clear();
|
|
305 |
MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders);
|
|
306 |
rec->set_next(cur_head);
|
|
307 |
while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders,
|
|
308 |
(void*)cur_head)) {
|
|
309 |
cur_head = const_cast<MemRecorder*>(_pooled_recorders);
|
|
310 |
rec->set_next(cur_head);
|
|
311 |
}
|
|
312 |
Atomic::inc(&_pooled_recorder_count);
|
|
313 |
}
|
|
314 |
|
|
315 |
/*
|
|
316 |
* This is the most important method in whole nmt implementation.
|
|
317 |
*
|
|
318 |
* Create a memory record.
|
|
319 |
* 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
|
|
320 |
* still in single thread mode.
|
|
321 |
* 2. For all threads other than JavaThread, ThreadCritical is needed
|
|
322 |
* to write to recorders to global recorder.
|
|
323 |
* 3. For JavaThreads that are not longer visible by safepoint, also
|
|
324 |
* need to take ThreadCritical and records are written to global
|
|
325 |
* recorders, since these threads are NOT walked by Threads.do_thread().
|
|
326 |
* 4. JavaThreads that are running in native state, have to transition
|
|
327 |
* to VM state before writing to per-thread recorders.
|
|
328 |
* 5. JavaThreads that are running in VM state do not need any lock and
|
|
329 |
* records are written to per-thread recorders.
|
|
330 |
* 6. For a thread has yet to attach VM 'Thread', they need to take
|
|
331 |
* ThreadCritical to write to global recorder.
|
|
332 |
*
|
|
333 |
* Important note:
|
|
334 |
* NO LOCK should be taken inside ThreadCritical lock !!!
|
|
335 |
*/
|
|
336 |
void MemTracker::create_memory_record(address addr, MEMFLAGS flags,
|
|
337 |
size_t size, address pc, Thread* thread) {
|
|
338 |
if (!shutdown_in_progress()) {
|
|
339 |
// single thread, we just write records direct to global recorder,'
|
|
340 |
// with any lock
|
|
341 |
if (_state == NMT_bootstrapping_single_thread) {
|
|
342 |
assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
|
|
343 |
thread = NULL;
|
|
344 |
} else {
|
|
345 |
if (thread == NULL) {
|
|
346 |
// don't use Thread::current(), since it is possible that
|
|
347 |
// the calling thread has yet to attach to VM 'Thread',
|
|
348 |
// which will result assertion failure
|
|
349 |
thread = ThreadLocalStorage::thread();
|
|
350 |
}
|
|
351 |
}
|
|
352 |
|
|
353 |
if (thread != NULL) {
|
|
354 |
if (thread->is_Java_thread() && ((JavaThread*)thread)->is_safepoint_visible()) {
|
13300
|
355 |
JavaThread* java_thread = static_cast<JavaThread*>(thread);
|
|
356 |
JavaThreadState state = java_thread->thread_state();
|
|
357 |
if (SafepointSynchronize::safepoint_safe(java_thread, state)) {
|
|
358 |
// JavaThreads that are safepoint safe, can run through safepoint,
|
|
359 |
// so ThreadCritical is needed to ensure no threads at safepoint create
|
|
360 |
// new records while the records are being gathered and the sequence number is changing
|
|
361 |
ThreadCritical tc;
|
|
362 |
create_record_in_recorder(addr, flags, size, pc, java_thread);
|
13195
|
363 |
} else {
|
13300
|
364 |
create_record_in_recorder(addr, flags, size, pc, java_thread);
|
13195
|
365 |
}
|
|
366 |
} else {
|
|
367 |
// other threads, such as worker and watcher threads, etc. need to
|
|
368 |
// take ThreadCritical to write to global recorder
|
|
369 |
ThreadCritical tc;
|
|
370 |
create_record_in_recorder(addr, flags, size, pc, NULL);
|
|
371 |
}
|
|
372 |
} else {
|
|
373 |
if (_state == NMT_bootstrapping_single_thread) {
|
|
374 |
// single thread, no lock needed
|
|
375 |
create_record_in_recorder(addr, flags, size, pc, NULL);
|
|
376 |
} else {
|
|
377 |
// for thread has yet to attach VM 'Thread', we can not use VM mutex.
|
|
378 |
// use native thread critical instead
|
|
379 |
ThreadCritical tc;
|
|
380 |
create_record_in_recorder(addr, flags, size, pc, NULL);
|
|
381 |
}
|
|
382 |
}
|
|
383 |
}
|
|
384 |
}
|
|
385 |
|
|
386 |
// write a record to proper recorder. No lock can be taken from this method
|
|
387 |
// down.
|
|
388 |
void MemTracker::create_record_in_recorder(address addr, MEMFLAGS flags,
|
13300
|
389 |
size_t size, address pc, JavaThread* thread) {
|
13195
|
390 |
|
13300
|
391 |
MemRecorder* rc = get_thread_recorder(thread);
|
13195
|
392 |
if (rc != NULL) {
|
|
393 |
rc->record(addr, flags, size, pc);
|
|
394 |
}
|
|
395 |
}
|
|
396 |
|
|
397 |
/**
|
|
398 |
* enqueue a recorder to pending queue
|
|
399 |
*/
|
|
400 |
void MemTracker::enqueue_pending_recorder(MemRecorder* rec) {
|
|
401 |
assert(rec != NULL, "null recorder");
|
|
402 |
|
|
403 |
// we are shutting down, so just delete it
|
|
404 |
if (shutdown_in_progress()) {
|
|
405 |
rec->set_next(NULL);
|
|
406 |
delete rec;
|
|
407 |
return;
|
|
408 |
}
|
|
409 |
|
|
410 |
MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
|
|
411 |
rec->set_next(cur_head);
|
|
412 |
while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue,
|
|
413 |
(void*)cur_head)) {
|
|
414 |
cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
|
|
415 |
rec->set_next(cur_head);
|
|
416 |
}
|
|
417 |
debug_only(Atomic::inc(&_pending_recorder_count);)
|
|
418 |
}
|
|
419 |
|
|
420 |
/*
|
|
421 |
* The method is called at global safepoint
|
|
422 |
* during it synchronization process.
|
|
423 |
* 1. enqueue all JavaThreads' per-thread recorders
|
|
424 |
* 2. enqueue global recorder
|
|
425 |
* 3. retrieve all pending recorders
|
|
426 |
* 4. reset global sequence number generator
|
|
427 |
* 5. call worker's sync
|
|
428 |
*/
|
|
429 |
#define MAX_SAFEPOINTS_TO_SKIP 128
|
|
430 |
#define SAFE_SEQUENCE_THRESHOLD 30
|
|
431 |
#define HIGH_GENERATION_THRESHOLD 60
|
|
432 |
|
|
433 |
void MemTracker::sync() {
|
|
434 |
assert(_tracking_level > NMT_off, "NMT is not enabled");
|
|
435 |
assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
|
|
436 |
|
|
437 |
// Some GC tests hit large number of safepoints in short period of time
|
|
438 |
// without meaningful activities. We should prevent going to
|
|
439 |
// sync point in these cases, which can potentially exhaust generation buffer.
|
|
440 |
// Here is the factots to determine if we should go into sync point:
|
|
441 |
// 1. not to overflow sequence number
|
|
442 |
// 2. if we are in danger to overflow generation buffer
|
|
443 |
// 3. how many safepoints we already skipped sync point
|
|
444 |
if (_state == NMT_started) {
|
|
445 |
// worker thread is not ready, no one can manage generation
|
|
446 |
// buffer, so skip this safepoint
|
|
447 |
if (_worker_thread == NULL) return;
|
|
448 |
|
|
449 |
if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) {
|
|
450 |
int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint;
|
|
451 |
int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS;
|
|
452 |
if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) {
|
|
453 |
_sync_point_skip_count ++;
|
|
454 |
return;
|
|
455 |
}
|
|
456 |
}
|
|
457 |
_sync_point_skip_count = 0;
|
|
458 |
{
|
|
459 |
// This method is running at safepoint, with ThreadCritical lock,
|
|
460 |
// it should guarantee that NMT is fully sync-ed.
|
|
461 |
ThreadCritical tc;
|
13300
|
462 |
|
|
463 |
// walk all JavaThreads to collect recorders
|
|
464 |
SyncThreadRecorderClosure stc;
|
|
465 |
Threads::threads_do(&stc);
|
|
466 |
|
|
467 |
_thread_count = stc.get_thread_count();
|
|
468 |
MemRecorder* pending_recorders = get_pending_recorders();
|
|
469 |
|
13195
|
470 |
if (_global_recorder != NULL) {
|
|
471 |
_global_recorder->set_next(pending_recorders);
|
|
472 |
pending_recorders = _global_recorder;
|
|
473 |
_global_recorder = NULL;
|
|
474 |
}
|
|
475 |
SequenceGenerator::reset();
|
|
476 |
// check _worker_thread with lock to avoid racing condition
|
|
477 |
if (_worker_thread != NULL) {
|
|
478 |
_worker_thread->at_sync_point(pending_recorders);
|
|
479 |
}
|
|
480 |
}
|
|
481 |
}
|
|
482 |
|
|
483 |
// now, it is the time to shut whole things off
|
|
484 |
if (_state == NMT_final_shutdown) {
|
|
485 |
// walk all JavaThreads to delete all recorders
|
|
486 |
SyncThreadRecorderClosure stc;
|
|
487 |
Threads::threads_do(&stc);
|
|
488 |
// delete global recorder
|
|
489 |
{
|
|
490 |
ThreadCritical tc;
|
|
491 |
if (_global_recorder != NULL) {
|
|
492 |
delete _global_recorder;
|
|
493 |
_global_recorder = NULL;
|
|
494 |
}
|
|
495 |
}
|
13300
|
496 |
MemRecorder* pending_recorders = get_pending_recorders();
|
|
497 |
if (pending_recorders != NULL) {
|
|
498 |
delete pending_recorders;
|
|
499 |
}
|
|
500 |
// try at a later sync point to ensure MemRecorder instance drops to zero to
|
|
501 |
// completely shutdown NMT
|
|
502 |
if (MemRecorder::_instance_count == 0) {
|
|
503 |
_state = NMT_shutdown;
|
|
504 |
_tracking_level = NMT_off;
|
|
505 |
}
|
13195
|
506 |
}
|
|
507 |
}
|
|
508 |
|
|
509 |
/*
|
|
510 |
* Start worker thread.
|
|
511 |
*/
|
|
512 |
bool MemTracker::start_worker() {
|
|
513 |
assert(_worker_thread == NULL, "Just Check");
|
|
514 |
_worker_thread = new (std::nothrow) MemTrackWorker();
|
|
515 |
if (_worker_thread == NULL || _worker_thread->has_error()) {
|
|
516 |
shutdown(NMT_initialization);
|
|
517 |
return false;
|
|
518 |
}
|
|
519 |
_worker_thread->start();
|
|
520 |
return true;
|
|
521 |
}
|
|
522 |
|
|
523 |
/*
|
|
524 |
* We need to collect a JavaThread's per-thread recorder
|
|
525 |
* before it exits.
|
|
526 |
*/
|
|
527 |
void MemTracker::thread_exiting(JavaThread* thread) {
|
|
528 |
if (is_on()) {
|
|
529 |
MemRecorder* rec = thread->get_recorder();
|
|
530 |
if (rec != NULL) {
|
|
531 |
enqueue_pending_recorder(rec);
|
|
532 |
thread->set_recorder(NULL);
|
|
533 |
}
|
|
534 |
}
|
|
535 |
}
|
|
536 |
|
|
537 |
// baseline current memory snapshot
|
|
538 |
bool MemTracker::baseline() {
|
|
539 |
MutexLockerEx lock(&_query_lock, true);
|
|
540 |
MemSnapshot* snapshot = get_snapshot();
|
|
541 |
if (snapshot != NULL) {
|
|
542 |
return _baseline.baseline(*snapshot, false);
|
|
543 |
}
|
|
544 |
return false;
|
|
545 |
}
|
|
546 |
|
|
547 |
// print memory usage from current snapshot
|
|
548 |
bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
|
|
549 |
MemBaseline baseline;
|
|
550 |
MutexLockerEx lock(&_query_lock, true);
|
|
551 |
MemSnapshot* snapshot = get_snapshot();
|
|
552 |
if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
|
|
553 |
BaselineReporter reporter(out, unit);
|
|
554 |
reporter.report_baseline(baseline, summary_only);
|
|
555 |
return true;
|
|
556 |
}
|
|
557 |
return false;
|
|
558 |
}
|
|
559 |
|
|
560 |
// compare memory usage between current snapshot and baseline
|
|
561 |
bool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
|
|
562 |
MutexLockerEx lock(&_query_lock, true);
|
|
563 |
if (_baseline.baselined()) {
|
|
564 |
MemBaseline baseline;
|
|
565 |
MemSnapshot* snapshot = get_snapshot();
|
|
566 |
if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
|
|
567 |
BaselineReporter reporter(out, unit);
|
|
568 |
reporter.diff_baselines(baseline, _baseline, summary_only);
|
|
569 |
return true;
|
|
570 |
}
|
|
571 |
}
|
|
572 |
return false;
|
|
573 |
}
|
|
574 |
|
|
575 |
#ifndef PRODUCT
|
|
576 |
void MemTracker::walk_stack(int toSkip, char* buf, int len) {
|
|
577 |
int cur_len = 0;
|
|
578 |
char tmp[1024];
|
|
579 |
address pc;
|
|
580 |
|
|
581 |
while (cur_len < len) {
|
|
582 |
pc = os::get_caller_pc(toSkip + 1);
|
|
583 |
if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) {
|
|
584 |
jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp);
|
|
585 |
cur_len = (int)strlen(buf);
|
|
586 |
} else {
|
|
587 |
buf[cur_len] = '\0';
|
|
588 |
break;
|
|
589 |
}
|
|
590 |
toSkip ++;
|
|
591 |
}
|
|
592 |
}
|
|
593 |
|
|
594 |
void MemTracker::print_tracker_stats(outputStream* st) {
|
|
595 |
st->print_cr("\nMemory Tracker Stats:");
|
|
596 |
st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num());
|
|
597 |
st->print_cr("\tthead count = %d", _thread_count);
|
|
598 |
st->print_cr("\tArena instance = %d", Arena::_instance_count);
|
|
599 |
st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count);
|
|
600 |
st->print_cr("\tqueued recorder count = %d", _pending_recorder_count);
|
|
601 |
st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count);
|
|
602 |
if (_worker_thread != NULL) {
|
|
603 |
st->print_cr("\tWorker thread:");
|
|
604 |
st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count);
|
|
605 |
st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders());
|
|
606 |
st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count);
|
|
607 |
} else {
|
|
608 |
st->print_cr("\tWorker thread is not started");
|
|
609 |
}
|
|
610 |
st->print_cr(" ");
|
|
611 |
|
|
612 |
if (_snapshot != NULL) {
|
|
613 |
_snapshot->print_snapshot_stats(st);
|
|
614 |
} else {
|
|
615 |
st->print_cr("No snapshot");
|
|
616 |
}
|
|
617 |
}
|
|
618 |
#endif
|
|
619 |
|