Mercurial > hg > truffle
annotate src/share/vm/services/memTracker.cpp @ 9093:cd22923e7ca7
removed unnecessary code
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Fri, 12 Apr 2013 21:35:27 +0200 |
parents | 4c8bb5e4f68f |
children | 35f8765422b9 |
rev | line source |
---|---|
6197 | 1 /* |
9061
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
6197 | 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 #include "precompiled.hpp" | |
25 | |
7464
ecd24264898b
8005048: NMT: #loaded classes needs to just show the # defined classes
zgu
parents:
7172
diff
changeset
|
26 #include "oops/instanceKlass.hpp" |
6197 | 27 #include "runtime/atomic.hpp" |
28 #include "runtime/interfaceSupport.hpp" | |
29 #include "runtime/mutexLocker.hpp" | |
30 #include "runtime/safepoint.hpp" | |
31 #include "runtime/threadCritical.hpp" | |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
32 #include "runtime/vm_operations.hpp" |
6197 | 33 #include "services/memPtr.hpp" |
34 #include "services/memReporter.hpp" | |
35 #include "services/memTracker.hpp" | |
36 #include "utilities/decoder.hpp" | |
37 #include "utilities/globalDefinitions.hpp" | |
38 | |
39 bool NMT_track_callsite = false; | |
40 | |
41 // walk all 'known' threads at NMT sync point, and collect their recorders | |
42 void SyncThreadRecorderClosure::do_thread(Thread* thread) { | |
43 assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required"); | |
44 if (thread->is_Java_thread()) { | |
45 JavaThread* javaThread = (JavaThread*)thread; | |
46 MemRecorder* recorder = javaThread->get_recorder(); | |
47 if (recorder != NULL) { | |
48 MemTracker::enqueue_pending_recorder(recorder); | |
49 javaThread->set_recorder(NULL); | |
50 } | |
51 } | |
52 _thread_count ++; | |
53 } | |
54 | |
55 | |
56 MemRecorder* MemTracker::_global_recorder = NULL; | |
57 MemSnapshot* MemTracker::_snapshot = NULL; | |
58 MemBaseline MemTracker::_baseline; | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
59 Mutex* MemTracker::_query_lock = NULL; |
6197 | 60 volatile MemRecorder* MemTracker::_merge_pending_queue = NULL; |
61 volatile MemRecorder* MemTracker::_pooled_recorders = NULL; | |
62 MemTrackWorker* MemTracker::_worker_thread = NULL; | |
63 int MemTracker::_sync_point_skip_count = 0; | |
64 MemTracker::NMTLevel MemTracker::_tracking_level = MemTracker::NMT_off; | |
65 volatile MemTracker::NMTStates MemTracker::_state = NMT_uninited; | |
66 MemTracker::ShutdownReason MemTracker::_reason = NMT_shutdown_none; | |
67 int MemTracker::_thread_count = 255; | |
68 volatile jint MemTracker::_pooled_recorder_count = 0; | |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
69 volatile unsigned long MemTracker::_processing_generation = 0; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
70 volatile bool MemTracker::_worker_thread_idle = false; |
8810
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
71 volatile bool MemTracker::_slowdown_calling_thread = false; |
6197 | 72 debug_only(intx MemTracker::_main_thread_tid = 0;) |
6607
e5bf1c79ed5b
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
6235
diff
changeset
|
73 NOT_PRODUCT(volatile jint MemTracker::_pending_recorder_count = 0;) |
6197 | 74 |
75 void MemTracker::init_tracking_options(const char* option_line) { | |
76 _tracking_level = NMT_off; | |
7172
bbc14465e7db
8003689: MemTracker::init_tracking_options() reads outside array if commandline argument is empty
zgu
parents:
6882
diff
changeset
|
77 if (strcmp(option_line, "=summary") == 0) { |
6197 | 78 _tracking_level = NMT_summary; |
7172
bbc14465e7db
8003689: MemTracker::init_tracking_options() reads outside array if commandline argument is empty
zgu
parents:
6882
diff
changeset
|
79 } else if (strcmp(option_line, "=detail") == 0) { |
6197 | 80 _tracking_level = NMT_detail; |
7172
bbc14465e7db
8003689: MemTracker::init_tracking_options() reads outside array if commandline argument is empty
zgu
parents:
6882
diff
changeset
|
81 } else if (strcmp(option_line, "=off") != 0) { |
bbc14465e7db
8003689: MemTracker::init_tracking_options() reads outside array if commandline argument is empty
zgu
parents:
6882
diff
changeset
|
82 vm_exit_during_initialization("Syntax error, expecting -XX:NativeMemoryTracking=[off|summary|detail]", NULL); |
6197 | 83 } |
84 } | |
85 | |
86 // first phase of bootstrapping, when VM is still in single-threaded mode. | |
87 void MemTracker::bootstrap_single_thread() { | |
88 if (_tracking_level > NMT_off) { | |
89 assert(_state == NMT_uninited, "wrong state"); | |
90 | |
91 // NMT is not supported with UseMallocOnly is on. NMT can NOT | |
92 // handle the amount of malloc data without significantly impacting | |
93 // runtime performance when this flag is on. | |
94 if (UseMallocOnly) { | |
95 shutdown(NMT_use_malloc_only); | |
96 return; | |
97 } | |
98 | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
99 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock"); |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
100 if (_query_lock == NULL) { |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
101 shutdown(NMT_out_of_memory); |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
102 return; |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
103 } |
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
104 |
6197 | 105 debug_only(_main_thread_tid = os::current_thread_id();) |
106 _state = NMT_bootstrapping_single_thread; | |
107 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); | |
108 } | |
109 } | |
110 | |
111 // second phase of bootstrapping, when VM is about to or already entered multi-theaded mode. | |
112 void MemTracker::bootstrap_multi_thread() { | |
113 if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) { | |
114 // create nmt lock for multi-thread execution | |
115 assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); | |
116 _state = NMT_bootstrapping_multi_thread; | |
117 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); | |
118 } | |
119 } | |
120 | |
121 // fully start nmt | |
122 void MemTracker::start() { | |
123 // Native memory tracking is off from command line option | |
124 if (_tracking_level == NMT_off || shutdown_in_progress()) return; | |
125 | |
126 assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); | |
127 assert(_state == NMT_bootstrapping_multi_thread, "wrong state"); | |
128 | |
129 _snapshot = new (std::nothrow)MemSnapshot(); | |
9061
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
130 if (_snapshot != NULL) { |
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
131 if (!_snapshot->out_of_memory() && start_worker()) { |
6197 | 132 _state = NMT_started; |
133 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); | |
134 return; | |
135 } | |
9061
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
136 |
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
137 delete _snapshot; |
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
138 _snapshot = NULL; |
6197 | 139 } |
140 | |
141 // fail to start native memory tracking, shut it down | |
142 shutdown(NMT_initialization); | |
143 } | |
144 | |
145 /** | |
146 * Shutting down native memory tracking. | |
147 * We can not shutdown native memory tracking immediately, so we just | |
148 * setup shutdown pending flag, every native memory tracking component | |
149 * should orderly shut itself down. | |
150 * | |
151 * The shutdown sequences: | |
152 * 1. MemTracker::shutdown() sets MemTracker to shutdown pending state | |
153 * 2. Worker thread calls MemTracker::final_shutdown(), which transites | |
154 * MemTracker to final shutdown state. | |
155 * 3. At sync point, MemTracker does final cleanup, before sets memory | |
156 * tracking level to off to complete shutdown. | |
157 */ | |
158 void MemTracker::shutdown(ShutdownReason reason) { | |
159 if (_tracking_level == NMT_off) return; | |
160 | |
161 if (_state <= NMT_bootstrapping_single_thread) { | |
162 // we still in single thread mode, there is not contention | |
163 _state = NMT_shutdown_pending; | |
164 _reason = reason; | |
165 } else { | |
166 // we want to know who initialized shutdown | |
167 if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending, | |
168 (jint*)&_state, (jint)NMT_started)) { | |
169 _reason = reason; | |
170 } | |
171 } | |
172 } | |
173 | |
174 // final phase of shutdown | |
175 void MemTracker::final_shutdown() { | |
176 // delete all pending recorders and pooled recorders | |
177 delete_all_pending_recorders(); | |
178 delete_all_pooled_recorders(); | |
179 | |
180 { | |
181 // shared baseline and snapshot are the only objects needed to | |
182 // create query results | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
183 MutexLockerEx locker(_query_lock, true); |
6197 | 184 // cleanup baseline data and snapshot |
185 _baseline.clear(); | |
186 delete _snapshot; | |
187 _snapshot = NULL; | |
188 } | |
189 | |
190 // shutdown shared decoder instance, since it is only | |
191 // used by native memory tracking so far. | |
192 Decoder::shutdown(); | |
193 | |
194 MemTrackWorker* worker = NULL; | |
195 { | |
196 ThreadCritical tc; | |
197 // can not delete worker inside the thread critical | |
198 if (_worker_thread != NULL && Thread::current() == _worker_thread) { | |
199 worker = _worker_thread; | |
200 _worker_thread = NULL; | |
201 } | |
202 } | |
203 if (worker != NULL) { | |
204 delete worker; | |
205 } | |
206 _state = NMT_final_shutdown; | |
207 } | |
208 | |
209 // delete all pooled recorders | |
210 void MemTracker::delete_all_pooled_recorders() { | |
211 // free all pooled recorders | |
212 volatile MemRecorder* cur_head = _pooled_recorders; | |
213 if (cur_head != NULL) { | |
214 MemRecorder* null_ptr = NULL; | |
215 while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, | |
216 (void*)&_pooled_recorders, (void*)cur_head)) { | |
217 cur_head = _pooled_recorders; | |
218 } | |
219 if (cur_head != NULL) { | |
220 delete cur_head; | |
221 _pooled_recorder_count = 0; | |
222 } | |
223 } | |
224 } | |
225 | |
226 // delete all recorders in pending queue | |
227 void MemTracker::delete_all_pending_recorders() { | |
228 // free all pending recorders | |
229 MemRecorder* pending_head = get_pending_recorders(); | |
230 if (pending_head != NULL) { | |
231 delete pending_head; | |
232 } | |
233 } | |
234 | |
235 /* | |
236 * retrieve per-thread recorder of specified thread. | |
237 * if thread == NULL, it means global recorder | |
238 */ | |
239 MemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) { | |
240 if (shutdown_in_progress()) return NULL; | |
241 | |
242 MemRecorder* rc; | |
243 if (thread == NULL) { | |
244 rc = _global_recorder; | |
245 } else { | |
246 rc = thread->get_recorder(); | |
247 } | |
248 | |
249 if (rc != NULL && rc->is_full()) { | |
250 enqueue_pending_recorder(rc); | |
251 rc = NULL; | |
252 } | |
253 | |
254 if (rc == NULL) { | |
255 rc = get_new_or_pooled_instance(); | |
256 if (thread == NULL) { | |
257 _global_recorder = rc; | |
258 } else { | |
259 thread->set_recorder(rc); | |
260 } | |
261 } | |
262 return rc; | |
263 } | |
264 | |
265 /* | |
266 * get a per-thread recorder from pool, or create a new one if | |
267 * there is not one available. | |
268 */ | |
269 MemRecorder* MemTracker::get_new_or_pooled_instance() { | |
270 MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders); | |
271 if (cur_head == NULL) { | |
272 MemRecorder* rec = new (std::nothrow)MemRecorder(); | |
273 if (rec == NULL || rec->out_of_memory()) { | |
274 shutdown(NMT_out_of_memory); | |
275 if (rec != NULL) { | |
276 delete rec; | |
277 rec = NULL; | |
278 } | |
279 } | |
280 return rec; | |
281 } else { | |
282 MemRecorder* next_head = cur_head->next(); | |
283 if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders, | |
284 (void*)cur_head)) { | |
285 return get_new_or_pooled_instance(); | |
286 } | |
287 cur_head->set_next(NULL); | |
288 Atomic::dec(&_pooled_recorder_count); | |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
289 cur_head->set_generation(); |
6197 | 290 return cur_head; |
291 } | |
292 } | |
293 | |
294 /* | |
295 * retrieve all recorders in pending queue, and empty the queue | |
296 */ | |
297 MemRecorder* MemTracker::get_pending_recorders() { | |
298 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue); | |
299 MemRecorder* null_ptr = NULL; | |
300 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue, | |
301 (void*)cur_head)) { | |
302 cur_head = const_cast<MemRecorder*>(_merge_pending_queue); | |
303 } | |
6607
e5bf1c79ed5b
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
6235
diff
changeset
|
304 NOT_PRODUCT(Atomic::store(0, &_pending_recorder_count)); |
6197 | 305 return cur_head; |
306 } | |
307 | |
308 /* | |
309 * release a recorder to recorder pool. | |
310 */ | |
311 void MemTracker::release_thread_recorder(MemRecorder* rec) { | |
312 assert(rec != NULL, "null recorder"); | |
313 // we don't want to pool too many recorders | |
314 rec->set_next(NULL); | |
315 if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) { | |
316 delete rec; | |
317 return; | |
318 } | |
319 | |
320 rec->clear(); | |
321 MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders); | |
322 rec->set_next(cur_head); | |
323 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders, | |
324 (void*)cur_head)) { | |
325 cur_head = const_cast<MemRecorder*>(_pooled_recorders); | |
326 rec->set_next(cur_head); | |
327 } | |
328 Atomic::inc(&_pooled_recorder_count); | |
329 } | |
330 | |
331 /* | |
332 * This is the most important method in whole nmt implementation. | |
333 * | |
334 * Create a memory record. | |
335 * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM | |
336 * still in single thread mode. | |
337 * 2. For all threads other than JavaThread, ThreadCritical is needed | |
338 * to write to recorders to global recorder. | |
339 * 3. For JavaThreads that are not longer visible by safepoint, also | |
340 * need to take ThreadCritical and records are written to global | |
341 * recorders, since these threads are NOT walked by Threads.do_thread(). | |
342 * 4. JavaThreads that are running in native state, have to transition | |
343 * to VM state before writing to per-thread recorders. | |
344 * 5. JavaThreads that are running in VM state do not need any lock and | |
345 * records are written to per-thread recorders. | |
346 * 6. For a thread has yet to attach VM 'Thread', they need to take | |
347 * ThreadCritical to write to global recorder. | |
348 * | |
349 * Important note: | |
350 * NO LOCK should be taken inside ThreadCritical lock !!! | |
351 */ | |
352 void MemTracker::create_memory_record(address addr, MEMFLAGS flags, | |
353 size_t size, address pc, Thread* thread) { | |
6768
716e6ef4482a
7190089: NMT ON: NMT failed assertion on thread's stack base address
zgu
parents:
6607
diff
changeset
|
354 assert(addr != NULL, "Sanity check"); |
6197 | 355 if (!shutdown_in_progress()) { |
356 // single thread, we just write records direct to global recorder,' | |
357 // with any lock | |
358 if (_state == NMT_bootstrapping_single_thread) { | |
359 assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); | |
360 thread = NULL; | |
361 } else { | |
362 if (thread == NULL) { | |
363 // don't use Thread::current(), since it is possible that | |
364 // the calling thread has yet to attach to VM 'Thread', | |
365 // which will result assertion failure | |
366 thread = ThreadLocalStorage::thread(); | |
367 } | |
368 } | |
369 | |
370 if (thread != NULL) { | |
8810
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
371 // slow down all calling threads except NMT worker thread, so it |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
372 // can catch up. |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
373 if (_slowdown_calling_thread && thread != _worker_thread) { |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
374 os::yield_all(); |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
375 } |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
376 |
6197 | 377 if (thread->is_Java_thread() && ((JavaThread*)thread)->is_safepoint_visible()) { |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6781
diff
changeset
|
378 JavaThread* java_thread = (JavaThread*)thread; |
6231 | 379 JavaThreadState state = java_thread->thread_state(); |
380 if (SafepointSynchronize::safepoint_safe(java_thread, state)) { | |
381 // JavaThreads that are safepoint safe, can run through safepoint, | |
382 // so ThreadCritical is needed to ensure no threads at safepoint create | |
383 // new records while the records are being gathered and the sequence number is changing | |
384 ThreadCritical tc; | |
385 create_record_in_recorder(addr, flags, size, pc, java_thread); | |
6197 | 386 } else { |
6231 | 387 create_record_in_recorder(addr, flags, size, pc, java_thread); |
6197 | 388 } |
389 } else { | |
390 // other threads, such as worker and watcher threads, etc. need to | |
391 // take ThreadCritical to write to global recorder | |
392 ThreadCritical tc; | |
393 create_record_in_recorder(addr, flags, size, pc, NULL); | |
394 } | |
395 } else { | |
396 if (_state == NMT_bootstrapping_single_thread) { | |
397 // single thread, no lock needed | |
398 create_record_in_recorder(addr, flags, size, pc, NULL); | |
399 } else { | |
400 // for thread has yet to attach VM 'Thread', we can not use VM mutex. | |
401 // use native thread critical instead | |
402 ThreadCritical tc; | |
403 create_record_in_recorder(addr, flags, size, pc, NULL); | |
404 } | |
405 } | |
406 } | |
407 } | |
408 | |
409 // write a record to proper recorder. No lock can be taken from this method | |
410 // down. | |
411 void MemTracker::create_record_in_recorder(address addr, MEMFLAGS flags, | |
6231 | 412 size_t size, address pc, JavaThread* thread) { |
6197 | 413 |
6231 | 414 MemRecorder* rc = get_thread_recorder(thread); |
6197 | 415 if (rc != NULL) { |
416 rc->record(addr, flags, size, pc); | |
417 } | |
418 } | |
419 | |
420 /** | |
421 * enqueue a recorder to pending queue | |
422 */ | |
423 void MemTracker::enqueue_pending_recorder(MemRecorder* rec) { | |
424 assert(rec != NULL, "null recorder"); | |
425 | |
426 // we are shutting down, so just delete it | |
427 if (shutdown_in_progress()) { | |
428 rec->set_next(NULL); | |
429 delete rec; | |
430 return; | |
431 } | |
432 | |
433 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue); | |
434 rec->set_next(cur_head); | |
435 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue, | |
436 (void*)cur_head)) { | |
437 cur_head = const_cast<MemRecorder*>(_merge_pending_queue); | |
438 rec->set_next(cur_head); | |
439 } | |
6607
e5bf1c79ed5b
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents:
6235
diff
changeset
|
440 NOT_PRODUCT(Atomic::inc(&_pending_recorder_count);) |
6197 | 441 } |
442 | |
443 /* | |
444 * The method is called at global safepoint | |
445 * during it synchronization process. | |
446 * 1. enqueue all JavaThreads' per-thread recorders | |
447 * 2. enqueue global recorder | |
448 * 3. retrieve all pending recorders | |
449 * 4. reset global sequence number generator | |
450 * 5. call worker's sync | |
451 */ | |
452 #define MAX_SAFEPOINTS_TO_SKIP 128 | |
453 #define SAFE_SEQUENCE_THRESHOLD 30 | |
454 #define HIGH_GENERATION_THRESHOLD 60 | |
8810
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
455 #define MAX_RECORDER_THREAD_RATIO 30 |
6197 | 456 |
457 void MemTracker::sync() { | |
458 assert(_tracking_level > NMT_off, "NMT is not enabled"); | |
459 assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required"); | |
460 | |
461 // Some GC tests hit large number of safepoints in short period of time | |
462 // without meaningful activities. We should prevent going to | |
463 // sync point in these cases, which can potentially exhaust generation buffer. | |
464 // Here is the factots to determine if we should go into sync point: | |
465 // 1. not to overflow sequence number | |
466 // 2. if we are in danger to overflow generation buffer | |
467 // 3. how many safepoints we already skipped sync point | |
468 if (_state == NMT_started) { | |
469 // worker thread is not ready, no one can manage generation | |
470 // buffer, so skip this safepoint | |
471 if (_worker_thread == NULL) return; | |
472 | |
473 if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) { | |
474 int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint; | |
475 int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS; | |
476 if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) { | |
477 _sync_point_skip_count ++; | |
478 return; | |
479 } | |
480 } | |
481 _sync_point_skip_count = 0; | |
482 { | |
483 // This method is running at safepoint, with ThreadCritical lock, | |
484 // it should guarantee that NMT is fully sync-ed. | |
485 ThreadCritical tc; | |
6231 | 486 |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6781
diff
changeset
|
487 SequenceGenerator::reset(); |
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6781
diff
changeset
|
488 |
6231 | 489 // walk all JavaThreads to collect recorders |
490 SyncThreadRecorderClosure stc; | |
491 Threads::threads_do(&stc); | |
492 | |
493 _thread_count = stc.get_thread_count(); | |
494 MemRecorder* pending_recorders = get_pending_recorders(); | |
495 | |
6197 | 496 if (_global_recorder != NULL) { |
497 _global_recorder->set_next(pending_recorders); | |
498 pending_recorders = _global_recorder; | |
499 _global_recorder = NULL; | |
500 } | |
8810
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
501 |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
502 // see if NMT has too many outstanding recorder instances, it usually |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
503 // means that worker thread is lagging behind in processing them. |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
504 if (!AutoShutdownNMT) { |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
505 _slowdown_calling_thread = (MemRecorder::_instance_count > MAX_RECORDER_THREAD_RATIO * _thread_count); |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
506 } |
06db4c0afbf3
8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
zgu
parents:
7971
diff
changeset
|
507 |
6197 | 508 // check _worker_thread with lock to avoid racing condition |
509 if (_worker_thread != NULL) { | |
7464
ecd24264898b
8005048: NMT: #loaded classes needs to just show the # defined classes
zgu
parents:
7172
diff
changeset
|
510 _worker_thread->at_sync_point(pending_recorders, InstanceKlass::number_of_instance_classes()); |
6197 | 511 } |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6781
diff
changeset
|
512 |
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6781
diff
changeset
|
513 assert(SequenceGenerator::peek() == 1, "Should not have memory activities during sync-point"); |
6197 | 514 } |
515 } | |
516 | |
517 // now, it is the time to shut whole things off | |
518 if (_state == NMT_final_shutdown) { | |
519 // walk all JavaThreads to delete all recorders | |
520 SyncThreadRecorderClosure stc; | |
521 Threads::threads_do(&stc); | |
522 // delete global recorder | |
523 { | |
524 ThreadCritical tc; | |
525 if (_global_recorder != NULL) { | |
526 delete _global_recorder; | |
527 _global_recorder = NULL; | |
528 } | |
529 } | |
6231 | 530 MemRecorder* pending_recorders = get_pending_recorders(); |
531 if (pending_recorders != NULL) { | |
532 delete pending_recorders; | |
533 } | |
534 // try at a later sync point to ensure MemRecorder instance drops to zero to | |
535 // completely shutdown NMT | |
536 if (MemRecorder::_instance_count == 0) { | |
537 _state = NMT_shutdown; | |
538 _tracking_level = NMT_off; | |
539 } | |
6197 | 540 } |
541 } | |
542 | |
543 /* | |
544 * Start worker thread. | |
545 */ | |
546 bool MemTracker::start_worker() { | |
547 assert(_worker_thread == NULL, "Just Check"); | |
548 _worker_thread = new (std::nothrow) MemTrackWorker(); | |
549 if (_worker_thread == NULL || _worker_thread->has_error()) { | |
9061
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
550 if (_worker_thread != NULL) { |
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
551 delete _worker_thread; |
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
552 _worker_thread = NULL; |
4c8bb5e4f68f
8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot
zgu
parents:
8810
diff
changeset
|
553 } |
6197 | 554 return false; |
555 } | |
556 _worker_thread->start(); | |
557 return true; | |
558 } | |
559 | |
560 /* | |
561 * We need to collect a JavaThread's per-thread recorder | |
562 * before it exits. | |
563 */ | |
564 void MemTracker::thread_exiting(JavaThread* thread) { | |
565 if (is_on()) { | |
566 MemRecorder* rec = thread->get_recorder(); | |
567 if (rec != NULL) { | |
568 enqueue_pending_recorder(rec); | |
569 thread->set_recorder(NULL); | |
570 } | |
571 } | |
572 } | |
573 | |
574 // baseline current memory snapshot | |
575 bool MemTracker::baseline() { | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
576 MutexLockerEx lock(_query_lock, true); |
6197 | 577 MemSnapshot* snapshot = get_snapshot(); |
578 if (snapshot != NULL) { | |
579 return _baseline.baseline(*snapshot, false); | |
580 } | |
581 return false; | |
582 } | |
583 | |
584 // print memory usage from current snapshot | |
585 bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) { | |
586 MemBaseline baseline; | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
587 MutexLockerEx lock(_query_lock, true); |
6197 | 588 MemSnapshot* snapshot = get_snapshot(); |
589 if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) { | |
590 BaselineReporter reporter(out, unit); | |
591 reporter.report_baseline(baseline, summary_only); | |
592 return true; | |
593 } | |
594 return false; | |
595 } | |
596 | |
7971
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
597 // Whitebox API for blocking until the current generation of NMT data has been merged |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
598 bool MemTracker::wbtest_wait_for_data_merge() { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
599 // NMT can't be shutdown while we're holding _query_lock |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
600 MutexLockerEx lock(_query_lock, true); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
601 assert(_worker_thread != NULL, "Invalid query"); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
602 // the generation at query time, so NMT will spin till this generation is processed |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
603 unsigned long generation_at_query_time = SequenceGenerator::current_generation(); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
604 unsigned long current_processing_generation = _processing_generation; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
605 // if generation counter overflown |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
606 bool generation_overflown = (generation_at_query_time < current_processing_generation); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
607 long generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
608 // spin |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
609 while (!shutdown_in_progress()) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
610 if (!generation_overflown) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
611 if (current_processing_generation > generation_at_query_time) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
612 return true; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
613 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
614 } else { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
615 assert(generations_to_wrap >= 0, "Sanity check"); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
616 long current_generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
617 assert(current_generations_to_wrap >= 0, "Sanity check"); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
618 // to overflow an unsigned long should take long time, so to_wrap check should be sufficient |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
619 if (current_generations_to_wrap > generations_to_wrap && |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
620 current_processing_generation > generation_at_query_time) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
621 return true; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
622 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
623 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
624 |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
625 // if worker thread is idle, but generation is not advancing, that means |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
626 // there is not safepoint to let NMT advance generation, force one. |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
627 if (_worker_thread_idle) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
628 VM_ForceSafepoint vfs; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
629 VMThread::execute(&vfs); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
630 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
631 MemSnapshot* snapshot = get_snapshot(); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
632 if (snapshot == NULL) { |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
633 return false; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
634 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
635 snapshot->wait(1000); |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
636 current_processing_generation = _processing_generation; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
637 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
638 // We end up here if NMT is shutting down before our data has been merged |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
639 return false; |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
640 } |
4102b59539ce
8005012: Add WB APIs to better support NMT testing
ctornqvi
parents:
7464
diff
changeset
|
641 |
6197 | 642 // compare memory usage between current snapshot and baseline |
643 bool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) { | |
6232
f1f45dddb0bd
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
zgu
parents:
6197
diff
changeset
|
644 MutexLockerEx lock(_query_lock, true); |
6197 | 645 if (_baseline.baselined()) { |
646 MemBaseline baseline; | |
647 MemSnapshot* snapshot = get_snapshot(); | |
648 if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) { | |
649 BaselineReporter reporter(out, unit); | |
650 reporter.diff_baselines(baseline, _baseline, summary_only); | |
651 return true; | |
652 } | |
653 } | |
654 return false; | |
655 } | |
656 | |
657 #ifndef PRODUCT | |
658 void MemTracker::walk_stack(int toSkip, char* buf, int len) { | |
659 int cur_len = 0; | |
660 char tmp[1024]; | |
661 address pc; | |
662 | |
663 while (cur_len < len) { | |
664 pc = os::get_caller_pc(toSkip + 1); | |
665 if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) { | |
666 jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp); | |
667 cur_len = (int)strlen(buf); | |
668 } else { | |
669 buf[cur_len] = '\0'; | |
670 break; | |
671 } | |
672 toSkip ++; | |
673 } | |
674 } | |
675 | |
676 void MemTracker::print_tracker_stats(outputStream* st) { | |
677 st->print_cr("\nMemory Tracker Stats:"); | |
678 st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num()); | |
679 st->print_cr("\tthead count = %d", _thread_count); | |
680 st->print_cr("\tArena instance = %d", Arena::_instance_count); | |
681 st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count); | |
682 st->print_cr("\tqueued recorder count = %d", _pending_recorder_count); | |
683 st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count); | |
684 if (_worker_thread != NULL) { | |
685 st->print_cr("\tWorker thread:"); | |
686 st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count); | |
687 st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders()); | |
688 st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count); | |
689 } else { | |
690 st->print_cr("\tWorker thread is not started"); | |
691 } | |
692 st->print_cr(" "); | |
693 | |
694 if (_snapshot != NULL) { | |
695 _snapshot->print_snapshot_stats(st); | |
696 } else { | |
697 st->print_cr("No snapshot"); | |
698 } | |
699 } | |
700 #endif | |
701 |