src/share/vm/runtime/sweeper.cpp

Thu, 19 Dec 2013 14:08:02 +0100

author
anoll
date
Thu, 19 Dec 2013 14:08:02 +0100
changeset 6207
6aa49042b101
parent 6205
908afcc9d1cb
child 6503
a9becfeecd1b
permissions
-rw-r--r--

8025277: Add -XX: flag to print code cache sweeper statistics
Summary: New diagnostic flag prints statistics about the code cache sweeper
Reviewed-by: kvn
Contributed-by: tobi.hartmann@gmail.com

     1 /*
     2  * Copyright (c) 1997, 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  */
    25 #include "precompiled.hpp"
    26 #include "code/codeCache.hpp"
    27 #include "code/compiledIC.hpp"
    28 #include "code/icBuffer.hpp"
    29 #include "code/nmethod.hpp"
    30 #include "compiler/compileBroker.hpp"
    31 #include "memory/resourceArea.hpp"
    32 #include "oops/method.hpp"
    33 #include "runtime/atomic.hpp"
    34 #include "runtime/compilationPolicy.hpp"
    35 #include "runtime/mutexLocker.hpp"
    36 #include "runtime/os.hpp"
    37 #include "runtime/sweeper.hpp"
    38 #include "runtime/vm_operations.hpp"
    39 #include "trace/tracing.hpp"
    40 #include "utilities/events.hpp"
    41 #include "utilities/ticks.inline.hpp"
    42 #include "utilities/xmlstream.hpp"
    44 #ifdef ASSERT
    46 #define SWEEP(nm) record_sweep(nm, __LINE__)
    47 // Sweeper logging code
    48 class SweeperRecord {
    49  public:
    50   int traversal;
    51   int invocation;
    52   int compile_id;
    53   long traversal_mark;
    54   int state;
    55   const char* kind;
    56   address vep;
    57   address uep;
    58   int line;
    60   void print() {
    61       tty->print_cr("traversal = %d invocation = %d compile_id = %d %s uep = " PTR_FORMAT " vep = "
    62                     PTR_FORMAT " state = %d traversal_mark %d line = %d",
    63                     traversal,
    64                     invocation,
    65                     compile_id,
    66                     kind == NULL ? "" : kind,
    67                     uep,
    68                     vep,
    69                     state,
    70                     traversal_mark,
    71                     line);
    72   }
    73 };
    75 static int _sweep_index = 0;
    76 static SweeperRecord* _records = NULL;
    78 void NMethodSweeper::report_events(int id, address entry) {
    79   if (_records != NULL) {
    80     for (int i = _sweep_index; i < SweeperLogEntries; i++) {
    81       if (_records[i].uep == entry ||
    82           _records[i].vep == entry ||
    83           _records[i].compile_id == id) {
    84         _records[i].print();
    85       }
    86     }
    87     for (int i = 0; i < _sweep_index; i++) {
    88       if (_records[i].uep == entry ||
    89           _records[i].vep == entry ||
    90           _records[i].compile_id == id) {
    91         _records[i].print();
    92       }
    93     }
    94   }
    95 }
    97 void NMethodSweeper::report_events() {
    98   if (_records != NULL) {
    99     for (int i = _sweep_index; i < SweeperLogEntries; i++) {
   100       // skip empty records
   101       if (_records[i].vep == NULL) continue;
   102       _records[i].print();
   103     }
   104     for (int i = 0; i < _sweep_index; i++) {
   105       // skip empty records
   106       if (_records[i].vep == NULL) continue;
   107       _records[i].print();
   108     }
   109   }
   110 }
   112 void NMethodSweeper::record_sweep(nmethod* nm, int line) {
   113   if (_records != NULL) {
   114     _records[_sweep_index].traversal = _traversals;
   115     _records[_sweep_index].traversal_mark = nm->_stack_traversal_mark;
   116     _records[_sweep_index].invocation = _sweep_fractions_left;
   117     _records[_sweep_index].compile_id = nm->compile_id();
   118     _records[_sweep_index].kind = nm->compile_kind();
   119     _records[_sweep_index].state = nm->_state;
   120     _records[_sweep_index].vep = nm->verified_entry_point();
   121     _records[_sweep_index].uep = nm->entry_point();
   122     _records[_sweep_index].line = line;
   123     _sweep_index = (_sweep_index + 1) % SweeperLogEntries;
   124   }
   125 }
   126 #else
   127 #define SWEEP(nm)
   128 #endif
   130 nmethod* NMethodSweeper::_current                      = NULL; // Current nmethod
   131 long     NMethodSweeper::_traversals                   = 0;    // Stack scan count, also sweep ID.
   132 long     NMethodSweeper::_total_nof_code_cache_sweeps  = 0;    // Total number of full sweeps of the code cache
   133 long     NMethodSweeper::_time_counter                 = 0;    // Virtual time used to periodically invoke sweeper
   134 long     NMethodSweeper::_last_sweep                   = 0;    // Value of _time_counter when the last sweep happened
   135 int      NMethodSweeper::_seen                         = 0;    // Nof. nmethod we have currently processed in current pass of CodeCache
   136 int      NMethodSweeper::_flushed_count                = 0;    // Nof. nmethods flushed in current sweep
   137 int      NMethodSweeper::_zombified_count              = 0;    // Nof. nmethods made zombie in current sweep
   138 int      NMethodSweeper::_marked_for_reclamation_count = 0;    // Nof. nmethods marked for reclaim in current sweep
   140 volatile bool NMethodSweeper::_should_sweep            = true; // Indicates if we should invoke the sweeper
   141 volatile int  NMethodSweeper::_sweep_fractions_left    = 0;    // Nof. invocations left until we are completed with this pass
   142 volatile int  NMethodSweeper::_sweep_started           = 0;    // Flag to control conc sweeper
   143 volatile int  NMethodSweeper::_bytes_changed           = 0;    // Counts the total nmethod size if the nmethod changed from:
   144                                                                //   1) alive       -> not_entrant
   145                                                                //   2) not_entrant -> zombie
   146                                                                //   3) zombie      -> marked_for_reclamation
   147 int    NMethodSweeper::_hotness_counter_reset_val       = 0;
   149 long   NMethodSweeper::_total_nof_methods_reclaimed     = 0;    // Accumulated nof methods flushed
   150 long   NMethodSweeper::_total_nof_c2_methods_reclaimed  = 0;    // Accumulated nof methods flushed
   151 size_t NMethodSweeper::_total_flushed_size              = 0;    // Total number of bytes flushed from the code cache
   152 Tickspan  NMethodSweeper::_total_time_sweeping;                 // Accumulated time sweeping
   153 Tickspan  NMethodSweeper::_total_time_this_sweep;               // Total time this sweep
   154 Tickspan  NMethodSweeper::_peak_sweep_time;                     // Peak time for a full sweep
   155 Tickspan  NMethodSweeper::_peak_sweep_fraction_time;            // Peak time sweeping one fraction
   159 class MarkActivationClosure: public CodeBlobClosure {
   160 public:
   161   virtual void do_code_blob(CodeBlob* cb) {
   162     if (cb->is_nmethod()) {
   163       nmethod* nm = (nmethod*)cb;
   164       nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
   165       // If we see an activation belonging to a non_entrant nmethod, we mark it.
   166       if (nm->is_not_entrant()) {
   167         nm->mark_as_seen_on_stack();
   168       }
   169     }
   170   }
   171 };
   172 static MarkActivationClosure mark_activation_closure;
   174 class SetHotnessClosure: public CodeBlobClosure {
   175 public:
   176   virtual void do_code_blob(CodeBlob* cb) {
   177     if (cb->is_nmethod()) {
   178       nmethod* nm = (nmethod*)cb;
   179       nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
   180     }
   181   }
   182 };
   183 static SetHotnessClosure set_hotness_closure;
   186 int NMethodSweeper::hotness_counter_reset_val() {
   187   if (_hotness_counter_reset_val == 0) {
   188     _hotness_counter_reset_val = (ReservedCodeCacheSize < M) ? 1 : (ReservedCodeCacheSize / M) * 2;
   189   }
   190   return _hotness_counter_reset_val;
   191 }
   192 bool NMethodSweeper::sweep_in_progress() {
   193   return (_current != NULL);
   194 }
   196 // Scans the stacks of all Java threads and marks activations of not-entrant methods.
   197 // No need to synchronize access, since 'mark_active_nmethods' is always executed at a
   198 // safepoint.
   199 void NMethodSweeper::mark_active_nmethods() {
   200   assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
   201   // If we do not want to reclaim not-entrant or zombie methods there is no need
   202   // to scan stacks
   203   if (!MethodFlushing) {
   204     return;
   205   }
   207   // Increase time so that we can estimate when to invoke the sweeper again.
   208   _time_counter++;
   210   // Check for restart
   211   assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
   212   if (!sweep_in_progress()) {
   213     _seen = 0;
   214     _sweep_fractions_left = NmethodSweepFraction;
   215     _current = CodeCache::first_nmethod();
   216     _traversals += 1;
   217     _total_time_this_sweep = Tickspan();
   219     if (PrintMethodFlushing) {
   220       tty->print_cr("### Sweep: stack traversal %d", _traversals);
   221     }
   222     Threads::nmethods_do(&mark_activation_closure);
   224   } else {
   225     // Only set hotness counter
   226     Threads::nmethods_do(&set_hotness_closure);
   227   }
   229   OrderAccess::storestore();
   230 }
   231 /**
   232  * This function invokes the sweeper if at least one of the three conditions is met:
   233  *    (1) The code cache is getting full
   234  *    (2) There are sufficient state changes in/since the last sweep.
   235  *    (3) We have not been sweeping for 'some time'
   236  */
   237 void NMethodSweeper::possibly_sweep() {
   238   assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode");
   239   // Only compiler threads are allowed to sweep
   240   if (!MethodFlushing || !sweep_in_progress() || !Thread::current()->is_Compiler_thread()) {
   241     return;
   242   }
   244   // If there was no state change while nmethod sweeping, 'should_sweep' will be false.
   245   // This is one of the two places where should_sweep can be set to true. The general
   246   // idea is as follows: If there is enough free space in the code cache, there is no
   247   // need to invoke the sweeper. The following formula (which determines whether to invoke
   248   // the sweeper or not) depends on the assumption that for larger ReservedCodeCacheSizes
   249   // we need less frequent sweeps than for smaller ReservedCodecCacheSizes. Furthermore,
   250   // the formula considers how much space in the code cache is currently used. Here are
   251   // some examples that will (hopefully) help in understanding.
   252   //
   253   // Small ReservedCodeCacheSizes:  (e.g., < 16M) We invoke the sweeper every time, since
   254   //                                              the result of the division is 0. This
   255   //                                              keeps the used code cache size small
   256   //                                              (important for embedded Java)
   257   // Large ReservedCodeCacheSize :  (e.g., 256M + code cache is 10% full). The formula
   258   //                                              computes: (256 / 16) - 1 = 15
   259   //                                              As a result, we invoke the sweeper after
   260   //                                              15 invocations of 'mark_active_nmethods.
   261   // Large ReservedCodeCacheSize:   (e.g., 256M + code Cache is 90% full). The formula
   262   //                                              computes: (256 / 16) - 10 = 6.
   263   if (!_should_sweep) {
   264     const int time_since_last_sweep = _time_counter - _last_sweep;
   265     // ReservedCodeCacheSize has an 'unsigned' type. We need a 'signed' type for max_wait_time,
   266     // since 'time_since_last_sweep' can be larger than 'max_wait_time'. If that happens using
   267     // an unsigned type would cause an underflow (wait_until_next_sweep becomes a large positive
   268     // value) that disables the intended periodic sweeps.
   269     const int max_wait_time = ReservedCodeCacheSize / (16 * M);
   270     double wait_until_next_sweep = max_wait_time - time_since_last_sweep - CodeCache::reverse_free_ratio();
   271     assert(wait_until_next_sweep <= (double)max_wait_time, "Calculation of code cache sweeper interval is incorrect");
   273     if ((wait_until_next_sweep <= 0.0) || !CompileBroker::should_compile_new_jobs()) {
   274       _should_sweep = true;
   275     }
   276   }
   278   if (_should_sweep && _sweep_fractions_left > 0) {
   279     // Only one thread at a time will sweep
   280     jint old = Atomic::cmpxchg( 1, &_sweep_started, 0 );
   281     if (old != 0) {
   282       return;
   283     }
   284 #ifdef ASSERT
   285     if (LogSweeper && _records == NULL) {
   286       // Create the ring buffer for the logging code
   287       _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
   288       memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
   289     }
   290 #endif
   292     if (_sweep_fractions_left > 0) {
   293       sweep_code_cache();
   294       _sweep_fractions_left--;
   295     }
   297     // We are done with sweeping the code cache once.
   298     if (_sweep_fractions_left == 0) {
   299       _total_nof_code_cache_sweeps++;
   300       _last_sweep = _time_counter;
   301       // Reset flag; temporarily disables sweeper
   302       _should_sweep = false;
   303       // If there was enough state change, 'possibly_enable_sweeper()'
   304       // sets '_should_sweep' to true
   305       possibly_enable_sweeper();
   306       // Reset _bytes_changed only if there was enough state change. _bytes_changed
   307       // can further increase by calls to 'report_state_change'.
   308       if (_should_sweep) {
   309         _bytes_changed = 0;
   310       }
   311     }
   312     _sweep_started = 0;
   313   }
   314 }
   316 void NMethodSweeper::sweep_code_cache() {
   317   Ticks sweep_start_counter = Ticks::now();
   319   _flushed_count                = 0;
   320   _zombified_count              = 0;
   321   _marked_for_reclamation_count = 0;
   323   if (PrintMethodFlushing && Verbose) {
   324     tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left);
   325   }
   327   if (!CompileBroker::should_compile_new_jobs()) {
   328     // If we have turned off compilations we might as well do full sweeps
   329     // in order to reach the clean state faster. Otherwise the sleeping compiler
   330     // threads will slow down sweeping.
   331     _sweep_fractions_left = 1;
   332   }
   334   // We want to visit all nmethods after NmethodSweepFraction
   335   // invocations so divide the remaining number of nmethods by the
   336   // remaining number of invocations.  This is only an estimate since
   337   // the number of nmethods changes during the sweep so the final
   338   // stage must iterate until it there are no more nmethods.
   339   int todo = (CodeCache::nof_nmethods() - _seen) / _sweep_fractions_left;
   340   int swept_count = 0;
   343   assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here");
   344   assert(!CodeCache_lock->owned_by_self(), "just checking");
   346   int freed_memory = 0;
   347   {
   348     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   350     // The last invocation iterates until there are no more nmethods
   351     for (int i = 0; (i < todo || _sweep_fractions_left == 1) && _current != NULL; i++) {
   352       swept_count++;
   353       if (SafepointSynchronize::is_synchronizing()) { // Safepoint request
   354         if (PrintMethodFlushing && Verbose) {
   355           tty->print_cr("### Sweep at %d out of %d, invocation: %d, yielding to safepoint", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left);
   356         }
   357         MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   359         assert(Thread::current()->is_Java_thread(), "should be java thread");
   360         JavaThread* thread = (JavaThread*)Thread::current();
   361         ThreadBlockInVM tbivm(thread);
   362         thread->java_suspend_self();
   363       }
   364       // Since we will give up the CodeCache_lock, always skip ahead
   365       // to the next nmethod.  Other blobs can be deleted by other
   366       // threads but nmethods are only reclaimed by the sweeper.
   367       nmethod* next = CodeCache::next_nmethod(_current);
   369       // Now ready to process nmethod and give up CodeCache_lock
   370       {
   371         MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   372         freed_memory += process_nmethod(_current);
   373       }
   374       _seen++;
   375       _current = next;
   376     }
   377   }
   379   assert(_sweep_fractions_left > 1 || _current == NULL, "must have scanned the whole cache");
   381   const Ticks sweep_end_counter = Ticks::now();
   382   const Tickspan sweep_time = sweep_end_counter - sweep_start_counter;
   383   _total_time_sweeping  += sweep_time;
   384   _total_time_this_sweep += sweep_time;
   385   _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
   386   _total_flushed_size += freed_memory;
   387   _total_nof_methods_reclaimed += _flushed_count;
   389   EventSweepCodeCache event(UNTIMED);
   390   if (event.should_commit()) {
   391     event.set_starttime(sweep_start_counter);
   392     event.set_endtime(sweep_end_counter);
   393     event.set_sweepIndex(_traversals);
   394     event.set_sweepFractionIndex(NmethodSweepFraction - _sweep_fractions_left + 1);
   395     event.set_sweptCount(swept_count);
   396     event.set_flushedCount(_flushed_count);
   397     event.set_markedCount(_marked_for_reclamation_count);
   398     event.set_zombifiedCount(_zombified_count);
   399     event.commit();
   400   }
   402 #ifdef ASSERT
   403   if(PrintMethodFlushing) {
   404     tty->print_cr("### sweeper:      sweep time(%d): "
   405       INT64_FORMAT, _sweep_fractions_left, (jlong)sweep_time.value());
   406   }
   407 #endif
   409   if (_sweep_fractions_left == 1) {
   410     _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep);
   411     log_sweep("finished");
   412   }
   414   // Sweeper is the only case where memory is released, check here if it
   415   // is time to restart the compiler. Only checking if there is a certain
   416   // amount of free memory in the code cache might lead to re-enabling
   417   // compilation although no memory has been released. For example, there are
   418   // cases when compilation was disabled although there is 4MB (or more) free
   419   // memory in the code cache. The reason is code cache fragmentation. Therefore,
   420   // it only makes sense to re-enable compilation if we have actually freed memory.
   421   // Note that typically several kB are released for sweeping 16MB of the code
   422   // cache. As a result, 'freed_memory' > 0 to restart the compiler.
   423   if (!CompileBroker::should_compile_new_jobs() && (freed_memory > 0)) {
   424     CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
   425     log_sweep("restart_compiler");
   426   }
   427 }
   429 /**
   430  * This function updates the sweeper statistics that keep track of nmethods
   431  * state changes. If there is 'enough' state change, the sweeper is invoked
   432  * as soon as possible. There can be data races on _bytes_changed. The data
   433  * races are benign, since it does not matter if we loose a couple of bytes.
   434  * In the worst case we call the sweeper a little later. Also, we are guaranteed
   435  * to invoke the sweeper if the code cache gets full.
   436  */
   437 void NMethodSweeper::report_state_change(nmethod* nm) {
   438   _bytes_changed += nm->total_size();
   439   possibly_enable_sweeper();
   440 }
   442 /**
   443  * Function determines if there was 'enough' state change in the code cache to invoke
   444  * the sweeper again. Currently, we determine 'enough' as more than 1% state change in
   445  * the code cache since the last sweep.
   446  */
   447 void NMethodSweeper::possibly_enable_sweeper() {
   448   double percent_changed = ((double)_bytes_changed / (double)ReservedCodeCacheSize) * 100;
   449   if (percent_changed > 1.0) {
   450     _should_sweep = true;
   451   }
   452 }
   454 class NMethodMarker: public StackObj {
   455  private:
   456   CompilerThread* _thread;
   457  public:
   458   NMethodMarker(nmethod* nm) {
   459     _thread = CompilerThread::current();
   460     if (!nm->is_zombie() && !nm->is_unloaded()) {
   461       // Only expose live nmethods for scanning
   462       _thread->set_scanned_nmethod(nm);
   463     }
   464   }
   465   ~NMethodMarker() {
   466     _thread->set_scanned_nmethod(NULL);
   467   }
   468 };
   470 void NMethodSweeper::release_nmethod(nmethod *nm) {
   471   // Clean up any CompiledICHolders
   472   {
   473     ResourceMark rm;
   474     MutexLocker ml_patch(CompiledIC_lock);
   475     RelocIterator iter(nm);
   476     while (iter.next()) {
   477       if (iter.type() == relocInfo::virtual_call_type) {
   478         CompiledIC::cleanup_call_site(iter.virtual_call_reloc());
   479       }
   480     }
   481   }
   483   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   484   nm->flush();
   485 }
   487 int NMethodSweeper::process_nmethod(nmethod *nm) {
   488   assert(!CodeCache_lock->owned_by_self(), "just checking");
   490   int freed_memory = 0;
   491   // Make sure this nmethod doesn't get unloaded during the scan,
   492   // since safepoints may happen during acquired below locks.
   493   NMethodMarker nmm(nm);
   494   SWEEP(nm);
   496   // Skip methods that are currently referenced by the VM
   497   if (nm->is_locked_by_vm()) {
   498     // But still remember to clean-up inline caches for alive nmethods
   499     if (nm->is_alive()) {
   500       // Clean inline caches that point to zombie/non-entrant methods
   501       MutexLocker cl(CompiledIC_lock);
   502       nm->cleanup_inline_caches();
   503       SWEEP(nm);
   504     }
   505     return freed_memory;
   506   }
   508   if (nm->is_zombie()) {
   509     // If it is the first time we see nmethod then we mark it. Otherwise,
   510     // we reclaim it. When we have seen a zombie method twice, we know that
   511     // there are no inline caches that refer to it.
   512     if (nm->is_marked_for_reclamation()) {
   513       assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
   514       if (PrintMethodFlushing && Verbose) {
   515         tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
   516       }
   517       freed_memory = nm->total_size();
   518       if (nm->is_compiled_by_c2()) {
   519         _total_nof_c2_methods_reclaimed++;
   520       }
   521       release_nmethod(nm);
   522       _flushed_count++;
   523     } else {
   524       if (PrintMethodFlushing && Verbose) {
   525         tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
   526       }
   527       nm->mark_for_reclamation();
   528       // Keep track of code cache state change
   529       _bytes_changed += nm->total_size();
   530       _marked_for_reclamation_count++;
   531       SWEEP(nm);
   532     }
   533   } else if (nm->is_not_entrant()) {
   534     // If there are no current activations of this method on the
   535     // stack we can safely convert it to a zombie method
   536     if (nm->can_not_entrant_be_converted()) {
   537       if (PrintMethodFlushing && Verbose) {
   538         tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
   539       }
   540       // Code cache state change is tracked in make_zombie()
   541       nm->make_zombie();
   542       _zombified_count++;
   543       SWEEP(nm);
   544     } else {
   545       // Still alive, clean up its inline caches
   546       MutexLocker cl(CompiledIC_lock);
   547       nm->cleanup_inline_caches();
   548       SWEEP(nm);
   549     }
   550   } else if (nm->is_unloaded()) {
   551     // Unloaded code, just make it a zombie
   552     if (PrintMethodFlushing && Verbose) {
   553       tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm);
   554     }
   555     if (nm->is_osr_method()) {
   556       SWEEP(nm);
   557       // No inline caches will ever point to osr methods, so we can just remove it
   558       freed_memory = nm->total_size();
   559       if (nm->is_compiled_by_c2()) {
   560         _total_nof_c2_methods_reclaimed++;
   561       }
   562       release_nmethod(nm);
   563       _flushed_count++;
   564     } else {
   565       // Code cache state change is tracked in make_zombie()
   566       nm->make_zombie();
   567       _zombified_count++;
   568       SWEEP(nm);
   569     }
   570   } else {
   571     if (UseCodeCacheFlushing) {
   572       if (!nm->is_locked_by_vm() && !nm->is_osr_method() && !nm->is_native_method()) {
   573         // Do not make native methods and OSR-methods not-entrant
   574         nm->dec_hotness_counter();
   575         // Get the initial value of the hotness counter. This value depends on the
   576         // ReservedCodeCacheSize
   577         int reset_val = hotness_counter_reset_val();
   578         int time_since_reset = reset_val - nm->hotness_counter();
   579         double threshold = -reset_val + (CodeCache::reverse_free_ratio() * NmethodSweepActivity);
   580         // The less free space in the code cache we have - the bigger reverse_free_ratio() is.
   581         // I.e., 'threshold' increases with lower available space in the code cache and a higher
   582         // NmethodSweepActivity. If the current hotness counter - which decreases from its initial
   583         // value until it is reset by stack walking - is smaller than the computed threshold, the
   584         // corresponding nmethod is considered for removal.
   585         if ((NmethodSweepActivity > 0) && (nm->hotness_counter() < threshold) && (time_since_reset > 10)) {
   586           // A method is marked as not-entrant if the method is
   587           // 1) 'old enough': nm->hotness_counter() < threshold
   588           // 2) The method was in_use for a minimum amount of time: (time_since_reset > 10)
   589           //    The second condition is necessary if we are dealing with very small code cache
   590           //    sizes (e.g., <10m) and the code cache size is too small to hold all hot methods.
   591           //    The second condition ensures that methods are not immediately made not-entrant
   592           //    after compilation.
   593           nm->make_not_entrant();
   594           // Code cache state change is tracked in make_not_entrant()
   595           if (PrintMethodFlushing && Verbose) {
   596             tty->print_cr("### Nmethod %d/" PTR_FORMAT "made not-entrant: hotness counter %d/%d threshold %f",
   597                           nm->compile_id(), nm, nm->hotness_counter(), reset_val, threshold);
   598           }
   599         }
   600       }
   601     }
   602     // Clean-up all inline caches that point to zombie/non-reentrant methods
   603     MutexLocker cl(CompiledIC_lock);
   604     nm->cleanup_inline_caches();
   605     SWEEP(nm);
   606   }
   607   return freed_memory;
   608 }
   610 // Print out some state information about the current sweep and the
   611 // state of the code cache if it's requested.
   612 void NMethodSweeper::log_sweep(const char* msg, const char* format, ...) {
   613   if (PrintMethodFlushing) {
   614     stringStream s;
   615     // Dump code cache state into a buffer before locking the tty,
   616     // because log_state() will use locks causing lock conflicts.
   617     CodeCache::log_state(&s);
   619     ttyLocker ttyl;
   620     tty->print("### sweeper: %s ", msg);
   621     if (format != NULL) {
   622       va_list ap;
   623       va_start(ap, format);
   624       tty->vprint(format, ap);
   625       va_end(ap);
   626     }
   627     tty->print_cr(s.as_string());
   628   }
   630   if (LogCompilation && (xtty != NULL)) {
   631     stringStream s;
   632     // Dump code cache state into a buffer before locking the tty,
   633     // because log_state() will use locks causing lock conflicts.
   634     CodeCache::log_state(&s);
   636     ttyLocker ttyl;
   637     xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count());
   638     if (format != NULL) {
   639       va_list ap;
   640       va_start(ap, format);
   641       xtty->vprint(format, ap);
   642       va_end(ap);
   643     }
   644     xtty->print(s.as_string());
   645     xtty->stamp();
   646     xtty->end_elem();
   647   }
   648 }
   650 void NMethodSweeper::print() {
   651   ttyLocker ttyl;
   652   tty->print_cr("Code cache sweeper statistics:");
   653   tty->print_cr("  Total sweep time:                %1.0lfms", (double)_total_time_sweeping.value()/1000000);
   654   tty->print_cr("  Total number of full sweeps:     %ld", _total_nof_code_cache_sweeps);
   655   tty->print_cr("  Total number of flushed methods: %ld(%ld C2 methods)", _total_nof_methods_reclaimed,
   656                                                     _total_nof_c2_methods_reclaimed);
   657   tty->print_cr("  Total size of flushed methods:   " SIZE_FORMAT "kB", _total_flushed_size/K);
   658 }

mercurial