rev 57079 : imported patch 8234796

   1 /*
   2  * Copyright (c) 1997, 2019, 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 "gc/shared/collectedHeap.hpp"
  32 #include "gc/shared/workgroup.hpp"
  33 #include "jfr/jfrEvents.hpp"
  34 #include "logging/log.hpp"
  35 #include "logging/logStream.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.hpp"
  39 #include "oops/method.hpp"
  40 #include "runtime/atomic.hpp"
  41 #include "runtime/interfaceSupport.inline.hpp"
  42 #include "runtime/handshake.hpp"
  43 #include "runtime/mutexLocker.hpp"
  44 #include "runtime/orderAccess.hpp"
  45 #include "runtime/os.hpp"
  46 #include "runtime/sweeper.hpp"
  47 #include "runtime/thread.inline.hpp"
  48 #include "runtime/vmOperations.hpp"
  49 #include "runtime/vmThread.hpp"
  50 #include "utilities/events.hpp"
  51 #include "utilities/xmlstream.hpp"
  52 
  53 #ifdef ASSERT
  54 
  55 #define SWEEP(nm) record_sweep(nm, __LINE__)
  56 // Sweeper logging code
  57 class SweeperRecord {
  58  public:
  59   int traversal;
  60   int compile_id;
  61   long traversal_mark;
  62   int state;
  63   const char* kind;
  64   address vep;
  65   address uep;
  66   int line;
  67 
  68   void print() {
  69       tty->print_cr("traversal = %d compile_id = %d %s uep = " PTR_FORMAT " vep = "
  70                     PTR_FORMAT " state = %d traversal_mark %ld line = %d",
  71                     traversal,
  72                     compile_id,
  73                     kind == NULL ? "" : kind,
  74                     p2i(uep),
  75                     p2i(vep),
  76                     state,
  77                     traversal_mark,
  78                     line);
  79   }
  80 };
  81 
  82 static int _sweep_index = 0;
  83 static SweeperRecord* _records = NULL;
  84 
  85 void NMethodSweeper::report_events(int id, address entry) {
  86   if (_records != NULL) {
  87     for (int i = _sweep_index; i < SweeperLogEntries; 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     for (int i = 0; i < _sweep_index; i++) {
  95       if (_records[i].uep == entry ||
  96           _records[i].vep == entry ||
  97           _records[i].compile_id == id) {
  98         _records[i].print();
  99       }
 100     }
 101   }
 102 }
 103 
 104 void NMethodSweeper::report_events() {
 105   if (_records != NULL) {
 106     for (int i = _sweep_index; i < SweeperLogEntries; i++) {
 107       // skip empty records
 108       if (_records[i].vep == NULL) continue;
 109       _records[i].print();
 110     }
 111     for (int i = 0; i < _sweep_index; i++) {
 112       // skip empty records
 113       if (_records[i].vep == NULL) continue;
 114       _records[i].print();
 115     }
 116   }
 117 }
 118 
 119 void NMethodSweeper::record_sweep(CompiledMethod* nm, int line) {
 120   if (_records != NULL) {
 121     _records[_sweep_index].traversal = _traversals;
 122     _records[_sweep_index].traversal_mark = nm->is_nmethod() ? ((nmethod*)nm)->stack_traversal_mark() : 0;
 123     _records[_sweep_index].compile_id = nm->compile_id();
 124     _records[_sweep_index].kind = nm->compile_kind();
 125     _records[_sweep_index].state = nm->get_state();
 126     _records[_sweep_index].vep = nm->verified_entry_point();
 127     _records[_sweep_index].uep = nm->entry_point();
 128     _records[_sweep_index].line = line;
 129     _sweep_index = (_sweep_index + 1) % SweeperLogEntries;
 130   }
 131 }
 132 
 133 void NMethodSweeper::init_sweeper_log() {
 134  if (LogSweeper && _records == NULL) {
 135    // Create the ring buffer for the logging code
 136    _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
 137    memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
 138   }
 139 }
 140 #else
 141 #define SWEEP(nm)
 142 #endif
 143 
 144 CompiledMethodIterator NMethodSweeper::_current(CompiledMethodIterator::all_blobs); // Current compiled method
 145 long     NMethodSweeper::_traversals                   = 0;    // Stack scan count, also sweep ID.
 146 long     NMethodSweeper::_total_nof_code_cache_sweeps  = 0;    // Total number of full sweeps of the code cache
 147 long     NMethodSweeper::_time_counter                 = 0;    // Virtual time used to periodically invoke sweeper
 148 long     NMethodSweeper::_last_sweep                   = 0;    // Value of _time_counter when the last sweep happened
 149 int      NMethodSweeper::_seen                         = 0;    // Nof. nmethod we have currently processed in current pass of CodeCache
 150 
 151 volatile bool NMethodSweeper::_should_sweep            = false;// Indicates if we should invoke the sweeper
 152 volatile bool NMethodSweeper::_force_sweep             = false;// Indicates if we should force a sweep
 153 volatile int  NMethodSweeper::_bytes_changed           = 0;    // Counts the total nmethod size if the nmethod changed from:
 154                                                                //   1) alive       -> not_entrant
 155                                                                //   2) not_entrant -> zombie
 156 int    NMethodSweeper::_hotness_counter_reset_val       = 0;
 157 
 158 long   NMethodSweeper::_total_nof_methods_reclaimed     = 0;   // Accumulated nof methods flushed
 159 long   NMethodSweeper::_total_nof_c2_methods_reclaimed  = 0;   // Accumulated nof methods flushed
 160 size_t NMethodSweeper::_total_flushed_size              = 0;   // Total number of bytes flushed from the code cache
 161 Tickspan NMethodSweeper::_total_time_sweeping;                 // Accumulated time sweeping
 162 Tickspan NMethodSweeper::_total_time_this_sweep;               // Total time this sweep
 163 Tickspan NMethodSweeper::_peak_sweep_time;                     // Peak time for a full sweep
 164 Tickspan NMethodSweeper::_peak_sweep_fraction_time;            // Peak time sweeping one fraction
 165 
 166 class MarkActivationClosure: public CodeBlobClosure {
 167 public:
 168   virtual void do_code_blob(CodeBlob* cb) {
 169     assert(cb->is_nmethod(), "CodeBlob should be nmethod");
 170     nmethod* nm = (nmethod*)cb;
 171     nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
 172     // If we see an activation belonging to a non_entrant nmethod, we mark it.
 173     if (nm->is_not_entrant()) {
 174       nm->mark_as_seen_on_stack();
 175     }
 176   }
 177 };
 178 static MarkActivationClosure mark_activation_closure;
 179 
 180 class SetHotnessClosure: public CodeBlobClosure {
 181 public:
 182   virtual void do_code_blob(CodeBlob* cb) {
 183     assert(cb->is_nmethod(), "CodeBlob should be nmethod");
 184     nmethod* nm = (nmethod*)cb;
 185     nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
 186   }
 187 };
 188 static SetHotnessClosure set_hotness_closure;
 189 
 190 
 191 int NMethodSweeper::hotness_counter_reset_val() {
 192   if (_hotness_counter_reset_val == 0) {
 193     _hotness_counter_reset_val = (ReservedCodeCacheSize < M) ? 1 : (ReservedCodeCacheSize / M) * 2;
 194   }
 195   return _hotness_counter_reset_val;
 196 }
 197 bool NMethodSweeper::wait_for_stack_scanning() {
 198   return _current.end();
 199 }
 200 
 201 class NMethodMarkingThreadClosure : public ThreadClosure {
 202 private:
 203   CodeBlobClosure* _cl;
 204 public:
 205   NMethodMarkingThreadClosure(CodeBlobClosure* cl) : _cl(cl) {}
 206   void do_thread(Thread* thread) {
 207     if (thread->is_Java_thread() && ! thread->is_Code_cache_sweeper_thread()) {
 208       JavaThread* jt = (JavaThread*) thread;
 209       jt->nmethods_do(_cl);
 210     }
 211   }
 212 };
 213 
 214 class NMethodMarkingTask : public AbstractGangTask {
 215 private:
 216   NMethodMarkingThreadClosure* _cl;
 217 public:
 218   NMethodMarkingTask(NMethodMarkingThreadClosure* cl) :
 219     AbstractGangTask("Parallel NMethod Marking"),
 220     _cl(cl) {
 221     Threads::change_thread_claim_token();
 222   }
 223 
 224   ~NMethodMarkingTask() {
 225     Threads::assert_all_threads_claimed();
 226   }
 227 
 228   void work(uint worker_id) {
 229     Threads::possibly_parallel_threads_do(true, _cl);
 230   }
 231 };
 232 
 233 /**
 234   * Scans the stacks of all Java threads and marks activations of not-entrant methods.
 235   * No need to synchronize access, since 'mark_active_nmethods' is always executed at a
 236   * safepoint.
 237   */
 238 void NMethodSweeper::mark_active_nmethods() {
 239   CodeBlobClosure* cl = prepare_mark_active_nmethods();
 240   if (cl != NULL) {
 241     WorkGang* workers = Universe::heap()->get_safepoint_workers();
 242     if (workers != NULL) {
 243       NMethodMarkingThreadClosure tcl(cl);
 244       NMethodMarkingTask task(&tcl);
 245       workers->run_task(&task);
 246     } else {
 247       Threads::nmethods_do(cl);
 248     }
 249   }
 250 }
 251 
 252 CodeBlobClosure* NMethodSweeper::prepare_mark_active_nmethods() {
 253 #ifdef ASSERT
 254   if (ThreadLocalHandshakes) {
 255     assert(Thread::current()->is_Code_cache_sweeper_thread(), "must be executed under CodeCache_lock and in sweeper thread");
 256     assert_lock_strong(CodeCache_lock);
 257   } else {
 258     assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
 259   }
 260 #endif
 261 
 262   // If we do not want to reclaim not-entrant or zombie methods there is no need
 263   // to scan stacks
 264   if (!MethodFlushing) {
 265     return NULL;
 266   }
 267 
 268   // Increase time so that we can estimate when to invoke the sweeper again.
 269   _time_counter++;
 270 
 271   // Check for restart
 272   assert(_current.method() == NULL, "should only happen between sweeper cycles");
 273   assert(wait_for_stack_scanning(), "should only happen between sweeper cycles");
 274 
 275   _seen = 0;
 276   _current = CompiledMethodIterator(CompiledMethodIterator::all_blobs);
 277   // Initialize to first nmethod
 278   _current.next();
 279   _traversals += 1;
 280   _total_time_this_sweep = Tickspan();
 281 
 282   if (PrintMethodFlushing) {
 283     tty->print_cr("### Sweep: stack traversal %ld", _traversals);
 284   }
 285   return &mark_activation_closure;
 286 }
 287 
 288 CodeBlobClosure* NMethodSweeper::prepare_reset_hotness_counters() {
 289   assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
 290 
 291   // If we do not want to reclaim not-entrant or zombie methods there is no need
 292   // to scan stacks
 293   if (!MethodFlushing) {
 294     return NULL;
 295   }
 296 
 297   // Increase time so that we can estimate when to invoke the sweeper again.
 298   _time_counter++;
 299 
 300   // Check for restart
 301   if (_current.method() != NULL) {
 302     if (_current.method()->is_nmethod()) {
 303       assert(CodeCache::find_blob_unsafe(_current.method()) == _current.method(), "Sweeper nmethod cached state invalid");
 304     } else if (_current.method()->is_aot()) {
 305       assert(CodeCache::find_blob_unsafe(_current.method()->code_begin()) == _current.method(), "Sweeper AOT method cached state invalid");
 306     } else {
 307       ShouldNotReachHere();
 308     }
 309   }
 310 
 311   return &set_hotness_closure;
 312 }
 313 
 314 class NMethodMarkingHandshake : public HandshakeOperation {
 315   NMethodMarkingThreadClosure* _cl;
 316  public:
 317   NMethodMarkingHandshake(NMethodMarkingThreadClosure* cl) : HandshakeOperation("NMethodMarking"), _cl(cl) {}
 318   void do_thread(JavaThread* jt) {
 319     _cl->do_thread(jt);
 320   }
 321 };
 322 
 323 /**
 324   * This function triggers a VM operation that does stack scanning of active
 325   * methods. Stack scanning is mandatory for the sweeper to make progress.
 326   */
 327 void NMethodSweeper::do_stack_scanning() {
 328   assert(!CodeCache_lock->owned_by_self(), "just checking");
 329   if (wait_for_stack_scanning()) {
 330     if (ThreadLocalHandshakes) {
 331       CodeBlobClosure* code_cl;
 332       {
 333         MutexLocker ccl(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 334         code_cl = prepare_mark_active_nmethods();
 335       }
 336       if (code_cl != NULL) {
 337         NMethodMarkingThreadClosure tcl(code_cl);
 338         NMethodMarkingHandshake nm_hs(&tcl);
 339         Handshake::execute(&nm_hs);
 340       }
 341     } else {
 342       VM_MarkActiveNMethods op;
 343       VMThread::execute(&op);
 344     }
 345   }
 346 }
 347 
 348 void NMethodSweeper::sweeper_loop() {
 349   bool timeout;
 350   while (true) {
 351     {
 352       ThreadBlockInVM tbivm(JavaThread::current());
 353       MonitorLocker waiter(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 354       const long wait_time = 60*60*24 * 1000;
 355       timeout = waiter.wait(wait_time);
 356     }
 357     if (!timeout) {
 358       possibly_sweep();
 359     }
 360   }
 361 }
 362 
 363 /**
 364   * Wakes up the sweeper thread to possibly sweep.
 365   */
 366 void NMethodSweeper::notify(int code_blob_type) {
 367   // Makes sure that we do not invoke the sweeper too often during startup.
 368   double start_threshold = 100.0 / (double)StartAggressiveSweepingAt;
 369   double aggressive_sweep_threshold = MIN2(start_threshold, 1.1);
 370   if (CodeCache::reverse_free_ratio(code_blob_type) >= aggressive_sweep_threshold) {
 371     assert_locked_or_safepoint(CodeCache_lock);
 372     CodeCache_lock->notify();
 373   }
 374 }
 375 
 376 /**
 377   * Wakes up the sweeper thread and forces a sweep. Blocks until it finished.
 378   */
 379 void NMethodSweeper::force_sweep() {
 380   ThreadBlockInVM tbivm(JavaThread::current());
 381   MonitorLocker waiter(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 382   // Request forced sweep
 383   _force_sweep = true;
 384   while (_force_sweep) {
 385     // Notify sweeper that we want to force a sweep and wait for completion.
 386     // In case a sweep currently takes place we timeout and try again because
 387     // we want to enforce a full sweep.
 388     CodeCache_lock->notify();
 389     waiter.wait(1000);
 390   }
 391 }
 392 
 393 /**
 394  * Handle a safepoint request
 395  */
 396 void NMethodSweeper::handle_safepoint_request() {
 397   JavaThread* thread = JavaThread::current();
 398   if (SafepointMechanism::should_block(thread)) {
 399     if (PrintMethodFlushing && Verbose) {
 400       tty->print_cr("### Sweep at %d out of %d, yielding to safepoint", _seen, CodeCache::nmethod_count());
 401     }
 402     MutexUnlocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 403 
 404     ThreadBlockInVM tbivm(thread);
 405     thread->java_suspend_self();
 406   }
 407 }
 408 
 409 /**
 410  * This function invokes the sweeper if at least one of the three conditions is met:
 411  *    (1) The code cache is getting full
 412  *    (2) There are sufficient state changes in/since the last sweep.
 413  *    (3) We have not been sweeping for 'some time'
 414  */
 415 void NMethodSweeper::possibly_sweep() {
 416   assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode");
 417   // If there was no state change while nmethod sweeping, 'should_sweep' will be false.
 418   // This is one of the two places where should_sweep can be set to true. The general
 419   // idea is as follows: If there is enough free space in the code cache, there is no
 420   // need to invoke the sweeper. The following formula (which determines whether to invoke
 421   // the sweeper or not) depends on the assumption that for larger ReservedCodeCacheSizes
 422   // we need less frequent sweeps than for smaller ReservedCodecCacheSizes. Furthermore,
 423   // the formula considers how much space in the code cache is currently used. Here are
 424   // some examples that will (hopefully) help in understanding.
 425   //
 426   // Small ReservedCodeCacheSizes:  (e.g., < 16M) We invoke the sweeper every time, since
 427   //                                              the result of the division is 0. This
 428   //                                              keeps the used code cache size small
 429   //                                              (important for embedded Java)
 430   // Large ReservedCodeCacheSize :  (e.g., 256M + code cache is 10% full). The formula
 431   //                                              computes: (256 / 16) - 1 = 15
 432   //                                              As a result, we invoke the sweeper after
 433   //                                              15 invocations of 'mark_active_nmethods.
 434   // Large ReservedCodeCacheSize:   (e.g., 256M + code Cache is 90% full). The formula
 435   //                                              computes: (256 / 16) - 10 = 6.
 436   if (!_should_sweep) {
 437     const int time_since_last_sweep = _time_counter - _last_sweep;
 438     // ReservedCodeCacheSize has an 'unsigned' type. We need a 'signed' type for max_wait_time,
 439     // since 'time_since_last_sweep' can be larger than 'max_wait_time'. If that happens using
 440     // an unsigned type would cause an underflow (wait_until_next_sweep becomes a large positive
 441     // value) that disables the intended periodic sweeps.
 442     const int max_wait_time = ReservedCodeCacheSize / (16 * M);
 443     double wait_until_next_sweep = max_wait_time - time_since_last_sweep -
 444         MAX2(CodeCache::reverse_free_ratio(CodeBlobType::MethodProfiled),
 445              CodeCache::reverse_free_ratio(CodeBlobType::MethodNonProfiled));
 446     assert(wait_until_next_sweep <= (double)max_wait_time, "Calculation of code cache sweeper interval is incorrect");
 447 
 448     if ((wait_until_next_sweep <= 0.0) || !CompileBroker::should_compile_new_jobs()) {
 449       _should_sweep = true;
 450     }
 451   }
 452 
 453   // Remember if this was a forced sweep
 454   bool forced = _force_sweep;
 455 
 456   // Force stack scanning if there is only 10% free space in the code cache.
 457   // We force stack scanning only if the non-profiled code heap gets full, since critical
 458   // allocations go to the non-profiled heap and we must be make sure that there is
 459   // enough space.
 460   double free_percent = 1 / CodeCache::reverse_free_ratio(CodeBlobType::MethodNonProfiled) * 100;
 461   if (free_percent <= StartAggressiveSweepingAt || forced || _should_sweep) {
 462     do_stack_scanning();
 463   }
 464 
 465   if (_should_sweep || forced) {
 466     init_sweeper_log();
 467     sweep_code_cache();
 468   }
 469 
 470   // We are done with sweeping the code cache once.
 471   _total_nof_code_cache_sweeps++;
 472   _last_sweep = _time_counter;
 473   // Reset flag; temporarily disables sweeper
 474   _should_sweep = false;
 475   // If there was enough state change, 'possibly_enable_sweeper()'
 476   // sets '_should_sweep' to true
 477   possibly_enable_sweeper();
 478   // Reset _bytes_changed only if there was enough state change. _bytes_changed
 479   // can further increase by calls to 'report_state_change'.
 480   if (_should_sweep) {
 481     _bytes_changed = 0;
 482   }
 483 
 484   if (forced) {
 485     // Notify requester that forced sweep finished
 486     assert(_force_sweep, "Should be a forced sweep");
 487     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 488     _force_sweep = false;
 489     CodeCache_lock->notify();
 490   }
 491 }
 492 
 493 static void post_sweep_event(EventSweepCodeCache* event,
 494                              const Ticks& start,
 495                              const Ticks& end,
 496                              s4 traversals,
 497                              int swept,
 498                              int flushed,
 499                              int zombified) {
 500   assert(event != NULL, "invariant");
 501   assert(event->should_commit(), "invariant");
 502   event->set_starttime(start);
 503   event->set_endtime(end);
 504   event->set_sweepId(traversals);
 505   event->set_sweptCount(swept);
 506   event->set_flushedCount(flushed);
 507   event->set_zombifiedCount(zombified);
 508   event->commit();
 509 }
 510 
 511 void NMethodSweeper::sweep_code_cache() {
 512   ResourceMark rm;
 513   Ticks sweep_start_counter = Ticks::now();
 514 
 515   log_debug(codecache, sweep, start)("CodeCache flushing");
 516 
 517   int flushed_count                = 0;
 518   int zombified_count              = 0;
 519   int flushed_c2_count     = 0;
 520 
 521   if (PrintMethodFlushing && Verbose) {
 522     tty->print_cr("### Sweep at %d out of %d", _seen, CodeCache::nmethod_count());
 523   }
 524 
 525   int swept_count = 0;
 526   assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here");
 527   assert(!CodeCache_lock->owned_by_self(), "just checking");
 528 
 529   int freed_memory = 0;
 530   {
 531     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 532 
 533     while (!_current.end()) {
 534       swept_count++;
 535       // Since we will give up the CodeCache_lock, always skip ahead
 536       // to the next nmethod.  Other blobs can be deleted by other
 537       // threads but nmethods are only reclaimed by the sweeper.
 538       CompiledMethod* nm = _current.method();
 539       _current.next();
 540 
 541       // Now ready to process nmethod and give up CodeCache_lock
 542       {
 543         MutexUnlocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 544         // Save information before potentially flushing the nmethod
 545         // Only flushing nmethods so size only matters for them.
 546         int size = nm->is_nmethod() ? ((nmethod*)nm)->total_size() : 0;
 547         bool is_c2_method = nm->is_compiled_by_c2();
 548         bool is_osr = nm->is_osr_method();
 549         int compile_id = nm->compile_id();
 550         intptr_t address = p2i(nm);
 551         const char* state_before = nm->state();
 552         const char* state_after = "";
 553 
 554         MethodStateChange type = process_compiled_method(nm);
 555         switch (type) {
 556           case Flushed:
 557             state_after = "flushed";
 558             freed_memory += size;
 559             ++flushed_count;
 560             if (is_c2_method) {
 561               ++flushed_c2_count;
 562             }
 563             break;
 564           case MadeZombie:
 565             state_after = "made zombie";
 566             ++zombified_count;
 567             break;
 568           case None:
 569             break;
 570           default:
 571            ShouldNotReachHere();
 572         }
 573         if (PrintMethodFlushing && Verbose && type != None) {
 574           tty->print_cr("### %s nmethod %3d/" PTR_FORMAT " (%s) %s", is_osr ? "osr" : "", compile_id, address, state_before, state_after);
 575         }
 576       }
 577 
 578       _seen++;
 579       handle_safepoint_request();
 580     }
 581   }
 582 
 583   assert(_current.end(), "must have scanned the whole cache");
 584 
 585   const Ticks sweep_end_counter = Ticks::now();
 586   const Tickspan sweep_time = sweep_end_counter - sweep_start_counter;
 587   {
 588     MutexLocker mu(NMethodSweeperStats_lock, Mutex::_no_safepoint_check_flag);
 589     _total_time_sweeping  += sweep_time;
 590     _total_time_this_sweep += sweep_time;
 591     _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
 592     _total_flushed_size += freed_memory;
 593     _total_nof_methods_reclaimed += flushed_count;
 594     _total_nof_c2_methods_reclaimed += flushed_c2_count;
 595     _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep);
 596   }
 597 
 598   EventSweepCodeCache event(UNTIMED);
 599   if (event.should_commit()) {
 600     post_sweep_event(&event, sweep_start_counter, sweep_end_counter, (s4)_traversals, swept_count, flushed_count, zombified_count);
 601   }
 602 
 603 #ifdef ASSERT
 604   if(PrintMethodFlushing) {
 605     tty->print_cr("### sweeper:      sweep time(" JLONG_FORMAT "): ", sweep_time.value());
 606   }
 607 #endif
 608 
 609   Log(codecache, sweep) log;
 610   if (log.is_debug()) {
 611     LogStream ls(log.debug());
 612     CodeCache::print_summary(&ls, false);
 613   }
 614   log_sweep("finished");
 615 
 616   // Sweeper is the only case where memory is released, check here if it
 617   // is time to restart the compiler. Only checking if there is a certain
 618   // amount of free memory in the code cache might lead to re-enabling
 619   // compilation although no memory has been released. For example, there are
 620   // cases when compilation was disabled although there is 4MB (or more) free
 621   // memory in the code cache. The reason is code cache fragmentation. Therefore,
 622   // it only makes sense to re-enable compilation if we have actually freed memory.
 623   // Note that typically several kB are released for sweeping 16MB of the code
 624   // cache. As a result, 'freed_memory' > 0 to restart the compiler.
 625   if (!CompileBroker::should_compile_new_jobs() && (freed_memory > 0)) {
 626     CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
 627     log.debug("restart compiler");
 628     log_sweep("restart_compiler");
 629   }
 630 }
 631 
 632 /**
 633  * This function updates the sweeper statistics that keep track of nmethods
 634  * state changes. If there is 'enough' state change, the sweeper is invoked
 635  * as soon as possible. There can be data races on _bytes_changed. The data
 636  * races are benign, since it does not matter if we loose a couple of bytes.
 637  * In the worst case we call the sweeper a little later. Also, we are guaranteed
 638  * to invoke the sweeper if the code cache gets full.
 639  */
 640 void NMethodSweeper::report_state_change(nmethod* nm) {
 641   _bytes_changed += nm->total_size();
 642   possibly_enable_sweeper();
 643 }
 644 
 645 /**
 646  * Function determines if there was 'enough' state change in the code cache to invoke
 647  * the sweeper again. Currently, we determine 'enough' as more than 1% state change in
 648  * the code cache since the last sweep.
 649  */
 650 void NMethodSweeper::possibly_enable_sweeper() {
 651   double percent_changed = ((double)_bytes_changed / (double)ReservedCodeCacheSize) * 100;
 652   if (percent_changed > 1.0) {
 653     _should_sweep = true;
 654   }
 655 }
 656 
 657 class CompiledMethodMarker: public StackObj {
 658  private:
 659   CodeCacheSweeperThread* _thread;
 660  public:
 661   CompiledMethodMarker(CompiledMethod* cm) {
 662     JavaThread* current = JavaThread::current();
 663     assert (current->is_Code_cache_sweeper_thread(), "Must be");
 664     _thread = (CodeCacheSweeperThread*)current;
 665     if (!cm->is_zombie() && !cm->is_unloading()) {
 666       // Only expose live nmethods for scanning
 667       _thread->set_scanned_compiled_method(cm);
 668     }
 669   }
 670   ~CompiledMethodMarker() {
 671     _thread->set_scanned_compiled_method(NULL);
 672   }
 673 };
 674 
 675 NMethodSweeper::MethodStateChange NMethodSweeper::process_compiled_method(CompiledMethod* cm) {
 676   assert(cm != NULL, "sanity");
 677   assert(!CodeCache_lock->owned_by_self(), "just checking");
 678 
 679   MethodStateChange result = None;
 680   // Make sure this nmethod doesn't get unloaded during the scan,
 681   // since safepoints may happen during acquired below locks.
 682   CompiledMethodMarker nmm(cm);
 683   SWEEP(cm);
 684 
 685   // Skip methods that are currently referenced by the VM
 686   if (cm->is_locked_by_vm()) {
 687     // But still remember to clean-up inline caches for alive nmethods
 688     if (cm->is_alive()) {
 689       // Clean inline caches that point to zombie/non-entrant/unloaded nmethods
 690       cm->cleanup_inline_caches(false);
 691       SWEEP(cm);
 692     }
 693     return result;
 694   }
 695 
 696   if (cm->is_zombie()) {
 697     // All inline caches that referred to this nmethod were cleaned in the
 698     // previous sweeper cycle. Now flush the nmethod from the code cache.
 699     assert(!cm->is_locked_by_vm(), "must not flush locked Compiled Methods");
 700     cm->flush();
 701     assert(result == None, "sanity");
 702     result = Flushed;
 703   } else if (cm->is_not_entrant()) {
 704     // If there are no current activations of this method on the
 705     // stack we can safely convert it to a zombie method
 706     OrderAccess::loadload(); // _stack_traversal_mark and _state
 707     if (cm->can_convert_to_zombie()) {
 708       // Code cache state change is tracked in make_zombie()
 709       cm->make_zombie();
 710       SWEEP(cm);
 711       assert(result == None, "sanity");
 712       result = MadeZombie;
 713       assert(cm->is_zombie(), "nmethod must be zombie");
 714     } else {
 715       // Still alive, clean up its inline caches
 716       cm->cleanup_inline_caches(false);
 717       SWEEP(cm);
 718     }
 719   } else if (cm->is_unloaded()) {
 720     // Code is unloaded, so there are no activations on the stack.
 721     // Convert the nmethod to zombie.
 722     // Code cache state change is tracked in make_zombie()
 723     cm->make_zombie();
 724     SWEEP(cm);
 725     assert(result == None, "sanity");
 726     result = MadeZombie;
 727   } else {
 728     if (cm->is_nmethod()) {
 729       possibly_flush((nmethod*)cm);
 730     }
 731     // Clean inline caches that point to zombie/non-entrant/unloaded nmethods
 732     cm->cleanup_inline_caches(false);
 733     SWEEP(cm);
 734   }
 735   return result;
 736 }
 737 
 738 
 739 void NMethodSweeper::possibly_flush(nmethod* nm) {
 740   if (UseCodeCacheFlushing) {
 741     if (!nm->is_locked_by_vm() && !nm->is_native_method() && !nm->is_not_installed() && !nm->is_unloading()) {
 742       bool make_not_entrant = false;
 743 
 744       // Do not make native methods not-entrant
 745       nm->dec_hotness_counter();
 746       // Get the initial value of the hotness counter. This value depends on the
 747       // ReservedCodeCacheSize
 748       int reset_val = hotness_counter_reset_val();
 749       int time_since_reset = reset_val - nm->hotness_counter();
 750       int code_blob_type = CodeCache::get_code_blob_type(nm);
 751       double threshold = -reset_val + (CodeCache::reverse_free_ratio(code_blob_type) * NmethodSweepActivity);
 752       // The less free space in the code cache we have - the bigger reverse_free_ratio() is.
 753       // I.e., 'threshold' increases with lower available space in the code cache and a higher
 754       // NmethodSweepActivity. If the current hotness counter - which decreases from its initial
 755       // value until it is reset by stack walking - is smaller than the computed threshold, the
 756       // corresponding nmethod is considered for removal.
 757       if ((NmethodSweepActivity > 0) && (nm->hotness_counter() < threshold) && (time_since_reset > MinPassesBeforeFlush)) {
 758         // A method is marked as not-entrant if the method is
 759         // 1) 'old enough': nm->hotness_counter() < threshold
 760         // 2) The method was in_use for a minimum amount of time: (time_since_reset > MinPassesBeforeFlush)
 761         //    The second condition is necessary if we are dealing with very small code cache
 762         //    sizes (e.g., <10m) and the code cache size is too small to hold all hot methods.
 763         //    The second condition ensures that methods are not immediately made not-entrant
 764         //    after compilation.
 765         make_not_entrant = true;
 766       }
 767 
 768       // The stack-scanning low-cost detection may not see the method was used (which can happen for
 769       // flat profiles). Check the age counter for possible data.
 770       if (UseCodeAging && make_not_entrant && (nm->is_compiled_by_c2() || nm->is_compiled_by_c1())) {
 771         MethodCounters* mc = nm->method()->get_method_counters(Thread::current());
 772         if (mc != NULL) {
 773           // Snapshot the value as it's changed concurrently
 774           int age = mc->nmethod_age();
 775           if (MethodCounters::is_nmethod_hot(age)) {
 776             // The method has gone through flushing, and it became relatively hot that it deopted
 777             // before we could take a look at it. Give it more time to appear in the stack traces,
 778             // proportional to the number of deopts.
 779             MethodData* md = nm->method()->method_data();
 780             if (md != NULL && time_since_reset > (int)(MinPassesBeforeFlush * (md->tenure_traps() + 1))) {
 781               // It's been long enough, we still haven't seen it on stack.
 782               // Try to flush it, but enable counters the next time.
 783               mc->reset_nmethod_age();
 784             } else {
 785               make_not_entrant = false;
 786             }
 787           } else if (MethodCounters::is_nmethod_warm(age)) {
 788             // Method has counters enabled, and the method was used within
 789             // previous MinPassesBeforeFlush sweeps. Reset the counter. Stay in the existing
 790             // compiled state.
 791             mc->reset_nmethod_age();
 792             // delay the next check
 793             nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
 794             make_not_entrant = false;
 795           } else if (MethodCounters::is_nmethod_age_unset(age)) {
 796             // No counters were used before. Set the counters to the detection
 797             // limit value. If the method is going to be used again it will be compiled
 798             // with counters that we're going to use for analysis the the next time.
 799             mc->reset_nmethod_age();
 800           } else {
 801             // Method was totally idle for 10 sweeps
 802             // The counter already has the initial value, flush it and may be recompile
 803             // later with counters
 804           }
 805         }
 806       }
 807 
 808       if (make_not_entrant) {
 809         nm->make_not_entrant();
 810 
 811         // Code cache state change is tracked in make_not_entrant()
 812         if (PrintMethodFlushing && Verbose) {
 813           tty->print_cr("### Nmethod %d/" PTR_FORMAT "made not-entrant: hotness counter %d/%d threshold %f",
 814               nm->compile_id(), p2i(nm), nm->hotness_counter(), reset_val, threshold);
 815         }
 816       }
 817     }
 818   }
 819 }
 820 
 821 // Print out some state information about the current sweep and the
 822 // state of the code cache if it's requested.
 823 void NMethodSweeper::log_sweep(const char* msg, const char* format, ...) {
 824   if (PrintMethodFlushing) {
 825     ResourceMark rm;
 826     stringStream s;
 827     // Dump code cache state into a buffer before locking the tty,
 828     // because log_state() will use locks causing lock conflicts.
 829     CodeCache::log_state(&s);
 830 
 831     ttyLocker ttyl;
 832     tty->print("### sweeper: %s ", msg);
 833     if (format != NULL) {
 834       va_list ap;
 835       va_start(ap, format);
 836       tty->vprint(format, ap);
 837       va_end(ap);
 838     }
 839     tty->print_cr("%s", s.as_string());
 840   }
 841 
 842   if (LogCompilation && (xtty != NULL)) {
 843     ResourceMark rm;
 844     stringStream s;
 845     // Dump code cache state into a buffer before locking the tty,
 846     // because log_state() will use locks causing lock conflicts.
 847     CodeCache::log_state(&s);
 848 
 849     ttyLocker ttyl;
 850     xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count());
 851     if (format != NULL) {
 852       va_list ap;
 853       va_start(ap, format);
 854       xtty->vprint(format, ap);
 855       va_end(ap);
 856     }
 857     xtty->print("%s", s.as_string());
 858     xtty->stamp();
 859     xtty->end_elem();
 860   }
 861 }
 862 
 863 void NMethodSweeper::print(outputStream* out) {
 864   ttyLocker ttyl;
 865   out = (out == NULL) ? tty : out;
 866   out->print_cr("Code cache sweeper statistics:");
 867   out->print_cr("  Total sweep time:                %1.0lf ms", (double)_total_time_sweeping.value()/1000000);
 868   out->print_cr("  Total number of full sweeps:     %ld", _total_nof_code_cache_sweeps);
 869   out->print_cr("  Total number of flushed methods: %ld (thereof %ld C2 methods)", _total_nof_methods_reclaimed,
 870                                                     _total_nof_c2_methods_reclaimed);
 871   out->print_cr("  Total size of flushed methods:   " SIZE_FORMAT " kB", _total_flushed_size/K);
 872 }
--- EOF ---