memTracker.cpp revision 4311
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "oops/instanceKlass.hpp"
#include "runtime/atomic.hpp"
#include "runtime/interfaceSupport.hpp"
#include "runtime/mutexLocker.hpp"
#include "runtime/safepoint.hpp"
#include "runtime/threadCritical.hpp"
#include "runtime/vm_operations.hpp"
#include "services/memPtr.hpp"
#include "services/memReporter.hpp"
#include "services/memTracker.hpp"
#include "utilities/decoder.hpp"
#include "utilities/globalDefinitions.hpp"
bool NMT_track_callsite = false;
// walk all 'known' threads at NMT sync point, and collect their recorders
if (thread->is_Java_thread()) {
}
}
_thread_count ++;
}
int MemTracker::_sync_point_skip_count = 0;
volatile unsigned long MemTracker::_processing_generation = 0;
volatile bool MemTracker::_worker_thread_idle = false;
volatile bool MemTracker::_slowdown_calling_thread = false;
vm_exit_during_initialization("Syntax error, expecting -XX:NativeMemoryTracking=[off|summary|detail]", NULL);
}
}
// first phase of bootstrapping, when VM is still in single-threaded mode.
void MemTracker::bootstrap_single_thread() {
if (_tracking_level > NMT_off) {
// NMT is not supported with UseMallocOnly is on. NMT can NOT
// handle the amount of malloc data without significantly impacting
// runtime performance when this flag is on.
if (UseMallocOnly) {
return;
}
if (_query_lock == NULL) {
return;
}
}
}
// second phase of bootstrapping, when VM is about to or already entered multi-theaded mode.
void MemTracker::bootstrap_multi_thread() {
// create nmt lock for multi-thread execution
}
}
// fully start nmt
void MemTracker::start() {
// Native memory tracking is off from command line option
if (start_worker()) {
return;
}
}
// fail to start native memory tracking, shut it down
}
/**
* Shutting down native memory tracking.
* We can not shutdown native memory tracking immediately, so we just
* setup shutdown pending flag, every native memory tracking component
* should orderly shut itself down.
*
* The shutdown sequences:
* 1. MemTracker::shutdown() sets MemTracker to shutdown pending state
* 2. Worker thread calls MemTracker::final_shutdown(), which transites
* MemTracker to final shutdown state.
* 3. At sync point, MemTracker does final cleanup, before sets memory
* tracking level to off to complete shutdown.
*/
if (_tracking_level == NMT_off) return;
if (_state <= NMT_bootstrapping_single_thread) {
// we still in single thread mode, there is not contention
} else {
// we want to know who initialized shutdown
}
}
}
// final phase of shutdown
void MemTracker::final_shutdown() {
// delete all pending recorders and pooled recorders
{
// shared baseline and snapshot are the only objects needed to
// create query results
// cleanup baseline data and snapshot
delete _snapshot;
}
// shutdown shared decoder instance, since it is only
// used by native memory tracking so far.
{
// can not delete worker inside the thread critical
}
}
delete worker;
}
}
// delete all pooled recorders
void MemTracker::delete_all_pooled_recorders() {
// free all pooled recorders
(void*)&_pooled_recorders, (void*)cur_head)) {
}
delete cur_head;
}
}
}
// delete all recorders in pending queue
void MemTracker::delete_all_pending_recorders() {
// free all pending recorders
if (pending_head != NULL) {
delete pending_head;
}
}
/*
* retrieve per-thread recorder of specified thread.
* if thread == NULL, it means global recorder
*/
if (shutdown_in_progress()) return NULL;
} else {
}
}
} else {
}
}
return rc;
}
/*
* get a per-thread recorder from pool, or create a new one if
* there is not one available.
*/
delete rec;
}
}
return rec;
} else {
(void*)cur_head)) {
return get_new_or_pooled_instance();
}
return cur_head;
}
}
/*
* retrieve all recorders in pending queue, and empty the queue
*/
(void*)cur_head)) {
}
return cur_head;
}
/*
* release a recorder to recorder pool.
*/
// we don't want to pool too many recorders
delete rec;
return;
}
(void*)cur_head)) {
}
}
/*
* This is the most important method in whole nmt implementation.
*
* Create a memory record.
* 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
* still in single thread mode.
* 2. For all threads other than JavaThread, ThreadCritical is needed
* to write to recorders to global recorder.
* 3. For JavaThreads that are not longer visible by safepoint, also
* need to take ThreadCritical and records are written to global
* recorders, since these threads are NOT walked by Threads.do_thread().
* 4. JavaThreads that are running in native state, have to transition
* to VM state before writing to per-thread recorders.
* 5. JavaThreads that are running in VM state do not need any lock and
* records are written to per-thread recorders.
* 6. For a thread has yet to attach VM 'Thread', they need to take
* ThreadCritical to write to global recorder.
*
* Important note:
* NO LOCK should be taken inside ThreadCritical lock !!!
*/
if (!shutdown_in_progress()) {
// single thread, we just write records direct to global recorder,'
// with any lock
if (_state == NMT_bootstrapping_single_thread) {
} else {
// don't use Thread::current(), since it is possible that
// the calling thread has yet to attach to VM 'Thread',
// which will result assertion failure
}
}
// slow down all calling threads except NMT worker thread, so it
// can catch up.
}
// JavaThreads that are safepoint safe, can run through safepoint,
// so ThreadCritical is needed to ensure no threads at safepoint create
// new records while the records are being gathered and the sequence number is changing
} else {
}
} else {
// other threads, such as worker and watcher threads, etc. need to
// take ThreadCritical to write to global recorder
}
} else {
if (_state == NMT_bootstrapping_single_thread) {
// single thread, no lock needed
} else {
// for thread has yet to attach VM 'Thread', we can not use VM mutex.
// use native thread critical instead
}
}
}
}
// write a record to proper recorder. No lock can be taken from this method
// down.
}
}
/**
* enqueue a recorder to pending queue
*/
// we are shutting down, so just delete it
if (shutdown_in_progress()) {
delete rec;
return;
}
(void*)cur_head)) {
}
}
/*
* The method is called at global safepoint
* during it synchronization process.
* 1. enqueue all JavaThreads' per-thread recorders
* 2. enqueue global recorder
* 3. retrieve all pending recorders
* 4. reset global sequence number generator
* 5. call worker's sync
*/
#define MAX_SAFEPOINTS_TO_SKIP 128
#define SAFE_SEQUENCE_THRESHOLD 30
#define HIGH_GENERATION_THRESHOLD 60
#define MAX_RECORDER_THREAD_RATIO 30
void MemTracker::sync() {
// Some GC tests hit large number of safepoints in short period of time
// without meaningful activities. We should prevent going to
// sync point in these cases, which can potentially exhaust generation buffer.
// Here is the factots to determine if we should go into sync point:
// 1. not to overflow sequence number
// 2. if we are in danger to overflow generation buffer
// 3. how many safepoints we already skipped sync point
if (_state == NMT_started) {
// worker thread is not ready, no one can manage generation
// buffer, so skip this safepoint
if (_worker_thread == NULL) return;
return;
}
}
{
// This method is running at safepoint, with ThreadCritical lock,
// it should guarantee that NMT is fully sync-ed.
// walk all JavaThreads to collect recorders
if (_global_recorder != NULL) {
}
// see if NMT has too many outstanding recorder instances, it usually
// means that worker thread is lagging behind in processing them.
if (!AutoShutdownNMT) {
_slowdown_calling_thread = (MemRecorder::_instance_count > MAX_RECORDER_THREAD_RATIO * _thread_count);
}
// check _worker_thread with lock to avoid racing condition
if (_worker_thread != NULL) {
}
}
}
// now, it is the time to shut whole things off
if (_state == NMT_final_shutdown) {
// walk all JavaThreads to delete all recorders
// delete global recorder
{
if (_global_recorder != NULL) {
delete _global_recorder;
}
}
if (pending_recorders != NULL) {
delete pending_recorders;
}
// try at a later sync point to ensure MemRecorder instance drops to zero to
// completely shutdown NMT
if (MemRecorder::_instance_count == 0) {
}
}
}
/*
* Start worker thread.
*/
bool MemTracker::start_worker() {
return false;
}
_worker_thread->start();
return true;
}
/*
* We need to collect a JavaThread's per-thread recorder
* before it exits.
*/
if (is_on()) {
}
}
}
// baseline current memory snapshot
bool MemTracker::baseline() {
}
return false;
}
// print memory usage from current snapshot
return true;
}
return false;
}
// Whitebox API for blocking until the current generation of NMT data has been merged
bool MemTracker::wbtest_wait_for_data_merge() {
// NMT can't be shutdown while we're holding _query_lock
// the generation at query time, so NMT will spin till this generation is processed
unsigned long current_processing_generation = _processing_generation;
// if generation counter overflown
// spin
while (!shutdown_in_progress()) {
if (!generation_overflown) {
return true;
}
} else {
// to overflow an unsigned long should take long time, so to_wrap check should be sufficient
return true;
}
}
// if worker thread is idle, but generation is not advancing, that means
// there is not safepoint to let NMT advance generation, force one.
if (_worker_thread_idle) {
}
return false;
}
}
// We end up here if NMT is shutting down before our data has been merged
return false;
}
// compare memory usage between current snapshot and baseline
return true;
}
}
return false;
}
#ifndef PRODUCT
int cur_len = 0;
char tmp[1024];
} else {
break;
}
toSkip ++;
}
}
if (_worker_thread != NULL) {
} else {
}
} else {
}
}
#endif