memTracker.cpp revision 3957
3863N/A/*
3863N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3863N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3863N/A *
3863N/A * This code is free software; you can redistribute it and/or modify it
3863N/A * under the terms of the GNU General Public License version 2 only, as
3863N/A * published by the Free Software Foundation.
3863N/A *
3863N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3863N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3863N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3863N/A * version 2 for more details (a copy is included in the LICENSE file that
3863N/A * accompanied this code).
3863N/A *
3863N/A * You should have received a copy of the GNU General Public License version
3863N/A * 2 along with this work; if not, write to the Free Software Foundation,
3863N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3863N/A *
3863N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3863N/A * or visit www.oracle.com if you need additional information or have any
3863N/A * questions.
3863N/A *
3863N/A */
3863N/A#include "precompiled.hpp"
3863N/A
3863N/A#include "runtime/atomic.hpp"
3863N/A#include "runtime/interfaceSupport.hpp"
3863N/A#include "runtime/mutexLocker.hpp"
3863N/A#include "runtime/safepoint.hpp"
3863N/A#include "runtime/threadCritical.hpp"
3863N/A#include "services/memPtr.hpp"
3863N/A#include "services/memReporter.hpp"
3863N/A#include "services/memTracker.hpp"
3863N/A#include "utilities/decoder.hpp"
3863N/A#include "utilities/globalDefinitions.hpp"
3863N/A
3863N/Abool NMT_track_callsite = false;
3863N/A
3863N/A// walk all 'known' threads at NMT sync point, and collect their recorders
3863N/Avoid SyncThreadRecorderClosure::do_thread(Thread* thread) {
3863N/A assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
3863N/A if (thread->is_Java_thread()) {
3863N/A JavaThread* javaThread = (JavaThread*)thread;
3863N/A MemRecorder* recorder = javaThread->get_recorder();
3863N/A if (recorder != NULL) {
3863N/A MemTracker::enqueue_pending_recorder(recorder);
3863N/A javaThread->set_recorder(NULL);
3863N/A }
3863N/A }
3863N/A _thread_count ++;
3863N/A}
3863N/A
3863N/A
3863N/AMemRecorder* MemTracker::_global_recorder = NULL;
3863N/AMemSnapshot* MemTracker::_snapshot = NULL;
3863N/AMemBaseline MemTracker::_baseline;
3898N/AMutex* MemTracker::_query_lock = NULL;
3863N/Avolatile MemRecorder* MemTracker::_merge_pending_queue = NULL;
3863N/Avolatile MemRecorder* MemTracker::_pooled_recorders = NULL;
3863N/AMemTrackWorker* MemTracker::_worker_thread = NULL;
3863N/Aint MemTracker::_sync_point_skip_count = 0;
3863N/AMemTracker::NMTLevel MemTracker::_tracking_level = MemTracker::NMT_off;
3863N/Avolatile MemTracker::NMTStates MemTracker::_state = NMT_uninited;
3863N/AMemTracker::ShutdownReason MemTracker::_reason = NMT_shutdown_none;
3863N/Aint MemTracker::_thread_count = 255;
3863N/Avolatile jint MemTracker::_pooled_recorder_count = 0;
3863N/Adebug_only(intx MemTracker::_main_thread_tid = 0;)
3957N/ANOT_PRODUCT(volatile jint MemTracker::_pending_recorder_count = 0;)
3863N/A
3863N/Avoid MemTracker::init_tracking_options(const char* option_line) {
3863N/A _tracking_level = NMT_off;
3863N/A if (strncmp(option_line, "=summary", 8) == 0) {
3863N/A _tracking_level = NMT_summary;
3863N/A } else if (strncmp(option_line, "=detail", 8) == 0) {
3863N/A _tracking_level = NMT_detail;
3863N/A }
3863N/A}
3863N/A
3863N/A// first phase of bootstrapping, when VM is still in single-threaded mode.
3863N/Avoid MemTracker::bootstrap_single_thread() {
3863N/A if (_tracking_level > NMT_off) {
3863N/A assert(_state == NMT_uninited, "wrong state");
3863N/A
3863N/A // NMT is not supported with UseMallocOnly is on. NMT can NOT
3863N/A // handle the amount of malloc data without significantly impacting
3863N/A // runtime performance when this flag is on.
3863N/A if (UseMallocOnly) {
3863N/A shutdown(NMT_use_malloc_only);
3863N/A return;
3863N/A }
3863N/A
3898N/A _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");
3898N/A if (_query_lock == NULL) {
3898N/A shutdown(NMT_out_of_memory);
3898N/A return;
3898N/A }
3898N/A
3863N/A debug_only(_main_thread_tid = os::current_thread_id();)
3863N/A _state = NMT_bootstrapping_single_thread;
3863N/A NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
3863N/A }
3863N/A}
3863N/A
3863N/A// second phase of bootstrapping, when VM is about to or already entered multi-theaded mode.
3863N/Avoid MemTracker::bootstrap_multi_thread() {
3863N/A if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) {
3863N/A // create nmt lock for multi-thread execution
3863N/A assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
3863N/A _state = NMT_bootstrapping_multi_thread;
3863N/A NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
3863N/A }
3863N/A}
3863N/A
3863N/A// fully start nmt
3863N/Avoid MemTracker::start() {
3863N/A // Native memory tracking is off from command line option
3863N/A if (_tracking_level == NMT_off || shutdown_in_progress()) return;
3863N/A
3863N/A assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
3863N/A assert(_state == NMT_bootstrapping_multi_thread, "wrong state");
3863N/A
3863N/A _snapshot = new (std::nothrow)MemSnapshot();
3863N/A if (_snapshot != NULL && !_snapshot->out_of_memory()) {
3863N/A if (start_worker()) {
3863N/A _state = NMT_started;
3863N/A NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
3863N/A return;
3863N/A }
3863N/A }
3863N/A
3863N/A // fail to start native memory tracking, shut it down
3863N/A shutdown(NMT_initialization);
3863N/A}
3863N/A
3863N/A/**
3863N/A * Shutting down native memory tracking.
3863N/A * We can not shutdown native memory tracking immediately, so we just
3863N/A * setup shutdown pending flag, every native memory tracking component
3863N/A * should orderly shut itself down.
3863N/A *
3863N/A * The shutdown sequences:
3863N/A * 1. MemTracker::shutdown() sets MemTracker to shutdown pending state
3863N/A * 2. Worker thread calls MemTracker::final_shutdown(), which transites
3863N/A * MemTracker to final shutdown state.
3863N/A * 3. At sync point, MemTracker does final cleanup, before sets memory
3863N/A * tracking level to off to complete shutdown.
3863N/A */
3863N/Avoid MemTracker::shutdown(ShutdownReason reason) {
3863N/A if (_tracking_level == NMT_off) return;
3863N/A
3863N/A if (_state <= NMT_bootstrapping_single_thread) {
3863N/A // we still in single thread mode, there is not contention
3863N/A _state = NMT_shutdown_pending;
3863N/A _reason = reason;
3863N/A } else {
3863N/A // we want to know who initialized shutdown
3863N/A if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending,
3863N/A (jint*)&_state, (jint)NMT_started)) {
3863N/A _reason = reason;
3863N/A }
3863N/A }
3863N/A}
3863N/A
3863N/A// final phase of shutdown
3863N/Avoid MemTracker::final_shutdown() {
3863N/A // delete all pending recorders and pooled recorders
3863N/A delete_all_pending_recorders();
3863N/A delete_all_pooled_recorders();
3863N/A
3863N/A {
3863N/A // shared baseline and snapshot are the only objects needed to
3863N/A // create query results
3898N/A MutexLockerEx locker(_query_lock, true);
3863N/A // cleanup baseline data and snapshot
3863N/A _baseline.clear();
3863N/A delete _snapshot;
3863N/A _snapshot = NULL;
3863N/A }
3863N/A
3863N/A // shutdown shared decoder instance, since it is only
3863N/A // used by native memory tracking so far.
3863N/A Decoder::shutdown();
3863N/A
3863N/A MemTrackWorker* worker = NULL;
3863N/A {
3863N/A ThreadCritical tc;
3863N/A // can not delete worker inside the thread critical
3863N/A if (_worker_thread != NULL && Thread::current() == _worker_thread) {
3863N/A worker = _worker_thread;
3863N/A _worker_thread = NULL;
3863N/A }
3863N/A }
3863N/A if (worker != NULL) {
3863N/A delete worker;
3863N/A }
3863N/A _state = NMT_final_shutdown;
3863N/A}
3863N/A
3863N/A// delete all pooled recorders
3863N/Avoid MemTracker::delete_all_pooled_recorders() {
3863N/A // free all pooled recorders
3863N/A volatile MemRecorder* cur_head = _pooled_recorders;
3863N/A if (cur_head != NULL) {
3863N/A MemRecorder* null_ptr = NULL;
3863N/A while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr,
3863N/A (void*)&_pooled_recorders, (void*)cur_head)) {
3863N/A cur_head = _pooled_recorders;
3863N/A }
3863N/A if (cur_head != NULL) {
3863N/A delete cur_head;
3863N/A _pooled_recorder_count = 0;
3863N/A }
3863N/A }
3863N/A}
3863N/A
3863N/A// delete all recorders in pending queue
3863N/Avoid MemTracker::delete_all_pending_recorders() {
3863N/A // free all pending recorders
3863N/A MemRecorder* pending_head = get_pending_recorders();
3863N/A if (pending_head != NULL) {
3863N/A delete pending_head;
3863N/A }
3863N/A}
3863N/A
3863N/A/*
3863N/A * retrieve per-thread recorder of specified thread.
3863N/A * if thread == NULL, it means global recorder
3863N/A */
3863N/AMemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) {
3863N/A if (shutdown_in_progress()) return NULL;
3863N/A
3863N/A MemRecorder* rc;
3863N/A if (thread == NULL) {
3863N/A rc = _global_recorder;
3863N/A } else {
3863N/A rc = thread->get_recorder();
3863N/A }
3863N/A
3863N/A if (rc != NULL && rc->is_full()) {
3863N/A enqueue_pending_recorder(rc);
3863N/A rc = NULL;
3863N/A }
3863N/A
3863N/A if (rc == NULL) {
3863N/A rc = get_new_or_pooled_instance();
3863N/A if (thread == NULL) {
3863N/A _global_recorder = rc;
3863N/A } else {
3863N/A thread->set_recorder(rc);
3863N/A }
3863N/A }
3863N/A return rc;
3863N/A}
3863N/A
3863N/A/*
3863N/A * get a per-thread recorder from pool, or create a new one if
3863N/A * there is not one available.
3863N/A */
3863N/AMemRecorder* MemTracker::get_new_or_pooled_instance() {
3863N/A MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders);
3863N/A if (cur_head == NULL) {
3863N/A MemRecorder* rec = new (std::nothrow)MemRecorder();
3863N/A if (rec == NULL || rec->out_of_memory()) {
3863N/A shutdown(NMT_out_of_memory);
3863N/A if (rec != NULL) {
3863N/A delete rec;
3863N/A rec = NULL;
3863N/A }
3863N/A }
3863N/A return rec;
3863N/A } else {
3863N/A MemRecorder* next_head = cur_head->next();
3863N/A if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders,
3863N/A (void*)cur_head)) {
3863N/A return get_new_or_pooled_instance();
3863N/A }
3863N/A cur_head->set_next(NULL);
3863N/A Atomic::dec(&_pooled_recorder_count);
3863N/A debug_only(cur_head->set_generation();)
3863N/A return cur_head;
3863N/A }
3863N/A}
3863N/A
3863N/A/*
3863N/A * retrieve all recorders in pending queue, and empty the queue
3863N/A */
3863N/AMemRecorder* MemTracker::get_pending_recorders() {
3863N/A MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
3863N/A MemRecorder* null_ptr = NULL;
3863N/A while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue,
3863N/A (void*)cur_head)) {
3863N/A cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
3863N/A }
3957N/A NOT_PRODUCT(Atomic::store(0, &_pending_recorder_count));
3863N/A return cur_head;
3863N/A}
3863N/A
3863N/A/*
3863N/A * release a recorder to recorder pool.
3863N/A */
3863N/Avoid MemTracker::release_thread_recorder(MemRecorder* rec) {
3863N/A assert(rec != NULL, "null recorder");
3863N/A // we don't want to pool too many recorders
3863N/A rec->set_next(NULL);
3863N/A if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) {
3863N/A delete rec;
3863N/A return;
3863N/A }
3863N/A
3863N/A rec->clear();
3863N/A MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders);
3863N/A rec->set_next(cur_head);
3863N/A while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders,
3863N/A (void*)cur_head)) {
3863N/A cur_head = const_cast<MemRecorder*>(_pooled_recorders);
3863N/A rec->set_next(cur_head);
3863N/A }
3863N/A Atomic::inc(&_pooled_recorder_count);
3863N/A}
3863N/A
3863N/A/*
3863N/A * This is the most important method in whole nmt implementation.
3863N/A *
3863N/A * Create a memory record.
3863N/A * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
3863N/A * still in single thread mode.
3863N/A * 2. For all threads other than JavaThread, ThreadCritical is needed
3863N/A * to write to recorders to global recorder.
3863N/A * 3. For JavaThreads that are not longer visible by safepoint, also
3863N/A * need to take ThreadCritical and records are written to global
3863N/A * recorders, since these threads are NOT walked by Threads.do_thread().
3863N/A * 4. JavaThreads that are running in native state, have to transition
3863N/A * to VM state before writing to per-thread recorders.
3863N/A * 5. JavaThreads that are running in VM state do not need any lock and
3863N/A * records are written to per-thread recorders.
3863N/A * 6. For a thread has yet to attach VM 'Thread', they need to take
3863N/A * ThreadCritical to write to global recorder.
3863N/A *
3863N/A * Important note:
3863N/A * NO LOCK should be taken inside ThreadCritical lock !!!
3863N/A */
3863N/Avoid MemTracker::create_memory_record(address addr, MEMFLAGS flags,
3863N/A size_t size, address pc, Thread* thread) {
3863N/A if (!shutdown_in_progress()) {
3863N/A // single thread, we just write records direct to global recorder,'
3863N/A // with any lock
3863N/A if (_state == NMT_bootstrapping_single_thread) {
3863N/A assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
3863N/A thread = NULL;
3863N/A } else {
3863N/A if (thread == NULL) {
3863N/A // don't use Thread::current(), since it is possible that
3863N/A // the calling thread has yet to attach to VM 'Thread',
3863N/A // which will result assertion failure
3863N/A thread = ThreadLocalStorage::thread();
3863N/A }
3863N/A }
3863N/A
3863N/A if (thread != NULL) {
3863N/A if (thread->is_Java_thread() && ((JavaThread*)thread)->is_safepoint_visible()) {
3897N/A JavaThread* java_thread = static_cast<JavaThread*>(thread);
3897N/A JavaThreadState state = java_thread->thread_state();
3897N/A if (SafepointSynchronize::safepoint_safe(java_thread, state)) {
3897N/A // JavaThreads that are safepoint safe, can run through safepoint,
3897N/A // so ThreadCritical is needed to ensure no threads at safepoint create
3897N/A // new records while the records are being gathered and the sequence number is changing
3897N/A ThreadCritical tc;
3897N/A create_record_in_recorder(addr, flags, size, pc, java_thread);
3863N/A } else {
3897N/A create_record_in_recorder(addr, flags, size, pc, java_thread);
3863N/A }
3863N/A } else {
3863N/A // other threads, such as worker and watcher threads, etc. need to
3863N/A // take ThreadCritical to write to global recorder
3863N/A ThreadCritical tc;
3863N/A create_record_in_recorder(addr, flags, size, pc, NULL);
3863N/A }
3863N/A } else {
3863N/A if (_state == NMT_bootstrapping_single_thread) {
3863N/A // single thread, no lock needed
3863N/A create_record_in_recorder(addr, flags, size, pc, NULL);
3863N/A } else {
3863N/A // for thread has yet to attach VM 'Thread', we can not use VM mutex.
3863N/A // use native thread critical instead
3863N/A ThreadCritical tc;
3863N/A create_record_in_recorder(addr, flags, size, pc, NULL);
3863N/A }
3863N/A }
3863N/A }
3863N/A}
3863N/A
3863N/A// write a record to proper recorder. No lock can be taken from this method
3863N/A// down.
3863N/Avoid MemTracker::create_record_in_recorder(address addr, MEMFLAGS flags,
3897N/A size_t size, address pc, JavaThread* thread) {
3863N/A
3897N/A MemRecorder* rc = get_thread_recorder(thread);
3863N/A if (rc != NULL) {
3863N/A rc->record(addr, flags, size, pc);
3863N/A }
3863N/A}
3863N/A
3863N/A/**
3863N/A * enqueue a recorder to pending queue
3863N/A */
3863N/Avoid MemTracker::enqueue_pending_recorder(MemRecorder* rec) {
3863N/A assert(rec != NULL, "null recorder");
3863N/A
3863N/A // we are shutting down, so just delete it
3863N/A if (shutdown_in_progress()) {
3863N/A rec->set_next(NULL);
3863N/A delete rec;
3863N/A return;
3863N/A }
3863N/A
3863N/A MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
3863N/A rec->set_next(cur_head);
3863N/A while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue,
3863N/A (void*)cur_head)) {
3863N/A cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
3863N/A rec->set_next(cur_head);
3863N/A }
3957N/A NOT_PRODUCT(Atomic::inc(&_pending_recorder_count);)
3863N/A}
3863N/A
3863N/A/*
3863N/A * The method is called at global safepoint
3863N/A * during it synchronization process.
3863N/A * 1. enqueue all JavaThreads' per-thread recorders
3863N/A * 2. enqueue global recorder
3863N/A * 3. retrieve all pending recorders
3863N/A * 4. reset global sequence number generator
3863N/A * 5. call worker's sync
3863N/A */
3863N/A#define MAX_SAFEPOINTS_TO_SKIP 128
3863N/A#define SAFE_SEQUENCE_THRESHOLD 30
3863N/A#define HIGH_GENERATION_THRESHOLD 60
3863N/A
3863N/Avoid MemTracker::sync() {
3863N/A assert(_tracking_level > NMT_off, "NMT is not enabled");
3863N/A assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
3863N/A
3863N/A // Some GC tests hit large number of safepoints in short period of time
3863N/A // without meaningful activities. We should prevent going to
3863N/A // sync point in these cases, which can potentially exhaust generation buffer.
3863N/A // Here is the factots to determine if we should go into sync point:
3863N/A // 1. not to overflow sequence number
3863N/A // 2. if we are in danger to overflow generation buffer
3863N/A // 3. how many safepoints we already skipped sync point
3863N/A if (_state == NMT_started) {
3863N/A // worker thread is not ready, no one can manage generation
3863N/A // buffer, so skip this safepoint
3863N/A if (_worker_thread == NULL) return;
3863N/A
3863N/A if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) {
3863N/A int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint;
3863N/A int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS;
3863N/A if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) {
3863N/A _sync_point_skip_count ++;
3863N/A return;
3863N/A }
3863N/A }
3863N/A _sync_point_skip_count = 0;
3863N/A {
3863N/A // This method is running at safepoint, with ThreadCritical lock,
3863N/A // it should guarantee that NMT is fully sync-ed.
3863N/A ThreadCritical tc;
3897N/A
3897N/A // walk all JavaThreads to collect recorders
3897N/A SyncThreadRecorderClosure stc;
3897N/A Threads::threads_do(&stc);
3897N/A
3897N/A _thread_count = stc.get_thread_count();
3897N/A MemRecorder* pending_recorders = get_pending_recorders();
3897N/A
3863N/A if (_global_recorder != NULL) {
3863N/A _global_recorder->set_next(pending_recorders);
3863N/A pending_recorders = _global_recorder;
3863N/A _global_recorder = NULL;
3863N/A }
3863N/A SequenceGenerator::reset();
3863N/A // check _worker_thread with lock to avoid racing condition
3863N/A if (_worker_thread != NULL) {
3863N/A _worker_thread->at_sync_point(pending_recorders);
3863N/A }
3863N/A }
3863N/A }
3863N/A
3863N/A // now, it is the time to shut whole things off
3863N/A if (_state == NMT_final_shutdown) {
3863N/A // walk all JavaThreads to delete all recorders
3863N/A SyncThreadRecorderClosure stc;
3863N/A Threads::threads_do(&stc);
3863N/A // delete global recorder
3863N/A {
3863N/A ThreadCritical tc;
3863N/A if (_global_recorder != NULL) {
3863N/A delete _global_recorder;
3863N/A _global_recorder = NULL;
3863N/A }
3863N/A }
3897N/A MemRecorder* pending_recorders = get_pending_recorders();
3897N/A if (pending_recorders != NULL) {
3897N/A delete pending_recorders;
3897N/A }
3897N/A // try at a later sync point to ensure MemRecorder instance drops to zero to
3897N/A // completely shutdown NMT
3897N/A if (MemRecorder::_instance_count == 0) {
3897N/A _state = NMT_shutdown;
3897N/A _tracking_level = NMT_off;
3897N/A }
3863N/A }
3863N/A}
3863N/A
3863N/A/*
3863N/A * Start worker thread.
3863N/A */
3863N/Abool MemTracker::start_worker() {
3863N/A assert(_worker_thread == NULL, "Just Check");
3863N/A _worker_thread = new (std::nothrow) MemTrackWorker();
3863N/A if (_worker_thread == NULL || _worker_thread->has_error()) {
3863N/A shutdown(NMT_initialization);
3863N/A return false;
3863N/A }
3863N/A _worker_thread->start();
3863N/A return true;
3863N/A}
3863N/A
3863N/A/*
3863N/A * We need to collect a JavaThread's per-thread recorder
3863N/A * before it exits.
3863N/A */
3863N/Avoid MemTracker::thread_exiting(JavaThread* thread) {
3863N/A if (is_on()) {
3863N/A MemRecorder* rec = thread->get_recorder();
3863N/A if (rec != NULL) {
3863N/A enqueue_pending_recorder(rec);
3863N/A thread->set_recorder(NULL);
3863N/A }
3863N/A }
3863N/A}
3863N/A
3863N/A// baseline current memory snapshot
3863N/Abool MemTracker::baseline() {
3898N/A MutexLockerEx lock(_query_lock, true);
3863N/A MemSnapshot* snapshot = get_snapshot();
3863N/A if (snapshot != NULL) {
3863N/A return _baseline.baseline(*snapshot, false);
3863N/A }
3863N/A return false;
3863N/A}
3863N/A
3863N/A// print memory usage from current snapshot
3863N/Abool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
3863N/A MemBaseline baseline;
3898N/A MutexLockerEx lock(_query_lock, true);
3863N/A MemSnapshot* snapshot = get_snapshot();
3863N/A if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
3863N/A BaselineReporter reporter(out, unit);
3863N/A reporter.report_baseline(baseline, summary_only);
3863N/A return true;
3863N/A }
3863N/A return false;
3863N/A}
3863N/A
3863N/A// compare memory usage between current snapshot and baseline
3863N/Abool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
3898N/A MutexLockerEx lock(_query_lock, true);
3863N/A if (_baseline.baselined()) {
3863N/A MemBaseline baseline;
3863N/A MemSnapshot* snapshot = get_snapshot();
3863N/A if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
3863N/A BaselineReporter reporter(out, unit);
3863N/A reporter.diff_baselines(baseline, _baseline, summary_only);
3863N/A return true;
3863N/A }
3863N/A }
3863N/A return false;
3863N/A}
3863N/A
3863N/A#ifndef PRODUCT
3863N/Avoid MemTracker::walk_stack(int toSkip, char* buf, int len) {
3863N/A int cur_len = 0;
3863N/A char tmp[1024];
3863N/A address pc;
3863N/A
3863N/A while (cur_len < len) {
3863N/A pc = os::get_caller_pc(toSkip + 1);
3863N/A if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) {
3863N/A jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp);
3863N/A cur_len = (int)strlen(buf);
3863N/A } else {
3863N/A buf[cur_len] = '\0';
3863N/A break;
3863N/A }
3863N/A toSkip ++;
3863N/A }
3863N/A}
3863N/A
3863N/Avoid MemTracker::print_tracker_stats(outputStream* st) {
3863N/A st->print_cr("\nMemory Tracker Stats:");
3863N/A st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num());
3863N/A st->print_cr("\tthead count = %d", _thread_count);
3863N/A st->print_cr("\tArena instance = %d", Arena::_instance_count);
3863N/A st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count);
3863N/A st->print_cr("\tqueued recorder count = %d", _pending_recorder_count);
3863N/A st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count);
3863N/A if (_worker_thread != NULL) {
3863N/A st->print_cr("\tWorker thread:");
3863N/A st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count);
3863N/A st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders());
3863N/A st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count);
3863N/A } else {
3863N/A st->print_cr("\tWorker thread is not started");
3863N/A }
3863N/A st->print_cr(" ");
3863N/A
3863N/A if (_snapshot != NULL) {
3863N/A _snapshot->print_snapshot_stats(st);
3863N/A } else {
3863N/A st->print_cr("No snapshot");
3863N/A }
3863N/A}
3863N/A#endif
3863N/A