src/share/vm/services/memTracker.hpp

Print this page
rev 3901 : JDK-8005012: Add WB APIs to better support NMT testing
Summary:


  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 extern bool NMT_track_callsite;
 113 




 114 #ifdef ASSERT
 115   #define DEBUG_CALLER_PC  (NMT_track_callsite ? os::get_caller_pc(2) : 0)
 116 #else
 117   #define DEBUG_CALLER_PC  0
 118 #endif
 119 
 120 // The thread closure walks threads to collect per-thread
 121 // memory recorders at NMT sync point
 122 class SyncThreadRecorderClosure : public ThreadClosure {
 123  private:
 124   int _thread_count;
 125 
 126  public:
 127   SyncThreadRecorderClosure() {
 128     _thread_count =0;
 129   }
 130 
 131   void do_thread(Thread* thread);
 132   int  get_thread_count() const {
 133     return _thread_count;


 362       create_memory_record(base, (flags | MemPointerRecord::virtual_memory_type_tag()),
 363                            0, DEBUG_CALLER_PC, thread);
 364     }
 365   }
 366 
 367 
 368   // create memory baseline of current memory snapshot
 369   static bool baseline();
 370   // is there a memory baseline
 371   static bool has_baseline() {
 372     return _baseline.baselined();
 373   }
 374 
 375   // print memory usage from current snapshot
 376   static bool print_memory_usage(BaselineOutputer& out, size_t unit,
 377            bool summary_only = true);
 378   // compare memory usage between current snapshot and baseline
 379   static bool compare_memory_usage(BaselineOutputer& out, size_t unit,
 380            bool summary_only = true);
 381 





 382   // sync is called within global safepoint to synchronize nmt data
 383   static void sync();
 384 
 385   // called when a thread is about to exit
 386   static void thread_exiting(JavaThread* thread);
 387 
 388   // retrieve global snapshot
 389   static MemSnapshot* get_snapshot() {
 390     if (shutdown_in_progress()) {
 391       return NULL;
 392     }
 393     return _snapshot;
 394   }
 395 
 396   // print tracker stats
 397   NOT_PRODUCT(static void print_tracker_stats(outputStream* st);)
 398   NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);)
 399 
 400  private:
 401   // start native memory tracking worker thread


 414 
 415   // per-thread recorder pool
 416   static void release_thread_recorder(MemRecorder* rec);
 417   static void delete_all_pooled_recorders();
 418 
 419   // pending recorder queue. Recorders are queued to pending queue
 420   // when they are overflowed or collected at nmt sync point.
 421   static void enqueue_pending_recorder(MemRecorder* rec);
 422   static MemRecorder* get_pending_recorders();
 423   static void delete_all_pending_recorders();
 424 
 425  private:
 426   // retrieve a pooled memory record or create new one if there is not
 427   // one available
 428   static MemRecorder* get_new_or_pooled_instance();
 429   static void create_memory_record(address addr, MEMFLAGS type,
 430                    size_t size, address pc, Thread* thread);
 431   static void create_record_in_recorder(address addr, MEMFLAGS type,
 432                    size_t size, address pc, JavaThread* thread);
 433 









 434  private:
 435   // global memory snapshot
 436   static MemSnapshot*     _snapshot;
 437 
 438   // a memory baseline of snapshot
 439   static MemBaseline      _baseline;
 440 
 441   // query lock
 442   static Mutex*           _query_lock;
 443 
 444   // a thread can start to allocate memory before it is attached
 445   // to VM 'Thread', those memory activities are recorded here.
 446   // ThreadCritical is required to guard this global recorder.
 447   static MemRecorder*     _global_recorder;
 448 
 449   // main thread id
 450   debug_only(static intx   _main_thread_tid;)
 451 
 452   // pending recorders to be merged
 453   static volatile MemRecorder*      _merge_pending_queue;


 465   static int               _thread_count;
 466   // pooled recorder count
 467   static volatile jint     _pooled_recorder_count;
 468 
 469 
 470   // worker thread to merge pending recorders into snapshot
 471   static MemTrackWorker*  _worker_thread;
 472 
 473   // how many safepoints we skipped without entering sync point
 474   static int              _sync_point_skip_count;
 475 
 476   // if the tracker is properly intialized
 477   static bool             _is_tracker_ready;
 478   // tracking level (off, summary and detail)
 479   static enum NMTLevel    _tracking_level;
 480 
 481   // current nmt state
 482   static volatile enum NMTStates   _state;
 483   // the reason for shutting down nmt
 484   static enum ShutdownReason       _reason;





 485 };
 486 
 487 #endif // !INCLUDE_NMT
 488 
 489 #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP


  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 bool wbtest_wait_for_data_merge() { }
  95 
  96    static inline void sync() { }
  97    static inline void thread_exiting(JavaThread* thread) { }

  98 };
  99 
 100 
 101 #else // !INCLUDE_NMT
 102 
 103 #include "memory/allocation.hpp"
 104 #include "runtime/globals.hpp"
 105 #include "runtime/mutex.hpp"
 106 #include "runtime/os.hpp"
 107 #include "runtime/thread.hpp"
 108 #include "services/memPtr.hpp"
 109 #include "services/memRecorder.hpp"
 110 #include "services/memSnapshot.hpp"
 111 #include "services/memTrackWorker.hpp"
 112 
 113 extern bool NMT_track_callsite;
 114 
 115 #ifndef MAX_UNSIGNED_LONG
 116 #define MAX_UNSIGNED_LONG    (unsigned long)(-1)
 117 #endif
 118 
 119 #ifdef ASSERT
 120   #define DEBUG_CALLER_PC  (NMT_track_callsite ? os::get_caller_pc(2) : 0)
 121 #else
 122   #define DEBUG_CALLER_PC  0
 123 #endif
 124 
 125 // The thread closure walks threads to collect per-thread
 126 // memory recorders at NMT sync point
 127 class SyncThreadRecorderClosure : public ThreadClosure {
 128  private:
 129   int _thread_count;
 130 
 131  public:
 132   SyncThreadRecorderClosure() {
 133     _thread_count =0;
 134   }
 135 
 136   void do_thread(Thread* thread);
 137   int  get_thread_count() const {
 138     return _thread_count;


 367       create_memory_record(base, (flags | MemPointerRecord::virtual_memory_type_tag()),
 368                            0, DEBUG_CALLER_PC, thread);
 369     }
 370   }
 371 
 372 
 373   // create memory baseline of current memory snapshot
 374   static bool baseline();
 375   // is there a memory baseline
 376   static bool has_baseline() {
 377     return _baseline.baselined();
 378   }
 379 
 380   // print memory usage from current snapshot
 381   static bool print_memory_usage(BaselineOutputer& out, size_t unit,
 382            bool summary_only = true);
 383   // compare memory usage between current snapshot and baseline
 384   static bool compare_memory_usage(BaselineOutputer& out, size_t unit,
 385            bool summary_only = true);
 386 
 387   // the version for whitebox testing support, it ensures that all memory
 388   // activities before this method call, are reflected in the snapshot
 389   // database.
 390   static bool wbtest_wait_for_data_merge();
 391 
 392   // sync is called within global safepoint to synchronize nmt data
 393   static void sync();
 394 
 395   // called when a thread is about to exit
 396   static void thread_exiting(JavaThread* thread);
 397 
 398   // retrieve global snapshot
 399   static MemSnapshot* get_snapshot() {
 400     if (shutdown_in_progress()) {
 401       return NULL;
 402     }
 403     return _snapshot;
 404   }
 405 
 406   // print tracker stats
 407   NOT_PRODUCT(static void print_tracker_stats(outputStream* st);)
 408   NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);)
 409 
 410  private:
 411   // start native memory tracking worker thread


 424 
 425   // per-thread recorder pool
 426   static void release_thread_recorder(MemRecorder* rec);
 427   static void delete_all_pooled_recorders();
 428 
 429   // pending recorder queue. Recorders are queued to pending queue
 430   // when they are overflowed or collected at nmt sync point.
 431   static void enqueue_pending_recorder(MemRecorder* rec);
 432   static MemRecorder* get_pending_recorders();
 433   static void delete_all_pending_recorders();
 434 
 435  private:
 436   // retrieve a pooled memory record or create new one if there is not
 437   // one available
 438   static MemRecorder* get_new_or_pooled_instance();
 439   static void create_memory_record(address addr, MEMFLAGS type,
 440                    size_t size, address pc, Thread* thread);
 441   static void create_record_in_recorder(address addr, MEMFLAGS type,
 442                    size_t size, address pc, JavaThread* thread);
 443 
 444   static void set_current_processing_generation(unsigned long generation) {
 445     _worker_thread_idle = false;
 446     _processing_generation = generation;
 447   }
 448 
 449   static void report_worker_idle() {
 450     _worker_thread_idle = true;
 451   }
 452 
 453  private:
 454   // global memory snapshot
 455   static MemSnapshot*     _snapshot;
 456 
 457   // a memory baseline of snapshot
 458   static MemBaseline      _baseline;
 459 
 460   // query lock
 461   static Mutex*           _query_lock;
 462 
 463   // a thread can start to allocate memory before it is attached
 464   // to VM 'Thread', those memory activities are recorded here.
 465   // ThreadCritical is required to guard this global recorder.
 466   static MemRecorder*     _global_recorder;
 467 
 468   // main thread id
 469   debug_only(static intx   _main_thread_tid;)
 470 
 471   // pending recorders to be merged
 472   static volatile MemRecorder*      _merge_pending_queue;


 484   static int               _thread_count;
 485   // pooled recorder count
 486   static volatile jint     _pooled_recorder_count;
 487 
 488 
 489   // worker thread to merge pending recorders into snapshot
 490   static MemTrackWorker*  _worker_thread;
 491 
 492   // how many safepoints we skipped without entering sync point
 493   static int              _sync_point_skip_count;
 494 
 495   // if the tracker is properly intialized
 496   static bool             _is_tracker_ready;
 497   // tracking level (off, summary and detail)
 498   static enum NMTLevel    _tracking_level;
 499 
 500   // current nmt state
 501   static volatile enum NMTStates   _state;
 502   // the reason for shutting down nmt
 503   static enum ShutdownReason       _reason;
 504   // the generation that NMT is processing
 505   static volatile unsigned long    _processing_generation;
 506   // although NMT is still procesing current generation, but
 507   // there is not more recorder to process, set idle state
 508   static volatile bool             _worker_thread_idle;
 509 };
 510 
 511 #endif // !INCLUDE_NMT
 512 
 513 #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP