1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/runtime/sweeper.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,161 @@ 1.4 +/* 1.5 + * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_sweeper.cpp.incl" 1.30 + 1.31 +long NMethodSweeper::_traversals = 0; // No. of stack traversals performed 1.32 +CodeBlob* NMethodSweeper::_current = NULL; // Current nmethod 1.33 +int NMethodSweeper::_seen = 0 ; // No. of blobs we have currently processed in current pass of CodeCache 1.34 +int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass 1.35 + 1.36 +jint NMethodSweeper::_locked_seen = 0; 1.37 +jint NMethodSweeper::_not_entrant_seen_on_stack = 0; 1.38 +bool NMethodSweeper::_rescan = false; 1.39 + 1.40 +void NMethodSweeper::sweep() { 1.41 + assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint"); 1.42 + if (!MethodFlushing) return; 1.43 + 1.44 + // No need to synchronize access, since this is always executed at a 1.45 + // safepoint. If we aren't in the middle of scan and a rescan 1.46 + // hasn't been requested then just return. 1.47 + if (_current == NULL && !_rescan) return; 1.48 + 1.49 + // Make sure CompiledIC_lock in unlocked, since we might update some 1.50 + // inline caches. If it is, we just bail-out and try later. 1.51 + if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return; 1.52 + 1.53 + // Check for restart 1.54 + assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid"); 1.55 + if (_current == NULL) { 1.56 + _seen = 0; 1.57 + _invocations = NmethodSweepFraction; 1.58 + _current = CodeCache::first(); 1.59 + _traversals += 1; 1.60 + if (PrintMethodFlushing) { 1.61 + tty->print_cr("### Sweep: stack traversal %d", _traversals); 1.62 + } 1.63 + Threads::nmethods_do(); 1.64 + 1.65 + // reset the flags since we started a scan from the beginning. 1.66 + _rescan = false; 1.67 + _locked_seen = 0; 1.68 + _not_entrant_seen_on_stack = 0; 1.69 + } 1.70 + 1.71 + if (PrintMethodFlushing && Verbose) { 1.72 + tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations); 1.73 + } 1.74 + 1.75 + // We want to visit all nmethods after NmethodSweepFraction invocations. 1.76 + // If invocation is 1 we do the rest 1.77 + int todo = CodeCache::nof_blobs(); 1.78 + if (_invocations != 1) { 1.79 + todo = (CodeCache::nof_blobs() - _seen) / _invocations; 1.80 + _invocations--; 1.81 + } 1.82 + 1.83 + for(int i = 0; i < todo && _current != NULL; i++) { 1.84 + CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current 1.85 + if (_current->is_nmethod()) { 1.86 + process_nmethod((nmethod *)_current); 1.87 + } 1.88 + _seen++; 1.89 + _current = next; 1.90 + } 1.91 + // Because we could stop on a codeBlob other than an nmethod we skip forward 1.92 + // to the next nmethod (if any). codeBlobs other than nmethods can be freed 1.93 + // async to us and make _current invalid while we sleep. 1.94 + while (_current != NULL && !_current->is_nmethod()) { 1.95 + _current = CodeCache::next(_current); 1.96 + } 1.97 + 1.98 + if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) { 1.99 + // we've completed a scan without making progress but there were 1.100 + // nmethods we were unable to process either because they were 1.101 + // locked or were still on stack. We don't have to aggresively 1.102 + // clean them up so just stop scanning. We could scan once more 1.103 + // but that complicates the control logic and it's unlikely to 1.104 + // matter much. 1.105 + if (PrintMethodFlushing) { 1.106 + tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep"); 1.107 + } 1.108 + } 1.109 +} 1.110 + 1.111 + 1.112 +void NMethodSweeper::process_nmethod(nmethod *nm) { 1.113 + // Skip methods that are currently referenced by the VM 1.114 + if (nm->is_locked_by_vm()) { 1.115 + // But still remember to clean-up inline caches for alive nmethods 1.116 + if (nm->is_alive()) { 1.117 + // Clean-up all inline caches that points to zombie/non-reentrant methods 1.118 + nm->cleanup_inline_caches(); 1.119 + } else { 1.120 + _locked_seen++; 1.121 + } 1.122 + return; 1.123 + } 1.124 + 1.125 + if (nm->is_zombie()) { 1.126 + // If it is first time, we see nmethod then we mark it. Otherwise, 1.127 + // we reclame it. When we have seen a zombie method twice, we know that 1.128 + // there are no inline caches that referes to it. 1.129 + if (nm->is_marked_for_reclamation()) { 1.130 + assert(!nm->is_locked_by_vm(), "must not flush locked nmethods"); 1.131 + nm->flush(); 1.132 + } else { 1.133 + nm->mark_for_reclamation(); 1.134 + _rescan = true; 1.135 + } 1.136 + } else if (nm->is_not_entrant()) { 1.137 + // If there is no current activations of this method on the 1.138 + // stack we can safely convert it to a zombie method 1.139 + if (nm->can_not_entrant_be_converted()) { 1.140 + nm->make_zombie(); 1.141 + _rescan = true; 1.142 + } else { 1.143 + // Still alive, clean up its inline caches 1.144 + nm->cleanup_inline_caches(); 1.145 + // we coudn't transition this nmethod so don't immediately 1.146 + // request a rescan. If this method stays on the stack for a 1.147 + // long time we don't want to keep rescanning at every safepoint. 1.148 + _not_entrant_seen_on_stack++; 1.149 + } 1.150 + } else if (nm->is_unloaded()) { 1.151 + // Unloaded code, just make it a zombie 1.152 + if (nm->is_osr_only_method()) { 1.153 + // No inline caches will ever point to osr methods, so we can just remove it 1.154 + nm->flush(); 1.155 + } else { 1.156 + nm->make_zombie(); 1.157 + _rescan = true; 1.158 + } 1.159 + } else { 1.160 + assert(nm->is_alive(), "should be alive"); 1.161 + // Clean-up all inline caches that points to zombie/non-reentrant methods 1.162 + nm->cleanup_inline_caches(); 1.163 + } 1.164 +}