Mercurial > hg > truffle
annotate src/share/vm/services/memTracker.hpp @ 6741:33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
Summary: Fixed virtual memory records merge and promotion logic, should be based on sequence number vs. base address order
Reviewed-by: coleenp, acorn
author | zgu |
---|---|
date | Tue, 11 Sep 2012 20:53:17 -0400 |
parents | 4acebbe310e1 |
children | 716e6ef4482a |
rev | line source |
---|---|
6197 | 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 | |
6741
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
42 #ifdef _DEBUG |
6197 | 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"; | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
129 case NMT_out_of_memory: |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
130 return "Native memory tracking has been shutdown due to out of native memory"; |
6197 | 131 case NMT_initialization: |
132 return "Native memory tracking failed to initialize"; | |
133 case NMT_error_reporting: | |
134 return "Native memory tracking has been shutdown due to error reporting"; | |
135 case NMT_out_of_generation: | |
136 return "Native memory tracking has been shutdown due to running out of generation buffer"; | |
137 case NMT_sequence_overflow: | |
138 return "Native memory tracking has been shutdown due to overflow the sequence number"; | |
139 case NMT_use_malloc_only: | |
140 return "Native memory tracking is not supported when UseMallocOnly is on"; | |
141 default: | |
142 ShouldNotReachHere(); | |
143 return NULL; | |
144 } | |
145 } | |
146 | |
147 // test if we can walk native stack | |
148 static bool can_walk_stack() { | |
149 // native stack is not walkable during bootstrapping on sparc | |
150 #if defined(SPARC) | |
151 return (_state == NMT_started); | |
152 #else | |
153 return (_state >= NMT_bootstrapping_single_thread && _state <= NMT_started); | |
154 #endif | |
155 } | |
156 | |
157 // if native memory tracking tracks callsite | |
158 static inline bool track_callsite() { return _tracking_level == NMT_detail; } | |
159 | |
160 // shutdown native memory tracking capability. Native memory tracking | |
161 // can be shutdown by VM when it encounters low memory scenarios. | |
162 // Memory tracker should gracefully shutdown itself, and preserve the | |
163 // latest memory statistics for post morten diagnosis. | |
164 static void shutdown(ShutdownReason reason); | |
165 | |
166 // if there is shutdown requested | |
167 static inline bool shutdown_in_progress() { | |
168 return (_state >= NMT_shutdown_pending); | |
169 } | |
170 | |
171 // bootstrap native memory tracking, so it can start to collect raw data | |
172 // before worker thread can start | |
173 | |
174 // the first phase of bootstrapping, when VM still in single-threaded mode | |
175 static void bootstrap_single_thread(); | |
176 // the second phase of bootstrapping, VM is about or already in multi-threaded mode | |
177 static void bootstrap_multi_thread(); | |
178 | |
179 | |
180 // start() has to be called when VM still in single thread mode, but after | |
181 // command line option parsing is done. | |
182 static void start(); | |
183 | |
184 // record a 'malloc' call | |
185 static inline void record_malloc(address addr, size_t size, MEMFLAGS flags, | |
186 address pc = 0, Thread* thread = NULL) { | |
187 if (NMT_CAN_TRACK(flags)) { | |
188 create_memory_record(addr, (flags|MemPointerRecord::malloc_tag()), size, pc, thread); | |
189 } | |
190 } | |
191 // record a 'free' call | |
192 static inline void record_free(address addr, MEMFLAGS flags, Thread* thread = NULL) { | |
193 if (is_on() && NMT_CAN_TRACK(flags)) { | |
194 create_memory_record(addr, MemPointerRecord::free_tag(), 0, 0, thread); | |
195 } | |
196 } | |
197 // record a 'realloc' call | |
198 static inline void record_realloc(address old_addr, address new_addr, size_t size, | |
199 MEMFLAGS flags, address pc = 0, Thread* thread = NULL) { | |
200 if (is_on()) { | |
201 record_free(old_addr, flags, thread); | |
202 record_malloc(new_addr, size, flags, pc, thread); | |
203 } | |
204 } | |
205 | |
206 // record arena size | |
207 static inline void record_arena_size(address addr, size_t size) { | |
208 // we add a positive offset to arena address, so we can have arena size record | |
209 // sorted after arena record | |
210 if (is_on() && !UseMallocOnly) { | |
211 create_memory_record((addr + sizeof(void*)), MemPointerRecord::arena_size_tag(), size, | |
212 0, NULL); | |
213 } | |
214 } | |
215 | |
216 // record a virtual memory 'reserve' call | |
217 static inline void record_virtual_memory_reserve(address addr, size_t size, | |
218 address pc = 0, Thread* thread = NULL) { | |
219 if (is_on()) { | |
220 assert(size > 0, "reserve szero size"); | |
221 create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag(), | |
222 size, pc, thread); | |
223 } | |
224 } | |
225 | |
6741
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
226 static inline void record_thread_stack(address addr, size_t size, Thread* thr, |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
227 address pc = 0) { |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
228 if (is_on()) { |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
229 assert(size > 0 && thr != NULL, "Sanity check"); |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
230 create_memory_record(addr, MemPointerRecord::virtual_memory_reserve_tag() | mtThreadStack, |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
231 size, pc, thr); |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
232 create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag() | mtThreadStack, |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
233 size, pc, thr); |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
234 } |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
235 } |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
236 |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
237 static inline void release_thread_stack(address addr, size_t size, Thread* thr) { |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
238 if (is_on()) { |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
239 assert(size > 0 && thr != NULL, "Sanity check"); |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
240 create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag() | mtThreadStack, |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
241 size, DEBUG_CALLER_PC, thr); |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
242 create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag() | mtThreadStack, |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
243 size, DEBUG_CALLER_PC, thr); |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
244 } |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
245 } |
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
246 |
6197 | 247 // record a virtual memory 'commit' call |
248 static inline void record_virtual_memory_commit(address addr, size_t size, | |
249 address pc = 0, Thread* thread = NULL) { | |
250 if (is_on()) { | |
251 create_memory_record(addr, MemPointerRecord::virtual_memory_commit_tag(), | |
6741
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
252 size, DEBUG_CALLER_PC, thread); |
6197 | 253 } |
254 } | |
255 | |
256 // record a virtual memory 'uncommit' call | |
257 static inline void record_virtual_memory_uncommit(address addr, size_t size, | |
258 Thread* thread = NULL) { | |
259 if (is_on()) { | |
260 create_memory_record(addr, MemPointerRecord::virtual_memory_uncommit_tag(), | |
6741
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
261 size, DEBUG_CALLER_PC, thread); |
6197 | 262 } |
263 } | |
264 | |
265 // record a virtual memory 'release' call | |
266 static inline void record_virtual_memory_release(address addr, size_t size, | |
267 Thread* thread = NULL) { | |
268 if (is_on()) { | |
269 create_memory_record(addr, MemPointerRecord::virtual_memory_release_tag(), | |
6741
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
270 size, DEBUG_CALLER_PC, thread); |
6197 | 271 } |
272 } | |
273 | |
274 // record memory type on virtual memory base address | |
275 static inline void record_virtual_memory_type(address base, MEMFLAGS flags, | |
276 Thread* thread = NULL) { | |
277 if (is_on()) { | |
278 assert(base > 0, "wrong base address"); | |
279 assert((flags & (~mt_masks)) == 0, "memory type only"); | |
280 create_memory_record(base, (flags | MemPointerRecord::virtual_memory_type_tag()), | |
6741
33143ee07800
7181995: NMT ON: NMT assertion failure assert(cur_vm->is_uncommit_record() || cur_vm->is_deallocation_record
zgu
parents:
6599
diff
changeset
|
281 0, DEBUG_CALLER_PC, thread); |
6197 | 282 } |
283 } | |
284 | |
285 | |
286 // create memory baseline of current memory snapshot | |
287 static bool baseline(); | |
288 // is there a memory baseline | |
289 static bool has_baseline() { | |
290 return _baseline.baselined(); | |
291 } | |
292 | |
293 // print memory usage from current snapshot | |
294 static bool print_memory_usage(BaselineOutputer& out, size_t unit, | |
295 bool summary_only = true); | |
296 // compare memory usage between current snapshot and baseline | |
297 static bool compare_memory_usage(BaselineOutputer& out, size_t unit, | |
298 bool summary_only = true); | |
299 | |
300 // sync is called within global safepoint to synchronize nmt data | |
301 static void sync(); | |
302 | |
303 // called when a thread is about to exit | |
304 static void thread_exiting(JavaThread* thread); | |
305 | |
306 // retrieve global snapshot | |
307 static MemSnapshot* get_snapshot() { | |
308 if (shutdown_in_progress()) { | |
309 return NULL; | |
310 } | |
311 return _snapshot; | |
312 } | |
313 | |
314 // print tracker stats | |
315 NOT_PRODUCT(static void print_tracker_stats(outputStream* st);) | |
316 NOT_PRODUCT(static void walk_stack(int toSkip, char* buf, int len);) | |
317 | |
318 private: | |
319 // start native memory tracking worker thread | |
320 static bool start_worker(); | |
321 | |
322 // called by worker thread to complete shutdown process | |
323 static void final_shutdown(); | |
324 | |
325 protected: | |
326 // retrieve per-thread recorder of the specified thread. | |
327 // if the recorder is full, it will be enqueued to overflow | |
328 // queue, a new recorder is acquired from recorder pool or a | |
329 // new instance is created. | |
330 // when thread == NULL, it means global recorder | |
331 static MemRecorder* get_thread_recorder(JavaThread* thread); | |
332 | |
333 // per-thread recorder pool | |
334 static void release_thread_recorder(MemRecorder* rec); | |
335 static void delete_all_pooled_recorders(); | |
336 | |
337 // pending recorder queue. Recorders are queued to pending queue | |
338 // when they are overflowed or collected at nmt sync point. | |
339 static void enqueue_pending_recorder(MemRecorder* rec); | |
340 static MemRecorder* get_pending_recorders(); | |
341 static void delete_all_pending_recorders(); | |
342 | |
343 private: | |
344 // retrieve a pooled memory record or create new one if there is not | |
345 // one available | |
346 static MemRecorder* get_new_or_pooled_instance(); | |
347 static void create_memory_record(address addr, MEMFLAGS type, | |
348 size_t size, address pc, Thread* thread); | |
349 static void create_record_in_recorder(address addr, MEMFLAGS type, | |
6231 | 350 size_t size, address pc, JavaThread* thread); |
6197 | 351 |
352 private: | |
353 // global memory snapshot | |
354 static MemSnapshot* _snapshot; | |
355 | |
356 // a memory baseline of snapshot | |
357 static MemBaseline _baseline; | |
358 | |
359 // query lock | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
360 static Mutex* _query_lock; |
6197 | 361 |
362 // a thread can start to allocate memory before it is attached | |
363 // to VM 'Thread', those memory activities are recorded here. | |
364 // ThreadCritical is required to guard this global recorder. | |
365 static MemRecorder* _global_recorder; | |
366 | |
367 // main thread id | |
368 debug_only(static intx _main_thread_tid;) | |
369 | |
370 // pending recorders to be merged | |
371 static volatile MemRecorder* _merge_pending_queue; | |
372 | |
373 NOT_PRODUCT(static volatile jint _pending_recorder_count;) | |
374 | |
375 // pooled memory recorders | |
376 static volatile MemRecorder* _pooled_recorders; | |
377 | |
378 // memory recorder pool management, uses following | |
379 // counter to determine if a released memory recorder | |
380 // should be pooled | |
381 | |
382 // latest thread count | |
383 static int _thread_count; | |
384 // pooled recorder count | |
385 static volatile jint _pooled_recorder_count; | |
386 | |
387 | |
388 // worker thread to merge pending recorders into snapshot | |
389 static MemTrackWorker* _worker_thread; | |
390 | |
391 // how many safepoints we skipped without entering sync point | |
392 static int _sync_point_skip_count; | |
393 | |
394 // if the tracker is properly intialized | |
395 static bool _is_tracker_ready; | |
396 // tracking level (off, summary and detail) | |
397 static enum NMTLevel _tracking_level; | |
398 | |
399 // current nmt state | |
400 static volatile enum NMTStates _state; | |
401 // the reason for shutting down nmt | |
402 static enum ShutdownReason _reason; | |
403 }; | |
404 | |
405 #endif // SHARE_VM_SERVICES_MEM_TRACKER_HPP |