// Code cache unloading: when compilers notice the code cache is getting full, // they will call a vm op that comes here. This code attempts to speculatively // unload the oldest half of the nmethods (based on the compile job id) by // saving the old code in a list in the CodeCache. Then // execution resumes. If a method so marked is not called by the second // safepoint from the current one, the nmethod will be marked non-entrant and // got rid of by normal sweeping. If the method is called, the methodOop's // _code field is restored and the methodOop/nmethod // go back to their normal state. void NMethodSweeper::handle_full_code_cache(bool is_full) { // Only the first one to notice can advise us to start early cleaning if (!is_full){ jint old = Atomic::cmpxchg( 1, &_advise_to_sweep, 0 ); if (old != 0) { return; } } if (is_full) { // Since code cache is full, immediately stop new compiles bool did_set = CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation); if (!did_set) { // only the first to notice can start the cleaning, // others will go back and block return; } set_was_full(true); // If we run out within MinCodeCacheFlushingInterval of the last unload time, give up jlong now = os::javaTimeMillis(); jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000; jlong curr_interval = now - _last_was_full; if (curr_interval < max_interval) { _rescan = true; if (PrintMethodFlushing) { tty->print_cr("### handle full too often, turning off compiler"); } if (LogCompilation && (xtty != NULL)) { ttyLocker ttyl; xtty->begin_elem("disable_compiler flushing_interval='" UINT64_FORMAT "' live_blobs='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'", curr_interval/1000, CodeCache::nof_blobs(), CodeCache::unallocated_capacity()); xtty->stamp(); xtty->end_elem(); } return; } } VM_HandleFullCodeCache op(is_full); VMThread::execute(&op); // rescan again as soon as possible _rescan = true; }
// Code cache unloading: when compilers notice the code cache is getting full, // they will call a vm op that comes here. This code attempts to speculatively // unload the oldest half of the nmethods (based on the compile job id) by // saving the old code in a list in the CodeCache. Then // execution resumes. If a method so marked is not called by the second sweeper // stack traversal after the current one, the nmethod will be marked non-entrant and // got rid of by normal sweeping. If the method is called, the methodOop's // _code field is restored and the methodOop/nmethod // go back to their normal state. void NMethodSweeper::handle_full_code_cache(bool is_full) { // Only the first one to notice can advise us to start early cleaning if (!is_full){ jint old = Atomic::cmpxchg( 1, &_advise_to_sweep, 0 ); if (old != 0) { return; } } if (is_full) { // Since code cache is full, immediately stop new compiles bool did_set = CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation); if (!did_set) { // only the first to notice can start the cleaning, // others will go back and block return; } set_was_full(true); // If we run out within MinCodeCacheFlushingInterval of the last unload time, give up jlong now = os::javaTimeMillis(); jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000; jlong curr_interval = now - _last_was_full; if (curr_interval < max_interval) { _rescan = true; log_sweep("disable_compiler", "flushing_interval='" UINT64_FORMAT "'", curr_interval/1000); return; } } VM_HandleFullCodeCache op(is_full); VMThread::execute(&op); // rescan again as soon as possible _rescan = true; }
void NMethodSweeper::scan_stacks() { assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint"); if (!MethodFlushing) return; _do_sweep = true; // No need to synchronize access, since this is always executed at a // safepoint. If we aren't in the middle of scan and a rescan // hasn't been requested then just return. If UseCodeCacheFlushing is on and // code cache flushing is in progress, don't skip sweeping to help make progress // clearing space in the code cache. if ((_current == NULL && !_rescan) && !(UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs())) { _do_sweep = false; return; } // Make sure CompiledIC_lock in unlocked, since we might update some // inline caches. If it is, we just bail-out and try later. if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return; // Check for restart assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid"); if (_current == NULL) { _seen = 0; _invocations = NmethodSweepFraction; _current = CodeCache::first_nmethod(); _traversals += 1; if (PrintMethodFlushing) { tty->print_cr("### Sweep: stack traversal %d", _traversals); } Threads::nmethods_do(&mark_activation_closure); // reset the flags since we started a scan from the beginning. _rescan = false; _locked_seen = 0; _not_entrant_seen_on_stack = 0; } if (UseCodeCacheFlushing) { if (!CodeCache::needs_flushing()) { // scan_stacks() runs during a safepoint, no race with setters _advise_to_sweep = 0; } if (was_full()) { // There was some progress so attempt to restart the compiler jlong now = os::javaTimeMillis(); jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000; jlong curr_interval = now - _last_was_full; if ((!CodeCache::needs_flushing()) && (curr_interval > max_interval)) { CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation); set_was_full(false); // Update the _last_was_full time so we can tell how fast the // code cache is filling up _last_was_full = os::javaTimeMillis(); log_sweep("restart_compiler"); } } } }
void NMethodSweeper::sweep() { assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint"); if (!MethodFlushing) return; // No need to synchronize access, since this is always executed at a // safepoint. If we aren't in the middle of scan and a rescan // hasn't been requested then just return. if (_current == NULL && !_rescan) return; // Make sure CompiledIC_lock in unlocked, since we might update some // inline caches. If it is, we just bail-out and try later. if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return; // Check for restart assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid"); if (_current == NULL) { _seen = 0; _invocations = NmethodSweepFraction; _current = CodeCache::first(); _traversals += 1; if (PrintMethodFlushing) { tty->print_cr("### Sweep: stack traversal %d", _traversals); } Threads::nmethods_do(&mark_activation_closure); // reset the flags since we started a scan from the beginning. _rescan = false; _locked_seen = 0; _not_entrant_seen_on_stack = 0; } if (PrintMethodFlushing && Verbose) { tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations); } // We want to visit all nmethods after NmethodSweepFraction invocations. // If invocation is 1 we do the rest int todo = CodeCache::nof_blobs(); if (_invocations != 1) { todo = (CodeCache::nof_blobs() - _seen) / _invocations; _invocations--; } for(int i = 0; i < todo && _current != NULL; i++) { CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current if (_current->is_nmethod()) { process_nmethod((nmethod *)_current); } _seen++; _current = next; } // Because we could stop on a codeBlob other than an nmethod we skip forward // to the next nmethod (if any). codeBlobs other than nmethods can be freed // async to us and make _current invalid while we sleep. while (_current != NULL && !_current->is_nmethod()) { _current = CodeCache::next(_current); } if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) { // we've completed a scan without making progress but there were // nmethods we were unable to process either because they were // locked or were still on stack. We don't have to aggresively // clean them up so just stop scanning. We could scan once more // but that complicates the control logic and it's unlikely to // matter much. if (PrintMethodFlushing) { tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep"); } } if (UseCodeCacheFlushing) { if (!CodeCache::needs_flushing()) { // In a safepoint, no race with setters _advise_to_sweep = 0; } if (was_full()) { // There was some progress so attempt to restart the compiler jlong now = os::javaTimeMillis(); jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000; jlong curr_interval = now - _last_was_full; if ((!CodeCache::needs_flushing()) && (curr_interval > max_interval)) { CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation); set_was_full(false); // Update the _last_was_full time so we can tell how fast the // code cache is filling up _last_was_full = os::javaTimeMillis(); if (PrintMethodFlushing) { tty->print_cr("### sweeper: Live blobs:" UINT32_FORMAT "/Free code cache:" SIZE_FORMAT " bytes, restarting compiler", CodeCache::nof_blobs(), CodeCache::unallocated_capacity()); } if (LogCompilation && (xtty != NULL)) { ttyLocker ttyl; xtty->begin_elem("restart_compiler live_blobs='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'", CodeCache::nof_blobs(), CodeCache::unallocated_capacity()); xtty->stamp(); xtty->end_elem(); } } } } }