1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP 26 #define SHARE_VM_SERVICES_MEM_TRACKER_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/globals.hpp" 30 #include "runtime/mutex.hpp" 31 #include "runtime/os.hpp" 32 #include "runtime/thread.hpp" 33 #include "services/memPtr.hpp" 34 #include "services/memRecorder.hpp" 35 #include "services/memSnapshot.hpp" 36 #include "services/memTrackWorker.hpp" 37 38 #ifdef SOLARIS 39 #include "thread_solaris.inline.hpp" 40 #endif 41 42 #ifdef _DEBUG_ 43 #define DEBUG_CALLER_PC os::get_caller_pc(3) 44 #else 45 #define DEBUG_CALLER_PC 0 46 #endif 47 48 // The thread closure walks threads to collect per-thread 49 // memory recorders at NMT sync point 50 class SyncThreadRecorderClosure : public ThreadClosure { 51 private: 52 int _thread_count; 53 54 public: 55 SyncThreadRecorderClosure() { 56 _thread_count =0; 57 } 58 59 void do_thread(Thread* thread); 60 int get_thread_count() const { 61 return _thread_count; 62 } 63 }; 64 65 class BaselineOutputer; 66 class MemSnapshot; 67 class MemTrackWorker; 68 class Thread; 69 /* 70 * MemTracker is the 'gate' class to native memory tracking runtime. 71 */ 72 class MemTracker : AllStatic { 73 friend class MemTrackWorker; 74 friend class MemSnapshot; 75 friend class SyncThreadRecorderClosure; 76 77 // NMT state 78 enum NMTStates { 79 NMT_uninited, // not yet initialized 80 NMT_bootstrapping_single_thread, // bootstrapping, VM is in single thread mode 81 NMT_bootstrapping_multi_thread, // bootstrapping, VM is about to enter multi-thread mode 82 NMT_started, // NMT fully started 83 NMT_shutdown_pending, // shutdown pending 84 NMT_final_shutdown, // in final phase of shutdown 85 NMT_shutdown // shutdown 86 }; 87 88 89 // native memory tracking level 90 enum NMTLevel { 91 NMT_off, // native memory tracking is off 92 NMT_summary, // don't track callsite 93 NMT_detail // track callsite also 94 }; 95 96 public: 97 enum ShutdownReason { 98 NMT_shutdown_none, // no shutdown requested 99 NMT_shutdown_user, // user requested shutdown 100 NMT_normal, // normal shutdown, process exit 101 NMT_out_of_memory, // shutdown due to out of memory 102 NMT_initialization, // shutdown due to initialization failure 103 NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag 104 NMT_error_reporting, // shutdown by vmError::report_and_die() 105 NMT_out_of_generation, // running out of generation queue 106 NMT_sequence_overflow // overflow the sequence number 107 }; 108 109 public: 110 // initialize NMT tracking level from command line options, called 111 // from VM command line parsing code 112 static void init_tracking_options(const char* option_line); 113 114 // if NMT is enabled to record memory activities 115 static inline bool is_on() { 116 return (_tracking_level >= NMT_summary && 117 _state >= NMT_bootstrapping_single_thread); 118 } 119 120 // user readable reason for shutting down NMT 121 static const char* reason() { 122 switch(_reason) { 123 case NMT_shutdown_none: 124 return "Native memory tracking is not enabled"; 125 case NMT_shutdown_user: 126 return "Native memory tracking has been shutdown by user"; 127 case NMT_normal: 128 return "Native memory tracking has been shutdown due to process exiting"; 129 case NMT_initialization: 130 return "Native memory tracking failed to initialize"; 131 case NMT_error_reporting: 132 return "Native memory tracking has been shutdown due to error reporting"; 133 case NMT_out_of_generation: 134 return "Native memory tracking has been shutdown due to running out of generation buffer"; 135 case NMT_sequence_overflow: 136 return "Native memory tracking has been shutdown due to overflow the sequence number"; 137 case NMT_use_malloc_only: 138 return "Native memory tracking is not supported when UseMallocOnly is on"; 139 default: 140 ShouldNotReachHere(); 141 return NULL; 142 } 143 } 144 145 // test if we can walk native stack 146 static bool can_walk_stack() { 147 // native stack is not walkable during bootstrapping on sparc 148 #if defined(SPARC) 149 return (_state == NMT_started); 150 #else 151 return (_state >= NMT_bootstrapping_single_thread && _state <= NMT_started); 152 #endif 153 } 154 155 // if native memory tracking tracks callsite 156 static inline bool track_callsite() { return _tracking_level == NMT_detail; } 157 158 // shutdown native memory tracking capability. Native memory tracking 159 // can be shutdown by VM when it encounters low memory scenarios. 160 // Memory tracker should gracefully shutdown itself, and preserve the 161 // latest memory statistics for post morten diagnosis. 162 static void shutdown(ShutdownReason reason); 163 164 // if there is shutdown requested 165 static inline bool shutdown_in_progress() { 166 return (_state >= NMT_shutdown_pending); 167 } 168 169 // bootstrap native memory tracking, so it can start to collect raw data 170 // before worker thread can start 171 172 // the first phase of bootstrapping, when VM still in single-threaded mode 173 static void bootstrap_single_thread(); 174 // the second phase of bootstrapping, VM is about or already in multi-threaded mode 175 static void bootstrap_multi_thread(); 176 177 178 // start() has to be called when VM still in single thread mode, but after 179 // command line option parsing is done. 180 static void start(); 181 182 // record a 'malloc' call 183 static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, 184 address pc = 0, Thread* thread = NULL) { 185 assert(is_on(), "check by caller"); 186 if (NMT_CAN_TRACK(flags)) { 187 create_memory_record(addr, (flags|MemPointerRecord::malloc_tag()), size, pc, thread); 188 } 189 } 190 // record a 'free' call 191 static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { 192 if (is_on() && NMT_CAN_TRACK(flags)) { 193 create_memory_record(addr, MemPointerRecord::free_tag(), 0, 0, thread); 194 } 195 } 196 // record a 'realloc' call 197 static inline void record_realloc(address old_addr, address new_addr, size_t size, 198 MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { 199 if (is_on()) { 200 record_free(old_addr, flags, thread); 201 record_malloc(new_addr, size, flags, pc, thread); 202 } 203 } 204 205 // record arena size 206 static inline void record_arena_size(address addr, size_t size) { 207 // we add a positive offset to arena address, so we can have arena size record 208 // sorted after arena record 209 if (is_on() && !UseMallocOnly) { 210 create_memory_record((addr + sizeof(void*)), MemPointerRecord::arena_size_tag(), size, 211 0, NULL); 212 } 213 } 214 215 // record a virtual memory 'reserve' call 216 static inline void record_virtual_memory_reserve(address addr, size_t size, 217 address pc = 0, Thread* thread = NULL) { 218 if (is_on()) { 219 assert(size > 0, "reserve szero size"); 220 create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag(), 221 size, pc, thread); 222 } 223 } 224 225 // record a virtual memory 'commit' call 226 static inline void record_virtual_memory_commit(address addr, size_t size, 227 address pc = 0, Thread* thread = NULL) { 228 if (is_on()) { 229 create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag(), 230 size, pc, thread); 231 } 232 } 233 234 // record a virtual memory 'uncommit' call 235 static inline void record_virtual_memory_uncommit(address addr, size_t size, 236 Thread* thread = NULL) { 237 if (is_on()) { 238 create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag(), 239 size, 0, thread); 240 } 241 } 242 243 // record a virtual memory 'release' call 244 static inline void record_virtual_memory_release(address addr, size_t size, 245 Thread* thread = NULL) { 246 if (is_on()) { 247 create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag(), 248 size, 0, thread); 249 } 250 } 251 252 // record memory type on virtual memory base address 253 static inline void record_virtual_memory_type(address base, MEMFLAGS flags, 254 Thread* thread = NULL) { 255 if (is_on()) { 256 assert(base > 0, "wrong base address"); 257 assert((flags & (~mt_masks)) == 0, "memory type only"); 258 create_memory_record(base, (flags | MemPointerRecord::virtual_memory_type_tag()), 259 0, 0, thread); 260 } 261 } 262 263 264 // create memory baseline of current memory snapshot 265 static bool baseline(); 266 // is there a memory baseline 267 static bool has_baseline() { 268 return _baseline.baselined(); 269 } 270 271 // print memory usage from current snapshot 272 static bool print_memory_usage(BaselineOutputer& out, size_t unit, 273 bool summary_only = true); 274 // compare memory usage between current snapshot and baseline 275 static bool compare_memory_usage(BaselineOutputer& out, size_t unit, 276 bool summary_only = true); 277 278 // sync is called within global safepoint to synchronize nmt data 279 static void sync(); 280 281 // called when a thread is about to exit 282 static void thread_exiting(JavaThread* thread); 283 284 // retrieve global snapshot 285 static MemSnapshot* get_snapshot() { 286 assert(is_on(), "native memory tracking is off"); 287 if (shutdown_in_progress()) { 288 return NULL; 289 } 290 return _snapshot; 291 } 292 293 // print tracker stats 294 NOT_PRODUCT(static void print_tracker_stats(outputStream* st);) 295 NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);) 296 297 private: 298 // start native memory tracking worker thread 299 static bool start_worker(); 300 301 // called by worker thread to complete shutdown process 302 static void final_shutdown(); 303 304 protected: 305 // retrieve per-thread recorder of the specified thread. 306 // if the recorder is full, it will be enqueued to overflow 307 // queue, a new recorder is acquired from recorder pool or a 308 // new instance is created. 309 // when thread == NULL, it means global recorder 310 static MemRecorder* get_thread_recorder(JavaThread* thread); 311 312 // per-thread recorder pool 313 static void release_thread_recorder(MemRecorder* rec); 314 static void delete_all_pooled_recorders(); 315 316 // pending recorder queue. Recorders are queued to pending queue 317 // when they are overflowed or collected at nmt sync point. 318 static void enqueue_pending_recorder(MemRecorder* rec); 319 static MemRecorder* get_pending_recorders(); 320 static void delete_all_pending_recorders(); 321 322 private: 323 // retrieve a pooled memory record or create new one if there is not 324 // one available 325 static MemRecorder* get_new_or_pooled_instance(); 326 static void create_memory_record(address addr, MEMFLAGS type, 327 size_t size, address pc, Thread* thread); 328 static void create_record_in_recorder(address addr, MEMFLAGS type, 329 size_t size, address pc, Thread* thread); 330 331 private: 332 // global memory snapshot 333 static MemSnapshot* _snapshot; 334 335 // a memory baseline of snapshot 336 static MemBaseline _baseline; 337 338 // query lock 339 static Mutex _query_lock; 340 341 // a thread can start to allocate memory before it is attached 342 // to VM 'Thread', those memory activities are recorded here. 343 // ThreadCritical is required to guard this global recorder. 344 static MemRecorder* _global_recorder; 345 346 // main thread id 347 debug_only(static intx _main_thread_tid;) 348 349 // pending recorders to be merged 350 static volatile MemRecorder* _merge_pending_queue; 351 352 NOT_PRODUCT(static volatile jint _pending_recorder_count;) 353 354 // pooled memory recorders 355 static volatile MemRecorder* _pooled_recorders; 356 357 // memory recorder pool management, uses following 358 // counter to determine if a released memory recorder 359 // should be pooled 360 361 // latest thread count 362 static int _thread_count; 363 // pooled recorder count 364 static volatile jint _pooled_recorder_count; 365 366 367 // worker thread to merge pending recorders into snapshot 368 static MemTrackWorker* _worker_thread; 369 370 // how many safepoints we skipped without entering sync point 371 static int _sync_point_skip_count; 372 373 // if the tracker is properly intialized 374 static bool _is_tracker_ready; 375 // tracking level (off, summary and detail) 376 static enum NMTLevel _tracking_level; 377 378 // current nmt state 379 static volatile enum NMTStates _state; 380 // the reason for shutting down nmt 381 static enum ShutdownReason _reason; 382 }; 383 384 #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP