annotate src/share/vm/runtime/sweeper.cpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 8b46c4d82093 148e5441d916
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 1997-2005 Sun Microsystems, Inc. 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 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 # include "incls/_precompiled.incl"
duke@0 26 # include "incls/_sweeper.cpp.incl"
duke@0 27
duke@0 28 long NMethodSweeper::_traversals = 0; // No. of stack traversals performed
duke@0 29 CodeBlob* NMethodSweeper::_current = NULL; // Current nmethod
duke@0 30 int NMethodSweeper::_seen = 0 ; // No. of blobs we have currently processed in current pass of CodeCache
duke@0 31 int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass
duke@0 32
duke@0 33 jint NMethodSweeper::_locked_seen = 0;
duke@0 34 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
duke@0 35 bool NMethodSweeper::_rescan = false;
duke@0 36
duke@0 37 void NMethodSweeper::sweep() {
duke@0 38 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
duke@0 39 if (!MethodFlushing) return;
duke@0 40
duke@0 41 // No need to synchronize access, since this is always executed at a
duke@0 42 // safepoint. If we aren't in the middle of scan and a rescan
duke@0 43 // hasn't been requested then just return.
duke@0 44 if (_current == NULL && !_rescan) return;
duke@0 45
duke@0 46 // Make sure CompiledIC_lock in unlocked, since we might update some
duke@0 47 // inline caches. If it is, we just bail-out and try later.
duke@0 48 if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return;
duke@0 49
duke@0 50 // Check for restart
duke@0 51 assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
duke@0 52 if (_current == NULL) {
duke@0 53 _seen = 0;
duke@0 54 _invocations = NmethodSweepFraction;
duke@0 55 _current = CodeCache::first();
duke@0 56 _traversals += 1;
duke@0 57 if (PrintMethodFlushing) {
duke@0 58 tty->print_cr("### Sweep: stack traversal %d", _traversals);
duke@0 59 }
duke@0 60 Threads::nmethods_do();
duke@0 61
duke@0 62 // reset the flags since we started a scan from the beginning.
duke@0 63 _rescan = false;
duke@0 64 _locked_seen = 0;
duke@0 65 _not_entrant_seen_on_stack = 0;
duke@0 66 }
duke@0 67
duke@0 68 if (PrintMethodFlushing && Verbose) {
duke@0 69 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations);
duke@0 70 }
duke@0 71
duke@0 72 // We want to visit all nmethods after NmethodSweepFraction invocations.
duke@0 73 // If invocation is 1 we do the rest
duke@0 74 int todo = CodeCache::nof_blobs();
duke@0 75 if (_invocations != 1) {
duke@0 76 todo = (CodeCache::nof_blobs() - _seen) / _invocations;
duke@0 77 _invocations--;
duke@0 78 }
duke@0 79
duke@0 80 for(int i = 0; i < todo && _current != NULL; i++) {
duke@0 81 CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current
duke@0 82 if (_current->is_nmethod()) {
duke@0 83 process_nmethod((nmethod *)_current);
duke@0 84 }
duke@0 85 _seen++;
duke@0 86 _current = next;
duke@0 87 }
duke@0 88 // Because we could stop on a codeBlob other than an nmethod we skip forward
duke@0 89 // to the next nmethod (if any). codeBlobs other than nmethods can be freed
duke@0 90 // async to us and make _current invalid while we sleep.
duke@0 91 while (_current != NULL && !_current->is_nmethod()) {
duke@0 92 _current = CodeCache::next(_current);
duke@0 93 }
duke@0 94
duke@0 95 if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) {
duke@0 96 // we've completed a scan without making progress but there were
duke@0 97 // nmethods we were unable to process either because they were
duke@0 98 // locked or were still on stack. We don't have to aggresively
duke@0 99 // clean them up so just stop scanning. We could scan once more
duke@0 100 // but that complicates the control logic and it's unlikely to
duke@0 101 // matter much.
duke@0 102 if (PrintMethodFlushing) {
duke@0 103 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
duke@0 104 }
duke@0 105 }
duke@0 106 }
duke@0 107
duke@0 108
duke@0 109 void NMethodSweeper::process_nmethod(nmethod *nm) {
duke@0 110 // Skip methods that are currently referenced by the VM
duke@0 111 if (nm->is_locked_by_vm()) {
duke@0 112 // But still remember to clean-up inline caches for alive nmethods
duke@0 113 if (nm->is_alive()) {
duke@0 114 // Clean-up all inline caches that points to zombie/non-reentrant methods
duke@0 115 nm->cleanup_inline_caches();
duke@0 116 } else {
duke@0 117 _locked_seen++;
duke@0 118 }
duke@0 119 return;
duke@0 120 }
duke@0 121
duke@0 122 if (nm->is_zombie()) {
duke@0 123 // If it is first time, we see nmethod then we mark it. Otherwise,
duke@0 124 // we reclame it. When we have seen a zombie method twice, we know that
duke@0 125 // there are no inline caches that referes to it.
duke@0 126 if (nm->is_marked_for_reclamation()) {
duke@0 127 assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
duke@0 128 nm->flush();
duke@0 129 } else {
duke@0 130 nm->mark_for_reclamation();
duke@0 131 _rescan = true;
duke@0 132 }
duke@0 133 } else if (nm->is_not_entrant()) {
duke@0 134 // If there is no current activations of this method on the
duke@0 135 // stack we can safely convert it to a zombie method
duke@0 136 if (nm->can_not_entrant_be_converted()) {
duke@0 137 nm->make_zombie();
duke@0 138 _rescan = true;
duke@0 139 } else {
duke@0 140 // Still alive, clean up its inline caches
duke@0 141 nm->cleanup_inline_caches();
duke@0 142 // we coudn't transition this nmethod so don't immediately
duke@0 143 // request a rescan. If this method stays on the stack for a
duke@0 144 // long time we don't want to keep rescanning at every safepoint.
duke@0 145 _not_entrant_seen_on_stack++;
duke@0 146 }
duke@0 147 } else if (nm->is_unloaded()) {
duke@0 148 // Unloaded code, just make it a zombie
duke@0 149 if (nm->is_osr_only_method()) {
duke@0 150 // No inline caches will ever point to osr methods, so we can just remove it
duke@0 151 nm->flush();
duke@0 152 } else {
duke@0 153 nm->make_zombie();
duke@0 154 _rescan = true;
duke@0 155 }
duke@0 156 } else {
duke@0 157 assert(nm->is_alive(), "should be alive");
duke@0 158 // Clean-up all inline caches that points to zombie/non-reentrant methods
duke@0 159 nm->cleanup_inline_caches();
duke@0 160 }
duke@0 161 }