annotate src/share/vm/runtime/sweeper.cpp @ 2481:f52ed367b66d

6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify Reviewed-by: kvn, iveresov
author never
date Mon, 16 May 2011 22:16:44 -0700
parents dbccacb79c63
children cfdfbeac0a5b
rev   line source
duke@0 1 /*
trims@2273 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "code/codeCache.hpp"
stefank@1879 27 #include "code/nmethod.hpp"
stefank@1879 28 #include "compiler/compileBroker.hpp"
stefank@1879 29 #include "memory/resourceArea.hpp"
stefank@1879 30 #include "oops/methodOop.hpp"
stefank@1879 31 #include "runtime/atomic.hpp"
stefank@1879 32 #include "runtime/compilationPolicy.hpp"
stefank@1879 33 #include "runtime/mutexLocker.hpp"
stefank@1879 34 #include "runtime/os.hpp"
stefank@1879 35 #include "runtime/sweeper.hpp"
stefank@1879 36 #include "runtime/vm_operations.hpp"
stefank@1879 37 #include "utilities/events.hpp"
stefank@1879 38 #include "utilities/xmlstream.hpp"
duke@0 39
never@2481 40 #ifdef ASSERT
never@2481 41
never@2481 42 #define SWEEP(nm) record_sweep(nm, __LINE__)
never@2481 43 // Sweeper logging code
never@2481 44 class SweeperRecord {
never@2481 45 public:
never@2481 46 int traversal;
never@2481 47 int invocation;
never@2481 48 int compile_id;
never@2481 49 long traversal_mark;
never@2481 50 int state;
never@2481 51 const char* kind;
never@2481 52 address vep;
never@2481 53 address uep;
never@2481 54 int line;
never@2481 55
never@2481 56 void print() {
never@2481 57 tty->print_cr("traversal = %d invocation = %d compile_id = %d %s uep = " PTR_FORMAT " vep = "
never@2481 58 PTR_FORMAT " state = %d traversal_mark %d line = %d",
never@2481 59 traversal,
never@2481 60 invocation,
never@2481 61 compile_id,
never@2481 62 kind == NULL ? "" : kind,
never@2481 63 uep,
never@2481 64 vep,
never@2481 65 state,
never@2481 66 traversal_mark,
never@2481 67 line);
never@2481 68 }
never@2481 69 };
never@2481 70
never@2481 71 static int _sweep_index = 0;
never@2481 72 static SweeperRecord* _records = NULL;
never@2481 73
never@2481 74 void NMethodSweeper::report_events(int id, address entry) {
never@2481 75 if (_records != NULL) {
never@2481 76 for (int i = _sweep_index; i < SweeperLogEntries; i++) {
never@2481 77 if (_records[i].uep == entry ||
never@2481 78 _records[i].vep == entry ||
never@2481 79 _records[i].compile_id == id) {
never@2481 80 _records[i].print();
never@2481 81 }
never@2481 82 }
never@2481 83 for (int i = 0; i < _sweep_index; i++) {
never@2481 84 if (_records[i].uep == entry ||
never@2481 85 _records[i].vep == entry ||
never@2481 86 _records[i].compile_id == id) {
never@2481 87 _records[i].print();
never@2481 88 }
never@2481 89 }
never@2481 90 }
never@2481 91 }
never@2481 92
never@2481 93 void NMethodSweeper::report_events() {
never@2481 94 if (_records != NULL) {
never@2481 95 for (int i = _sweep_index; i < SweeperLogEntries; i++) {
never@2481 96 // skip empty records
never@2481 97 if (_records[i].vep == NULL) continue;
never@2481 98 _records[i].print();
never@2481 99 }
never@2481 100 for (int i = 0; i < _sweep_index; i++) {
never@2481 101 // skip empty records
never@2481 102 if (_records[i].vep == NULL) continue;
never@2481 103 _records[i].print();
never@2481 104 }
never@2481 105 }
never@2481 106 }
never@2481 107
never@2481 108 void NMethodSweeper::record_sweep(nmethod* nm, int line) {
never@2481 109 if (_records != NULL) {
never@2481 110 _records[_sweep_index].traversal = _traversals;
never@2481 111 _records[_sweep_index].traversal_mark = nm->_stack_traversal_mark;
never@2481 112 _records[_sweep_index].invocation = _invocations;
never@2481 113 _records[_sweep_index].compile_id = nm->compile_id();
never@2481 114 _records[_sweep_index].kind = nm->compile_kind();
never@2481 115 _records[_sweep_index].state = nm->_state;
never@2481 116 _records[_sweep_index].vep = nm->verified_entry_point();
never@2481 117 _records[_sweep_index].uep = nm->entry_point();
never@2481 118 _records[_sweep_index].line = line;
never@2481 119
never@2481 120 _sweep_index = (_sweep_index + 1) % SweeperLogEntries;
never@2481 121 }
never@2481 122 }
never@2481 123 #else
never@2481 124 #define SWEEP(nm)
never@2481 125 #endif
never@2481 126
never@2481 127
duke@0 128 long NMethodSweeper::_traversals = 0; // No. of stack traversals performed
never@1535 129 nmethod* NMethodSweeper::_current = NULL; // Current nmethod
never@1564 130 int NMethodSweeper::_seen = 0 ; // No. of nmethods we have currently processed in current pass of CodeCache
never@1564 131
never@1564 132 volatile int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass
never@1564 133 volatile int NMethodSweeper::_sweep_started = 0; // Whether a sweep is in progress.
duke@0 134
duke@0 135 jint NMethodSweeper::_locked_seen = 0;
duke@0 136 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
duke@0 137 bool NMethodSweeper::_rescan = false;
never@1458 138 bool NMethodSweeper::_do_sweep = false;
kvn@1202 139 bool NMethodSweeper::_was_full = false;
kvn@1202 140 jint NMethodSweeper::_advise_to_sweep = 0;
kvn@1202 141 jlong NMethodSweeper::_last_was_full = 0;
kvn@1202 142 uint NMethodSweeper::_highest_marked = 0;
kvn@1202 143 long NMethodSweeper::_was_full_traversal = 0;
duke@0 144
jrose@989 145 class MarkActivationClosure: public CodeBlobClosure {
jrose@989 146 public:
jrose@989 147 virtual void do_code_blob(CodeBlob* cb) {
jrose@989 148 // If we see an activation belonging to a non_entrant nmethod, we mark it.
jrose@989 149 if (cb->is_nmethod() && ((nmethod*)cb)->is_not_entrant()) {
jrose@989 150 ((nmethod*)cb)->mark_as_seen_on_stack();
jrose@989 151 }
jrose@989 152 }
jrose@989 153 };
jrose@989 154 static MarkActivationClosure mark_activation_closure;
jrose@989 155
never@1458 156 void NMethodSweeper::scan_stacks() {
duke@0 157 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
duke@0 158 if (!MethodFlushing) return;
never@1458 159 _do_sweep = true;
duke@0 160
duke@0 161 // No need to synchronize access, since this is always executed at a
duke@0 162 // safepoint. If we aren't in the middle of scan and a rescan
never@1458 163 // hasn't been requested then just return. If UseCodeCacheFlushing is on and
never@1458 164 // code cache flushing is in progress, don't skip sweeping to help make progress
never@1458 165 // clearing space in the code cache.
never@1458 166 if ((_current == NULL && !_rescan) && !(UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs())) {
never@1458 167 _do_sweep = false;
never@1458 168 return;
never@1458 169 }
duke@0 170
duke@0 171 // Make sure CompiledIC_lock in unlocked, since we might update some
duke@0 172 // inline caches. If it is, we just bail-out and try later.
duke@0 173 if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return;
duke@0 174
duke@0 175 // Check for restart
duke@0 176 assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
duke@0 177 if (_current == NULL) {
duke@0 178 _seen = 0;
duke@0 179 _invocations = NmethodSweepFraction;
never@1458 180 _current = CodeCache::first_nmethod();
duke@0 181 _traversals += 1;
duke@0 182 if (PrintMethodFlushing) {
duke@0 183 tty->print_cr("### Sweep: stack traversal %d", _traversals);
duke@0 184 }
jrose@989 185 Threads::nmethods_do(&mark_activation_closure);
duke@0 186
duke@0 187 // reset the flags since we started a scan from the beginning.
duke@0 188 _rescan = false;
duke@0 189 _locked_seen = 0;
duke@0 190 _not_entrant_seen_on_stack = 0;
duke@0 191 }
duke@0 192
kvn@1202 193 if (UseCodeCacheFlushing) {
kvn@1202 194 if (!CodeCache::needs_flushing()) {
never@1458 195 // scan_stacks() runs during a safepoint, no race with setters
kvn@1202 196 _advise_to_sweep = 0;
kvn@1202 197 }
kvn@1202 198
kvn@1202 199 if (was_full()) {
kvn@1202 200 // There was some progress so attempt to restart the compiler
kvn@1202 201 jlong now = os::javaTimeMillis();
kvn@1202 202 jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000;
kvn@1202 203 jlong curr_interval = now - _last_was_full;
kvn@1202 204 if ((!CodeCache::needs_flushing()) && (curr_interval > max_interval)) {
kvn@1202 205 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
kvn@1202 206 set_was_full(false);
kvn@1202 207
kvn@1202 208 // Update the _last_was_full time so we can tell how fast the
kvn@1202 209 // code cache is filling up
kvn@1202 210 _last_was_full = os::javaTimeMillis();
kvn@1202 211
never@1564 212 log_sweep("restart_compiler");
kvn@1202 213 }
kvn@1202 214 }
kvn@1202 215 }
duke@0 216 }
duke@0 217
never@1458 218 void NMethodSweeper::possibly_sweep() {
never@1564 219 assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode");
never@1458 220 if ((!MethodFlushing) || (!_do_sweep)) return;
never@1458 221
never@1458 222 if (_invocations > 0) {
never@1458 223 // Only one thread at a time will sweep
never@1458 224 jint old = Atomic::cmpxchg( 1, &_sweep_started, 0 );
never@1458 225 if (old != 0) {
never@1458 226 return;
never@1458 227 }
never@2481 228 #ifdef ASSERT
never@2481 229 if (LogSweeper && _records == NULL) {
never@2481 230 // Create the ring buffer for the logging code
never@2481 231 _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries);
never@2481 232 memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
never@2481 233 }
never@2481 234 #endif
never@1564 235 if (_invocations > 0) {
never@1564 236 sweep_code_cache();
never@1564 237 _invocations--;
never@1564 238 }
never@1564 239 _sweep_started = 0;
never@1458 240 }
never@1458 241 }
never@1458 242
never@1458 243 void NMethodSweeper::sweep_code_cache() {
never@1458 244 #ifdef ASSERT
never@1458 245 jlong sweep_start;
never@1564 246 if (PrintMethodFlushing) {
never@1458 247 sweep_start = os::javaTimeMillis();
never@1458 248 }
never@1458 249 #endif
never@1458 250 if (PrintMethodFlushing && Verbose) {
never@1564 251 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _invocations);
never@1458 252 }
never@1458 253
never@1564 254 // We want to visit all nmethods after NmethodSweepFraction
never@1564 255 // invocations so divide the remaining number of nmethods by the
never@1564 256 // remaining number of invocations. This is only an estimate since
never@1564 257 // the number of nmethods changes during the sweep so the final
never@1564 258 // stage must iterate until it there are no more nmethods.
never@1564 259 int todo = (CodeCache::nof_nmethods() - _seen) / _invocations;
never@1458 260
never@1458 261 assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here");
never@1458 262 assert(!CodeCache_lock->owned_by_self(), "just checking");
never@1458 263
never@1458 264 {
never@1458 265 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1458 266
never@1564 267 // The last invocation iterates until there are no more nmethods
never@1564 268 for (int i = 0; (i < todo || _invocations == 1) && _current != NULL; i++) {
never@1458 269
never@1564 270 // Since we will give up the CodeCache_lock, always skip ahead
never@1564 271 // to the next nmethod. Other blobs can be deleted by other
never@1564 272 // threads but nmethods are only reclaimed by the sweeper.
never@1535 273 nmethod* next = CodeCache::next_nmethod(_current);
never@1458 274
never@1458 275 // Now ready to process nmethod and give up CodeCache_lock
never@1458 276 {
never@1458 277 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1535 278 process_nmethod(_current);
never@1458 279 }
never@1458 280 _seen++;
never@1458 281 _current = next;
never@1458 282 }
never@1458 283 }
never@1458 284
never@1564 285 assert(_invocations > 1 || _current == NULL, "must have scanned the whole cache");
never@1564 286
never@1458 287 if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) {
never@1458 288 // we've completed a scan without making progress but there were
never@1458 289 // nmethods we were unable to process either because they were
never@1458 290 // locked or were still on stack. We don't have to aggresively
never@1458 291 // clean them up so just stop scanning. We could scan once more
never@1458 292 // but that complicates the control logic and it's unlikely to
never@1458 293 // matter much.
never@1458 294 if (PrintMethodFlushing) {
never@1458 295 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
never@1458 296 }
never@1458 297 }
never@1458 298
never@1458 299 #ifdef ASSERT
never@1458 300 if(PrintMethodFlushing) {
never@1458 301 jlong sweep_end = os::javaTimeMillis();
never@1458 302 tty->print_cr("### sweeper: sweep time(%d): " INT64_FORMAT, _invocations, sweep_end - sweep_start);
never@1458 303 }
never@1458 304 #endif
never@1564 305
never@1564 306 if (_invocations == 1) {
never@1564 307 log_sweep("finished");
never@1564 308 }
never@1458 309 }
never@1458 310
never@2481 311 class NMethodMarker: public StackObj {
never@2481 312 private:
never@2481 313 CompilerThread* _thread;
never@2481 314 public:
never@2481 315 NMethodMarker(nmethod* nm) {
never@2481 316 _thread = CompilerThread::current();
never@2481 317 _thread->set_scanned_nmethod(nm);
never@2481 318 }
never@2481 319 ~NMethodMarker() {
never@2481 320 _thread->set_scanned_nmethod(NULL);
never@2481 321 }
never@2481 322 };
never@2481 323
duke@0 324
duke@0 325 void NMethodSweeper::process_nmethod(nmethod *nm) {
never@1458 326 assert(!CodeCache_lock->owned_by_self(), "just checking");
never@1458 327
never@2481 328 // Make sure this nmethod doesn't get unloaded during the scan,
never@2481 329 // since the locks acquired below might safepoint.
never@2481 330 NMethodMarker nmm(nm);
never@2481 331
never@2481 332 SWEEP(nm);
never@2481 333
duke@0 334 // Skip methods that are currently referenced by the VM
duke@0 335 if (nm->is_locked_by_vm()) {
duke@0 336 // But still remember to clean-up inline caches for alive nmethods
duke@0 337 if (nm->is_alive()) {
duke@0 338 // Clean-up all inline caches that points to zombie/non-reentrant methods
never@1458 339 MutexLocker cl(CompiledIC_lock);
duke@0 340 nm->cleanup_inline_caches();
never@2481 341 SWEEP(nm);
duke@0 342 } else {
duke@0 343 _locked_seen++;
never@2481 344 SWEEP(nm);
duke@0 345 }
duke@0 346 return;
duke@0 347 }
duke@0 348
duke@0 349 if (nm->is_zombie()) {
duke@0 350 // If it is first time, we see nmethod then we mark it. Otherwise,
duke@0 351 // we reclame it. When we have seen a zombie method twice, we know that
never@1564 352 // there are no inline caches that refer to it.
duke@0 353 if (nm->is_marked_for_reclamation()) {
duke@0 354 assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
ysr@941 355 if (PrintMethodFlushing && Verbose) {
kvn@1202 356 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
ysr@941 357 }
never@1458 358 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@0 359 nm->flush();
duke@0 360 } else {
ysr@941 361 if (PrintMethodFlushing && Verbose) {
kvn@1202 362 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
ysr@941 363 }
duke@0 364 nm->mark_for_reclamation();
duke@0 365 _rescan = true;
never@2481 366 SWEEP(nm);
duke@0 367 }
duke@0 368 } else if (nm->is_not_entrant()) {
duke@0 369 // If there is no current activations of this method on the
duke@0 370 // stack we can safely convert it to a zombie method
duke@0 371 if (nm->can_not_entrant_be_converted()) {
ysr@941 372 if (PrintMethodFlushing && Verbose) {
kvn@1202 373 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
ysr@941 374 }
duke@0 375 nm->make_zombie();
duke@0 376 _rescan = true;
never@2481 377 SWEEP(nm);
duke@0 378 } else {
duke@0 379 // Still alive, clean up its inline caches
never@1458 380 MutexLocker cl(CompiledIC_lock);
duke@0 381 nm->cleanup_inline_caches();
duke@0 382 // we coudn't transition this nmethod so don't immediately
duke@0 383 // request a rescan. If this method stays on the stack for a
never@1458 384 // long time we don't want to keep rescanning the code cache.
duke@0 385 _not_entrant_seen_on_stack++;
never@2481 386 SWEEP(nm);
duke@0 387 }
duke@0 388 } else if (nm->is_unloaded()) {
duke@0 389 // Unloaded code, just make it a zombie
ysr@941 390 if (PrintMethodFlushing && Verbose)
kvn@1202 391 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm);
ysr@941 392 if (nm->is_osr_method()) {
duke@0 393 // No inline caches will ever point to osr methods, so we can just remove it
never@1458 394 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@2481 395 SWEEP(nm);
duke@0 396 nm->flush();
duke@0 397 } else {
duke@0 398 nm->make_zombie();
duke@0 399 _rescan = true;
never@2481 400 SWEEP(nm);
duke@0 401 }
duke@0 402 } else {
duke@0 403 assert(nm->is_alive(), "should be alive");
kvn@1202 404
kvn@1202 405 if (UseCodeCacheFlushing) {
kvn@1202 406 if ((nm->method()->code() != nm) && !(nm->is_locked_by_vm()) && !(nm->is_osr_method()) &&
kvn@1202 407 (_traversals > _was_full_traversal+2) && (((uint)nm->compile_id()) < _highest_marked) &&
kvn@1202 408 CodeCache::needs_flushing()) {
kvn@1202 409 // This method has not been called since the forced cleanup happened
kvn@1202 410 nm->make_not_entrant();
kvn@1202 411 }
kvn@1202 412 }
kvn@1202 413
duke@0 414 // Clean-up all inline caches that points to zombie/non-reentrant methods
never@1458 415 MutexLocker cl(CompiledIC_lock);
duke@0 416 nm->cleanup_inline_caches();
never@2481 417 SWEEP(nm);
duke@0 418 }
duke@0 419 }
kvn@1202 420
kvn@1202 421 // Code cache unloading: when compilers notice the code cache is getting full,
kvn@1202 422 // they will call a vm op that comes here. This code attempts to speculatively
kvn@1202 423 // unload the oldest half of the nmethods (based on the compile job id) by
kvn@1202 424 // saving the old code in a list in the CodeCache. Then
never@1458 425 // execution resumes. If a method so marked is not called by the second sweeper
never@1458 426 // stack traversal after the current one, the nmethod will be marked non-entrant and
kvn@1202 427 // got rid of by normal sweeping. If the method is called, the methodOop's
kvn@1202 428 // _code field is restored and the methodOop/nmethod
kvn@1202 429 // go back to their normal state.
kvn@1202 430 void NMethodSweeper::handle_full_code_cache(bool is_full) {
kvn@1202 431 // Only the first one to notice can advise us to start early cleaning
kvn@1202 432 if (!is_full){
kvn@1202 433 jint old = Atomic::cmpxchg( 1, &_advise_to_sweep, 0 );
kvn@1202 434 if (old != 0) {
kvn@1202 435 return;
kvn@1202 436 }
kvn@1202 437 }
kvn@1202 438
kvn@1202 439 if (is_full) {
kvn@1202 440 // Since code cache is full, immediately stop new compiles
kvn@1202 441 bool did_set = CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation);
kvn@1202 442 if (!did_set) {
kvn@1202 443 // only the first to notice can start the cleaning,
kvn@1202 444 // others will go back and block
kvn@1202 445 return;
kvn@1202 446 }
kvn@1202 447 set_was_full(true);
kvn@1202 448
kvn@1202 449 // If we run out within MinCodeCacheFlushingInterval of the last unload time, give up
kvn@1202 450 jlong now = os::javaTimeMillis();
kvn@1202 451 jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000;
kvn@1202 452 jlong curr_interval = now - _last_was_full;
kvn@1202 453 if (curr_interval < max_interval) {
kvn@1202 454 _rescan = true;
never@1564 455 log_sweep("disable_compiler", "flushing_interval='" UINT64_FORMAT "'",
never@1564 456 curr_interval/1000);
kvn@1202 457 return;
kvn@1202 458 }
kvn@1202 459 }
kvn@1202 460
kvn@1202 461 VM_HandleFullCodeCache op(is_full);
kvn@1202 462 VMThread::execute(&op);
kvn@1202 463
kvn@1202 464 // rescan again as soon as possible
kvn@1202 465 _rescan = true;
kvn@1202 466 }
kvn@1202 467
kvn@1202 468 void NMethodSweeper::speculative_disconnect_nmethods(bool is_full) {
kvn@1202 469 // If there was a race in detecting full code cache, only run
kvn@1202 470 // one vm op for it or keep the compiler shut off
kvn@1202 471
kvn@1202 472 debug_only(jlong start = os::javaTimeMillis();)
kvn@1202 473
kvn@1202 474 if ((!was_full()) && (is_full)) {
kvn@1202 475 if (!CodeCache::needs_flushing()) {
never@1564 476 log_sweep("restart_compiler");
kvn@1202 477 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
kvn@1202 478 return;
kvn@1202 479 }
kvn@1202 480 }
kvn@1202 481
kvn@1202 482 // Traverse the code cache trying to dump the oldest nmethods
kvn@1202 483 uint curr_max_comp_id = CompileBroker::get_compilation_id();
kvn@1202 484 uint flush_target = ((curr_max_comp_id - _highest_marked) >> 1) + _highest_marked;
never@1564 485 log_sweep("start_cleaning");
kvn@1202 486
kvn@1202 487 nmethod* nm = CodeCache::alive_nmethod(CodeCache::first());
kvn@1202 488 jint disconnected = 0;
kvn@1202 489 jint made_not_entrant = 0;
kvn@1202 490 while ((nm != NULL)){
kvn@1202 491 uint curr_comp_id = nm->compile_id();
kvn@1202 492
kvn@1202 493 // OSR methods cannot be flushed like this. Also, don't flush native methods
kvn@1202 494 // since they are part of the JDK in most cases
kvn@1202 495 if (nm->is_in_use() && (!nm->is_osr_method()) && (!nm->is_locked_by_vm()) &&
kvn@1202 496 (!nm->is_native_method()) && ((curr_comp_id < flush_target))) {
kvn@1202 497
kvn@1202 498 if ((nm->method()->code() == nm)) {
kvn@1202 499 // This method has not been previously considered for
kvn@1202 500 // unloading or it was restored already
kvn@1202 501 CodeCache::speculatively_disconnect(nm);
kvn@1202 502 disconnected++;
kvn@1202 503 } else if (nm->is_speculatively_disconnected()) {
kvn@1202 504 // This method was previously considered for preemptive unloading and was not called since then
iveresov@1703 505 CompilationPolicy::policy()->delay_compilation(nm->method());
kvn@1202 506 nm->make_not_entrant();
kvn@1202 507 made_not_entrant++;
kvn@1202 508 }
kvn@1202 509
kvn@1202 510 if (curr_comp_id > _highest_marked) {
kvn@1202 511 _highest_marked = curr_comp_id;
kvn@1202 512 }
kvn@1202 513 }
kvn@1202 514 nm = CodeCache::alive_nmethod(CodeCache::next(nm));
kvn@1202 515 }
kvn@1202 516
never@1564 517 log_sweep("stop_cleaning",
never@1564 518 "disconnected='" UINT32_FORMAT "' made_not_entrant='" UINT32_FORMAT "'",
never@1564 519 disconnected, made_not_entrant);
kvn@1202 520
never@1458 521 // Shut off compiler. Sweeper will start over with a new stack scan and
never@1458 522 // traversal cycle and turn it back on if it clears enough space.
kvn@1202 523 if (was_full()) {
kvn@1202 524 _last_was_full = os::javaTimeMillis();
kvn@1202 525 CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation);
kvn@1202 526 }
kvn@1202 527
kvn@1202 528 // After two more traversals the sweeper will get rid of unrestored nmethods
kvn@1202 529 _was_full_traversal = _traversals;
kvn@1202 530 #ifdef ASSERT
kvn@1202 531 jlong end = os::javaTimeMillis();
kvn@1202 532 if(PrintMethodFlushing && Verbose) {
kvn@1202 533 tty->print_cr("### sweeper: unload time: " INT64_FORMAT, end-start);
kvn@1202 534 }
kvn@1202 535 #endif
kvn@1202 536 }
never@1564 537
never@1564 538
never@1564 539 // Print out some state information about the current sweep and the
never@1564 540 // state of the code cache if it's requested.
never@1564 541 void NMethodSweeper::log_sweep(const char* msg, const char* format, ...) {
never@1564 542 if (PrintMethodFlushing) {
iveresov@2329 543 stringStream s;
iveresov@2329 544 // Dump code cache state into a buffer before locking the tty,
iveresov@2329 545 // because log_state() will use locks causing lock conflicts.
iveresov@2329 546 CodeCache::log_state(&s);
iveresov@2329 547
never@1564 548 ttyLocker ttyl;
never@1564 549 tty->print("### sweeper: %s ", msg);
never@1564 550 if (format != NULL) {
never@1564 551 va_list ap;
never@1564 552 va_start(ap, format);
never@1564 553 tty->vprint(format, ap);
never@1564 554 va_end(ap);
never@1564 555 }
iveresov@2329 556 tty->print_cr(s.as_string());
never@1564 557 }
never@1564 558
never@1564 559 if (LogCompilation && (xtty != NULL)) {
iveresov@2329 560 stringStream s;
iveresov@2329 561 // Dump code cache state into a buffer before locking the tty,
iveresov@2329 562 // because log_state() will use locks causing lock conflicts.
iveresov@2329 563 CodeCache::log_state(&s);
iveresov@2329 564
never@1564 565 ttyLocker ttyl;
never@1566 566 xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count());
never@1564 567 if (format != NULL) {
never@1564 568 va_list ap;
never@1564 569 va_start(ap, format);
never@1564 570 xtty->vprint(format, ap);
never@1564 571 va_end(ap);
never@1564 572 }
iveresov@2329 573 xtty->print(s.as_string());
never@1564 574 xtty->stamp();
never@1564 575 xtty->end_elem();
never@1564 576 }
never@1564 577 }