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