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
3863N/A#ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP
3863N/A#define SHARE_VM_SERVICES_MEM_TRACKER_HPP
3863N/A
3863N/A#include "memory/allocation.hpp"
3863N/A#include "runtime/globals.hpp"
3863N/A#include "runtime/mutex.hpp"
3863N/A#include "runtime/os.hpp"
3863N/A#include "runtime/thread.hpp"
3863N/A#include "services/memPtr.hpp"
3863N/A#include "services/memRecorder.hpp"
3863N/A#include "services/memSnapshot.hpp"
3863N/A#include "services/memTrackWorker.hpp"
3863N/A
3863N/A#ifdef SOLARIS
3863N/A#include "thread_solaris.inline.hpp"
3863N/A#endif
3863N/A
4064N/Aextern bool NMT_track_callsite;
4064N/A
4186N/A#ifndef MAX_UNSIGNED_LONG
4186N/A#define MAX_UNSIGNED_LONG (unsigned long)(-1)
4186N/A#endif
4186N/A
4064N/A#ifdef ASSERT
4064N/A #define DEBUG_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0)
3863N/A#else
3863N/A #define DEBUG_CALLER_PC 0
3863N/A#endif
3863N/A
3863N/A// The thread closure walks threads to collect per-thread
3863N/A// memory recorders at NMT sync point
3863N/Aclass SyncThreadRecorderClosure : public ThreadClosure {
3863N/A private:
3863N/A int _thread_count;
3863N/A
3863N/A public:
3863N/A SyncThreadRecorderClosure() {
3863N/A _thread_count =0;
3863N/A }
3863N/A
3863N/A void do_thread(Thread* thread);
3863N/A int get_thread_count() const {
3863N/A return _thread_count;
3863N/A }
3863N/A};
3863N/A
3863N/Aclass BaselineOutputer;
3863N/Aclass MemSnapshot;
3863N/Aclass MemTrackWorker;
3863N/Aclass Thread;
3863N/A/*
3863N/A * MemTracker is the 'gate' class to native memory tracking runtime.
3863N/A */
3863N/Aclass MemTracker : AllStatic {
4168N/A friend class GenerationData;
3863N/A friend class MemTrackWorker;
3863N/A friend class MemSnapshot;
3863N/A friend class SyncThreadRecorderClosure;
3863N/A
3863N/A // NMT state
3863N/A enum NMTStates {
3863N/A NMT_uninited, // not yet initialized
3863N/A NMT_bootstrapping_single_thread, // bootstrapping, VM is in single thread mode
3863N/A NMT_bootstrapping_multi_thread, // bootstrapping, VM is about to enter multi-thread mode
3863N/A NMT_started, // NMT fully started
3863N/A NMT_shutdown_pending, // shutdown pending
3863N/A NMT_final_shutdown, // in final phase of shutdown
3863N/A NMT_shutdown // shutdown
3863N/A };
3863N/A
4061N/A public:
4559N/A class Tracker : public StackObj {
4559N/A friend class MemTracker;
4559N/A public:
4559N/A enum MemoryOperation {
4559N/A NoOp, // no op
4559N/A Malloc, // malloc
4559N/A Realloc, // realloc
4559N/A Free, // free
4559N/A Reserve, // virtual memory reserve
4559N/A Commit, // virtual memory commit
4559N/A ReserveAndCommit, // virtual memory reserve and commit
4559N/A StackAlloc = ReserveAndCommit, // allocate thread stack
4559N/A Type, // assign virtual memory type
4559N/A Uncommit, // virtual memory uncommit
4559N/A Release, // virtual memory release
4559N/A ArenaSize, // set arena size
4559N/A StackRelease // release thread stack
4559N/A };
4559N/A
4559N/A
4559N/A protected:
4559N/A Tracker(MemoryOperation op, Thread* thr = NULL);
4559N/A
4559N/A public:
4559N/A void discard();
4559N/A
4559N/A void record(address addr, size_t size = 0, MEMFLAGS flags = mtNone, address pc = NULL);
4559N/A void record(address old_addr, address new_addr, size_t size,
4559N/A MEMFLAGS flags, address pc = NULL);
4559N/A
4559N/A private:
4559N/A bool _need_thread_critical_lock;
4559N/A JavaThread* _java_thread;
4559N/A MemoryOperation _op; // memory operation
4559N/A jint _seq; // reserved sequence number
4559N/A };
4559N/A
4559N/A
4559N/A public:
3863N/A // native memory tracking level
3863N/A enum NMTLevel {
3863N/A NMT_off, // native memory tracking is off
3863N/A NMT_summary, // don't track callsite
3863N/A NMT_detail // track callsite also
3863N/A };
3863N/A
3863N/A enum ShutdownReason {
3863N/A NMT_shutdown_none, // no shutdown requested
3863N/A NMT_shutdown_user, // user requested shutdown
3863N/A NMT_normal, // normal shutdown, process exit
3863N/A NMT_out_of_memory, // shutdown due to out of memory
3863N/A NMT_initialization, // shutdown due to initialization failure
3863N/A NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag
3863N/A NMT_error_reporting, // shutdown by vmError::report_and_die()
3863N/A NMT_out_of_generation, // running out of generation queue
3863N/A NMT_sequence_overflow // overflow the sequence number
3863N/A };
3863N/A
3863N/A public:
3863N/A // initialize NMT tracking level from command line options, called
3863N/A // from VM command line parsing code
3863N/A static void init_tracking_options(const char* option_line);
3863N/A
3863N/A // if NMT is enabled to record memory activities
3863N/A static inline bool is_on() {
3863N/A return (_tracking_level >= NMT_summary &&
3863N/A _state >= NMT_bootstrapping_single_thread);
3863N/A }
3863N/A
4061N/A static inline enum NMTLevel tracking_level() {
4061N/A return _tracking_level;
4061N/A }
4061N/A
3863N/A // user readable reason for shutting down NMT
3863N/A static const char* reason() {
3863N/A switch(_reason) {
3863N/A case NMT_shutdown_none:
3863N/A return "Native memory tracking is not enabled";
3863N/A case NMT_shutdown_user:
3863N/A return "Native memory tracking has been shutdown by user";
3863N/A case NMT_normal:
3863N/A return "Native memory tracking has been shutdown due to process exiting";
3898N/A case NMT_out_of_memory:
3898N/A return "Native memory tracking has been shutdown due to out of native memory";
3863N/A case NMT_initialization:
3863N/A return "Native memory tracking failed to initialize";
3863N/A case NMT_error_reporting:
3863N/A return "Native memory tracking has been shutdown due to error reporting";
3863N/A case NMT_out_of_generation:
3863N/A return "Native memory tracking has been shutdown due to running out of generation buffer";
3863N/A case NMT_sequence_overflow:
3863N/A return "Native memory tracking has been shutdown due to overflow the sequence number";
3863N/A case NMT_use_malloc_only:
3863N/A return "Native memory tracking is not supported when UseMallocOnly is on";
3863N/A default:
3863N/A ShouldNotReachHere();
3863N/A return NULL;
3863N/A }
3863N/A }
3863N/A
3863N/A // test if we can walk native stack
3863N/A static bool can_walk_stack() {
3863N/A // native stack is not walkable during bootstrapping on sparc
3863N/A#if defined(SPARC)
3863N/A return (_state == NMT_started);
3863N/A#else
3863N/A return (_state >= NMT_bootstrapping_single_thread && _state <= NMT_started);
3863N/A#endif
3863N/A }
3863N/A
3863N/A // if native memory tracking tracks callsite
3863N/A static inline bool track_callsite() { return _tracking_level == NMT_detail; }
3863N/A
4311N/A // NMT automatically shuts itself down under extreme situation by default.
4311N/A // When the value is set to false, NMT will try its best to stay alive,
4311N/A // even it has to slow down VM.
4311N/A static inline void set_autoShutdown(bool value) {
4311N/A AutoShutdownNMT = value;
4311N/A if (AutoShutdownNMT && _slowdown_calling_thread) {
4311N/A _slowdown_calling_thread = false;
4311N/A }
4311N/A }
4311N/A
3863N/A // shutdown native memory tracking capability. Native memory tracking
3863N/A // can be shutdown by VM when it encounters low memory scenarios.
3863N/A // Memory tracker should gracefully shutdown itself, and preserve the
3863N/A // latest memory statistics for post morten diagnosis.
3863N/A static void shutdown(ShutdownReason reason);
3863N/A
3863N/A // if there is shutdown requested
3863N/A static inline bool shutdown_in_progress() {
3863N/A return (_state >= NMT_shutdown_pending);
3863N/A }
3863N/A
3863N/A // bootstrap native memory tracking, so it can start to collect raw data
3863N/A // before worker thread can start
3863N/A
3863N/A // the first phase of bootstrapping, when VM still in single-threaded mode
3863N/A static void bootstrap_single_thread();
3863N/A // the second phase of bootstrapping, VM is about or already in multi-threaded mode
3863N/A static void bootstrap_multi_thread();
3863N/A
4559N/A private:
4559N/A static Tracker _tkr;
3863N/A
4559N/A public:
3863N/A // start() has to be called when VM still in single thread mode, but after
3863N/A // command line option parsing is done.
3863N/A static void start();
3863N/A
3863N/A // record a 'malloc' call
3863N/A static inline void record_malloc(address addr, size_t size, MEMFLAGS flags,
3863N/A address pc = 0, Thread* thread = NULL) {
4559N/A Tracker tkr(Tracker::Malloc, thread);
4559N/A tkr.record(addr, size, flags, pc);
3863N/A }
3863N/A // record a 'free' call
3863N/A static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) {
4559N/A Tracker tkr(Tracker::Free, thread);
4559N/A tkr.record(addr, 0, flags, DEBUG_CALLER_PC);
3863N/A }
3863N/A
3863N/A static inline void record_arena_size(address addr, size_t size) {
4559N/A Tracker tkr(Tracker::ArenaSize);
4559N/A tkr.record(addr, size);
3863N/A }
3863N/A
3863N/A // record a virtual memory 'reserve' call
3863N/A static inline void record_virtual_memory_reserve(address addr, size_t size,
4559N/A MEMFLAGS flags, address pc = 0, Thread* thread = NULL) {
3863N/A if (is_on()) {
4060N/A assert(size > 0, "Sanity check");
4559N/A Tracker tkr(Tracker::Reserve, thread);
4559N/A tkr.record(addr, size, flags, pc);
3863N/A }
3863N/A }
3863N/A
4058N/A static inline void record_thread_stack(address addr, size_t size, Thread* thr,
4058N/A address pc = 0) {
4058N/A if (is_on()) {
4559N/A Tracker tkr(Tracker::StackAlloc, thr);
4559N/A tkr.record(addr, size, mtThreadStack, pc);
4058N/A }
4058N/A }
4058N/A
4058N/A static inline void release_thread_stack(address addr, size_t size, Thread* thr) {
4058N/A if (is_on()) {
4559N/A Tracker tkr(Tracker::StackRelease, thr);
4559N/A tkr.record(addr, size, mtThreadStack, DEBUG_CALLER_PC);
4058N/A }
4058N/A }
4058N/A
3863N/A // record a virtual memory 'commit' call
3863N/A static inline void record_virtual_memory_commit(address addr, size_t size,
4064N/A address pc, Thread* thread = NULL) {
3863N/A if (is_on()) {
4559N/A Tracker tkr(Tracker::Commit, thread);
4559N/A tkr.record(addr, size, mtNone, pc);
3863N/A }
3863N/A }
3863N/A
4559N/A static inline void record_virtual_memory_reserve_and_commit(address addr, size_t size,
4559N/A MEMFLAGS flags, address pc, Thread* thread = NULL) {
3863N/A if (is_on()) {
4559N/A Tracker tkr(Tracker::ReserveAndCommit, thread);
4559N/A tkr.record(addr, size, flags, pc);
3863N/A }
3863N/A }
3863N/A
3863N/A
3863N/A // record memory type on virtual memory base address
3863N/A static inline void record_virtual_memory_type(address base, MEMFLAGS flags,
3863N/A Thread* thread = NULL) {
3863N/A if (is_on()) {
4559N/A Tracker tkr(Tracker::Type);
4559N/A tkr.record(base, 0, flags);
3863N/A }
3863N/A }
3863N/A
4559N/A // Get memory trackers for memory operations that can result race conditions.
4559N/A // The memory tracker has to be obtained before realloc, virtual memory uncommit
4559N/A // and virtual memory release, and call tracker.record() method if operation
4559N/A // succeeded, or tracker.discard() to abort the tracking.
4559N/A static inline Tracker get_realloc_tracker() {
4559N/A return Tracker(Tracker::Realloc);
4559N/A }
4559N/A
4559N/A static inline Tracker get_virtual_memory_uncommit_tracker() {
4559N/A return Tracker(Tracker::Uncommit);
4559N/A }
4559N/A
4559N/A static inline Tracker get_virtual_memory_release_tracker() {
4559N/A return Tracker(Tracker::Release);
4559N/A }
4559N/A
3863N/A
3863N/A // create memory baseline of current memory snapshot
3863N/A static bool baseline();
3863N/A // is there a memory baseline
3863N/A static bool has_baseline() {
3863N/A return _baseline.baselined();
3863N/A }
3863N/A
3863N/A // print memory usage from current snapshot
3863N/A static bool print_memory_usage(BaselineOutputer& out, size_t unit,
3863N/A bool summary_only = true);
3863N/A // compare memory usage between current snapshot and baseline
3863N/A static bool compare_memory_usage(BaselineOutputer& out, size_t unit,
3863N/A bool summary_only = true);
3863N/A
4186N/A // the version for whitebox testing support, it ensures that all memory
4186N/A // activities before this method call, are reflected in the snapshot
4186N/A // database.
4186N/A static bool wbtest_wait_for_data_merge();
4186N/A
3863N/A // sync is called within global safepoint to synchronize nmt data
3863N/A static void sync();
3863N/A
3863N/A // called when a thread is about to exit
3863N/A static void thread_exiting(JavaThread* thread);
3863N/A
3863N/A // retrieve global snapshot
3863N/A static MemSnapshot* get_snapshot() {
3863N/A if (shutdown_in_progress()) {
3863N/A return NULL;
3863N/A }
3863N/A return _snapshot;
3863N/A }
3863N/A
3863N/A // print tracker stats
3863N/A NOT_PRODUCT(static void print_tracker_stats(outputStream* st);)
3863N/A NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);)
3863N/A
3863N/A private:
3863N/A // start native memory tracking worker thread
4413N/A static bool start_worker(MemSnapshot* snapshot);
3863N/A
3863N/A // called by worker thread to complete shutdown process
3863N/A static void final_shutdown();
3863N/A
3863N/A protected:
3863N/A // retrieve per-thread recorder of the specified thread.
3863N/A // if the recorder is full, it will be enqueued to overflow
3863N/A // queue, a new recorder is acquired from recorder pool or a
3863N/A // new instance is created.
3863N/A // when thread == NULL, it means global recorder
3863N/A static MemRecorder* get_thread_recorder(JavaThread* thread);
3863N/A
3863N/A // per-thread recorder pool
3863N/A static void release_thread_recorder(MemRecorder* rec);
3863N/A static void delete_all_pooled_recorders();
3863N/A
3863N/A // pending recorder queue. Recorders are queued to pending queue
3863N/A // when they are overflowed or collected at nmt sync point.
3863N/A static void enqueue_pending_recorder(MemRecorder* rec);
3863N/A static MemRecorder* get_pending_recorders();
3863N/A static void delete_all_pending_recorders();
3863N/A
4559N/A // write a memory tracking record in recorder
4559N/A static void write_tracking_record(address addr, MEMFLAGS type,
4559N/A size_t size, jint seq, address pc, JavaThread* thread);
4559N/A
4559N/A static bool is_single_threaded_bootstrap() {
4559N/A return _state == NMT_bootstrapping_single_thread;
4559N/A }
4559N/A
4559N/A static void check_NMT_load(Thread* thr) {
4559N/A assert(thr != NULL, "Sanity check");
4559N/A if (_slowdown_calling_thread && thr != _worker_thread) {
4559N/A os::yield_all();
4559N/A }
4559N/A }
4559N/A
4559N/A static void inc_pending_op_count() {
4559N/A Atomic::inc(&_pending_op_count);
4559N/A }
4559N/A
4559N/A static void dec_pending_op_count() {
4559N/A Atomic::dec(&_pending_op_count);
4559N/A assert(_pending_op_count >= 0, "Sanity check");
4559N/A }
4559N/A
4559N/A
3863N/A private:
3863N/A // retrieve a pooled memory record or create new one if there is not
3863N/A // one available
3863N/A static MemRecorder* get_new_or_pooled_instance();
3863N/A static void create_memory_record(address addr, MEMFLAGS type,
3863N/A size_t size, address pc, Thread* thread);
3863N/A static void create_record_in_recorder(address addr, MEMFLAGS type,
3897N/A size_t size, address pc, JavaThread* thread);
3863N/A
4186N/A static void set_current_processing_generation(unsigned long generation) {
4186N/A _worker_thread_idle = false;
4186N/A _processing_generation = generation;
4186N/A }
4186N/A
4186N/A static void report_worker_idle() {
4186N/A _worker_thread_idle = true;
4186N/A }
4186N/A
3863N/A private:
3863N/A // global memory snapshot
3863N/A static MemSnapshot* _snapshot;
3863N/A
3863N/A // a memory baseline of snapshot
3863N/A static MemBaseline _baseline;
3863N/A
3863N/A // query lock
3898N/A static Mutex* _query_lock;
3863N/A
3863N/A // a thread can start to allocate memory before it is attached
3863N/A // to VM 'Thread', those memory activities are recorded here.
3863N/A // ThreadCritical is required to guard this global recorder.
4413N/A static MemRecorder* volatile _global_recorder;
3863N/A
3863N/A // main thread id
3863N/A debug_only(static intx _main_thread_tid;)
3863N/A
3863N/A // pending recorders to be merged
4413N/A static MemRecorder* volatile _merge_pending_queue;
3863N/A
3863N/A NOT_PRODUCT(static volatile jint _pending_recorder_count;)
3863N/A
3863N/A // pooled memory recorders
4413N/A static MemRecorder* volatile _pooled_recorders;
3863N/A
3863N/A // memory recorder pool management, uses following
3863N/A // counter to determine if a released memory recorder
3863N/A // should be pooled
3863N/A
3863N/A // latest thread count
3863N/A static int _thread_count;
3863N/A // pooled recorder count
3863N/A static volatile jint _pooled_recorder_count;
3863N/A
3863N/A
3863N/A // worker thread to merge pending recorders into snapshot
3863N/A static MemTrackWorker* _worker_thread;
3863N/A
3863N/A // how many safepoints we skipped without entering sync point
3863N/A static int _sync_point_skip_count;
3863N/A
3863N/A // if the tracker is properly intialized
3863N/A static bool _is_tracker_ready;
3863N/A // tracking level (off, summary and detail)
3863N/A static enum NMTLevel _tracking_level;
3863N/A
3863N/A // current nmt state
3863N/A static volatile enum NMTStates _state;
3863N/A // the reason for shutting down nmt
3863N/A static enum ShutdownReason _reason;
4186N/A // the generation that NMT is processing
4186N/A static volatile unsigned long _processing_generation;
4186N/A // although NMT is still procesing current generation, but
4186N/A // there is not more recorder to process, set idle state
4186N/A static volatile bool _worker_thread_idle;
4311N/A
4311N/A // if NMT should slow down calling thread to allow
4311N/A // worker thread to catch up
4311N/A static volatile bool _slowdown_calling_thread;
4559N/A
4559N/A // pending memory op count.
4559N/A // Certain memory ops need to pre-reserve sequence number
4559N/A // before memory operation can happen to avoid race condition.
4559N/A // See MemTracker::Tracker for detail
4559N/A static volatile jint _pending_op_count;
3863N/A};
3863N/A
3863N/A#endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP