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