annotate src/share/vm/runtime/sweeper.cpp @ 989:148e5441d916

6863023: need non-perm oops in code cache for JSR 292 Summary: Make a special root-list for those few nmethods which might contain non-perm oops. Reviewed-by: twisti, kvn, never, jmasa, ysr
author jrose
date Tue, 15 Sep 2009 21:53:47 -0700
parents a61af66fc99e
children 54b3b351d6f9
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
jrose@989 37 class MarkActivationClosure: public CodeBlobClosure {
jrose@989 38 public:
jrose@989 39 virtual void do_code_blob(CodeBlob* cb) {
jrose@989 40 // If we see an activation belonging to a non_entrant nmethod, we mark it.
jrose@989 41 if (cb->is_nmethod() && ((nmethod*)cb)->is_not_entrant()) {
jrose@989 42 ((nmethod*)cb)->mark_as_seen_on_stack();
jrose@989 43 }
jrose@989 44 }
jrose@989 45 };
jrose@989 46 static MarkActivationClosure mark_activation_closure;
jrose@989 47
duke@0 48 void NMethodSweeper::sweep() {
duke@0 49 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
duke@0 50 if (!MethodFlushing) return;
duke@0 51
duke@0 52 // No need to synchronize access, since this is always executed at a
duke@0 53 // safepoint. If we aren't in the middle of scan and a rescan
duke@0 54 // hasn't been requested then just return.
duke@0 55 if (_current == NULL && !_rescan) return;
duke@0 56
duke@0 57 // Make sure CompiledIC_lock in unlocked, since we might update some
duke@0 58 // inline caches. If it is, we just bail-out and try later.
duke@0 59 if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return;
duke@0 60
duke@0 61 // Check for restart
duke@0 62 assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
duke@0 63 if (_current == NULL) {
duke@0 64 _seen = 0;
duke@0 65 _invocations = NmethodSweepFraction;
duke@0 66 _current = CodeCache::first();
duke@0 67 _traversals += 1;
duke@0 68 if (PrintMethodFlushing) {
duke@0 69 tty->print_cr("### Sweep: stack traversal %d", _traversals);
duke@0 70 }
jrose@989 71 Threads::nmethods_do(&mark_activation_closure);
duke@0 72
duke@0 73 // reset the flags since we started a scan from the beginning.
duke@0 74 _rescan = false;
duke@0 75 _locked_seen = 0;
duke@0 76 _not_entrant_seen_on_stack = 0;
duke@0 77 }
duke@0 78
duke@0 79 if (PrintMethodFlushing && Verbose) {
duke@0 80 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations);
duke@0 81 }
duke@0 82
duke@0 83 // We want to visit all nmethods after NmethodSweepFraction invocations.
duke@0 84 // If invocation is 1 we do the rest
duke@0 85 int todo = CodeCache::nof_blobs();
duke@0 86 if (_invocations != 1) {
duke@0 87 todo = (CodeCache::nof_blobs() - _seen) / _invocations;
duke@0 88 _invocations--;
duke@0 89 }
duke@0 90
duke@0 91 for(int i = 0; i < todo && _current != NULL; i++) {
duke@0 92 CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current
duke@0 93 if (_current->is_nmethod()) {
duke@0 94 process_nmethod((nmethod *)_current);
duke@0 95 }
duke@0 96 _seen++;
duke@0 97 _current = next;
duke@0 98 }
duke@0 99 // Because we could stop on a codeBlob other than an nmethod we skip forward
duke@0 100 // to the next nmethod (if any). codeBlobs other than nmethods can be freed
duke@0 101 // async to us and make _current invalid while we sleep.
duke@0 102 while (_current != NULL && !_current->is_nmethod()) {
duke@0 103 _current = CodeCache::next(_current);
duke@0 104 }
duke@0 105
duke@0 106 if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) {
duke@0 107 // we've completed a scan without making progress but there were
duke@0 108 // nmethods we were unable to process either because they were
duke@0 109 // locked or were still on stack. We don't have to aggresively
duke@0 110 // clean them up so just stop scanning. We could scan once more
duke@0 111 // but that complicates the control logic and it's unlikely to
duke@0 112 // matter much.
duke@0 113 if (PrintMethodFlushing) {
duke@0 114 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
duke@0 115 }
duke@0 116 }
duke@0 117 }
duke@0 118
duke@0 119
duke@0 120 void NMethodSweeper::process_nmethod(nmethod *nm) {
duke@0 121 // Skip methods that are currently referenced by the VM
duke@0 122 if (nm->is_locked_by_vm()) {
duke@0 123 // But still remember to clean-up inline caches for alive nmethods
duke@0 124 if (nm->is_alive()) {
duke@0 125 // Clean-up all inline caches that points to zombie/non-reentrant methods
duke@0 126 nm->cleanup_inline_caches();
duke@0 127 } else {
duke@0 128 _locked_seen++;
duke@0 129 }
duke@0 130 return;
duke@0 131 }
duke@0 132
duke@0 133 if (nm->is_zombie()) {
duke@0 134 // If it is first time, we see nmethod then we mark it. Otherwise,
duke@0 135 // we reclame it. When we have seen a zombie method twice, we know that
duke@0 136 // there are no inline caches that referes to it.
duke@0 137 if (nm->is_marked_for_reclamation()) {
duke@0 138 assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
duke@0 139 nm->flush();
duke@0 140 } else {
duke@0 141 nm->mark_for_reclamation();
duke@0 142 _rescan = true;
duke@0 143 }
duke@0 144 } else if (nm->is_not_entrant()) {
duke@0 145 // If there is no current activations of this method on the
duke@0 146 // stack we can safely convert it to a zombie method
duke@0 147 if (nm->can_not_entrant_be_converted()) {
duke@0 148 nm->make_zombie();
duke@0 149 _rescan = true;
duke@0 150 } else {
duke@0 151 // Still alive, clean up its inline caches
duke@0 152 nm->cleanup_inline_caches();
duke@0 153 // we coudn't transition this nmethod so don't immediately
duke@0 154 // request a rescan. If this method stays on the stack for a
duke@0 155 // long time we don't want to keep rescanning at every safepoint.
duke@0 156 _not_entrant_seen_on_stack++;
duke@0 157 }
duke@0 158 } else if (nm->is_unloaded()) {
duke@0 159 // Unloaded code, just make it a zombie
duke@0 160 if (nm->is_osr_only_method()) {
duke@0 161 // No inline caches will ever point to osr methods, so we can just remove it
duke@0 162 nm->flush();
duke@0 163 } else {
duke@0 164 nm->make_zombie();
duke@0 165 _rescan = true;
duke@0 166 }
duke@0 167 } else {
duke@0 168 assert(nm->is_alive(), "should be alive");
duke@0 169 // Clean-up all inline caches that points to zombie/non-reentrant methods
duke@0 170 nm->cleanup_inline_caches();
duke@0 171 }
duke@0 172 }