duke@435: /* duke@435: * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_sweeper.cpp.incl" duke@435: duke@435: long NMethodSweeper::_traversals = 0; // No. of stack traversals performed duke@435: CodeBlob* NMethodSweeper::_current = NULL; // Current nmethod duke@435: int NMethodSweeper::_seen = 0 ; // No. of blobs we have currently processed in current pass of CodeCache duke@435: int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass duke@435: duke@435: jint NMethodSweeper::_locked_seen = 0; duke@435: jint NMethodSweeper::_not_entrant_seen_on_stack = 0; duke@435: bool NMethodSweeper::_rescan = false; duke@435: duke@435: void NMethodSweeper::sweep() { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint"); duke@435: if (!MethodFlushing) return; duke@435: duke@435: // No need to synchronize access, since this is always executed at a duke@435: // safepoint. If we aren't in the middle of scan and a rescan duke@435: // hasn't been requested then just return. duke@435: if (_current == NULL && !_rescan) return; duke@435: duke@435: // Make sure CompiledIC_lock in unlocked, since we might update some duke@435: // inline caches. If it is, we just bail-out and try later. duke@435: if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return; duke@435: duke@435: // Check for restart duke@435: assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid"); duke@435: if (_current == NULL) { duke@435: _seen = 0; duke@435: _invocations = NmethodSweepFraction; duke@435: _current = CodeCache::first(); duke@435: _traversals += 1; duke@435: if (PrintMethodFlushing) { duke@435: tty->print_cr("### Sweep: stack traversal %d", _traversals); duke@435: } duke@435: Threads::nmethods_do(); duke@435: duke@435: // reset the flags since we started a scan from the beginning. duke@435: _rescan = false; duke@435: _locked_seen = 0; duke@435: _not_entrant_seen_on_stack = 0; duke@435: } duke@435: duke@435: if (PrintMethodFlushing && Verbose) { duke@435: tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations); duke@435: } duke@435: duke@435: // We want to visit all nmethods after NmethodSweepFraction invocations. duke@435: // If invocation is 1 we do the rest duke@435: int todo = CodeCache::nof_blobs(); duke@435: if (_invocations != 1) { duke@435: todo = (CodeCache::nof_blobs() - _seen) / _invocations; duke@435: _invocations--; duke@435: } duke@435: duke@435: for(int i = 0; i < todo && _current != NULL; i++) { duke@435: CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current duke@435: if (_current->is_nmethod()) { duke@435: process_nmethod((nmethod *)_current); duke@435: } duke@435: _seen++; duke@435: _current = next; duke@435: } duke@435: // Because we could stop on a codeBlob other than an nmethod we skip forward duke@435: // to the next nmethod (if any). codeBlobs other than nmethods can be freed duke@435: // async to us and make _current invalid while we sleep. duke@435: while (_current != NULL && !_current->is_nmethod()) { duke@435: _current = CodeCache::next(_current); duke@435: } duke@435: duke@435: if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) { duke@435: // we've completed a scan without making progress but there were duke@435: // nmethods we were unable to process either because they were duke@435: // locked or were still on stack. We don't have to aggresively duke@435: // clean them up so just stop scanning. We could scan once more duke@435: // but that complicates the control logic and it's unlikely to duke@435: // matter much. duke@435: if (PrintMethodFlushing) { duke@435: tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void NMethodSweeper::process_nmethod(nmethod *nm) { duke@435: // Skip methods that are currently referenced by the VM duke@435: if (nm->is_locked_by_vm()) { duke@435: // But still remember to clean-up inline caches for alive nmethods duke@435: if (nm->is_alive()) { duke@435: // Clean-up all inline caches that points to zombie/non-reentrant methods duke@435: nm->cleanup_inline_caches(); duke@435: } else { duke@435: _locked_seen++; duke@435: } duke@435: return; duke@435: } duke@435: duke@435: if (nm->is_zombie()) { duke@435: // If it is first time, we see nmethod then we mark it. Otherwise, duke@435: // we reclame it. When we have seen a zombie method twice, we know that duke@435: // there are no inline caches that referes to it. duke@435: if (nm->is_marked_for_reclamation()) { duke@435: assert(!nm->is_locked_by_vm(), "must not flush locked nmethods"); duke@435: nm->flush(); duke@435: } else { duke@435: nm->mark_for_reclamation(); duke@435: _rescan = true; duke@435: } duke@435: } else if (nm->is_not_entrant()) { duke@435: // If there is no current activations of this method on the duke@435: // stack we can safely convert it to a zombie method duke@435: if (nm->can_not_entrant_be_converted()) { duke@435: nm->make_zombie(); duke@435: _rescan = true; duke@435: } else { duke@435: // Still alive, clean up its inline caches duke@435: nm->cleanup_inline_caches(); duke@435: // we coudn't transition this nmethod so don't immediately duke@435: // request a rescan. If this method stays on the stack for a duke@435: // long time we don't want to keep rescanning at every safepoint. duke@435: _not_entrant_seen_on_stack++; duke@435: } duke@435: } else if (nm->is_unloaded()) { duke@435: // Unloaded code, just make it a zombie duke@435: if (nm->is_osr_only_method()) { duke@435: // No inline caches will ever point to osr methods, so we can just remove it duke@435: nm->flush(); duke@435: } else { duke@435: nm->make_zombie(); duke@435: _rescan = true; duke@435: } duke@435: } else { duke@435: assert(nm->is_alive(), "should be alive"); duke@435: // Clean-up all inline caches that points to zombie/non-reentrant methods duke@435: nm->cleanup_inline_caches(); duke@435: } duke@435: }