comparison src/share/vm/services/memTracker.hpp @ 20804:7848fc12602b

Merge with jdk8u40-b25
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Tue, 07 Apr 2015 14:58:49 +0200
parents 417e3b8d04c5
children
comparison
equal deleted inserted replaced
20184:84105dcdb05b 20804:7848fc12602b
1 /* 1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
23 */ 23 */
24 24
25 #ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP 25 #ifndef SHARE_VM_SERVICES_MEM_TRACKER_HPP
26 #define SHARE_VM_SERVICES_MEM_TRACKER_HPP 26 #define SHARE_VM_SERVICES_MEM_TRACKER_HPP
27 27
28 #include "utilities/macros.hpp" 28 #include "services/nmtCommon.hpp"
29 #include "utilities/nativeCallStack.hpp"
30
29 31
30 #if !INCLUDE_NMT 32 #if !INCLUDE_NMT
31 33
32 #include "utilities/ostream.hpp" 34 #define CURRENT_PC NativeCallStack::EMPTY_STACK
33 35 #define CALLER_PC NativeCallStack::EMPTY_STACK
34 class BaselineOutputer : public StackObj { 36
35 37 class Tracker : public StackObj {
38 public:
39 Tracker() { }
40 void record(address addr, size_t size) { }
36 }; 41 };
37 42
38 class BaselineTTYOutputer : public BaselineOutputer { 43 class MemTracker : AllStatic {
39 public: 44 public:
40 BaselineTTYOutputer(outputStream* st) { } 45 static inline NMT_TrackingLevel tracking_level() { return NMT_off; }
46 static inline void shutdown() { }
47 static inline void init() { }
48 static bool check_launcher_nmt_support(const char* value) { return true; }
49 static bool verify_nmt_option() { return true; }
50
51 static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag,
52 const NativeCallStack& stack, NMT_TrackingLevel level) { return mem_base; }
53 static inline size_t malloc_header_size(NMT_TrackingLevel level) { return 0; }
54 static inline size_t malloc_header_size(void* memblock) { return 0; }
55 static inline void* malloc_base(void* memblock) { return memblock; }
56 static inline void* record_free(void* memblock) { return memblock; }
57
58 static inline void record_new_arena(MEMFLAGS flag) { }
59 static inline void record_arena_free(MEMFLAGS flag) { }
60 static inline void record_arena_size_change(int diff, MEMFLAGS flag) { }
61 static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack,
62 MEMFLAGS flag = mtNone) { }
63 static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size,
64 const NativeCallStack& stack, MEMFLAGS flag = mtNone) { }
65 static inline void record_virtual_memory_commit(void* addr, size_t size, const NativeCallStack& stack) { }
66 static inline Tracker get_virtual_memory_uncommit_tracker() { return Tracker(); }
67 static inline Tracker get_virtual_memory_release_tracker() { }
68 static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) { }
69 static inline void record_thread_stack(void* addr, size_t size) { }
70 static inline void release_thread_stack(void* addr, size_t size) { }
71
72 static void final_report(outputStream*) { }
73 static void error_report(outputStream*) { }
41 }; 74 };
42 75
76 #else
77
78 #include "runtime/atomic.hpp"
79 #include "runtime/threadCritical.hpp"
80 #include "services/mallocTracker.hpp"
81 #include "services/virtualMemoryTracker.hpp"
82
83 extern volatile bool NMT_stack_walkable;
84
85 #define CURRENT_PC ((MemTracker::tracking_level() == NMT_detail && NMT_stack_walkable) ? \
86 NativeCallStack(0, true) : NativeCallStack::EMPTY_STACK)
87 #define CALLER_PC ((MemTracker::tracking_level() == NMT_detail && NMT_stack_walkable) ? \
88 NativeCallStack(1, true) : NativeCallStack::EMPTY_STACK)
89
90 class MemBaseline;
91 class Mutex;
92
93 // Tracker is used for guarding 'release' semantics of virtual memory operation, to avoid
94 // the other thread obtains and records the same region that is just 'released' by current
95 // thread but before it can record the operation.
96 class Tracker : public StackObj {
97 public:
98 enum TrackerType {
99 uncommit,
100 release
101 };
102
103 public:
104 Tracker(enum TrackerType type) : _type(type) { }
105 void record(address addr, size_t size);
106 private:
107 enum TrackerType _type;
108 // Virtual memory tracking data structures are protected by ThreadCritical lock.
109 ThreadCritical _tc;
110 };
111
43 class MemTracker : AllStatic { 112 class MemTracker : AllStatic {
44 public: 113 public:
45 enum ShutdownReason { 114 static inline NMT_TrackingLevel tracking_level() {
46 NMT_shutdown_none, // no shutdown requested 115 if (_tracking_level == NMT_unknown) {
47 NMT_shutdown_user, // user requested shutdown 116 // No fencing is needed here, since JVM is in single-threaded
48 NMT_normal, // normal shutdown, process exit 117 // mode.
49 NMT_out_of_memory, // shutdown due to out of memory 118 _tracking_level = init_tracking_level();
50 NMT_initialization, // shutdown due to initialization failure 119 _cmdline_tracking_level = _tracking_level;
51 NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag 120 }
52 NMT_error_reporting, // shutdown by vmError::report_and_die() 121 return _tracking_level;
53 NMT_out_of_generation, // running out of generation queue 122 }
54 NMT_sequence_overflow // overflow the sequence number 123
55 }; 124 // A late initialization, for the stuff(s) can not be
56 125 // done in init_tracking_level(), which can NOT malloc
57 class Tracker { 126 // any memory.
58 public: 127 static void init();
59 void discard() { } 128
60 129 // Shutdown native memory tracking
61 void record(address addr, size_t size = 0, MEMFLAGS flags = mtNone, address pc = NULL) { } 130 static void shutdown();
62 void record(address old_addr, address new_addr, size_t size, 131
63 MEMFLAGS flags, address pc = NULL) { } 132 // Verify native memory tracking command line option.
64 }; 133 // This check allows JVM to detect if compatible launcher
65 134 // is used.
66 private: 135 // If an incompatible launcher is used, NMT may not be
67 static Tracker _tkr; 136 // able to start, even it is enabled by command line option.
68 137 // A warning message should be given if it is encountered.
69 138 static bool check_launcher_nmt_support(const char* value);
70 public: 139
71 static inline void init_tracking_options(const char* option_line) { } 140 // This method checks native memory tracking environment
72 static inline bool is_on() { return false; } 141 // variable value passed by launcher.
73 static const char* reason() { return "Native memory tracking is not implemented"; } 142 // Launcher only obligates to pass native memory tracking
74 static inline bool can_walk_stack() { return false; } 143 // option value, but not obligates to validate the value,
75 144 // and launcher has option to discard native memory tracking
76 static inline void bootstrap_single_thread() { } 145 // option from the command line once it sets up the environment
77 static inline void bootstrap_multi_thread() { } 146 // variable, so NMT has to catch the bad value here.
78 static inline void start() { } 147 static bool verify_nmt_option();
79 148
80 static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, 149 // Transition the tracking level to specified level
81 address pc = 0, Thread* thread = NULL) { } 150 static bool transition_to(NMT_TrackingLevel level);
82 static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { } 151
83 static inline void record_arena_size(address addr, size_t size) { } 152 static inline void* record_malloc(void* mem_base, size_t size, MEMFLAGS flag,
84 static inline void record_virtual_memory_reserve(address addr, size_t size, 153 const NativeCallStack& stack, NMT_TrackingLevel level) {
85 MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { } 154 return MallocTracker::record_malloc(mem_base, size, flag, stack, level);
86 static inline void record_virtual_memory_reserve_and_commit(address addr, size_t size, 155 }
87 MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { } 156
88 static inline void record_virtual_memory_commit(address addr, size_t size, 157 static inline size_t malloc_header_size(NMT_TrackingLevel level) {
89 address pc = 0, Thread* thread = NULL) { } 158 return MallocTracker::malloc_header_size(level);
90 static inline void record_virtual_memory_release(address addr, size_t size, 159 }
91 Thread* thread = NULL) { } 160
92 static inline void record_virtual_memory_type(address base, MEMFLAGS flags, 161 static size_t malloc_header_size(void* memblock) {
93 Thread* thread = NULL) { } 162 if (tracking_level() != NMT_off) {
94 static inline Tracker get_realloc_tracker() { return _tkr; } 163 return MallocTracker::get_header_size(memblock);
95 static inline Tracker get_virtual_memory_uncommit_tracker() { return _tkr; } 164 }
96 static inline Tracker get_virtual_memory_release_tracker() { return _tkr; } 165 return 0;
97 static inline bool baseline() { return false; } 166 }
98 static inline bool has_baseline() { return false; } 167
99 168 // To malloc base address, which is the starting address
100 static inline void set_autoShutdown(bool value) { } 169 // of malloc tracking header if tracking is enabled.
101 static void shutdown(ShutdownReason reason) { } 170 // Otherwise, it returns the same address.
102 static inline bool shutdown_in_progress() { return false; } 171 static void* malloc_base(void* memblock);
103 static bool print_memory_usage(BaselineOutputer& out, size_t unit, 172
104 bool summary_only = true) { return false; } 173 // Record malloc free and return malloc base address
105 static bool compare_memory_usage(BaselineOutputer& out, size_t unit, 174 static inline void* record_free(void* memblock) {
106 bool summary_only = true) { return false; } 175 return MallocTracker::record_free(memblock);
107 176 }
108 static bool wbtest_wait_for_data_merge() { return false; } 177
109 178
110 static inline void sync() { } 179 // Record creation of an arena
111 static inline void thread_exiting(JavaThread* thread) { } 180 static inline void record_new_arena(MEMFLAGS flag) {
181 if (tracking_level() < NMT_summary) return;
182 MallocTracker::record_new_arena(flag);
183 }
184
185 // Record destruction of an arena
186 static inline void record_arena_free(MEMFLAGS flag) {
187 if (tracking_level() < NMT_summary) return;
188 MallocTracker::record_arena_free(flag);
189 }
190
191 // Record arena size change. Arena size is the size of all arena
192 // chuncks that backing up the arena.
193 static inline void record_arena_size_change(int diff, MEMFLAGS flag) {
194 if (tracking_level() < NMT_summary) return;
195 MallocTracker::record_arena_size_change(diff, flag);
196 }
197
198 static inline void record_virtual_memory_reserve(void* addr, size_t size, const NativeCallStack& stack,
199 MEMFLAGS flag = mtNone) {
200 if (tracking_level() < NMT_summary) return;
201 if (addr != NULL) {
202 ThreadCritical tc;
203 // Recheck to avoid potential racing during NMT shutdown
204 if (tracking_level() < NMT_summary) return;
205 VirtualMemoryTracker::add_reserved_region((address)addr, size, stack, flag);
206 }
207 }
208
209 static inline void record_virtual_memory_reserve_and_commit(void* addr, size_t size,
210 const NativeCallStack& stack, MEMFLAGS flag = mtNone) {
211 if (tracking_level() < NMT_summary) return;
212 if (addr != NULL) {
213 ThreadCritical tc;
214 if (tracking_level() < NMT_summary) return;
215 VirtualMemoryTracker::add_reserved_region((address)addr, size,
216 stack, flag, true);
217 }
218 }
219
220 static inline void record_virtual_memory_commit(void* addr, size_t size,
221 const NativeCallStack& stack) {
222 if (tracking_level() < NMT_summary) return;
223 if (addr != NULL) {
224 ThreadCritical tc;
225 if (tracking_level() < NMT_summary) return;
226 VirtualMemoryTracker::add_committed_region((address)addr, size, stack);
227 }
228 }
229
230 static inline Tracker get_virtual_memory_uncommit_tracker() {
231 assert(tracking_level() >= NMT_summary, "Check by caller");
232 return Tracker(Tracker::uncommit);
233 }
234
235 static inline Tracker get_virtual_memory_release_tracker() {
236 assert(tracking_level() >= NMT_summary, "Check by caller");
237 return Tracker(Tracker::release);
238 }
239
240 static inline void record_virtual_memory_type(void* addr, MEMFLAGS flag) {
241 if (tracking_level() < NMT_summary) return;
242 if (addr != NULL) {
243 ThreadCritical tc;
244 if (tracking_level() < NMT_summary) return;
245 VirtualMemoryTracker::set_reserved_region_type((address)addr, flag);
246 }
247 }
248
249 static inline void record_thread_stack(void* addr, size_t size) {
250 if (tracking_level() < NMT_summary) return;
251 if (addr != NULL) {
252 // uses thread stack malloc slot for book keeping number of threads
253 MallocMemorySummary::record_malloc(0, mtThreadStack);
254 record_virtual_memory_reserve_and_commit(addr, size, CALLER_PC, mtThreadStack);
255 }
256 }
257
258 static inline void release_thread_stack(void* addr, size_t size) {
259 if (tracking_level() < NMT_summary) return;
260 if (addr != NULL) {
261 // uses thread stack malloc slot for book keeping number of threads
262 MallocMemorySummary::record_free(0, mtThreadStack);
263 ThreadCritical tc;
264 if (tracking_level() < NMT_summary) return;
265 VirtualMemoryTracker::remove_released_region((address)addr, size);
266 }
267 }
268
269 // Query lock is used to synchronize the access to tracking data.
270 // So far, it is only used by JCmd query, but it may be used by
271 // other tools.
272 static inline Mutex* query_lock() { return _query_lock; }
273
274 // Make a final report or report for hs_err file.
275 static void error_report(outputStream* output) {
276 if (tracking_level() >= NMT_summary) {
277 report(true, output); // just print summary for error case.
278 }
279 }
280
281 static void final_report(outputStream* output) {
282 NMT_TrackingLevel level = tracking_level();
283 if (level >= NMT_summary) {
284 report(level == NMT_summary, output);
285 }
286 }
287
288
289 // Stored baseline
290 static inline MemBaseline& get_baseline() {
291 return _baseline;
292 }
293
294 static NMT_TrackingLevel cmdline_tracking_level() {
295 return _cmdline_tracking_level;
296 }
297
298 static void tuning_statistics(outputStream* out);
299
300 private:
301 static NMT_TrackingLevel init_tracking_level();
302 static void report(bool summary_only, outputStream* output);
303
304 private:
305 // Tracking level
306 static volatile NMT_TrackingLevel _tracking_level;
307 // If NMT option value passed by launcher through environment
308 // variable is valid
309 static bool _is_nmt_env_valid;
310 // command line tracking level
311 static NMT_TrackingLevel _cmdline_tracking_level;
312 // Stored baseline
313 static MemBaseline _baseline;
314 // Query lock
315 static Mutex* _query_lock;
112 }; 316 };
113 317
114 318 #endif // INCLUDE_NMT
115 #else // !INCLUDE_NMT
116
117 #include "memory/allocation.hpp"
118 #include "runtime/globals.hpp"
119 #include "runtime/mutex.hpp"
120 #include "runtime/os.hpp"
121 #include "runtime/thread.hpp"
122 #include "services/memPtr.hpp"
123 #include "services/memRecorder.hpp"
124 #include "services/memSnapshot.hpp"
125 #include "services/memTrackWorker.hpp"
126
127 extern bool NMT_track_callsite;
128
129 #ifndef MAX_UNSIGNED_LONG
130 #define MAX_UNSIGNED_LONG (unsigned long)(-1)
131 #endif
132
133 #ifdef ASSERT
134 #define DEBUG_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0)
135 #else
136 #define DEBUG_CALLER_PC 0
137 #endif
138
139 // The thread closure walks threads to collect per-thread
140 // memory recorders at NMT sync point
141 class SyncThreadRecorderClosure : public ThreadClosure {
142 private:
143 int _thread_count;
144
145 public:
146 SyncThreadRecorderClosure() {
147 _thread_count =0;
148 }
149
150 void do_thread(Thread* thread);
151 int get_thread_count() const {
152 return _thread_count;
153 }
154 };
155
156 class BaselineOutputer;
157 class MemSnapshot;
158 class MemTrackWorker;
159 class Thread;
160 /*
161 * MemTracker is the 'gate' class to native memory tracking runtime.
162 */
163 class MemTracker : AllStatic {
164 friend class GenerationData;
165 friend class MemTrackWorker;
166 friend class MemSnapshot;
167 friend class SyncThreadRecorderClosure;
168
169 // NMT state
170 enum NMTStates {
171 NMT_uninited, // not yet initialized
172 NMT_bootstrapping_single_thread, // bootstrapping, VM is in single thread mode
173 NMT_bootstrapping_multi_thread, // bootstrapping, VM is about to enter multi-thread mode
174 NMT_started, // NMT fully started
175 NMT_shutdown_pending, // shutdown pending
176 NMT_final_shutdown, // in final phase of shutdown
177 NMT_shutdown // shutdown
178 };
179
180 public:
181 class Tracker : public StackObj {
182 friend class MemTracker;
183 public:
184 enum MemoryOperation {
185 NoOp, // no op
186 Malloc, // malloc
187 Realloc, // realloc
188 Free, // free
189 Reserve, // virtual memory reserve
190 Commit, // virtual memory commit
191 ReserveAndCommit, // virtual memory reserve and commit
192 StackAlloc = ReserveAndCommit, // allocate thread stack
193 Type, // assign virtual memory type
194 Uncommit, // virtual memory uncommit
195 Release, // virtual memory release
196 ArenaSize, // set arena size
197 StackRelease // release thread stack
198 };
199
200
201 protected:
202 Tracker(MemoryOperation op, Thread* thr = NULL);
203
204 public:
205 void discard();
206
207 void record(address addr, size_t size = 0, MEMFLAGS flags = mtNone, address pc = NULL);
208 void record(address old_addr, address new_addr, size_t size,
209 MEMFLAGS flags, address pc = NULL);
210
211 private:
212 bool _need_thread_critical_lock;
213 JavaThread* _java_thread;
214 MemoryOperation _op; // memory operation
215 jint _seq; // reserved sequence number
216 };
217
218
219 public:
220 // native memory tracking level
221 enum NMTLevel {
222 NMT_off, // native memory tracking is off
223 NMT_summary, // don't track callsite
224 NMT_detail // track callsite also
225 };
226
227 enum ShutdownReason {
228 NMT_shutdown_none, // no shutdown requested
229 NMT_shutdown_user, // user requested shutdown
230 NMT_normal, // normal shutdown, process exit
231 NMT_out_of_memory, // shutdown due to out of memory
232 NMT_initialization, // shutdown due to initialization failure
233 NMT_use_malloc_only, // can not combine NMT with UseMallocOnly flag
234 NMT_error_reporting, // shutdown by vmError::report_and_die()
235 NMT_out_of_generation, // running out of generation queue
236 NMT_sequence_overflow // overflow the sequence number
237 };
238
239 public:
240 // initialize NMT tracking level from command line options, called
241 // from VM command line parsing code
242 static void init_tracking_options(const char* option_line);
243
244 // if NMT is enabled to record memory activities
245 static inline bool is_on() {
246 return (_tracking_level >= NMT_summary &&
247 _state >= NMT_bootstrapping_single_thread);
248 }
249
250 static inline enum NMTLevel tracking_level() {
251 return _tracking_level;
252 }
253
254 // user readable reason for shutting down NMT
255 static const char* reason() {
256 switch(_reason) {
257 case NMT_shutdown_none:
258 return "Native memory tracking is not enabled";
259 case NMT_shutdown_user:
260 return "Native memory tracking has been shutdown by user";
261 case NMT_normal:
262 return "Native memory tracking has been shutdown due to process exiting";
263 case NMT_out_of_memory:
264 return "Native memory tracking has been shutdown due to out of native memory";
265 case NMT_initialization:
266 return "Native memory tracking failed to initialize";
267 case NMT_error_reporting:
268 return "Native memory tracking has been shutdown due to error reporting";
269 case NMT_out_of_generation:
270 return "Native memory tracking has been shutdown due to running out of generation buffer";
271 case NMT_sequence_overflow:
272 return "Native memory tracking has been shutdown due to overflow the sequence number";
273 case NMT_use_malloc_only:
274 return "Native memory tracking is not supported when UseMallocOnly is on";
275 default:
276 ShouldNotReachHere();
277 return NULL;
278 }
279 }
280
281 // test if we can walk native stack
282 static bool can_walk_stack() {
283 // native stack is not walkable during bootstrapping on sparc
284 #if defined(SPARC)
285 return (_state == NMT_started);
286 #else
287 return (_state >= NMT_bootstrapping_single_thread && _state <= NMT_started);
288 #endif
289 }
290
291 // if native memory tracking tracks callsite
292 static inline bool track_callsite() { return _tracking_level == NMT_detail; }
293
294 // NMT automatically shuts itself down under extreme situation by default.
295 // When the value is set to false, NMT will try its best to stay alive,
296 // even it has to slow down VM.
297 static inline void set_autoShutdown(bool value) {
298 AutoShutdownNMT = value;
299 if (AutoShutdownNMT && _slowdown_calling_thread) {
300 _slowdown_calling_thread = false;
301 }
302 }
303
304 // shutdown native memory tracking capability. Native memory tracking
305 // can be shutdown by VM when it encounters low memory scenarios.
306 // Memory tracker should gracefully shutdown itself, and preserve the
307 // latest memory statistics for post morten diagnosis.
308 static void shutdown(ShutdownReason reason);
309
310 // if there is shutdown requested
311 static inline bool shutdown_in_progress() {
312 return (_state >= NMT_shutdown_pending);
313 }
314
315 // bootstrap native memory tracking, so it can start to collect raw data
316 // before worker thread can start
317
318 // the first phase of bootstrapping, when VM still in single-threaded mode
319 static void bootstrap_single_thread();
320 // the second phase of bootstrapping, VM is about or already in multi-threaded mode
321 static void bootstrap_multi_thread();
322
323
324 // start() has to be called when VM still in single thread mode, but after
325 // command line option parsing is done.
326 static void start();
327
328 // record a 'malloc' call
329 static inline void record_malloc(address addr, size_t size, MEMFLAGS flags,
330 address pc = 0, Thread* thread = NULL) {
331 Tracker tkr(Tracker::Malloc, thread);
332 tkr.record(addr, size, flags, pc);
333 }
334 // record a 'free' call
335 static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) {
336 Tracker tkr(Tracker::Free, thread);
337 tkr.record(addr, 0, flags, DEBUG_CALLER_PC);
338 }
339
340 static inline void record_arena_size(address addr, size_t size) {
341 Tracker tkr(Tracker::ArenaSize);
342 tkr.record(addr, size);
343 }
344
345 // record a virtual memory 'reserve' call
346 static inline void record_virtual_memory_reserve(address addr, size_t size,
347 MEMFLAGS flags, address pc = 0, Thread* thread = NULL) {
348 assert(size > 0, "Sanity check");
349 Tracker tkr(Tracker::Reserve, thread);
350 tkr.record(addr, size, flags, pc);
351 }
352
353 static inline void record_thread_stack(address addr, size_t size, Thread* thr,
354 address pc = 0) {
355 Tracker tkr(Tracker::StackAlloc, thr);
356 tkr.record(addr, size, mtThreadStack, pc);
357 }
358
359 static inline void release_thread_stack(address addr, size_t size, Thread* thr) {
360 Tracker tkr(Tracker::StackRelease, thr);
361 tkr.record(addr, size, mtThreadStack, DEBUG_CALLER_PC);
362 }
363
364 // record a virtual memory 'commit' call
365 static inline void record_virtual_memory_commit(address addr, size_t size,
366 address pc, Thread* thread = NULL) {
367 Tracker tkr(Tracker::Commit, thread);
368 tkr.record(addr, size, mtNone, pc);
369 }
370
371 static inline void record_virtual_memory_reserve_and_commit(address addr, size_t size,
372 MEMFLAGS flags, address pc, Thread* thread = NULL) {
373 Tracker tkr(Tracker::ReserveAndCommit, thread);
374 tkr.record(addr, size, flags, pc);
375 }
376
377 static inline void record_virtual_memory_release(address addr, size_t size,
378 Thread* thread = NULL) {
379 if (is_on()) {
380 Tracker tkr(Tracker::Release, thread);
381 tkr.record(addr, size);
382 }
383 }
384
385 // record memory type on virtual memory base address
386 static inline void record_virtual_memory_type(address base, MEMFLAGS flags,
387 Thread* thread = NULL) {
388 Tracker tkr(Tracker::Type);
389 tkr.record(base, 0, flags);
390 }
391
392 // Get memory trackers for memory operations that can result race conditions.
393 // The memory tracker has to be obtained before realloc, virtual memory uncommit
394 // and virtual memory release, and call tracker.record() method if operation
395 // succeeded, or tracker.discard() to abort the tracking.
396 static inline Tracker get_realloc_tracker() {
397 return Tracker(Tracker::Realloc);
398 }
399
400 static inline Tracker get_virtual_memory_uncommit_tracker() {
401 return Tracker(Tracker::Uncommit);
402 }
403
404 static inline Tracker get_virtual_memory_release_tracker() {
405 return Tracker(Tracker::Release);
406 }
407
408
409 // create memory baseline of current memory snapshot
410 static bool baseline();
411 // is there a memory baseline
412 static bool has_baseline() {
413 return _baseline.baselined();
414 }
415
416 // print memory usage from current snapshot
417 static bool print_memory_usage(BaselineOutputer& out, size_t unit,
418 bool summary_only = true);
419 // compare memory usage between current snapshot and baseline
420 static bool compare_memory_usage(BaselineOutputer& out, size_t unit,
421 bool summary_only = true);
422
423 // the version for whitebox testing support, it ensures that all memory
424 // activities before this method call, are reflected in the snapshot
425 // database.
426 static bool wbtest_wait_for_data_merge();
427
428 // sync is called within global safepoint to synchronize nmt data
429 static void sync();
430
431 // called when a thread is about to exit
432 static void thread_exiting(JavaThread* thread);
433
434 // retrieve global snapshot
435 static MemSnapshot* get_snapshot() {
436 if (shutdown_in_progress()) {
437 return NULL;
438 }
439 return _snapshot;
440 }
441
442 // print tracker stats
443 NOT_PRODUCT(static void print_tracker_stats(outputStream* st);)
444 NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);)
445
446 private:
447 // start native memory tracking worker thread
448 static bool start_worker(MemSnapshot* snapshot);
449
450 // called by worker thread to complete shutdown process
451 static void final_shutdown();
452
453 protected:
454 // retrieve per-thread recorder of the specified thread.
455 // if the recorder is full, it will be enqueued to overflow
456 // queue, a new recorder is acquired from recorder pool or a
457 // new instance is created.
458 // when thread == NULL, it means global recorder
459 static MemRecorder* get_thread_recorder(JavaThread* thread);
460
461 // per-thread recorder pool
462 static void release_thread_recorder(MemRecorder* rec);
463 static void delete_all_pooled_recorders();
464
465 // pending recorder queue. Recorders are queued to pending queue
466 // when they are overflowed or collected at nmt sync point.
467 static void enqueue_pending_recorder(MemRecorder* rec);
468 static MemRecorder* get_pending_recorders();
469 static void delete_all_pending_recorders();
470
471 // write a memory tracking record in recorder
472 static void write_tracking_record(address addr, MEMFLAGS type,
473 size_t size, jint seq, address pc, JavaThread* thread);
474
475 static bool is_single_threaded_bootstrap() {
476 return _state == NMT_bootstrapping_single_thread;
477 }
478
479 static void check_NMT_load(Thread* thr) {
480 assert(thr != NULL, "Sanity check");
481 if (_slowdown_calling_thread && thr != _worker_thread) {
482 #ifdef _WINDOWS
483 // On Windows, os::NakedYield() does not work as well
484 // as os::yield_all()
485 os::yield_all();
486 #else
487 // On Solaris, os::yield_all() depends on os::sleep()
488 // which requires JavaTherad in _thread_in_vm state.
489 // Transits thread to _thread_in_vm state can be dangerous
490 // if caller holds lock, as it may deadlock with Threads_lock.
491 // So use NaKedYield instead.
492 //
493 // Linux and BSD, NakedYield() and yield_all() implementations
494 // are the same.
495 os::NakedYield();
496 #endif
497 }
498 }
499
500 static void inc_pending_op_count() {
501 Atomic::inc(&_pending_op_count);
502 }
503
504 static void dec_pending_op_count() {
505 Atomic::dec(&_pending_op_count);
506 assert(_pending_op_count >= 0, "Sanity check");
507 }
508
509
510 private:
511 // retrieve a pooled memory record or create new one if there is not
512 // one available
513 static MemRecorder* get_new_or_pooled_instance();
514 static void create_memory_record(address addr, MEMFLAGS type,
515 size_t size, address pc, Thread* thread);
516 static void create_record_in_recorder(address addr, MEMFLAGS type,
517 size_t size, address pc, JavaThread* thread);
518
519 static void set_current_processing_generation(unsigned long generation) {
520 _worker_thread_idle = false;
521 _processing_generation = generation;
522 }
523
524 static void report_worker_idle() {
525 _worker_thread_idle = true;
526 }
527
528 private:
529 // global memory snapshot
530 static MemSnapshot* _snapshot;
531
532 // a memory baseline of snapshot
533 static MemBaseline _baseline;
534
535 // query lock
536 static Mutex* _query_lock;
537
538 // a thread can start to allocate memory before it is attached
539 // to VM 'Thread', those memory activities are recorded here.
540 // ThreadCritical is required to guard this global recorder.
541 static MemRecorder* volatile _global_recorder;
542
543 // main thread id
544 debug_only(static intx _main_thread_tid;)
545
546 // pending recorders to be merged
547 static MemRecorder* volatile _merge_pending_queue;
548
549 NOT_PRODUCT(static volatile jint _pending_recorder_count;)
550
551 // pooled memory recorders
552 static MemRecorder* volatile _pooled_recorders;
553
554 // memory recorder pool management, uses following
555 // counter to determine if a released memory recorder
556 // should be pooled
557
558 // latest thread count
559 static int _thread_count;
560 // pooled recorder count
561 static volatile jint _pooled_recorder_count;
562
563
564 // worker thread to merge pending recorders into snapshot
565 static MemTrackWorker* _worker_thread;
566
567 // how many safepoints we skipped without entering sync point
568 static int _sync_point_skip_count;
569
570 // if the tracker is properly intialized
571 static bool _is_tracker_ready;
572 // tracking level (off, summary and detail)
573 static enum NMTLevel _tracking_level;
574
575 // current nmt state
576 static volatile enum NMTStates _state;
577 // the reason for shutting down nmt
578 static enum ShutdownReason _reason;
579 // the generation that NMT is processing
580 static volatile unsigned long _processing_generation;
581 // although NMT is still procesing current generation, but
582 // there is not more recorder to process, set idle state
583 static volatile bool _worker_thread_idle;
584
585 // if NMT should slow down calling thread to allow
586 // worker thread to catch up
587 static volatile bool _slowdown_calling_thread;
588
589 // pending memory op count.
590 // Certain memory ops need to pre-reserve sequence number
591 // before memory operation can happen to avoid race condition.
592 // See MemTracker::Tracker for detail
593 static volatile jint _pending_op_count;
594 };
595
596 #endif // !INCLUDE_NMT
597 319
598 #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP 320 #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP
321