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