Mon, 16 Jul 2012 14:10:34 -0400
7181986: NMT ON: Assertion failure when running jdi ExpiredRequestDeletionTest
Summary: Changed _query_lock to heap object from static object. Also fixed _query_lock and snapshot lock ranks, so they can participate deadlock detection.
Reviewed-by: coleenp, dholmes, kvn
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 #include "precompiled.hpp"
26 #include "runtime/atomic.hpp"
27 #include "runtime/interfaceSupport.hpp"
28 #include "runtime/mutexLocker.hpp"
29 #include "runtime/safepoint.hpp"
30 #include "runtime/threadCritical.hpp"
31 #include "services/memPtr.hpp"
32 #include "services/memReporter.hpp"
33 #include "services/memTracker.hpp"
34 #include "utilities/decoder.hpp"
35 #include "utilities/globalDefinitions.hpp"
37 bool NMT_track_callsite = false;
39 // walk all 'known' threads at NMT sync point, and collect their recorders
40 void SyncThreadRecorderClosure::do_thread(Thread* thread) {
41 assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
42 if (thread->is_Java_thread()) {
43 JavaThread* javaThread = (JavaThread*)thread;
44 MemRecorder* recorder = javaThread->get_recorder();
45 if (recorder != NULL) {
46 MemTracker::enqueue_pending_recorder(recorder);
47 javaThread->set_recorder(NULL);
48 }
49 }
50 _thread_count ++;
51 }
54 MemRecorder* MemTracker::_global_recorder = NULL;
55 MemSnapshot* MemTracker::_snapshot = NULL;
56 MemBaseline MemTracker::_baseline;
57 Mutex* MemTracker::_query_lock = NULL;
58 volatile MemRecorder* MemTracker::_merge_pending_queue = NULL;
59 volatile MemRecorder* MemTracker::_pooled_recorders = NULL;
60 MemTrackWorker* MemTracker::_worker_thread = NULL;
61 int MemTracker::_sync_point_skip_count = 0;
62 MemTracker::NMTLevel MemTracker::_tracking_level = MemTracker::NMT_off;
63 volatile MemTracker::NMTStates MemTracker::_state = NMT_uninited;
64 MemTracker::ShutdownReason MemTracker::_reason = NMT_shutdown_none;
65 int MemTracker::_thread_count = 255;
66 volatile jint MemTracker::_pooled_recorder_count = 0;
67 debug_only(intx MemTracker::_main_thread_tid = 0;)
68 debug_only(volatile jint MemTracker::_pending_recorder_count = 0;)
70 void MemTracker::init_tracking_options(const char* option_line) {
71 _tracking_level = NMT_off;
72 if (strncmp(option_line, "=summary", 8) == 0) {
73 _tracking_level = NMT_summary;
74 } else if (strncmp(option_line, "=detail", 8) == 0) {
75 _tracking_level = NMT_detail;
76 }
77 }
79 // first phase of bootstrapping, when VM is still in single-threaded mode.
80 void MemTracker::bootstrap_single_thread() {
81 if (_tracking_level > NMT_off) {
82 assert(_state == NMT_uninited, "wrong state");
84 // NMT is not supported with UseMallocOnly is on. NMT can NOT
85 // handle the amount of malloc data without significantly impacting
86 // runtime performance when this flag is on.
87 if (UseMallocOnly) {
88 shutdown(NMT_use_malloc_only);
89 return;
90 }
92 _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");
93 if (_query_lock == NULL) {
94 shutdown(NMT_out_of_memory);
95 return;
96 }
98 debug_only(_main_thread_tid = os::current_thread_id();)
99 _state = NMT_bootstrapping_single_thread;
100 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
101 }
102 }
104 // second phase of bootstrapping, when VM is about to or already entered multi-theaded mode.
105 void MemTracker::bootstrap_multi_thread() {
106 if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) {
107 // create nmt lock for multi-thread execution
108 assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
109 _state = NMT_bootstrapping_multi_thread;
110 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
111 }
112 }
114 // fully start nmt
115 void MemTracker::start() {
116 // Native memory tracking is off from command line option
117 if (_tracking_level == NMT_off || shutdown_in_progress()) return;
119 assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
120 assert(_state == NMT_bootstrapping_multi_thread, "wrong state");
122 _snapshot = new (std::nothrow)MemSnapshot();
123 if (_snapshot != NULL && !_snapshot->out_of_memory()) {
124 if (start_worker()) {
125 _state = NMT_started;
126 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
127 return;
128 }
129 }
131 // fail to start native memory tracking, shut it down
132 shutdown(NMT_initialization);
133 }
135 /**
136 * Shutting down native memory tracking.
137 * We can not shutdown native memory tracking immediately, so we just
138 * setup shutdown pending flag, every native memory tracking component
139 * should orderly shut itself down.
140 *
141 * The shutdown sequences:
142 * 1. MemTracker::shutdown() sets MemTracker to shutdown pending state
143 * 2. Worker thread calls MemTracker::final_shutdown(), which transites
144 * MemTracker to final shutdown state.
145 * 3. At sync point, MemTracker does final cleanup, before sets memory
146 * tracking level to off to complete shutdown.
147 */
148 void MemTracker::shutdown(ShutdownReason reason) {
149 if (_tracking_level == NMT_off) return;
151 if (_state <= NMT_bootstrapping_single_thread) {
152 // we still in single thread mode, there is not contention
153 _state = NMT_shutdown_pending;
154 _reason = reason;
155 } else {
156 // we want to know who initialized shutdown
157 if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending,
158 (jint*)&_state, (jint)NMT_started)) {
159 _reason = reason;
160 }
161 }
162 }
164 // final phase of shutdown
165 void MemTracker::final_shutdown() {
166 // delete all pending recorders and pooled recorders
167 delete_all_pending_recorders();
168 delete_all_pooled_recorders();
170 {
171 // shared baseline and snapshot are the only objects needed to
172 // create query results
173 MutexLockerEx locker(_query_lock, true);
174 // cleanup baseline data and snapshot
175 _baseline.clear();
176 delete _snapshot;
177 _snapshot = NULL;
178 }
180 // shutdown shared decoder instance, since it is only
181 // used by native memory tracking so far.
182 Decoder::shutdown();
184 MemTrackWorker* worker = NULL;
185 {
186 ThreadCritical tc;
187 // can not delete worker inside the thread critical
188 if (_worker_thread != NULL && Thread::current() == _worker_thread) {
189 worker = _worker_thread;
190 _worker_thread = NULL;
191 }
192 }
193 if (worker != NULL) {
194 delete worker;
195 }
196 _state = NMT_final_shutdown;
197 }
199 // delete all pooled recorders
200 void MemTracker::delete_all_pooled_recorders() {
201 // free all pooled recorders
202 volatile MemRecorder* cur_head = _pooled_recorders;
203 if (cur_head != NULL) {
204 MemRecorder* null_ptr = NULL;
205 while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr,
206 (void*)&_pooled_recorders, (void*)cur_head)) {
207 cur_head = _pooled_recorders;
208 }
209 if (cur_head != NULL) {
210 delete cur_head;
211 _pooled_recorder_count = 0;
212 }
213 }
214 }
216 // delete all recorders in pending queue
217 void MemTracker::delete_all_pending_recorders() {
218 // free all pending recorders
219 MemRecorder* pending_head = get_pending_recorders();
220 if (pending_head != NULL) {
221 delete pending_head;
222 }
223 }
225 /*
226 * retrieve per-thread recorder of specified thread.
227 * if thread == NULL, it means global recorder
228 */
229 MemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) {
230 if (shutdown_in_progress()) return NULL;
232 MemRecorder* rc;
233 if (thread == NULL) {
234 rc = _global_recorder;
235 } else {
236 rc = thread->get_recorder();
237 }
239 if (rc != NULL && rc->is_full()) {
240 enqueue_pending_recorder(rc);
241 rc = NULL;
242 }
244 if (rc == NULL) {
245 rc = get_new_or_pooled_instance();
246 if (thread == NULL) {
247 _global_recorder = rc;
248 } else {
249 thread->set_recorder(rc);
250 }
251 }
252 return rc;
253 }
255 /*
256 * get a per-thread recorder from pool, or create a new one if
257 * there is not one available.
258 */
259 MemRecorder* MemTracker::get_new_or_pooled_instance() {
260 MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders);
261 if (cur_head == NULL) {
262 MemRecorder* rec = new (std::nothrow)MemRecorder();
263 if (rec == NULL || rec->out_of_memory()) {
264 shutdown(NMT_out_of_memory);
265 if (rec != NULL) {
266 delete rec;
267 rec = NULL;
268 }
269 }
270 return rec;
271 } else {
272 MemRecorder* next_head = cur_head->next();
273 if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders,
274 (void*)cur_head)) {
275 return get_new_or_pooled_instance();
276 }
277 cur_head->set_next(NULL);
278 Atomic::dec(&_pooled_recorder_count);
279 debug_only(cur_head->set_generation();)
280 return cur_head;
281 }
282 }
284 /*
285 * retrieve all recorders in pending queue, and empty the queue
286 */
287 MemRecorder* MemTracker::get_pending_recorders() {
288 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
289 MemRecorder* null_ptr = NULL;
290 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue,
291 (void*)cur_head)) {
292 cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
293 }
294 debug_only(Atomic::store(0, &_pending_recorder_count));
295 return cur_head;
296 }
298 /*
299 * release a recorder to recorder pool.
300 */
301 void MemTracker::release_thread_recorder(MemRecorder* rec) {
302 assert(rec != NULL, "null recorder");
303 // we don't want to pool too many recorders
304 rec->set_next(NULL);
305 if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) {
306 delete rec;
307 return;
308 }
310 rec->clear();
311 MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders);
312 rec->set_next(cur_head);
313 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders,
314 (void*)cur_head)) {
315 cur_head = const_cast<MemRecorder*>(_pooled_recorders);
316 rec->set_next(cur_head);
317 }
318 Atomic::inc(&_pooled_recorder_count);
319 }
321 /*
322 * This is the most important method in whole nmt implementation.
323 *
324 * Create a memory record.
325 * 1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
326 * still in single thread mode.
327 * 2. For all threads other than JavaThread, ThreadCritical is needed
328 * to write to recorders to global recorder.
329 * 3. For JavaThreads that are not longer visible by safepoint, also
330 * need to take ThreadCritical and records are written to global
331 * recorders, since these threads are NOT walked by Threads.do_thread().
332 * 4. JavaThreads that are running in native state, have to transition
333 * to VM state before writing to per-thread recorders.
334 * 5. JavaThreads that are running in VM state do not need any lock and
335 * records are written to per-thread recorders.
336 * 6. For a thread has yet to attach VM 'Thread', they need to take
337 * ThreadCritical to write to global recorder.
338 *
339 * Important note:
340 * NO LOCK should be taken inside ThreadCritical lock !!!
341 */
342 void MemTracker::create_memory_record(address addr, MEMFLAGS flags,
343 size_t size, address pc, Thread* thread) {
344 if (!shutdown_in_progress()) {
345 // single thread, we just write records direct to global recorder,'
346 // with any lock
347 if (_state == NMT_bootstrapping_single_thread) {
348 assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
349 thread = NULL;
350 } else {
351 if (thread == NULL) {
352 // don't use Thread::current(), since it is possible that
353 // the calling thread has yet to attach to VM 'Thread',
354 // which will result assertion failure
355 thread = ThreadLocalStorage::thread();
356 }
357 }
359 if (thread != NULL) {
360 #ifdef ASSERT
361 // cause assertion on stack base. This ensures that threads call
362 // Thread::record_stack_base_and_size() method, which will create
363 // thread native stack records.
364 thread->stack_base();
365 #endif
366 // for a JavaThread, if it is running in native state, we need to transition it to
367 // VM state, so it can stop at safepoint. JavaThread running in VM state does not
368 // need lock to write records.
369 if (thread->is_Java_thread() && ((JavaThread*)thread)->is_safepoint_visible()) {
370 if (((JavaThread*)thread)->thread_state() == _thread_in_native) {
371 ThreadInVMfromNative trans((JavaThread*)thread);
372 create_record_in_recorder(addr, flags, size, pc, thread);
373 } else {
374 create_record_in_recorder(addr, flags, size, pc, thread);
375 }
376 } else {
377 // other threads, such as worker and watcher threads, etc. need to
378 // take ThreadCritical to write to global recorder
379 ThreadCritical tc;
380 create_record_in_recorder(addr, flags, size, pc, NULL);
381 }
382 } else {
383 if (_state == NMT_bootstrapping_single_thread) {
384 // single thread, no lock needed
385 create_record_in_recorder(addr, flags, size, pc, NULL);
386 } else {
387 // for thread has yet to attach VM 'Thread', we can not use VM mutex.
388 // use native thread critical instead
389 ThreadCritical tc;
390 create_record_in_recorder(addr, flags, size, pc, NULL);
391 }
392 }
393 }
394 }
396 // write a record to proper recorder. No lock can be taken from this method
397 // down.
398 void MemTracker::create_record_in_recorder(address addr, MEMFLAGS flags,
399 size_t size, address pc, Thread* thread) {
400 assert(thread == NULL || thread->is_Java_thread(), "wrong thread");
402 MemRecorder* rc = get_thread_recorder((JavaThread*)thread);
403 if (rc != NULL) {
404 rc->record(addr, flags, size, pc);
405 }
406 }
408 /**
409 * enqueue a recorder to pending queue
410 */
411 void MemTracker::enqueue_pending_recorder(MemRecorder* rec) {
412 assert(rec != NULL, "null recorder");
414 // we are shutting down, so just delete it
415 if (shutdown_in_progress()) {
416 rec->set_next(NULL);
417 delete rec;
418 return;
419 }
421 MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
422 rec->set_next(cur_head);
423 while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue,
424 (void*)cur_head)) {
425 cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
426 rec->set_next(cur_head);
427 }
428 debug_only(Atomic::inc(&_pending_recorder_count);)
429 }
431 /*
432 * The method is called at global safepoint
433 * during it synchronization process.
434 * 1. enqueue all JavaThreads' per-thread recorders
435 * 2. enqueue global recorder
436 * 3. retrieve all pending recorders
437 * 4. reset global sequence number generator
438 * 5. call worker's sync
439 */
440 #define MAX_SAFEPOINTS_TO_SKIP 128
441 #define SAFE_SEQUENCE_THRESHOLD 30
442 #define HIGH_GENERATION_THRESHOLD 60
444 void MemTracker::sync() {
445 assert(_tracking_level > NMT_off, "NMT is not enabled");
446 assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
448 // Some GC tests hit large number of safepoints in short period of time
449 // without meaningful activities. We should prevent going to
450 // sync point in these cases, which can potentially exhaust generation buffer.
451 // Here is the factots to determine if we should go into sync point:
452 // 1. not to overflow sequence number
453 // 2. if we are in danger to overflow generation buffer
454 // 3. how many safepoints we already skipped sync point
455 if (_state == NMT_started) {
456 // worker thread is not ready, no one can manage generation
457 // buffer, so skip this safepoint
458 if (_worker_thread == NULL) return;
460 if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) {
461 int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint;
462 int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS;
463 if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) {
464 _sync_point_skip_count ++;
465 return;
466 }
467 }
468 _sync_point_skip_count = 0;
469 // walk all JavaThreads to collect recorders
470 SyncThreadRecorderClosure stc;
471 Threads::threads_do(&stc);
473 _thread_count = stc.get_thread_count();
474 MemRecorder* pending_recorders = get_pending_recorders();
476 {
477 // This method is running at safepoint, with ThreadCritical lock,
478 // it should guarantee that NMT is fully sync-ed.
479 ThreadCritical tc;
480 if (_global_recorder != NULL) {
481 _global_recorder->set_next(pending_recorders);
482 pending_recorders = _global_recorder;
483 _global_recorder = NULL;
484 }
485 SequenceGenerator::reset();
486 // check _worker_thread with lock to avoid racing condition
487 if (_worker_thread != NULL) {
488 _worker_thread->at_sync_point(pending_recorders);
489 }
490 }
491 }
493 // now, it is the time to shut whole things off
494 if (_state == NMT_final_shutdown) {
495 _tracking_level = NMT_off;
497 // walk all JavaThreads to delete all recorders
498 SyncThreadRecorderClosure stc;
499 Threads::threads_do(&stc);
500 // delete global recorder
501 {
502 ThreadCritical tc;
503 if (_global_recorder != NULL) {
504 delete _global_recorder;
505 _global_recorder = NULL;
506 }
507 }
509 _state = NMT_shutdown;
510 }
511 }
513 /*
514 * Start worker thread.
515 */
516 bool MemTracker::start_worker() {
517 assert(_worker_thread == NULL, "Just Check");
518 _worker_thread = new (std::nothrow) MemTrackWorker();
519 if (_worker_thread == NULL || _worker_thread->has_error()) {
520 shutdown(NMT_initialization);
521 return false;
522 }
523 _worker_thread->start();
524 return true;
525 }
527 /*
528 * We need to collect a JavaThread's per-thread recorder
529 * before it exits.
530 */
531 void MemTracker::thread_exiting(JavaThread* thread) {
532 if (is_on()) {
533 MemRecorder* rec = thread->get_recorder();
534 if (rec != NULL) {
535 enqueue_pending_recorder(rec);
536 thread->set_recorder(NULL);
537 }
538 }
539 }
541 // baseline current memory snapshot
542 bool MemTracker::baseline() {
543 MutexLockerEx lock(_query_lock, true);
544 MemSnapshot* snapshot = get_snapshot();
545 if (snapshot != NULL) {
546 return _baseline.baseline(*snapshot, false);
547 }
548 return false;
549 }
551 // print memory usage from current snapshot
552 bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
553 MemBaseline baseline;
554 MutexLockerEx lock(_query_lock, true);
555 MemSnapshot* snapshot = get_snapshot();
556 if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
557 BaselineReporter reporter(out, unit);
558 reporter.report_baseline(baseline, summary_only);
559 return true;
560 }
561 return false;
562 }
564 // compare memory usage between current snapshot and baseline
565 bool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
566 MutexLockerEx lock(_query_lock, true);
567 if (_baseline.baselined()) {
568 MemBaseline baseline;
569 MemSnapshot* snapshot = get_snapshot();
570 if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
571 BaselineReporter reporter(out, unit);
572 reporter.diff_baselines(baseline, _baseline, summary_only);
573 return true;
574 }
575 }
576 return false;
577 }
579 #ifndef PRODUCT
580 void MemTracker::walk_stack(int toSkip, char* buf, int len) {
581 int cur_len = 0;
582 char tmp[1024];
583 address pc;
585 while (cur_len < len) {
586 pc = os::get_caller_pc(toSkip + 1);
587 if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) {
588 jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp);
589 cur_len = (int)strlen(buf);
590 } else {
591 buf[cur_len] = '\0';
592 break;
593 }
594 toSkip ++;
595 }
596 }
598 void MemTracker::print_tracker_stats(outputStream* st) {
599 st->print_cr("\nMemory Tracker Stats:");
600 st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num());
601 st->print_cr("\tthead count = %d", _thread_count);
602 st->print_cr("\tArena instance = %d", Arena::_instance_count);
603 st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count);
604 st->print_cr("\tqueued recorder count = %d", _pending_recorder_count);
605 st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count);
606 if (_worker_thread != NULL) {
607 st->print_cr("\tWorker thread:");
608 st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count);
609 st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders());
610 st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count);
611 } else {
612 st->print_cr("\tWorker thread is not started");
613 }
614 st->print_cr(" ");
616 if (_snapshot != NULL) {
617 _snapshot->print_snapshot_stats(st);
618 } else {
619 st->print_cr("No snapshot");
620 }
621 }
622 #endif