zgu@3900: /* zgu@3900: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. zgu@3900: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@3900: * zgu@3900: * This code is free software; you can redistribute it and/or modify it zgu@3900: * under the terms of the GNU General Public License version 2 only, as zgu@3900: * published by the Free Software Foundation. zgu@3900: * zgu@3900: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@3900: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@3900: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@3900: * version 2 for more details (a copy is included in the LICENSE file that zgu@3900: * accompanied this code). zgu@3900: * zgu@3900: * You should have received a copy of the GNU General Public License version zgu@3900: * 2 along with this work; if not, write to the Free Software Foundation, zgu@3900: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@3900: * zgu@3900: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@3900: * or visit www.oracle.com if you need additional information or have any zgu@3900: * questions. zgu@3900: * zgu@3900: */ zgu@3900: zgu@3900: #ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP zgu@3900: #define SHARE_VM_SERVICES_MEM_TRACKER_HPP zgu@3900: jprovino@4165: #include "utilities/macros.hpp" jprovino@4165: jprovino@4165: #if !INCLUDE_NMT jprovino@4165: jprovino@4165: #include "utilities/ostream.hpp" jprovino@4165: jprovino@4165: class BaselineOutputer : public StackObj { jprovino@4165: jprovino@4165: }; jprovino@4165: jprovino@4165: class BaselineTTYOutputer : public BaselineOutputer { jprovino@4165: public: jprovino@4165: BaselineTTYOutputer(outputStream* st) { } jprovino@4165: }; jprovino@4165: jprovino@4165: class MemTracker : AllStatic { jprovino@4165: public: jprovino@4165: enum ShutdownReason { jprovino@4165: NMT_shutdown_none, // no shutdown requested jprovino@4165: NMT_shutdown_user, // user requested shutdown jprovino@4165: NMT_normal, // normal shutdown, process exit jprovino@4165: NMT_out_of_memory, // shutdown due to out of memory jprovino@4165: NMT_initialization, // shutdown due to initialization failure jprovino@4165: NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag jprovino@4165: NMT_error_reporting, // shutdown by vmError::report_and_die() jprovino@4165: NMT_out_of_generation, // running out of generation queue jprovino@4165: NMT_sequence_overflow // overflow the sequence number jprovino@4165: }; jprovino@4165: jprovino@4165: jprovino@4165: public: jprovino@4165: static inline void init_tracking_options(const char* option_line) { } jprovino@4165: static inline bool is_on() { return false; } jprovino@4165: static const char* reason() { return "Native memory tracking is not implemented"; } jprovino@4165: static inline bool can_walk_stack() { return false; } jprovino@4165: jprovino@4165: static inline void bootstrap_single_thread() { } jprovino@4165: static inline void bootstrap_multi_thread() { } jprovino@4165: static inline void start() { } jprovino@4165: jprovino@4165: static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, jprovino@4165: address pc = 0, Thread* thread = NULL) { } jprovino@4165: static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { } jprovino@4165: static inline void record_realloc(address old_addr, address new_addr, size_t size, jprovino@4165: MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { } jprovino@4165: static inline void record_arena_size(address addr, size_t size) { } jprovino@4165: static inline void record_virtual_memory_reserve(address addr, size_t size, jprovino@4165: address pc = 0, Thread* thread = NULL) { } jprovino@4165: static inline void record_virtual_memory_commit(address addr, size_t size, jprovino@4165: address pc = 0, Thread* thread = NULL) { } jprovino@4165: static inline void record_virtual_memory_uncommit(address addr, size_t size, jprovino@4165: Thread* thread = NULL) { } jprovino@4165: static inline void record_virtual_memory_release(address addr, size_t size, jprovino@4165: Thread* thread = NULL) { } jprovino@4165: static inline void record_virtual_memory_type(address base, MEMFLAGS flags, jprovino@4165: Thread* thread = NULL) { } jprovino@4165: static inline bool baseline() { return false; } jprovino@4165: static inline bool has_baseline() { return false; } jprovino@4165: jprovino@4165: static void shutdown(ShutdownReason reason) { } jprovino@4165: static inline bool shutdown_in_progress() { } jprovino@4165: static bool print_memory_usage(BaselineOutputer& out, size_t unit, jprovino@4165: bool summary_only = true) { } jprovino@4165: static bool compare_memory_usage(BaselineOutputer& out, size_t unit, jprovino@4165: bool summary_only = true) { } jprovino@4165: jprovino@4165: static inline void sync() { } jprovino@4165: static inline void thread_exiting(JavaThread* thread) { } jprovino@4165: jprovino@4165: }; jprovino@4165: jprovino@4165: jprovino@4165: #else // !INCLUDE_NMT jprovino@4165: zgu@3900: #include "memory/allocation.hpp" zgu@3900: #include "runtime/globals.hpp" zgu@3900: #include "runtime/mutex.hpp" zgu@3900: #include "runtime/os.hpp" zgu@3900: #include "runtime/thread.hpp" zgu@3900: #include "services/memPtr.hpp" zgu@3900: #include "services/memRecorder.hpp" zgu@3900: #include "services/memSnapshot.hpp" zgu@3900: #include "services/memTrackWorker.hpp" zgu@3900: zgu@4193: extern bool NMT_track_callsite; zgu@4193: zgu@4193: #ifdef ASSERT zgu@4193: #define DEBUG_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0) zgu@3900: #else zgu@3900: #define DEBUG_CALLER_PC 0 zgu@3900: #endif zgu@3900: zgu@3900: // The thread closure walks threads to collect per-thread zgu@3900: // memory recorders at NMT sync point zgu@3900: class SyncThreadRecorderClosure : public ThreadClosure { zgu@3900: private: zgu@3900: int _thread_count; zgu@3900: zgu@3900: public: zgu@3900: SyncThreadRecorderClosure() { zgu@3900: _thread_count =0; zgu@3900: } zgu@3900: zgu@3900: void do_thread(Thread* thread); zgu@3900: int get_thread_count() const { zgu@3900: return _thread_count; zgu@3900: } zgu@3900: }; zgu@3900: zgu@3900: class BaselineOutputer; zgu@3900: class MemSnapshot; zgu@3900: class MemTrackWorker; zgu@3900: class Thread; zgu@3900: /* zgu@3900: * MemTracker is the 'gate' class to native memory tracking runtime. zgu@3900: */ zgu@3900: class MemTracker : AllStatic { zgu@3900: friend class MemTrackWorker; zgu@3900: friend class MemSnapshot; zgu@3900: friend class SyncThreadRecorderClosure; zgu@3900: zgu@3900: // NMT state zgu@3900: enum NMTStates { zgu@3900: NMT_uninited, // not yet initialized zgu@3900: NMT_bootstrapping_single_thread, // bootstrapping, VM is in single thread mode zgu@3900: NMT_bootstrapping_multi_thread, // bootstrapping, VM is about to enter multi-thread mode zgu@3900: NMT_started, // NMT fully started zgu@3900: NMT_shutdown_pending, // shutdown pending zgu@3900: NMT_final_shutdown, // in final phase of shutdown zgu@3900: NMT_shutdown // shutdown zgu@3900: }; zgu@3900: zgu@4081: public: zgu@3900: // native memory tracking level zgu@3900: enum NMTLevel { zgu@3900: NMT_off, // native memory tracking is off zgu@3900: NMT_summary, // don't track callsite zgu@3900: NMT_detail // track callsite also zgu@3900: }; zgu@3900: zgu@3900: enum ShutdownReason { zgu@3900: NMT_shutdown_none, // no shutdown requested zgu@3900: NMT_shutdown_user, // user requested shutdown zgu@3900: NMT_normal, // normal shutdown, process exit zgu@3900: NMT_out_of_memory, // shutdown due to out of memory zgu@3900: NMT_initialization, // shutdown due to initialization failure zgu@3900: NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag zgu@3900: NMT_error_reporting, // shutdown by vmError::report_and_die() zgu@3900: NMT_out_of_generation, // running out of generation queue zgu@3900: NMT_sequence_overflow // overflow the sequence number zgu@3900: }; zgu@3900: zgu@3900: public: zgu@3900: // initialize NMT tracking level from command line options, called zgu@3900: // from VM command line parsing code zgu@3900: static void init_tracking_options(const char* option_line); zgu@3900: zgu@3900: // if NMT is enabled to record memory activities zgu@3900: static inline bool is_on() { zgu@3900: return (_tracking_level >= NMT_summary && zgu@3900: _state >= NMT_bootstrapping_single_thread); zgu@3900: } zgu@3900: zgu@4081: static inline enum NMTLevel tracking_level() { zgu@4081: return _tracking_level; zgu@4081: } zgu@4081: zgu@3900: // user readable reason for shutting down NMT zgu@3900: static const char* reason() { zgu@3900: switch(_reason) { zgu@3900: case NMT_shutdown_none: zgu@3900: return "Native memory tracking is not enabled"; zgu@3900: case NMT_shutdown_user: zgu@3900: return "Native memory tracking has been shutdown by user"; zgu@3900: case NMT_normal: zgu@3900: return "Native memory tracking has been shutdown due to process exiting"; zgu@3936: case NMT_out_of_memory: zgu@3936: return "Native memory tracking has been shutdown due to out of native memory"; zgu@3900: case NMT_initialization: zgu@3900: return "Native memory tracking failed to initialize"; zgu@3900: case NMT_error_reporting: zgu@3900: return "Native memory tracking has been shutdown due to error reporting"; zgu@3900: case NMT_out_of_generation: zgu@3900: return "Native memory tracking has been shutdown due to running out of generation buffer"; zgu@3900: case NMT_sequence_overflow: zgu@3900: return "Native memory tracking has been shutdown due to overflow the sequence number"; zgu@3900: case NMT_use_malloc_only: zgu@3900: return "Native memory tracking is not supported when UseMallocOnly is on"; zgu@3900: default: zgu@3900: ShouldNotReachHere(); zgu@3900: return NULL; zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: // test if we can walk native stack zgu@3900: static bool can_walk_stack() { zgu@3900: // native stack is not walkable during bootstrapping on sparc zgu@3900: #if defined(SPARC) zgu@3900: return (_state == NMT_started); zgu@3900: #else zgu@3900: return (_state >= NMT_bootstrapping_single_thread && _state <= NMT_started); zgu@3900: #endif zgu@3900: } zgu@3900: zgu@3900: // if native memory tracking tracks callsite zgu@3900: static inline bool track_callsite() { return _tracking_level == NMT_detail; } zgu@3900: zgu@3900: // shutdown native memory tracking capability. Native memory tracking zgu@3900: // can be shutdown by VM when it encounters low memory scenarios. zgu@3900: // Memory tracker should gracefully shutdown itself, and preserve the zgu@3900: // latest memory statistics for post morten diagnosis. zgu@3900: static void shutdown(ShutdownReason reason); zgu@3900: zgu@3900: // if there is shutdown requested zgu@3900: static inline bool shutdown_in_progress() { zgu@3900: return (_state >= NMT_shutdown_pending); zgu@3900: } zgu@3900: zgu@3900: // bootstrap native memory tracking, so it can start to collect raw data zgu@3900: // before worker thread can start zgu@3900: zgu@3900: // the first phase of bootstrapping, when VM still in single-threaded mode zgu@3900: static void bootstrap_single_thread(); zgu@3900: // the second phase of bootstrapping, VM is about or already in multi-threaded mode zgu@3900: static void bootstrap_multi_thread(); zgu@3900: zgu@3900: zgu@3900: // start() has to be called when VM still in single thread mode, but after zgu@3900: // command line option parsing is done. zgu@3900: static void start(); zgu@3900: zgu@3900: // record a 'malloc' call zgu@3900: static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, zgu@3900: address pc = 0, Thread* thread = NULL) { zgu@4193: if (is_on() && NMT_CAN_TRACK(flags)) { zgu@4079: assert(size > 0, "Sanity check"); zgu@3900: create_memory_record(addr, (flags|MemPointerRecord::malloc_tag()), size, pc, thread); zgu@3900: } zgu@3900: } zgu@3900: // record a 'free' call zgu@3900: static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { zgu@3900: if (is_on() && NMT_CAN_TRACK(flags)) { zgu@3900: create_memory_record(addr, MemPointerRecord::free_tag(), 0, 0, thread); zgu@3900: } zgu@3900: } zgu@3900: // record a 'realloc' call zgu@3900: static inline void record_realloc(address old_addr, address new_addr, size_t size, zgu@3900: MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { zgu@4193: if (is_on() && NMT_CAN_TRACK(flags)) { zgu@4079: assert(size > 0, "Sanity check"); zgu@3900: record_free(old_addr, flags, thread); zgu@3900: record_malloc(new_addr, size, flags, pc, thread); zgu@3900: } zgu@3900: } zgu@3900: zgu@4274: // record arena memory size zgu@3900: static inline void record_arena_size(address addr, size_t size) { zgu@4274: // we add a positive offset to arena address, so we can have arena memory record zgu@3900: // sorted after arena record zgu@3900: if (is_on() && !UseMallocOnly) { zgu@4079: assert(addr != NULL, "Sanity check"); zgu@3900: create_memory_record((addr + sizeof(void*)), MemPointerRecord::arena_size_tag(), size, zgu@4274: DEBUG_CALLER_PC, NULL); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: // record a virtual memory 'reserve' call zgu@3900: static inline void record_virtual_memory_reserve(address addr, size_t size, zgu@3900: address pc = 0, Thread* thread = NULL) { zgu@3900: if (is_on()) { zgu@4079: assert(size > 0, "Sanity check"); zgu@3900: create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag(), zgu@3900: size, pc, thread); zgu@3900: } zgu@3900: } zgu@3900: zgu@4053: static inline void record_thread_stack(address addr, size_t size, Thread* thr, zgu@4053: address pc = 0) { zgu@4053: if (is_on()) { zgu@4053: assert(size > 0 && thr != NULL, "Sanity check"); zgu@4053: create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag() | mtThreadStack, zgu@4053: size, pc, thr); zgu@4053: create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag() | mtThreadStack, zgu@4053: size, pc, thr); zgu@4053: } zgu@4053: } zgu@4053: zgu@4053: static inline void release_thread_stack(address addr, size_t size, Thread* thr) { zgu@4053: if (is_on()) { zgu@4053: assert(size > 0 && thr != NULL, "Sanity check"); zgu@4193: assert(!thr->is_Java_thread(), "too early"); zgu@4053: create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag() | mtThreadStack, zgu@4053: size, DEBUG_CALLER_PC, thr); zgu@4053: create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag() | mtThreadStack, zgu@4053: size, DEBUG_CALLER_PC, thr); zgu@4053: } zgu@4053: } zgu@4053: zgu@3900: // record a virtual memory 'commit' call zgu@3900: static inline void record_virtual_memory_commit(address addr, size_t size, zgu@4193: address pc, Thread* thread = NULL) { zgu@3900: if (is_on()) { zgu@4079: assert(size > 0, "Sanity check"); zgu@3900: create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag(), zgu@4193: size, pc, thread); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: // record a virtual memory 'uncommit' call zgu@3900: static inline void record_virtual_memory_uncommit(address addr, size_t size, zgu@3900: Thread* thread = NULL) { zgu@3900: if (is_on()) { zgu@4079: assert(size > 0, "Sanity check"); zgu@3900: create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag(), zgu@4053: size, DEBUG_CALLER_PC, thread); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: // record a virtual memory 'release' call zgu@3900: static inline void record_virtual_memory_release(address addr, size_t size, zgu@3900: Thread* thread = NULL) { zgu@3900: if (is_on()) { zgu@4079: assert(size > 0, "Sanity check"); zgu@3900: create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag(), zgu@4053: size, DEBUG_CALLER_PC, thread); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: // record memory type on virtual memory base address zgu@3900: static inline void record_virtual_memory_type(address base, MEMFLAGS flags, zgu@3900: Thread* thread = NULL) { zgu@3900: if (is_on()) { zgu@3900: assert(base > 0, "wrong base address"); zgu@3900: assert((flags & (~mt_masks)) == 0, "memory type only"); zgu@3900: create_memory_record(base, (flags | MemPointerRecord::virtual_memory_type_tag()), zgu@4053: 0, DEBUG_CALLER_PC, thread); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: zgu@3900: // create memory baseline of current memory snapshot zgu@3900: static bool baseline(); zgu@3900: // is there a memory baseline zgu@3900: static bool has_baseline() { zgu@3900: return _baseline.baselined(); zgu@3900: } zgu@3900: zgu@3900: // print memory usage from current snapshot zgu@3900: static bool print_memory_usage(BaselineOutputer& out, size_t unit, zgu@3900: bool summary_only = true); zgu@3900: // compare memory usage between current snapshot and baseline zgu@3900: static bool compare_memory_usage(BaselineOutputer& out, size_t unit, zgu@3900: bool summary_only = true); zgu@3900: zgu@3900: // sync is called within global safepoint to synchronize nmt data zgu@3900: static void sync(); zgu@3900: zgu@3900: // called when a thread is about to exit zgu@3900: static void thread_exiting(JavaThread* thread); zgu@3900: zgu@3900: // retrieve global snapshot zgu@3900: static MemSnapshot* get_snapshot() { zgu@3900: if (shutdown_in_progress()) { zgu@3900: return NULL; zgu@3900: } zgu@3900: return _snapshot; zgu@3900: } zgu@3900: zgu@3900: // print tracker stats zgu@3900: NOT_PRODUCT(static void print_tracker_stats(outputStream* st);) zgu@3900: NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);) zgu@3900: zgu@3900: private: zgu@3900: // start native memory tracking worker thread zgu@3900: static bool start_worker(); zgu@3900: zgu@3900: // called by worker thread to complete shutdown process zgu@3900: static void final_shutdown(); zgu@3900: zgu@3900: protected: zgu@3900: // retrieve per-thread recorder of the specified thread. zgu@3900: // if the recorder is full, it will be enqueued to overflow zgu@3900: // queue, a new recorder is acquired from recorder pool or a zgu@3900: // new instance is created. zgu@3900: // when thread == NULL, it means global recorder zgu@3900: static MemRecorder* get_thread_recorder(JavaThread* thread); zgu@3900: zgu@3900: // per-thread recorder pool zgu@3900: static void release_thread_recorder(MemRecorder* rec); zgu@3900: static void delete_all_pooled_recorders(); zgu@3900: zgu@3900: // pending recorder queue. Recorders are queued to pending queue zgu@3900: // when they are overflowed or collected at nmt sync point. zgu@3900: static void enqueue_pending_recorder(MemRecorder* rec); zgu@3900: static MemRecorder* get_pending_recorders(); zgu@3900: static void delete_all_pending_recorders(); zgu@3900: zgu@3900: private: zgu@3900: // retrieve a pooled memory record or create new one if there is not zgu@3900: // one available zgu@3900: static MemRecorder* get_new_or_pooled_instance(); zgu@3900: static void create_memory_record(address addr, MEMFLAGS type, zgu@3900: size_t size, address pc, Thread* thread); zgu@3900: static void create_record_in_recorder(address addr, MEMFLAGS type, zgu@3935: size_t size, address pc, JavaThread* thread); zgu@3900: zgu@3900: private: zgu@3900: // global memory snapshot zgu@3900: static MemSnapshot* _snapshot; zgu@3900: zgu@3900: // a memory baseline of snapshot zgu@3900: static MemBaseline _baseline; zgu@3900: zgu@3900: // query lock zgu@3936: static Mutex* _query_lock; zgu@3900: zgu@3900: // a thread can start to allocate memory before it is attached zgu@3900: // to VM 'Thread', those memory activities are recorded here. zgu@3900: // ThreadCritical is required to guard this global recorder. zgu@3900: static MemRecorder* _global_recorder; zgu@3900: zgu@3900: // main thread id zgu@3900: debug_only(static intx _main_thread_tid;) zgu@3900: zgu@3900: // pending recorders to be merged zgu@3900: static volatile MemRecorder* _merge_pending_queue; zgu@3900: zgu@3900: NOT_PRODUCT(static volatile jint _pending_recorder_count;) zgu@3900: zgu@3900: // pooled memory recorders zgu@3900: static volatile MemRecorder* _pooled_recorders; zgu@3900: zgu@3900: // memory recorder pool management, uses following zgu@3900: // counter to determine if a released memory recorder zgu@3900: // should be pooled zgu@3900: zgu@3900: // latest thread count zgu@3900: static int _thread_count; zgu@3900: // pooled recorder count zgu@3900: static volatile jint _pooled_recorder_count; zgu@3900: zgu@3900: zgu@3900: // worker thread to merge pending recorders into snapshot zgu@3900: static MemTrackWorker* _worker_thread; zgu@3900: zgu@3900: // how many safepoints we skipped without entering sync point zgu@3900: static int _sync_point_skip_count; zgu@3900: zgu@3900: // if the tracker is properly intialized zgu@3900: static bool _is_tracker_ready; zgu@3900: // tracking level (off, summary and detail) zgu@3900: static enum NMTLevel _tracking_level; zgu@3900: zgu@3900: // current nmt state zgu@3900: static volatile enum NMTStates _state; zgu@3900: // the reason for shutting down nmt zgu@3900: static enum ShutdownReason _reason; zgu@3900: }; zgu@3900: jprovino@4165: #endif // !INCLUDE_NMT jprovino@4165: zgu@3900: #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP