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