3863N/A/*
4168N/A * Copyright (c) 2012, 2013, 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
4559N/A
4168N/A#include "oops/instanceKlass.hpp"
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"
4186N/A#include "runtime/vm_operations.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
4559N/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
4413N/AMemRecorder* volatile MemTracker::_global_recorder = NULL;
3863N/AMemSnapshot* MemTracker::_snapshot = NULL;
3863N/AMemBaseline MemTracker::_baseline;
3898N/AMutex* MemTracker::_query_lock = NULL;
4413N/AMemRecorder* volatile MemTracker::_merge_pending_queue = NULL;
4413N/AMemRecorder* volatile 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;
4186N/Avolatile unsigned long MemTracker::_processing_generation = 0;
4186N/Avolatile bool MemTracker::_worker_thread_idle = false;
4559N/Avolatile jint MemTracker::_pending_op_count = 0;
4311N/Avolatile bool MemTracker::_slowdown_calling_thread = false;
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;
4072N/A if (strcmp(option_line, "=summary") == 0) {
3863N/A _tracking_level = NMT_summary;
4072N/A } else if (strcmp(option_line, "=detail") == 0) {
3863N/A _tracking_level = NMT_detail;
4072N/A } else if (strcmp(option_line, "=off") != 0) {
4072N/A vm_exit_during_initialization("Syntax error, expecting -XX:NativeMemoryTracking=[off|summary|detail]", NULL);
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();
4410N/A if (_snapshot != NULL) {
4413N/A if (!_snapshot->out_of_memory() && start_worker(_snapshot)) {
3863N/A _state = NMT_started;
3863N/A NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
3863N/A return;
3863N/A }
4410N/A
4410N/A delete _snapshot;
4410N/A _snapshot = NULL;
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
4413N/A MemRecorder* volatile 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);
4186N/A 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// write a record to proper recorder. No lock can be taken from this method
3863N/A// down.
4559N/Avoid MemTracker::write_tracking_record(address addr, MEMFLAGS flags,
4559N/A size_t size, jint seq, address pc, JavaThread* thread) {
3863N/A
3897N/A MemRecorder* rc = get_thread_recorder(thread);
3863N/A if (rc != NULL) {
4559N/A rc->record(addr, flags, size, seq, 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
4311N/A#define MAX_RECORDER_THREAD_RATIO 30
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 {
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
4559N/A // We can NOT execute NMT sync-point if there are pending tracking ops.
4559N/A if (_pending_op_count == 0) {
4064N/A SequenceGenerator::reset();
4559N/A _sync_point_skip_count = 0;
4064N/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 }
4311N/A
4311N/A // see if NMT has too many outstanding recorder instances, it usually
4311N/A // means that worker thread is lagging behind in processing them.
4311N/A if (!AutoShutdownNMT) {
4311N/A _slowdown_calling_thread = (MemRecorder::_instance_count > MAX_RECORDER_THREAD_RATIO * _thread_count);
4311N/A }
4311N/A
3863N/A // check _worker_thread with lock to avoid racing condition
3863N/A if (_worker_thread != NULL) {
4168N/A _worker_thread->at_sync_point(pending_recorders, instanceKlass::number_of_instance_classes());
3863N/A }
4064N/A assert(SequenceGenerator::peek() == 1, "Should not have memory activities during sync-point");
4559N/A } else {
4559N/A _sync_point_skip_count ++;
4559N/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 */
4413N/Abool MemTracker::start_worker(MemSnapshot* snapshot) {
4413N/A assert(_worker_thread == NULL && _snapshot != NULL, "Just Check");
4413N/A _worker_thread = new (std::nothrow) MemTrackWorker(snapshot);
4413N/A if (_worker_thread == NULL) {
4413N/A return false;
4413N/A } else if (_worker_thread->has_error()) {
4413N/A delete _worker_thread;
4413N/A _worker_thread = NULL;
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() {
4477N/A MutexLocker lock(_query_lock);
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;
4477N/A MutexLocker lock(_query_lock);
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
4186N/A// Whitebox API for blocking until the current generation of NMT data has been merged
4186N/Abool MemTracker::wbtest_wait_for_data_merge() {
4186N/A // NMT can't be shutdown while we're holding _query_lock
4477N/A MutexLocker lock(_query_lock);
4186N/A assert(_worker_thread != NULL, "Invalid query");
4186N/A // the generation at query time, so NMT will spin till this generation is processed
4186N/A unsigned long generation_at_query_time = SequenceGenerator::current_generation();
4186N/A unsigned long current_processing_generation = _processing_generation;
4186N/A // if generation counter overflown
4186N/A bool generation_overflown = (generation_at_query_time < current_processing_generation);
4186N/A long generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation;
4186N/A // spin
4186N/A while (!shutdown_in_progress()) {
4186N/A if (!generation_overflown) {
4186N/A if (current_processing_generation > generation_at_query_time) {
4186N/A return true;
4186N/A }
4186N/A } else {
4186N/A assert(generations_to_wrap >= 0, "Sanity check");
4186N/A long current_generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation;
4186N/A assert(current_generations_to_wrap >= 0, "Sanity check");
4186N/A // to overflow an unsigned long should take long time, so to_wrap check should be sufficient
4186N/A if (current_generations_to_wrap > generations_to_wrap &&
4186N/A current_processing_generation > generation_at_query_time) {
4186N/A return true;
4186N/A }
4186N/A }
4186N/A
4186N/A // if worker thread is idle, but generation is not advancing, that means
4186N/A // there is not safepoint to let NMT advance generation, force one.
4186N/A if (_worker_thread_idle) {
4186N/A VM_ForceSafepoint vfs;
4186N/A VMThread::execute(&vfs);
4186N/A }
4186N/A MemSnapshot* snapshot = get_snapshot();
4186N/A if (snapshot == NULL) {
4186N/A return false;
4186N/A }
4186N/A snapshot->wait(1000);
4186N/A current_processing_generation = _processing_generation;
4186N/A }
4186N/A // We end up here if NMT is shutting down before our data has been merged
4186N/A return false;
4186N/A}
4186N/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) {
4477N/A MutexLocker lock(_query_lock);
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
4559N/A
4559N/A// Tracker Implementation
4559N/A
4559N/A/*
4559N/A * Create a tracker.
4559N/A * This is a fairly complicated constructor, as it has to make two important decisions:
4559N/A * 1) Does it need to take ThreadCritical lock to write tracking record
4559N/A * 2) Does it need to pre-reserve a sequence number for the tracking record
4559N/A *
4559N/A * The rules to determine if ThreadCritical is needed:
4559N/A * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
4559N/A * still in single thread mode.
4559N/A * 2. For all threads other than JavaThread, ThreadCritical is needed
4559N/A * to write to recorders to global recorder.
4559N/A * 3. For JavaThreads that are no longer visible by safepoint, also
4559N/A * need to take ThreadCritical and records are written to global
4559N/A * recorders, since these threads are NOT walked by Threads.do_thread().
4559N/A * 4. JavaThreads that are running in safepoint-safe states do not stop
4559N/A * for safepoints, ThreadCritical lock should be taken to write
4559N/A * memory records.
4559N/A * 5. JavaThreads that are running in VM state do not need any lock and
4559N/A * records are written to per-thread recorders.
4559N/A * 6. For a thread has yet to attach VM 'Thread', they need to take
4559N/A * ThreadCritical to write to global recorder.
4559N/A *
4559N/A * The memory operations that need pre-reserve sequence numbers:
4559N/A * The memory operations that "release" memory blocks and the
4559N/A * operations can fail, need to pre-reserve sequence number. They
4559N/A * are realloc, uncommit and release.
4559N/A *
4559N/A * The reason for pre-reserve sequence number, is to prevent race condition:
4559N/A * Thread 1 Thread 2
4559N/A * <release>
4559N/A * <allocate>
4559N/A * <write allocate record>
4559N/A * <write release record>
4559N/A * if Thread 2 happens to obtain the memory address Thread 1 just released,
4559N/A * then NMT can mistakenly report the memory is free.
4559N/A *
4559N/A * Noticeably, free() does not need pre-reserve sequence number, because the call
4559N/A * does not fail, so we can alway write "release" record before the memory is actaully
4559N/A * freed.
4559N/A *
4559N/A * For realloc, uncommit and release, following coding pattern should be used:
4559N/A *
4559N/A * MemTracker::Tracker tkr = MemTracker::get_realloc_tracker();
4559N/A * ptr = ::realloc(...);
4559N/A * if (ptr != NULL) {
4559N/A * tkr.record(...)
4559N/A * } else {
4559N/A * tkr.discard();
4559N/A * }
4559N/A *
4559N/A * MemTracker::Tracker tkr = MemTracker::get_virtual_memory_uncommit_tracker();
4559N/A * if (uncommit(...)) {
4559N/A * tkr.record(...);
4559N/A * } else {
4559N/A * tkr.discard();
4559N/A * }
4559N/A *
4559N/A * MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker();
4559N/A * if (release(...)) {
4559N/A * tkr.record(...);
4559N/A * } else {
4559N/A * tkr.discard();
4559N/A * }
4559N/A *
4559N/A * Since pre-reserved sequence number is only good for the generation that it is acquired,
4559N/A * when there is pending Tracker that reserved sequence number, NMT sync-point has
4559N/A * to be skipped to prevent from advancing generation. This is done by inc and dec
4559N/A * MemTracker::_pending_op_count, when MemTracker::_pending_op_count > 0, NMT sync-point is skipped.
4559N/A * Not all pre-reservation of sequence number will increment pending op count. For JavaThreads
4559N/A * that honor safepoints, safepoint can not occur during the memory operations, so the
4559N/A * pre-reserved sequence number won't cross the generation boundary.
4559N/A */
4559N/AMemTracker::Tracker::Tracker(MemoryOperation op, Thread* thr) {
4559N/A _op = NoOp;
4559N/A _seq = 0;
4559N/A if (MemTracker::is_on()) {
4559N/A _java_thread = NULL;
4559N/A _op = op;
4559N/A
4559N/A // figure out if ThreadCritical lock is needed to write this operation
4559N/A // to MemTracker
4559N/A if (MemTracker::is_single_threaded_bootstrap()) {
4559N/A thr = NULL;
4559N/A } else if (thr == NULL) {
4559N/A // don't use Thread::current(), since it is possible that
4559N/A // the calling thread has yet to attach to VM 'Thread',
4559N/A // which will result assertion failure
4559N/A thr = ThreadLocalStorage::thread();
4559N/A }
4559N/A
4559N/A if (thr != NULL) {
4559N/A // Check NMT load
4559N/A MemTracker::check_NMT_load(thr);
4559N/A
4559N/A if (thr->is_Java_thread() && ((JavaThread*)thr)->is_safepoint_visible()) {
4559N/A _java_thread = (JavaThread*)thr;
4559N/A JavaThreadState state = _java_thread->thread_state();
4559N/A // JavaThreads that are safepoint safe, can run through safepoint,
4559N/A // so ThreadCritical is needed to ensure no threads at safepoint create
4559N/A // new records while the records are being gathered and the sequence number is changing
4559N/A _need_thread_critical_lock =
4559N/A SafepointSynchronize::safepoint_safe(_java_thread, state);
4559N/A } else {
4559N/A _need_thread_critical_lock = true;
4559N/A }
4559N/A } else {
4559N/A _need_thread_critical_lock
4559N/A = !MemTracker::is_single_threaded_bootstrap();
4559N/A }
4559N/A
4559N/A // see if we need to pre-reserve sequence number for this operation
4559N/A if (_op == Realloc || _op == Uncommit || _op == Release) {
4559N/A if (_need_thread_critical_lock) {
4559N/A ThreadCritical tc;
4559N/A MemTracker::inc_pending_op_count();
4559N/A _seq = SequenceGenerator::next();
4559N/A } else {
4559N/A // for the threads that honor safepoints, no safepoint can occur
4559N/A // during the lifespan of tracker, so we don't need to increase
4559N/A // pending op count.
4559N/A _seq = SequenceGenerator::next();
4559N/A }
4559N/A }
4559N/A }
4559N/A}
4559N/A
4559N/Avoid MemTracker::Tracker::discard() {
4559N/A if (MemTracker::is_on() && _seq != 0) {
4559N/A if (_need_thread_critical_lock) {
4559N/A ThreadCritical tc;
4559N/A MemTracker::dec_pending_op_count();
4559N/A }
4559N/A _seq = 0;
4559N/A }
4559N/A}
4559N/A
4559N/A
4559N/Avoid MemTracker::Tracker::record(address old_addr, address new_addr, size_t size,
4559N/A MEMFLAGS flags, address pc) {
4559N/A assert(old_addr != NULL && new_addr != NULL, "Sanity check");
4559N/A assert(_op == Realloc || _op == NoOp, "Wrong call");
4559N/A if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) {
4559N/A assert(_seq > 0, "Need pre-reserve sequence number");
4559N/A if (_need_thread_critical_lock) {
4559N/A ThreadCritical tc;
4559N/A // free old address, use pre-reserved sequence number
4559N/A MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(),
4559N/A 0, _seq, pc, _java_thread);
4559N/A MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(),
4559N/A size, SequenceGenerator::next(), pc, _java_thread);
4559N/A // decrement MemTracker pending_op_count
4559N/A MemTracker::dec_pending_op_count();
4559N/A } else {
4559N/A // free old address, use pre-reserved sequence number
4559N/A MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(),
4559N/A 0, _seq, pc, _java_thread);
4559N/A MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(),
4559N/A size, SequenceGenerator::next(), pc, _java_thread);
4559N/A }
4559N/A _seq = 0;
4559N/A }
4559N/A}
4559N/A
4559N/Avoid MemTracker::Tracker::record(address addr, size_t size, MEMFLAGS flags, address pc) {
4559N/A // OOM already?
4559N/A if (addr == NULL) return;
4559N/A
4559N/A if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) {
4559N/A bool pre_reserved_seq = (_seq != 0);
4559N/A address pc = CALLER_CALLER_PC;
4559N/A MEMFLAGS orig_flags = flags;
4559N/A
4559N/A // or the tagging flags
4559N/A switch(_op) {
4559N/A case Malloc:
4559N/A flags |= MemPointerRecord::malloc_tag();
4559N/A break;
4559N/A case Free:
4559N/A flags = MemPointerRecord::free_tag();
4559N/A break;
4559N/A case Realloc:
4559N/A fatal("Use the other Tracker::record()");
4559N/A break;
4559N/A case Reserve:
4559N/A case ReserveAndCommit:
4559N/A flags |= MemPointerRecord::virtual_memory_reserve_tag();
4559N/A break;
4559N/A case Commit:
4559N/A flags = MemPointerRecord::virtual_memory_commit_tag();
4559N/A break;
4559N/A case Type:
4559N/A flags |= MemPointerRecord::virtual_memory_type_tag();
4559N/A break;
4559N/A case Uncommit:
4559N/A assert(pre_reserved_seq, "Need pre-reserve sequence number");
4559N/A flags = MemPointerRecord::virtual_memory_uncommit_tag();
4559N/A break;
4559N/A case Release:
4559N/A assert(pre_reserved_seq, "Need pre-reserve sequence number");
4559N/A flags = MemPointerRecord::virtual_memory_release_tag();
4559N/A break;
4559N/A case ArenaSize:
4559N/A // a bit of hack here, add a small postive offset to arena
4559N/A // address for its size record, so the size record is sorted
4559N/A // right after arena record.
4559N/A flags = MemPointerRecord::arena_size_tag();
4559N/A addr += sizeof(void*);
4559N/A break;
4559N/A case StackRelease:
4559N/A flags = MemPointerRecord::virtual_memory_release_tag();
4559N/A break;
4559N/A default:
4559N/A ShouldNotReachHere();
4559N/A }
4559N/A
4559N/A // write memory tracking record
4559N/A if (_need_thread_critical_lock) {
4559N/A ThreadCritical tc;
4559N/A if (_seq == 0) _seq = SequenceGenerator::next();
4559N/A MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread);
4559N/A if (_op == ReserveAndCommit) {
4559N/A MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(),
4559N/A size, SequenceGenerator::next(), pc, _java_thread);
4559N/A }
4559N/A if (pre_reserved_seq) MemTracker::dec_pending_op_count();
4559N/A } else {
4559N/A if (_seq == 0) _seq = SequenceGenerator::next();
4559N/A MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread);
4559N/A if (_op == ReserveAndCommit) {
4559N/A MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(),
4559N/A size, SequenceGenerator::next(), pc, _java_thread);
4559N/A }
4559N/A }
4559N/A _seq = 0;
4559N/A }
4559N/A}
4559N/A