src/share/vm/services/memTracker.cpp

Tue, 28 May 2013 11:32:46 -0400

author
jprovino
date
Tue, 28 May 2013 11:32:46 -0400
changeset 5188
fb14e9ed1594
parent 4980
fbca7eaeac2e
child 5272
1f4355cee9a2
child 5402
16b10327b00d
permissions
-rw-r--r--

8011064: Some tests have failed with SIGSEGV on arm-hflt on build b82
Summary: NMT_detail is only supported when frame pointers are not omitted (-fno-omit-frame-pointer).
Reviewed-by: dholmes, cjplummer

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

mercurial