/*
* 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 ++;
}
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.
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.
// create nmt lock for multi-thread execution
}
}
// fully start nmt
// Native memory tracking is off from command line option
return;
}
delete _snapshot;
}
// 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
// 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
// free all pooled recorders
(void*)&_pooled_recorders, (void*)cur_head)) {
}
delete cur_head;
}
}
}
// delete all recorders in pending queue
// 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)) {
}
}
// 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
*/
// 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.
// We can NOT execute NMT sync-point if there are pending tracking ops.
if (_pending_op_count == 0) {
// 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) {
}
} else {
}
}
}
// 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.
*/
if (_worker_thread == NULL) {
return false;
} else if (_worker_thread->has_error()) {
delete _worker_thread;
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
}
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
// 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
// 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;
} else {
break;
}
toSkip ++;
}
}
if (_worker_thread != NULL) {
} else {
}
} else {
}
}
#endif
// Tracker Implementation
/*
* Create a tracker.
* This is a fairly complicated constructor, as it has to make two important decisions:
* 1) Does it need to take ThreadCritical lock to write tracking record
* 2) Does it need to pre-reserve a sequence number for the tracking record
*
* The rules to determine if ThreadCritical is needed:
* 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 no 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 safepoint-safe states do not stop
* for safepoints, ThreadCritical lock should be taken to write
* memory records.
* 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.
*
* The memory operations that need pre-reserve sequence numbers:
* The memory operations that "release" memory blocks and the
* operations can fail, need to pre-reserve sequence number. They
* are realloc, uncommit and release.
*
* The reason for pre-reserve sequence number, is to prevent race condition:
* Thread 1 Thread 2
* <release>
* <allocate>
* <write allocate record>
* <write release record>
* if Thread 2 happens to obtain the memory address Thread 1 just released,
* then NMT can mistakenly report the memory is free.
*
* Noticeably, free() does not need pre-reserve sequence number, because the call
* does not fail, so we can alway write "release" record before the memory is actaully
* freed.
*
* For realloc, uncommit and release, following coding pattern should be used:
*
* MemTracker::Tracker tkr = MemTracker::get_realloc_tracker();
* ptr = ::realloc(...);
* if (ptr != NULL) {
* tkr.record(...)
* } else {
* tkr.discard();
* }
*
* MemTracker::Tracker tkr = MemTracker::get_virtual_memory_uncommit_tracker();
* if (uncommit(...)) {
* tkr.record(...);
* } else {
* tkr.discard();
* }
*
* MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker();
* if (release(...)) {
* tkr.record(...);
* } else {
* tkr.discard();
* }
*
* Since pre-reserved sequence number is only good for the generation that it is acquired,
* when there is pending Tracker that reserved sequence number, NMT sync-point has
* to be skipped to prevent from advancing generation. This is done by inc and dec
* MemTracker::_pending_op_count, when MemTracker::_pending_op_count > 0, NMT sync-point is skipped.
* Not all pre-reservation of sequence number will increment pending op count. For JavaThreads
* that honor safepoints, safepoint can not occur during the memory operations, so the
* pre-reserved sequence number won't cross the generation boundary.
*/
_seq = 0;
if (MemTracker::is_on()) {
_java_thread = NULL;
// figure out if ThreadCritical lock is needed to write this operation
// to MemTracker
if (MemTracker::is_single_threaded_bootstrap()) {
// 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
}
// Check NMT load
// 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 {
_need_thread_critical_lock = true;
}
} else {
}
// see if we need to pre-reserve sequence number for this operation
if (_need_thread_critical_lock) {
} else {
// for the threads that honor safepoints, no safepoint can occur
// during the lifespan of tracker, so we don't need to increase
// pending op count.
}
}
}
}
if (_need_thread_critical_lock) {
}
_seq = 0;
}
}
if (_need_thread_critical_lock) {
// free old address, use pre-reserved sequence number
// decrement MemTracker pending_op_count
} else {
// free old address, use pre-reserved sequence number
}
_seq = 0;
}
}
// OOM already?
// or the tagging flags
switch(_op) {
case Malloc:
break;
case Free:
break;
case Realloc:
fatal("Use the other Tracker::record()");
break;
case Reserve:
case ReserveAndCommit:
break;
case Commit:
break;
case Type:
break;
case Uncommit:
break;
case Release:
break;
case ArenaSize:
// a bit of hack here, add a small postive offset to arena
// address for its size record, so the size record is sorted
// right after arena record.
addr += sizeof(void*);
break;
case StackRelease:
break;
default:
}
// write memory tracking record
if (_need_thread_critical_lock) {
if (_op == ReserveAndCommit) {
}
} else {
if (_op == ReserveAndCommit) {
}
}
_seq = 0;
}
}