src/share/vm/services/memTracker.cpp

Mon, 12 Nov 2012 16:15:05 -0500

author
hseigel
date
Mon, 12 Nov 2012 16:15:05 -0500
changeset 4278
070d523b96a7
parent 4193
716c64bda5ba
child 4291
bbc14465e7db
permissions
-rw-r--r--

8001471: Klass::cast() does nothing
Summary: Remove function Klass::cast() and calls to it.
Reviewed-by: dholmes, coleenp

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

mercurial