annotate src/share/vm/services/memTracker.cpp @ 3501:f1f45dddb0bd

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