src/share/vm/services/memTracker.cpp

Wed, 20 Mar 2013 09:42:48 -0400

author
zgu
date
Wed, 20 Mar 2013 09:42:48 -0400
changeset 4810
06db4c0afbf3
parent 4512
4102b59539ce
child 4890
4c8bb5e4f68f
permissions
-rw-r--r--

8009298: NMT: Special version of class loading/unloading with runThese stresses out NMT
8009777: NMT: add new NMT dcmd to control auto shutdown option
Summary: Added diagnostic VM option and DCmd command to allow NMT stay alive under stress situation
Reviewed-by: dcubed, coleenp

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

mercurial