51 }
52 _thread_count ++;
53 }
54
55
56 MemRecorder* volatile MemTracker::_global_recorder = NULL;
57 MemSnapshot* MemTracker::_snapshot = NULL;
58 MemBaseline MemTracker::_baseline;
59 Mutex* MemTracker::_query_lock = NULL;
60 MemRecorder* volatile MemTracker::_merge_pending_queue = NULL;
61 MemRecorder* volatile 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;)
74
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 }
85
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");
90
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 }
319
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 }
330
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 }
369
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 }
376
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 }
408
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) {
413
414 MemRecorder* rc = get_thread_recorder(thread);
415 if (rc != NULL) {
416 rc->record(addr, flags, size, pc);
417 }
418 }
419
420 /**
421 * enqueue a recorder to pending queue
422 */
423 void MemTracker::enqueue_pending_recorder(MemRecorder* rec) {
424 assert(rec != NULL, "null recorder");
425
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 }
432
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)) {
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;
472
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;
486
487 SequenceGenerator::reset();
488
489 // walk all JavaThreads to collect recorders
490 SyncThreadRecorderClosure stc;
491 Threads::threads_do(&stc);
492
493 _thread_count = stc.get_thread_count();
494 MemRecorder* pending_recorders = get_pending_recorders();
495
496 if (_global_recorder != NULL) {
497 _global_recorder->set_next(pending_recorders);
498 pending_recorders = _global_recorder;
499 _global_recorder = NULL;
500 }
501
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 }
507
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 }
512
513 assert(SequenceGenerator::peek() == 1, "Should not have memory activities during sync-point");
514 }
515 }
516
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 }
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(" ");
693
694 if (_snapshot != NULL) {
695 _snapshot->print_snapshot_stats(st);
696 } else {
697 st->print_cr("No snapshot");
698 }
699 }
700 #endif
701
|
51 }
52 _thread_count ++;
53 }
54
55
56 MemRecorder* volatile MemTracker::_global_recorder = NULL;
57 MemSnapshot* MemTracker::_snapshot = NULL;
58 MemBaseline MemTracker::_baseline;
59 Mutex* MemTracker::_query_lock = NULL;
60 MemRecorder* volatile MemTracker::_merge_pending_queue = NULL;
61 MemRecorder* volatile 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 jint MemTracker::_pending_op_count = 0;
72 volatile bool MemTracker::_slowdown_calling_thread = false;
73 debug_only(intx MemTracker::_main_thread_tid = 0;)
74 NOT_PRODUCT(volatile jint MemTracker::_pending_recorder_count = 0;)
75
76 void MemTracker::init_tracking_options(const char* option_line) {
77 _tracking_level = NMT_off;
78 if (strcmp(option_line, "=summary") == 0) {
79 _tracking_level = NMT_summary;
80 } else if (strcmp(option_line, "=detail") == 0) {
81 _tracking_level = NMT_detail;
82 } else if (strcmp(option_line, "=off") != 0) {
83 vm_exit_during_initialization("Syntax error, expecting -XX:NativeMemoryTracking=[off|summary|detail]", NULL);
84 }
85 }
86
87 // first phase of bootstrapping, when VM is still in single-threaded mode.
88 void MemTracker::bootstrap_single_thread() {
89 if (_tracking_level > NMT_off) {
90 assert(_state == NMT_uninited, "wrong state");
91
312 void MemTracker::release_thread_recorder(MemRecorder* rec) {
313 assert(rec != NULL, "null recorder");
314 // we don't want to pool too many recorders
315 rec->set_next(NULL);
316 if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) {
317 delete rec;
318 return;
319 }
320
321 rec->clear();
322 MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders);
323 rec->set_next(cur_head);
324 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders,
325 (void*)cur_head)) {
326 cur_head = const_cast<MemRecorder*>(_pooled_recorders);
327 rec->set_next(cur_head);
328 }
329 Atomic::inc(&_pooled_recorder_count);
330 }
331
332 // write a record to proper recorder. No lock can be taken from this method
333 // down.
334 void MemTracker::write_tracking_record(address addr, MEMFLAGS flags,
335 size_t size, jint seq, address pc, JavaThread* thread) {
336
337 MemRecorder* rc = get_thread_recorder(thread);
338 if (rc != NULL) {
339 rc->record(addr, flags, size, seq, pc);
340 }
341 }
342
343 /**
344 * enqueue a recorder to pending queue
345 */
346 void MemTracker::enqueue_pending_recorder(MemRecorder* rec) {
347 assert(rec != NULL, "null recorder");
348
349 // we are shutting down, so just delete it
350 if (shutdown_in_progress()) {
351 rec->set_next(NULL);
352 delete rec;
353 return;
354 }
355
356 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
357 rec->set_next(cur_head);
358 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue,
359 (void*)cur_head)) {
384 // Some GC tests hit large number of safepoints in short period of time
385 // without meaningful activities. We should prevent going to
386 // sync point in these cases, which can potentially exhaust generation buffer.
387 // Here is the factots to determine if we should go into sync point:
388 // 1. not to overflow sequence number
389 // 2. if we are in danger to overflow generation buffer
390 // 3. how many safepoints we already skipped sync point
391 if (_state == NMT_started) {
392 // worker thread is not ready, no one can manage generation
393 // buffer, so skip this safepoint
394 if (_worker_thread == NULL) return;
395
396 if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) {
397 int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint;
398 int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS;
399 if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) {
400 _sync_point_skip_count ++;
401 return;
402 }
403 }
404 {
405 // This method is running at safepoint, with ThreadCritical lock,
406 // it should guarantee that NMT is fully sync-ed.
407 ThreadCritical tc;
408
409 // We can NOT execute NMT sync-point if there are pending tracking ops.
410 if (_pending_op_count == 0) {
411 SequenceGenerator::reset();
412 _sync_point_skip_count = 0;
413
414 // walk all JavaThreads to collect recorders
415 SyncThreadRecorderClosure stc;
416 Threads::threads_do(&stc);
417
418 _thread_count = stc.get_thread_count();
419 MemRecorder* pending_recorders = get_pending_recorders();
420
421 if (_global_recorder != NULL) {
422 _global_recorder->set_next(pending_recorders);
423 pending_recorders = _global_recorder;
424 _global_recorder = NULL;
425 }
426
427 // see if NMT has too many outstanding recorder instances, it usually
428 // means that worker thread is lagging behind in processing them.
429 if (!AutoShutdownNMT) {
430 _slowdown_calling_thread = (MemRecorder::_instance_count > MAX_RECORDER_THREAD_RATIO * _thread_count);
431 }
432
433 // check _worker_thread with lock to avoid racing condition
434 if (_worker_thread != NULL) {
435 _worker_thread->at_sync_point(pending_recorders, InstanceKlass::number_of_instance_classes());
436 }
437 assert(SequenceGenerator::peek() == 1, "Should not have memory activities during sync-point");
438 } else {
439 _sync_point_skip_count ++;
440 }
441 }
442 }
443
444 // now, it is the time to shut whole things off
445 if (_state == NMT_final_shutdown) {
446 // walk all JavaThreads to delete all recorders
447 SyncThreadRecorderClosure stc;
448 Threads::threads_do(&stc);
449 // delete global recorder
450 {
451 ThreadCritical tc;
452 if (_global_recorder != NULL) {
453 delete _global_recorder;
454 _global_recorder = NULL;
455 }
456 }
457 MemRecorder* pending_recorders = get_pending_recorders();
458 if (pending_recorders != NULL) {
459 delete pending_recorders;
460 }
609 st->print_cr("\tqueued recorder count = %d", _pending_recorder_count);
610 st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count);
611 if (_worker_thread != NULL) {
612 st->print_cr("\tWorker thread:");
613 st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count);
614 st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders());
615 st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count);
616 } else {
617 st->print_cr("\tWorker thread is not started");
618 }
619 st->print_cr(" ");
620
621 if (_snapshot != NULL) {
622 _snapshot->print_snapshot_stats(st);
623 } else {
624 st->print_cr("No snapshot");
625 }
626 }
627 #endif
628
629
630 // Tracker Implementation
631
632 /*
633 * Create a tracker.
634 * This is a fairly complicated constructor, as it has to make two important decisions:
635 * 1) Does it need to take ThreadCritical lock to write tracking record
636 * 2) Does it need to pre-reserve a sequence number for the tracking record
637 *
638 * The rules to determine if ThreadCritical is needed:
639 * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
640 * still in single thread mode.
641 * 2. For all threads other than JavaThread, ThreadCritical is needed
642 * to write to recorders to global recorder.
643 * 3. For JavaThreads that are not longer visible by safepoint, also
644 * need to take ThreadCritical and records are written to global
645 * recorders, since these threads are NOT walked by Threads.do_thread().
646 * 4. JavaThreads that are running in safepoint-safe states do not stop
647 * for safepoints, ThreadCritical lock shoul be taken to write
648 * memory records.
649 * 5. JavaThreads that are running in VM state do not need any lock and
650 * records are written to per-thread recorders.
651 * 6. For a thread has yet to attach VM 'Thread', they need to take
652 * ThreadCritical to write to global recorder.
653 *
654 * The memory operations that need pre-reserve sequence numbers:
655 * The memory operations that "release" memory blocks and the
656 * operations can fail, need to pre-reserve sequence number. They
657 * are realloc, uncommit and release.
658 *
659 * The reason for pre-reserve sequence number, is to prevent race condition:
660 * Thread 1 Thread 2
661 * <release>
662 * <allocate>
663 * <write allocate record>
664 * <write release record>
665 * if Thread 2 happens to obtain the memory address Thread 1 just released,
666 * then NMT can mistakenly report the memory is free.
667 *
668 * Noticeably, free() does not need pre-reserve sequence number, because the call
669 * does not fail, so we can alway write "release" record before the memory is actaully
670 * freed.
671 *
672 * For realloc, uncommit and release, following coding pattern should be used:
673 *
674 * MemTracker::Tracker tkr = MemTracker::get_realloc_tracker();
675 * ptr = ::realloc(...);
676 * if (ptr == NULL) {
677 * tkr.record(...)
678 * }
679 *
680 * MemTracker::Tracker tkr = MemTracker::get_virtual_memory_uncommit_tracker();
681 * if (uncommit(...)) {
682 * tkr.record(...);
683 * }
684 *
685 * MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker();
686 * if (release(...)) {
687 * tkr.record(...);
688 * }
689 *
690 * Since pre-reserved sequence number is only good for the generation that it is acquired,
691 * when there is pending Tracker that reserved sequence number, NMT sync-point has
692 * to be skipped to prevent from advancing generation. This is done by inc and dec
693 * MemTracker::_pending_op_count, when MemTracker::_pending_op_count > 0, NMT sync-point is skipped.
694 * Not all pre-reservation of sequence number will increment pending op count. For JavaThreads
695 * that honor safepoints, safepoint can not occur during the memory operations, so the
696 * pre-reserved sequence number won't cross the generation boundry.
697 */
698 MemTracker::Tracker::Tracker(MemoryOperation op, Thread* thr) {
699 _op = NoOp;
700 _seq = 0;
701 if (MemTracker::is_on()) {
702 _java_thread = NULL;
703 _op = op;
704
705 // figure out if ThreadCritical lock is needed to write this operation
706 // to MemTracker
707 if (MemTracker::is_single_threaded_bootstrap()) {
708 thr = NULL;
709 } else if (thr == NULL) {
710 // don't use Thread::current(), since it is possible that
711 // the calling thread has yet to attach to VM 'Thread',
712 // which will result assertion failure
713 thr = ThreadLocalStorage::thread();
714 }
715
716 if (thr != NULL) {
717 // Check NMT load
718 MemTracker::check_NMT_load(thr);
719
720 if (thr->is_Java_thread() && ((JavaThread*)thr)->is_safepoint_visible()) {
721 _java_thread = (JavaThread*)thr;
722 JavaThreadState state = _java_thread->thread_state();
723 // JavaThreads that are safepoint safe, can run through safepoint,
724 // so ThreadCritical is needed to ensure no threads at safepoint create
725 // new records while the records are being gathered and the sequence number is changing
726 _need_thread_critical_lock =
727 SafepointSynchronize::safepoint_safe(_java_thread, state);
728 } else {
729 _need_thread_critical_lock = true;
730 }
731 } else {
732 _need_thread_critical_lock
733 = !MemTracker::is_single_threaded_bootstrap();
734 }
735
736 // see if we need to pre-reserve sequence number for this operation
737 if (_op == Realloc || _op == Uncommit || _op == Release) {
738 if (_need_thread_critical_lock) {
739 ThreadCritical tc;
740 MemTracker::inc_pending_op_count();
741 _seq = SequenceGenerator::next();
742 } else {
743 // for the threads that honor safepoints, no safepoint can occur
744 // during the lifespan of tracker, so we don't need to increase
745 // pending op count.
746 _seq = SequenceGenerator::next();
747 }
748 }
749 }
750 }
751
752 void MemTracker::Tracker::discard() {
753 if (MemTracker::is_on() && _seq != 0) {
754 if (_need_thread_critical_lock) {
755 ThreadCritical tc;
756 MemTracker::dec_pending_op_count();
757 }
758 _seq = 0;
759 }
760 }
761
762
763 void MemTracker::Tracker::record(address old_addr, address new_addr, size_t size,
764 MEMFLAGS flags, address pc) {
765 assert(old_addr != NULL && new_addr != NULL, "Sanity check");
766 assert(_op == Realloc || _op == NoOp, "Wrong call");
767 if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) {
768 assert(_seq > 0, "Need pre-reserve sequence number");
769 if (_need_thread_critical_lock) {
770 ThreadCritical tc;
771 // free old address, use pre-reserved sequence number
772 MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(),
773 0, _seq, pc, _java_thread);
774 MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(),
775 size, SequenceGenerator::next(), pc, _java_thread);
776 // decrement MemTracker pending_op_count
777 MemTracker::dec_pending_op_count();
778 } else {
779 // free old address, use pre-reserved sequence number
780 MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(),
781 0, _seq, pc, _java_thread);
782 MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(),
783 size, SequenceGenerator::next(), pc, _java_thread);
784 }
785 _seq = 0;
786 }
787 }
788
789 void MemTracker::Tracker::record(address addr, size_t size, MEMFLAGS flags, address pc) {
790 // OOM already?
791 if (addr == NULL) return;
792
793 if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) {
794 bool pre_reserved_seq = (_seq != 0);
795 address pc = CALLER_CALLER_PC;
796 MEMFLAGS orig_flags = flags;
797
798 // or the tagging flags
799 switch(_op) {
800 case Malloc:
801 flags |= MemPointerRecord::malloc_tag();
802 break;
803 case Free:
804 flags = MemPointerRecord::free_tag();
805 break;
806 case Realloc:
807 fatal("Use the other Tracker::record()");
808 break;
809 case Reserve:
810 case ReserveAndCommit:
811 flags |= MemPointerRecord::virtual_memory_reserve_tag();
812 break;
813 case Commit:
814 flags = MemPointerRecord::virtual_memory_commit_tag();
815 break;
816 case Type:
817 flags |= MemPointerRecord::virtual_memory_type_tag();
818 break;
819 case Uncommit:
820 assert(pre_reserved_seq, "Need pre-reserve sequence number");
821 flags = MemPointerRecord::virtual_memory_uncommit_tag();
822 break;
823 case Release:
824 assert(pre_reserved_seq, "Need pre-reserve sequence number");
825 flags = MemPointerRecord::virtual_memory_release_tag();
826 break;
827 case ArenaSize:
828 // a bit of hack here, add a small postive offset to arena
829 // address for its size record, so the size record is sorted
830 // right after arena record.
831 flags = MemPointerRecord::arena_size_tag();
832 addr += sizeof(void*);
833 break;
834 case StackRelease:
835 flags = MemPointerRecord::virtual_memory_release_tag();
836 break;
837 default:
838 ShouldNotReachHere();
839 }
840
841 // write memory tracking record
842 if (_need_thread_critical_lock) {
843 ThreadCritical tc;
844 if (_seq == 0) _seq = SequenceGenerator::next();
845 MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread);
846 if (_op == ReserveAndCommit) {
847 MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(),
848 size, SequenceGenerator::next(), pc, _java_thread);
849 }
850 if (pre_reserved_seq) MemTracker::dec_pending_op_count();
851 } else {
852 if (_seq == 0) _seq = SequenceGenerator::next();
853 MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread);
854 if (_op == ReserveAndCommit) {
855 MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(),
856 size, SequenceGenerator::next(), pc, _java_thread);
857 }
858 }
859 _seq = 0;
860 }
861 }
862
|