src/share/vm/services/memTracker.cpp

Wed, 27 Aug 2014 09:36:55 +0200

author
tschatzl
date
Wed, 27 Aug 2014 09:36:55 +0200
changeset 7073
4d3a43351904
parent 6911
ce8f6bb717c9
child 7074
833b0f92429a
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2012, 2013, 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/thread.inline.hpp"
    33 #include "runtime/vm_operations.hpp"
    34 #include "services/memPtr.hpp"
    35 #include "services/memReporter.hpp"
    36 #include "services/memTracker.hpp"
    37 #include "utilities/decoder.hpp"
    38 #include "utilities/defaultStream.hpp"
    39 #include "utilities/globalDefinitions.hpp"
    41 bool NMT_track_callsite = false;
    43 // walk all 'known' threads at NMT sync point, and collect their recorders
    44 void SyncThreadRecorderClosure::do_thread(Thread* thread) {
    45   assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
    46   if (thread->is_Java_thread()) {
    47     JavaThread* javaThread = (JavaThread*)thread;
    48     MemRecorder* recorder = javaThread->get_recorder();
    49     if (recorder != NULL) {
    50       MemTracker::enqueue_pending_recorder(recorder);
    51       javaThread->set_recorder(NULL);
    52     }
    53   }
    54   _thread_count ++;
    55 }
    58 MemRecorder* volatile           MemTracker::_global_recorder = NULL;
    59 MemSnapshot*                    MemTracker::_snapshot = NULL;
    60 MemBaseline                     MemTracker::_baseline;
    61 Mutex*                          MemTracker::_query_lock = NULL;
    62 MemRecorder* volatile           MemTracker::_merge_pending_queue = NULL;
    63 MemRecorder* volatile           MemTracker::_pooled_recorders = NULL;
    64 MemTrackWorker*                 MemTracker::_worker_thread = NULL;
    65 int                             MemTracker::_sync_point_skip_count = 0;
    66 MemTracker::NMTLevel            MemTracker::_tracking_level = MemTracker::NMT_off;
    67 volatile MemTracker::NMTStates  MemTracker::_state = NMT_uninited;
    68 MemTracker::ShutdownReason      MemTracker::_reason = NMT_shutdown_none;
    69 int                             MemTracker::_thread_count = 255;
    70 volatile jint                   MemTracker::_pooled_recorder_count = 0;
    71 volatile unsigned long          MemTracker::_processing_generation = 0;
    72 volatile bool                   MemTracker::_worker_thread_idle = false;
    73 volatile jint                   MemTracker::_pending_op_count = 0;
    74 volatile bool                   MemTracker::_slowdown_calling_thread = false;
    75 debug_only(intx                 MemTracker::_main_thread_tid = 0;)
    76 NOT_PRODUCT(volatile jint       MemTracker::_pending_recorder_count = 0;)
    78 void MemTracker::init_tracking_options(const char* option_line) {
    79   _tracking_level = NMT_off;
    80   if (strcmp(option_line, "=summary") == 0) {
    81     _tracking_level = NMT_summary;
    82   } else if (strcmp(option_line, "=detail") == 0) {
    83     // detail relies on a stack-walking ability that may not
    84     // be available depending on platform and/or compiler flags
    85 #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
    86       _tracking_level = NMT_detail;
    87 #else
    88       jio_fprintf(defaultStream::error_stream(),
    89         "NMT detail is not supported on this platform.  Using NMT summary instead.\n");
    90       _tracking_level = NMT_summary;
    91 #endif
    92   } else if (strcmp(option_line, "=off") != 0) {
    93     vm_exit_during_initialization("Syntax error, expecting -XX:NativeMemoryTracking=[off|summary|detail]", NULL);
    94   }
    95 }
    97 // first phase of bootstrapping, when VM is still in single-threaded mode.
    98 void MemTracker::bootstrap_single_thread() {
    99   if (_tracking_level > NMT_off) {
   100     assert(_state == NMT_uninited, "wrong state");
   102     // NMT is not supported with UseMallocOnly is on. NMT can NOT
   103     // handle the amount of malloc data without significantly impacting
   104     // runtime performance when this flag is on.
   105     if (UseMallocOnly) {
   106       shutdown(NMT_use_malloc_only);
   107       return;
   108     }
   110     _query_lock = new (std::nothrow) Mutex(Monitor::max_nonleaf, "NMT_queryLock");
   111     if (_query_lock == NULL) {
   112       shutdown(NMT_out_of_memory);
   113       return;
   114     }
   116     debug_only(_main_thread_tid = os::current_thread_id();)
   117     _state = NMT_bootstrapping_single_thread;
   118     NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
   119   }
   120 }
   122 // second phase of bootstrapping, when VM is about to or already entered multi-theaded mode.
   123 void MemTracker::bootstrap_multi_thread() {
   124   if (_tracking_level > NMT_off && _state == NMT_bootstrapping_single_thread) {
   125   // create nmt lock for multi-thread execution
   126     assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
   127     _state = NMT_bootstrapping_multi_thread;
   128     NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
   129   }
   130 }
   132 // fully start nmt
   133 void MemTracker::start() {
   134   // Native memory tracking is off from command line option
   135   if (_tracking_level == NMT_off || shutdown_in_progress()) return;
   137   assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
   138   assert(_state == NMT_bootstrapping_multi_thread, "wrong state");
   140   _snapshot = new (std::nothrow)MemSnapshot();
   141   if (_snapshot != NULL) {
   142     if (!_snapshot->out_of_memory() && start_worker(_snapshot)) {
   143       _state = NMT_started;
   144       NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
   145       return;
   146     }
   148     delete _snapshot;
   149     _snapshot = NULL;
   150   }
   152   // fail to start native memory tracking, shut it down
   153   shutdown(NMT_initialization);
   154 }
   156 /**
   157  * Shutting down native memory tracking.
   158  * We can not shutdown native memory tracking immediately, so we just
   159  * setup shutdown pending flag, every native memory tracking component
   160  * should orderly shut itself down.
   161  *
   162  * The shutdown sequences:
   163  *  1. MemTracker::shutdown() sets MemTracker to shutdown pending state
   164  *  2. Worker thread calls MemTracker::final_shutdown(), which transites
   165  *     MemTracker to final shutdown state.
   166  *  3. At sync point, MemTracker does final cleanup, before sets memory
   167  *     tracking level to off to complete shutdown.
   168  */
   169 void MemTracker::shutdown(ShutdownReason reason) {
   170   if (_tracking_level == NMT_off) return;
   172   if (_state <= NMT_bootstrapping_single_thread) {
   173     // we still in single thread mode, there is not contention
   174     _state = NMT_shutdown_pending;
   175     _reason = reason;
   176   } else {
   177     // we want to know who initialized shutdown
   178     if ((jint)NMT_started == Atomic::cmpxchg((jint)NMT_shutdown_pending,
   179                                        (jint*)&_state, (jint)NMT_started)) {
   180         _reason = reason;
   181     }
   182   }
   183 }
   185 // final phase of shutdown
   186 void MemTracker::final_shutdown() {
   187   // delete all pending recorders and pooled recorders
   188   delete_all_pending_recorders();
   189   delete_all_pooled_recorders();
   191   {
   192     // shared baseline and snapshot are the only objects needed to
   193     // create query results
   194     MutexLockerEx locker(_query_lock, true);
   195     // cleanup baseline data and snapshot
   196     _baseline.clear();
   197     delete _snapshot;
   198     _snapshot = NULL;
   199   }
   201   // shutdown shared decoder instance, since it is only
   202   // used by native memory tracking so far.
   203   Decoder::shutdown();
   205   MemTrackWorker* worker = NULL;
   206   {
   207     ThreadCritical tc;
   208     // can not delete worker inside the thread critical
   209     if (_worker_thread != NULL && Thread::current() == _worker_thread) {
   210       worker = _worker_thread;
   211       _worker_thread = NULL;
   212     }
   213   }
   214   if (worker != NULL) {
   215     delete worker;
   216   }
   217   _state = NMT_final_shutdown;
   218 }
   220 // delete all pooled recorders
   221 void MemTracker::delete_all_pooled_recorders() {
   222   // free all pooled recorders
   223   MemRecorder* volatile cur_head = _pooled_recorders;
   224   if (cur_head != NULL) {
   225     MemRecorder* null_ptr = NULL;
   226     while (cur_head != NULL && (void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr,
   227       (void*)&_pooled_recorders, (void*)cur_head)) {
   228       cur_head = _pooled_recorders;
   229     }
   230     if (cur_head != NULL) {
   231       delete cur_head;
   232       _pooled_recorder_count = 0;
   233     }
   234   }
   235 }
   237 // delete all recorders in pending queue
   238 void MemTracker::delete_all_pending_recorders() {
   239   // free all pending recorders
   240   MemRecorder* pending_head = get_pending_recorders();
   241   if (pending_head != NULL) {
   242     delete pending_head;
   243   }
   244 }
   246 /*
   247  * retrieve per-thread recorder of specified thread.
   248  * if thread == NULL, it means global recorder
   249  */
   250 MemRecorder* MemTracker::get_thread_recorder(JavaThread* thread) {
   251   if (shutdown_in_progress()) return NULL;
   253   MemRecorder* rc;
   254   if (thread == NULL) {
   255     rc = _global_recorder;
   256   } else {
   257     rc = thread->get_recorder();
   258   }
   260   if (rc != NULL && rc->is_full()) {
   261     enqueue_pending_recorder(rc);
   262     rc = NULL;
   263   }
   265   if (rc == NULL) {
   266     rc = get_new_or_pooled_instance();
   267     if (thread == NULL) {
   268       _global_recorder = rc;
   269     } else {
   270       thread->set_recorder(rc);
   271     }
   272   }
   273   return rc;
   274 }
   276 /*
   277  * get a per-thread recorder from pool, or create a new one if
   278  * there is not one available.
   279  */
   280 MemRecorder* MemTracker::get_new_or_pooled_instance() {
   281    MemRecorder* cur_head = const_cast<MemRecorder*> (_pooled_recorders);
   282    if (cur_head == NULL) {
   283      MemRecorder* rec = new (std::nothrow)MemRecorder();
   284      if (rec == NULL || rec->out_of_memory()) {
   285        shutdown(NMT_out_of_memory);
   286        if (rec != NULL) {
   287          delete rec;
   288          rec = NULL;
   289        }
   290      }
   291      return rec;
   292    } else {
   293      MemRecorder* next_head = cur_head->next();
   294      if ((void*)cur_head != Atomic::cmpxchg_ptr((void*)next_head, (void*)&_pooled_recorders,
   295        (void*)cur_head)) {
   296        return get_new_or_pooled_instance();
   297      }
   298      cur_head->set_next(NULL);
   299      Atomic::dec(&_pooled_recorder_count);
   300      cur_head->set_generation();
   301      return cur_head;
   302   }
   303 }
   305 /*
   306  * retrieve all recorders in pending queue, and empty the queue
   307  */
   308 MemRecorder* MemTracker::get_pending_recorders() {
   309   MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
   310   MemRecorder* null_ptr = NULL;
   311   while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)null_ptr, (void*)&_merge_pending_queue,
   312     (void*)cur_head)) {
   313     cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
   314   }
   315   NOT_PRODUCT(Atomic::store(0, &_pending_recorder_count));
   316   return cur_head;
   317 }
   319 /*
   320  * release a recorder to recorder pool.
   321  */
   322 void MemTracker::release_thread_recorder(MemRecorder* rec) {
   323   assert(rec != NULL, "null recorder");
   324   // we don't want to pool too many recorders
   325   rec->set_next(NULL);
   326   if (shutdown_in_progress() || _pooled_recorder_count > _thread_count * 2) {
   327     delete rec;
   328     return;
   329   }
   331   rec->clear();
   332   MemRecorder* cur_head = const_cast<MemRecorder*>(_pooled_recorders);
   333   rec->set_next(cur_head);
   334   while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_pooled_recorders,
   335     (void*)cur_head)) {
   336     cur_head = const_cast<MemRecorder*>(_pooled_recorders);
   337     rec->set_next(cur_head);
   338   }
   339   Atomic::inc(&_pooled_recorder_count);
   340 }
   342 // write a record to proper recorder. No lock can be taken from this method
   343 // down.
   344 void MemTracker::write_tracking_record(address addr, MEMFLAGS flags,
   345     size_t size, jint seq, address pc, JavaThread* thread) {
   347     MemRecorder* rc = get_thread_recorder(thread);
   348     if (rc != NULL) {
   349       rc->record(addr, flags, size, seq, pc);
   350     }
   351 }
   353 /**
   354  * enqueue a recorder to pending queue
   355  */
   356 void MemTracker::enqueue_pending_recorder(MemRecorder* rec) {
   357   assert(rec != NULL, "null recorder");
   359   // we are shutting down, so just delete it
   360   if (shutdown_in_progress()) {
   361     rec->set_next(NULL);
   362     delete rec;
   363     return;
   364   }
   366   MemRecorder* cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
   367   rec->set_next(cur_head);
   368   while ((void*)cur_head != Atomic::cmpxchg_ptr((void*)rec, (void*)&_merge_pending_queue,
   369     (void*)cur_head)) {
   370     cur_head = const_cast<MemRecorder*>(_merge_pending_queue);
   371     rec->set_next(cur_head);
   372   }
   373   NOT_PRODUCT(Atomic::inc(&_pending_recorder_count);)
   374 }
   376 /*
   377  * The method is called at global safepoint
   378  * during it synchronization process.
   379  *   1. enqueue all JavaThreads' per-thread recorders
   380  *   2. enqueue global recorder
   381  *   3. retrieve all pending recorders
   382  *   4. reset global sequence number generator
   383  *   5. call worker's sync
   384  */
   385 #define MAX_SAFEPOINTS_TO_SKIP     128
   386 #define SAFE_SEQUENCE_THRESHOLD    30
   387 #define HIGH_GENERATION_THRESHOLD  60
   388 #define MAX_RECORDER_THREAD_RATIO  30
   389 #define MAX_RECORDER_PER_THREAD    100
   391 void MemTracker::sync() {
   392   assert(_tracking_level > NMT_off, "NMT is not enabled");
   393   assert(SafepointSynchronize::is_at_safepoint(), "Safepoint required");
   395   // Some GC tests hit large number of safepoints in short period of time
   396   // without meaningful activities. We should prevent going to
   397   // sync point in these cases, which can potentially exhaust generation buffer.
   398   // Here is the factots to determine if we should go into sync point:
   399   // 1. not to overflow sequence number
   400   // 2. if we are in danger to overflow generation buffer
   401   // 3. how many safepoints we already skipped sync point
   402   if (_state == NMT_started) {
   403     // worker thread is not ready, no one can manage generation
   404     // buffer, so skip this safepoint
   405     if (_worker_thread == NULL) return;
   407     if (_sync_point_skip_count < MAX_SAFEPOINTS_TO_SKIP) {
   408       int per_seq_in_use = SequenceGenerator::peek() * 100 / max_jint;
   409       int per_gen_in_use = _worker_thread->generations_in_use() * 100 / MAX_GENERATIONS;
   410       if (per_seq_in_use < SAFE_SEQUENCE_THRESHOLD && per_gen_in_use >= HIGH_GENERATION_THRESHOLD) {
   411         _sync_point_skip_count ++;
   412         return;
   413       }
   414     }
   415     {
   416       // This method is running at safepoint, with ThreadCritical lock,
   417       // it should guarantee that NMT is fully sync-ed.
   418       ThreadCritical tc;
   420       // We can NOT execute NMT sync-point if there are pending tracking ops.
   421       if (_pending_op_count == 0) {
   422         SequenceGenerator::reset();
   423         _sync_point_skip_count = 0;
   425         // walk all JavaThreads to collect recorders
   426         SyncThreadRecorderClosure stc;
   427         Threads::threads_do(&stc);
   429         _thread_count = stc.get_thread_count();
   430         MemRecorder* pending_recorders = get_pending_recorders();
   432         if (_global_recorder != NULL) {
   433           _global_recorder->set_next(pending_recorders);
   434           pending_recorders = _global_recorder;
   435           _global_recorder = NULL;
   436         }
   438         // see if NMT has too many outstanding recorder instances, it usually
   439         // means that worker thread is lagging behind in processing them.
   440         if (!AutoShutdownNMT) {
   441           _slowdown_calling_thread = (MemRecorder::_instance_count > MAX_RECORDER_THREAD_RATIO * _thread_count);
   442         } else {
   443           // If auto shutdown is on, enforce MAX_RECORDER_PER_THREAD threshold to prevent OOM
   444           if (MemRecorder::_instance_count >= _thread_count * MAX_RECORDER_PER_THREAD) {
   445             shutdown(NMT_out_of_memory);
   446           }
   447         }
   449         // check _worker_thread with lock to avoid racing condition
   450         if (_worker_thread != NULL) {
   451           _worker_thread->at_sync_point(pending_recorders, InstanceKlass::number_of_instance_classes());
   452         }
   453         assert(SequenceGenerator::peek() == 1, "Should not have memory activities during sync-point");
   454       } else {
   455         _sync_point_skip_count ++;
   456       }
   457     }
   458   }
   460   // now, it is the time to shut whole things off
   461   if (_state == NMT_final_shutdown) {
   462     // walk all JavaThreads to delete all recorders
   463     SyncThreadRecorderClosure stc;
   464     Threads::threads_do(&stc);
   465     // delete global recorder
   466     {
   467       ThreadCritical tc;
   468       if (_global_recorder != NULL) {
   469         delete _global_recorder;
   470         _global_recorder = NULL;
   471       }
   472     }
   473     MemRecorder* pending_recorders = get_pending_recorders();
   474     if (pending_recorders != NULL) {
   475       delete pending_recorders;
   476     }
   477     // try at a later sync point to ensure MemRecorder instance drops to zero to
   478     // completely shutdown NMT
   479     if (MemRecorder::_instance_count == 0) {
   480       _state = NMT_shutdown;
   481       _tracking_level = NMT_off;
   482     }
   483   }
   484 }
   486 /*
   487  * Start worker thread.
   488  */
   489 bool MemTracker::start_worker(MemSnapshot* snapshot) {
   490   assert(_worker_thread == NULL && _snapshot != NULL, "Just Check");
   491   _worker_thread = new (std::nothrow) MemTrackWorker(snapshot);
   492   if (_worker_thread == NULL) {
   493     return false;
   494   } else if (_worker_thread->has_error()) {
   495     delete _worker_thread;
   496     _worker_thread = NULL;
   497     return false;
   498   }
   499   _worker_thread->start();
   500   return true;
   501 }
   503 /*
   504  * We need to collect a JavaThread's per-thread recorder
   505  * before it exits.
   506  */
   507 void MemTracker::thread_exiting(JavaThread* thread) {
   508   if (is_on()) {
   509     MemRecorder* rec = thread->get_recorder();
   510     if (rec != NULL) {
   511       enqueue_pending_recorder(rec);
   512       thread->set_recorder(NULL);
   513     }
   514   }
   515 }
   517 // baseline current memory snapshot
   518 bool MemTracker::baseline() {
   519   MutexLocker lock(_query_lock);
   520   MemSnapshot* snapshot = get_snapshot();
   521   if (snapshot != NULL) {
   522     return _baseline.baseline(*snapshot, false);
   523   }
   524   return false;
   525 }
   527 // print memory usage from current snapshot
   528 bool MemTracker::print_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
   529   MemBaseline  baseline;
   530   MutexLocker  lock(_query_lock);
   531   MemSnapshot* snapshot = get_snapshot();
   532   if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
   533     BaselineReporter reporter(out, unit);
   534     reporter.report_baseline(baseline, summary_only);
   535     return true;
   536   }
   537   return false;
   538 }
   540 // Whitebox API for blocking until the current generation of NMT data has been merged
   541 bool MemTracker::wbtest_wait_for_data_merge() {
   542   // NMT can't be shutdown while we're holding _query_lock
   543   MutexLocker lock(_query_lock);
   544   assert(_worker_thread != NULL, "Invalid query");
   545   // the generation at query time, so NMT will spin till this generation is processed
   546   unsigned long generation_at_query_time = SequenceGenerator::current_generation();
   547   unsigned long current_processing_generation = _processing_generation;
   548   // if generation counter overflown
   549   bool generation_overflown = (generation_at_query_time < current_processing_generation);
   550   long generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation;
   551   // spin
   552   while (!shutdown_in_progress()) {
   553     if (!generation_overflown) {
   554       if (current_processing_generation > generation_at_query_time) {
   555         return true;
   556       }
   557     } else {
   558       assert(generations_to_wrap >= 0, "Sanity check");
   559       long current_generations_to_wrap = MAX_UNSIGNED_LONG - current_processing_generation;
   560       assert(current_generations_to_wrap >= 0, "Sanity check");
   561       // to overflow an unsigned long should take long time, so to_wrap check should be sufficient
   562       if (current_generations_to_wrap > generations_to_wrap &&
   563           current_processing_generation > generation_at_query_time) {
   564         return true;
   565       }
   566     }
   568     // if worker thread is idle, but generation is not advancing, that means
   569     // there is not safepoint to let NMT advance generation, force one.
   570     if (_worker_thread_idle) {
   571       VM_ForceSafepoint vfs;
   572       VMThread::execute(&vfs);
   573     }
   574     MemSnapshot* snapshot = get_snapshot();
   575     if (snapshot == NULL) {
   576       return false;
   577     }
   578     snapshot->wait(1000);
   579     current_processing_generation = _processing_generation;
   580   }
   581   // We end up here if NMT is shutting down before our data has been merged
   582   return false;
   583 }
   585 // compare memory usage between current snapshot and baseline
   586 bool MemTracker::compare_memory_usage(BaselineOutputer& out, size_t unit, bool summary_only) {
   587   MutexLocker lock(_query_lock);
   588   if (_baseline.baselined()) {
   589     MemBaseline baseline;
   590     MemSnapshot* snapshot = get_snapshot();
   591     if (snapshot != NULL && baseline.baseline(*snapshot, summary_only)) {
   592       BaselineReporter reporter(out, unit);
   593       reporter.diff_baselines(baseline, _baseline, summary_only);
   594       return true;
   595     }
   596   }
   597   return false;
   598 }
   600 #ifndef PRODUCT
   601 void MemTracker::walk_stack(int toSkip, char* buf, int len) {
   602   int cur_len = 0;
   603   char tmp[1024];
   604   address pc;
   606   while (cur_len < len) {
   607     pc = os::get_caller_pc(toSkip + 1);
   608     if (pc != NULL && os::dll_address_to_function_name(pc, tmp, sizeof(tmp), NULL)) {
   609       jio_snprintf(&buf[cur_len], (len - cur_len), "%s\n", tmp);
   610       cur_len = (int)strlen(buf);
   611     } else {
   612       buf[cur_len] = '\0';
   613       break;
   614     }
   615     toSkip ++;
   616   }
   617 }
   619 void MemTracker::print_tracker_stats(outputStream* st) {
   620   st->print_cr("\nMemory Tracker Stats:");
   621   st->print_cr("\tMax sequence number = %d", SequenceGenerator::max_seq_num());
   622   st->print_cr("\tthead count = %d", _thread_count);
   623   st->print_cr("\tArena instance = %d", Arena::_instance_count);
   624   st->print_cr("\tpooled recorder count = %d", _pooled_recorder_count);
   625   st->print_cr("\tqueued recorder count = %d", _pending_recorder_count);
   626   st->print_cr("\tmemory recorder instance count = %d", MemRecorder::_instance_count);
   627   if (_worker_thread != NULL) {
   628     st->print_cr("\tWorker thread:");
   629     st->print_cr("\t\tSync point count = %d", _worker_thread->_sync_point_count);
   630     st->print_cr("\t\tpending recorder count = %d", _worker_thread->count_pending_recorders());
   631     st->print_cr("\t\tmerge count = %d", _worker_thread->_merge_count);
   632   } else {
   633     st->print_cr("\tWorker thread is not started");
   634   }
   635   st->print_cr(" ");
   637   if (_snapshot != NULL) {
   638     _snapshot->print_snapshot_stats(st);
   639   } else {
   640     st->print_cr("No snapshot");
   641   }
   642 }
   643 #endif
   646 // Tracker Implementation
   648 /*
   649  * Create a tracker.
   650  * This is a fairly complicated constructor, as it has to make two important decisions:
   651  *   1) Does it need to take ThreadCritical lock to write tracking record
   652  *   2) Does it need to pre-reserve a sequence number for the tracking record
   653  *
   654  * The rules to determine if ThreadCritical is needed:
   655  *   1. When nmt is in single-threaded bootstrapping mode, no lock is needed as VM
   656  *      still in single thread mode.
   657  *   2. For all threads other than JavaThread, ThreadCritical is needed
   658  *      to write to recorders to global recorder.
   659  *   3. For JavaThreads that are no longer visible by safepoint, also
   660  *      need to take ThreadCritical and records are written to global
   661  *      recorders, since these threads are NOT walked by Threads.do_thread().
   662  *   4. JavaThreads that are running in safepoint-safe states do not stop
   663  *      for safepoints, ThreadCritical lock should be taken to write
   664  *      memory records.
   665  *   5. JavaThreads that are running in VM state do not need any lock and
   666  *      records are written to per-thread recorders.
   667  *   6. For a thread has yet to attach VM 'Thread', they need to take
   668  *      ThreadCritical to write to global recorder.
   669  *
   670  *  The memory operations that need pre-reserve sequence numbers:
   671  *    The memory operations that "release" memory blocks and the
   672  *    operations can fail, need to pre-reserve sequence number. They
   673  *    are realloc, uncommit and release.
   674  *
   675  *  The reason for pre-reserve sequence number, is to prevent race condition:
   676  *    Thread 1                      Thread 2
   677  *    <release>
   678  *                                  <allocate>
   679  *                                  <write allocate record>
   680  *   <write release record>
   681  *   if Thread 2 happens to obtain the memory address Thread 1 just released,
   682  *   then NMT can mistakenly report the memory is free.
   683  *
   684  *  Noticeably, free() does not need pre-reserve sequence number, because the call
   685  *  does not fail, so we can alway write "release" record before the memory is actaully
   686  *  freed.
   687  *
   688  *  For realloc, uncommit and release, following coding pattern should be used:
   689  *
   690  *     MemTracker::Tracker tkr = MemTracker::get_realloc_tracker();
   691  *     ptr = ::realloc(...);
   692  *     if (ptr == NULL) {
   693  *       tkr.record(...)
   694  *     } else {
   695  *       tkr.discard();
   696  *     }
   697  *
   698  *     MemTracker::Tracker tkr = MemTracker::get_virtual_memory_uncommit_tracker();
   699  *     if (uncommit(...)) {
   700  *       tkr.record(...);
   701  *     } else {
   702  *       tkr.discard();
   703  *     }
   704  *
   705  *     MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker();
   706  *     if (release(...)) {
   707  *       tkr.record(...);
   708  *     } else {
   709  *       tkr.discard();
   710  *     }
   711  *
   712  * Since pre-reserved sequence number is only good for the generation that it is acquired,
   713  * when there is pending Tracker that reserved sequence number, NMT sync-point has
   714  * to be skipped to prevent from advancing generation. This is done by inc and dec
   715  * MemTracker::_pending_op_count, when MemTracker::_pending_op_count > 0, NMT sync-point is skipped.
   716  * Not all pre-reservation of sequence number will increment pending op count. For JavaThreads
   717  * that honor safepoints, safepoint can not occur during the memory operations, so the
   718  * pre-reserved sequence number won't cross the generation boundry.
   719  */
   720 MemTracker::Tracker::Tracker(MemoryOperation op, Thread* thr) {
   721   _op = NoOp;
   722   _seq = 0;
   723   if (MemTracker::is_on()) {
   724     _java_thread = NULL;
   725     _op = op;
   727     // figure out if ThreadCritical lock is needed to write this operation
   728     // to MemTracker
   729     if (MemTracker::is_single_threaded_bootstrap()) {
   730       thr = NULL;
   731     } else if (thr == NULL) {
   732       // don't use Thread::current(), since it is possible that
   733       // the calling thread has yet to attach to VM 'Thread',
   734       // which will result assertion failure
   735       thr = ThreadLocalStorage::thread();
   736     }
   738     if (thr != NULL) {
   739       // Check NMT load
   740       MemTracker::check_NMT_load(thr);
   742       if (thr->is_Java_thread() && ((JavaThread*)thr)->is_safepoint_visible()) {
   743         _java_thread = (JavaThread*)thr;
   744         JavaThreadState  state = _java_thread->thread_state();
   745         // JavaThreads that are safepoint safe, can run through safepoint,
   746         // so ThreadCritical is needed to ensure no threads at safepoint create
   747         // new records while the records are being gathered and the sequence number is changing
   748         _need_thread_critical_lock =
   749           SafepointSynchronize::safepoint_safe(_java_thread, state);
   750       } else {
   751         _need_thread_critical_lock = true;
   752       }
   753     } else {
   754        _need_thread_critical_lock
   755          = !MemTracker::is_single_threaded_bootstrap();
   756     }
   758     // see if we need to pre-reserve sequence number for this operation
   759     if (_op == Realloc || _op == Uncommit || _op == Release) {
   760       if (_need_thread_critical_lock) {
   761         ThreadCritical tc;
   762         MemTracker::inc_pending_op_count();
   763         _seq = SequenceGenerator::next();
   764       } else {
   765         // for the threads that honor safepoints, no safepoint can occur
   766         // during the lifespan of tracker, so we don't need to increase
   767         // pending op count.
   768         _seq = SequenceGenerator::next();
   769       }
   770     }
   771   }
   772 }
   774 void MemTracker::Tracker::discard() {
   775   if (MemTracker::is_on() && _seq != 0) {
   776     if (_need_thread_critical_lock) {
   777       ThreadCritical tc;
   778       MemTracker::dec_pending_op_count();
   779     }
   780     _seq = 0;
   781   }
   782 }
   785 void MemTracker::Tracker::record(address old_addr, address new_addr, size_t size,
   786   MEMFLAGS flags, address pc) {
   787   assert(old_addr != NULL && new_addr != NULL, "Sanity check");
   788   assert(_op == Realloc || _op == NoOp, "Wrong call");
   789   if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) {
   790     assert(_seq > 0, "Need pre-reserve sequence number");
   791     if (_need_thread_critical_lock) {
   792       ThreadCritical tc;
   793       // free old address, use pre-reserved sequence number
   794       MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(),
   795         0, _seq, pc, _java_thread);
   796       MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(),
   797         size, SequenceGenerator::next(), pc, _java_thread);
   798       // decrement MemTracker pending_op_count
   799       MemTracker::dec_pending_op_count();
   800     } else {
   801       // free old address, use pre-reserved sequence number
   802       MemTracker::write_tracking_record(old_addr, MemPointerRecord::free_tag(),
   803         0, _seq, pc, _java_thread);
   804       MemTracker::write_tracking_record(new_addr, flags | MemPointerRecord::malloc_tag(),
   805         size, SequenceGenerator::next(), pc, _java_thread);
   806     }
   807     _seq = 0;
   808   }
   809 }
   811 void MemTracker::Tracker::record(address addr, size_t size, MEMFLAGS flags, address pc) {
   812   // OOM already?
   813   if (addr == NULL) return;
   815   if (MemTracker::is_on() && NMT_CAN_TRACK(flags) && _op != NoOp) {
   816     bool pre_reserved_seq = (_seq != 0);
   817     address  pc = CALLER_CALLER_PC;
   818     MEMFLAGS orig_flags = flags;
   820     // or the tagging flags
   821     switch(_op) {
   822       case Malloc:
   823         flags |= MemPointerRecord::malloc_tag();
   824         break;
   825       case Free:
   826         flags = MemPointerRecord::free_tag();
   827         break;
   828       case Realloc:
   829         fatal("Use the other Tracker::record()");
   830         break;
   831       case Reserve:
   832       case ReserveAndCommit:
   833         flags |= MemPointerRecord::virtual_memory_reserve_tag();
   834         break;
   835       case Commit:
   836         flags = MemPointerRecord::virtual_memory_commit_tag();
   837         break;
   838       case Type:
   839         flags |= MemPointerRecord::virtual_memory_type_tag();
   840         break;
   841       case Uncommit:
   842         assert(pre_reserved_seq, "Need pre-reserve sequence number");
   843         flags = MemPointerRecord::virtual_memory_uncommit_tag();
   844         break;
   845       case Release:
   846         assert(pre_reserved_seq, "Need pre-reserve sequence number");
   847         flags = MemPointerRecord::virtual_memory_release_tag();
   848         break;
   849       case ArenaSize:
   850         // a bit of hack here, add a small postive offset to arena
   851         // address for its size record, so the size record is sorted
   852         // right after arena record.
   853         flags = MemPointerRecord::arena_size_tag();
   854         addr += sizeof(void*);
   855         break;
   856       case StackRelease:
   857         flags = MemPointerRecord::virtual_memory_release_tag();
   858         break;
   859       default:
   860         ShouldNotReachHere();
   861     }
   863     // write memory tracking record
   864     if (_need_thread_critical_lock) {
   865       ThreadCritical tc;
   866       if (_seq == 0) _seq = SequenceGenerator::next();
   867       MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread);
   868       if (_op == ReserveAndCommit) {
   869         MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(),
   870           size, SequenceGenerator::next(), pc, _java_thread);
   871       }
   872       if (pre_reserved_seq) MemTracker::dec_pending_op_count();
   873     } else {
   874       if (_seq == 0) _seq = SequenceGenerator::next();
   875       MemTracker::write_tracking_record(addr, flags, size, _seq, pc, _java_thread);
   876       if (_op == ReserveAndCommit) {
   877         MemTracker::write_tracking_record(addr, orig_flags | MemPointerRecord::virtual_memory_commit_tag(),
   878           size, SequenceGenerator::next(), pc, _java_thread);
   879       }
   880     }
   881     _seq = 0;
   882   }
   883 }

mercurial