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 "utilities/macros.hpp" 29 30 #if !INCLUDE_NMT 31 32 #include "utilities/ostream.hpp" 33 34 class BaselineOutputer : public StackObj { 35 36 }; 37 38 class BaselineTTYOutputer : public BaselineOutputer { 39 public: 40 BaselineTTYOutputer(outputStream* st) { } 41 }; 42 43 class MemTracker: AllStatic { 44 public: 45 enum ShutdownReason { 46 NMT_shutdown_none, // no shutdown requested 47 NMT_shutdown_user, // user requested shutdown 48 NMT_normal, // normal shutdown, process exit 49 NMT_out_of_memory, // shutdown due to out of memory 50 NMT_initialization, // shutdown due to initialization failure 51 NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag 52 NMT_error_reporting, // shutdown by vmError::report_and_die() 53 NMT_out_of_generation, // running out of generation queue 54 NMT_sequence_overflow // overflow the sequence number 55 }; 56 57 58 public: 59 static inline void init_tracking_options(const char* option_line) { } 60 static inline bool is_on() { return false; } 61 static const char* reason() { return "Native memory tracking is not implemented"; } 62 static inline bool can_walk_stack() { return false; } 63 64 static inline void bootstrap_single_thread() { } 65 static inline void bootstrap_multi_thread() { } 66 static inline void start() { } 67 68 static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, 69 address pc = 0, Thread* thread = NULL) { } 70 static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { } 71 static inline void record_realloc(address old_addr, address new_addr, size_t size, 72 MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { } 73 static inline void record_arena_size(address addr, size_t size) { } 74 static inline void record_virtual_memory_reserve(address addr, size_t size, 75 address pc = 0, Thread* thread = NULL) { } 76 static inline void record_virtual_memory_commit(address addr, size_t size, 77 address pc = 0, Thread* thread = NULL) { } 78 static inline void record_virtual_memory_uncommit(address addr, size_t size, 79 Thread* thread = NULL) { } 80 static inline void record_virtual_memory_release(address addr, size_t size, 81 Thread* thread = NULL) { } 82 static inline void record_virtual_memory_type(address base, MEMFLAGS flags, 83 Thread* thread = NULL) { } 84 static inline bool baseline() { return false; } 85 static inline bool has_baseline() { return false; } 86 87 static void shutdown(ShutdownReason reason) { } 88 static inline bool shutdown_in_progress() { } 89 static bool print_memory_usage(BaselineOutputer& out, size_t unit, 90 bool summary_only = true) { } 91 static bool compare_memory_usage(BaselineOutputer& out, size_t unit, 92 bool summary_only = true) { } 93 94 static inline void sync() { } 95 static inline void thread_exiting(JavaThread* thread) { } 96 97 }; 98 99 100 #else // !INCLUDE_NMT 101 102 #include "memory/allocation.hpp" 103 #include "runtime/globals.hpp" 104 #include "runtime/mutex.hpp" 105 #include "runtime/os.hpp" 106 #include "runtime/thread.hpp" 107 #include "services/memPtr.hpp" 108 #include "services/memRecorder.hpp" 109 #include "services/memSnapshot.hpp" 110 #include "services/memTrackWorker.hpp" 111 112 #ifdef SOLARIS 113 #include "thread_solaris.inline.hpp" 114 #endif 115 116 #ifdef _DEBUG 117 #define DEBUG_CALLER_PC os::get_caller_pc(3) 118 #else 119 #define DEBUG_CALLER_PC 0 120 #endif 121 122 // The thread closure walks threads to collect per-thread 123 // memory recorders at NMT sync point 124 class SyncThreadRecorderClosure : public ThreadClosure { 125 private: 126 int _thread_count; 127 128 public: 129 SyncThreadRecorderClosure() { 130 _thread_count =0; 131 } 132 133 void do_thread(Thread* thread); 134 int get_thread_count() const { 135 return _thread_count; 136 } 137 }; 138 139 class BaselineOutputer; 140 class MemSnapshot; 141 class MemTrackWorker; 142 class Thread; 143 /* 144 * MemTracker is the 'gate' class to native memory tracking runtime. 145 */ 146 class MemTracker : AllStatic { 147 friend class MemTrackWorker; 148 friend class MemSnapshot; 149 friend class SyncThreadRecorderClosure; 150 151 // NMT state 152 enum NMTStates { 153 NMT_uninited, // not yet initialized 154 NMT_bootstrapping_single_thread, // bootstrapping, VM is in single thread mode 155 NMT_bootstrapping_multi_thread, // bootstrapping, VM is about to enter multi-thread mode 156 NMT_started, // NMT fully started 157 NMT_shutdown_pending, // shutdown pending 158 NMT_final_shutdown, // in final phase of shutdown 159 NMT_shutdown // shutdown 160 }; 161 162 163 // native memory tracking level 164 enum NMTLevel { 165 NMT_off, // native memory tracking is off 166 NMT_summary, // don't track callsite 167 NMT_detail // track callsite also 168 }; 169 170 public: 171 enum ShutdownReason { 172 NMT_shutdown_none, // no shutdown requested 173 NMT_shutdown_user, // user requested shutdown 174 NMT_normal, // normal shutdown, process exit 175 NMT_out_of_memory, // shutdown due to out of memory 176 NMT_initialization, // shutdown due to initialization failure 177 NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag 178 NMT_error_reporting, // shutdown by vmError::report_and_die() 179 NMT_out_of_generation, // running out of generation queue 180 NMT_sequence_overflow // overflow the sequence number 181 }; 182 183 public: 184 // initialize NMT tracking level from command line options, called 185 // from VM command line parsing code 186 static void init_tracking_options(const char* option_line); 187 188 // if NMT is enabled to record memory activities 189 static inline bool is_on() { 190 return (_tracking_level >= NMT_summary && 191 _state >= NMT_bootstrapping_single_thread); 192 } 193 194 // user readable reason for shutting down NMT 195 static const char* reason() { 196 switch(_reason) { 197 case NMT_shutdown_none: 198 return "Native memory tracking is not enabled"; 199 case NMT_shutdown_user: 200 return "Native memory tracking has been shutdown by user"; 201 case NMT_normal: 202 return "Native memory tracking has been shutdown due to process exiting"; 203 case NMT_out_of_memory: 204 return "Native memory tracking has been shutdown due to out of native memory"; 205 case NMT_initialization: 206 return "Native memory tracking failed to initialize"; 207 case NMT_error_reporting: 208 return "Native memory tracking has been shutdown due to error reporting"; 209 case NMT_out_of_generation: 210 return "Native memory tracking has been shutdown due to running out of generation buffer"; 211 case NMT_sequence_overflow: 212 return "Native memory tracking has been shutdown due to overflow the sequence number"; 213 case NMT_use_malloc_only: 214 return "Native memory tracking is not supported when UseMallocOnly is on"; 215 default: 216 ShouldNotReachHere(); 217 return NULL; 218 } 219 } 220 221 // test if we can walk native stack 222 static bool can_walk_stack() { 223 // native stack is not walkable during bootstrapping on sparc 224 #if defined(SPARC) 225 return (_state == NMT_started); 226 #else 227 return (_state >= NMT_bootstrapping_single_thread && _state <= NMT_started); 228 #endif 229 } 230 231 // if native memory tracking tracks callsite 232 static inline bool track_callsite() { return _tracking_level == NMT_detail; } 233 234 // shutdown native memory tracking capability. Native memory tracking 235 // can be shutdown by VM when it encounters low memory scenarios. 236 // Memory tracker should gracefully shutdown itself, and preserve the 237 // latest memory statistics for post morten diagnosis. 238 static void shutdown(ShutdownReason reason); 239 240 // if there is shutdown requested 241 static inline bool shutdown_in_progress() { 242 return (_state >= NMT_shutdown_pending); 243 } 244 245 // bootstrap native memory tracking, so it can start to collect raw data 246 // before worker thread can start 247 248 // the first phase of bootstrapping, when VM still in single-threaded mode 249 static void bootstrap_single_thread(); 250 // the second phase of bootstrapping, VM is about or already in multi-threaded mode 251 static void bootstrap_multi_thread(); 252 253 254 // start() has to be called when VM still in single thread mode, but after 255 // command line option parsing is done. 256 static void start(); 257 258 // record a 'malloc' call 259 static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, 260 address pc = 0, Thread* thread = NULL) { 261 if (NMT_CAN_TRACK(flags)) { 262 create_memory_record(addr, (flags|MemPointerRecord::malloc_tag()), size, pc, thread); 263 } 264 } 265 // record a 'free' call 266 static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { 267 if (is_on() && NMT_CAN_TRACK(flags)) { 268 create_memory_record(addr, MemPointerRecord::free_tag(), 0, 0, thread); 269 } 270 } 271 // record a 'realloc' call 272 static inline void record_realloc(address old_addr, address new_addr, size_t size, 273 MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { 274 if (is_on()) { 275 record_free(old_addr, flags, thread); 276 record_malloc(new_addr, size, flags, pc, thread); 277 } 278 } 279 280 // record arena size 281 static inline void record_arena_size(address addr, size_t size) { 282 // we add a positive offset to arena address, so we can have arena size record 283 // sorted after arena record 284 if (is_on() && !UseMallocOnly) { 285 create_memory_record((addr + sizeof(void*)), MemPointerRecord::arena_size_tag(), size, 286 0, NULL); 287 } 288 } 289 290 // record a virtual memory 'reserve' call 291 static inline void record_virtual_memory_reserve(address addr, size_t size, 292 address pc = 0, Thread* thread = NULL) { 293 if (is_on()) { 294 assert(size > 0, "reserve szero size"); 295 create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag(), 296 size, pc, thread); 297 } 298 } 299 300 static inline void record_thread_stack(address addr, size_t size, Thread* thr, 301 address pc = 0) { 302 if (is_on()) { 303 assert(size > 0 && thr != NULL, "Sanity check"); 304 create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag() | mtThreadStack, 305 size, pc, thr); 306 create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag() | mtThreadStack, 307 size, pc, thr); 308 } 309 } 310 311 static inline void release_thread_stack(address addr, size_t size, Thread* thr) { 312 if (is_on()) { 313 assert(size > 0 && thr != NULL, "Sanity check"); 314 create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag() | mtThreadStack, 315 size, DEBUG_CALLER_PC, thr); 316 create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag() | mtThreadStack, 317 size, DEBUG_CALLER_PC, thr); 318 } 319 } 320 321 // record a virtual memory 'commit' call 322 static inline void record_virtual_memory_commit(address addr, size_t size, 323 address pc = 0, Thread* thread = NULL) { 324 if (is_on()) { 325 create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag(), 326 size, DEBUG_CALLER_PC, thread); 327 } 328 } 329 330 // record a virtual memory 'uncommit' call 331 static inline void record_virtual_memory_uncommit(address addr, size_t size, 332 Thread* thread = NULL) { 333 if (is_on()) { 334 create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag(), 335 size, DEBUG_CALLER_PC, thread); 336 } 337 } 338 339 // record a virtual memory 'release' call 340 static inline void record_virtual_memory_release(address addr, size_t size, 341 Thread* thread = NULL) { 342 if (is_on()) { 343 create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag(), 344 size, DEBUG_CALLER_PC, thread); 345 } 346 } 347 348 // record memory type on virtual memory base address 349 static inline void record_virtual_memory_type(address base, MEMFLAGS flags, 350 Thread* thread = NULL) { 351 if (is_on()) { 352 assert(base > 0, "wrong base address"); 353 assert((flags & (~mt_masks)) == 0, "memory type only"); 354 create_memory_record(base, (flags | MemPointerRecord::virtual_memory_type_tag()), 355 0, DEBUG_CALLER_PC, thread); 356 } 357 } 358 359 360 // create memory baseline of current memory snapshot 361 static bool baseline(); 362 // is there a memory baseline 363 static bool has_baseline() { 364 return _baseline.baselined(); 365 } 366 367 // print memory usage from current snapshot 368 static bool print_memory_usage(BaselineOutputer& out, size_t unit, 369 bool summary_only = true); 370 // compare memory usage between current snapshot and baseline 371 static bool compare_memory_usage(BaselineOutputer& out, size_t unit, 372 bool summary_only = true); 373 374 // sync is called within global safepoint to synchronize nmt data 375 static void sync(); 376 377 // called when a thread is about to exit 378 static void thread_exiting(JavaThread* thread); 379 380 // retrieve global snapshot 381 static MemSnapshot* get_snapshot() { 382 if (shutdown_in_progress()) { 383 return NULL; 384 } 385 return _snapshot; 386 } 387 388 // print tracker stats 389 NOT_PRODUCT(static void print_tracker_stats(outputStream* st);) 390 NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);) 391 392 private: 393 // start native memory tracking worker thread 394 static bool start_worker(); 395 396 // called by worker thread to complete shutdown process 397 static void final_shutdown(); 398 399 protected: 400 // retrieve per-thread recorder of the specified thread. 401 // if the recorder is full, it will be enqueued to overflow 402 // queue, a new recorder is acquired from recorder pool or a 403 // new instance is created. 404 // when thread == NULL, it means global recorder 405 static MemRecorder* get_thread_recorder(JavaThread* thread); 406 407 // per-thread recorder pool 408 static void release_thread_recorder(MemRecorder* rec); 409 static void delete_all_pooled_recorders(); 410 411 // pending recorder queue. Recorders are queued to pending queue 412 // when they are overflowed or collected at nmt sync point. 413 static void enqueue_pending_recorder(MemRecorder* rec); 414 static MemRecorder* get_pending_recorders(); 415 static void delete_all_pending_recorders(); 416 417 private: 418 // retrieve a pooled memory record or create new one if there is not 419 // one available 420 static MemRecorder* get_new_or_pooled_instance(); 421 static void create_memory_record(address addr, MEMFLAGS type, 422 size_t size, address pc, Thread* thread); 423 static void create_record_in_recorder(address addr, MEMFLAGS type, 424 size_t size, address pc, JavaThread* thread); 425 426 private: 427 // global memory snapshot 428 static MemSnapshot* _snapshot; 429 430 // a memory baseline of snapshot 431 static MemBaseline _baseline; 432 433 // query lock 434 static Mutex* _query_lock; 435 436 // a thread can start to allocate memory before it is attached 437 // to VM 'Thread', those memory activities are recorded here. 438 // ThreadCritical is required to guard this global recorder. 439 static MemRecorder* _global_recorder; 440 441 // main thread id 442 debug_only(static intx _main_thread_tid;) 443 444 // pending recorders to be merged 445 static volatile MemRecorder* _merge_pending_queue; 446 447 NOT_PRODUCT(static volatile jint _pending_recorder_count;) 448 449 // pooled memory recorders 450 static volatile MemRecorder* _pooled_recorders; 451 452 // memory recorder pool management, uses following 453 // counter to determine if a released memory recorder 454 // should be pooled 455 456 // latest thread count 457 static int _thread_count; 458 // pooled recorder count 459 static volatile jint _pooled_recorder_count; 460 461 462 // worker thread to merge pending recorders into snapshot 463 static MemTrackWorker* _worker_thread; 464 465 // how many safepoints we skipped without entering sync point 466 static int _sync_point_skip_count; 467 468 // if the tracker is properly intialized 469 static bool _is_tracker_ready; 470 // tracking level (off, summary and detail) 471 static enum NMTLevel _tracking_level; 472 473 // current nmt state 474 static volatile enum NMTStates _state; 475 // the reason for shutting down nmt 476 static enum ShutdownReason _reason; 477 }; 478 479 #endif // !INCLUDE_NMT 480 481 #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP