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