src/share/vm/runtime/sweeper.cpp

Fri, 29 Jan 2010 09:27:22 -0800

author
kvn
date
Fri, 29 Jan 2010 09:27:22 -0800
changeset 1637
5f24d0319e54
parent 1435
a1423fe86a18
child 1893
bfe29ec02863
permissions
-rw-r--r--

4360113: Evict nmethods when code cache gets full
Summary: Speculatively unload the oldest nmethods when code cache gets full.
Reviewed-by: never, kvn
Contributed-by: eric.caspole@amd.com

duke@435 1 /*
xdono@1383 2 * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_sweeper.cpp.incl"
duke@435 27
duke@435 28 long NMethodSweeper::_traversals = 0; // No. of stack traversals performed
duke@435 29 CodeBlob* NMethodSweeper::_current = NULL; // Current nmethod
duke@435 30 int NMethodSweeper::_seen = 0 ; // No. of blobs we have currently processed in current pass of CodeCache
duke@435 31 int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass
duke@435 32
duke@435 33 jint NMethodSweeper::_locked_seen = 0;
duke@435 34 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
duke@435 35 bool NMethodSweeper::_rescan = false;
kvn@1637 36 bool NMethodSweeper::_was_full = false;
kvn@1637 37 jint NMethodSweeper::_advise_to_sweep = 0;
kvn@1637 38 jlong NMethodSweeper::_last_was_full = 0;
kvn@1637 39 uint NMethodSweeper::_highest_marked = 0;
kvn@1637 40 long NMethodSweeper::_was_full_traversal = 0;
duke@435 41
jrose@1424 42 class MarkActivationClosure: public CodeBlobClosure {
jrose@1424 43 public:
jrose@1424 44 virtual void do_code_blob(CodeBlob* cb) {
jrose@1424 45 // If we see an activation belonging to a non_entrant nmethod, we mark it.
jrose@1424 46 if (cb->is_nmethod() && ((nmethod*)cb)->is_not_entrant()) {
jrose@1424 47 ((nmethod*)cb)->mark_as_seen_on_stack();
jrose@1424 48 }
jrose@1424 49 }
jrose@1424 50 };
jrose@1424 51 static MarkActivationClosure mark_activation_closure;
jrose@1424 52
duke@435 53 void NMethodSweeper::sweep() {
duke@435 54 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
duke@435 55 if (!MethodFlushing) return;
duke@435 56
duke@435 57 // No need to synchronize access, since this is always executed at a
duke@435 58 // safepoint. If we aren't in the middle of scan and a rescan
duke@435 59 // hasn't been requested then just return.
duke@435 60 if (_current == NULL && !_rescan) return;
duke@435 61
duke@435 62 // Make sure CompiledIC_lock in unlocked, since we might update some
duke@435 63 // inline caches. If it is, we just bail-out and try later.
duke@435 64 if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return;
duke@435 65
duke@435 66 // Check for restart
duke@435 67 assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
duke@435 68 if (_current == NULL) {
duke@435 69 _seen = 0;
duke@435 70 _invocations = NmethodSweepFraction;
duke@435 71 _current = CodeCache::first();
duke@435 72 _traversals += 1;
duke@435 73 if (PrintMethodFlushing) {
duke@435 74 tty->print_cr("### Sweep: stack traversal %d", _traversals);
duke@435 75 }
jrose@1424 76 Threads::nmethods_do(&mark_activation_closure);
duke@435 77
duke@435 78 // reset the flags since we started a scan from the beginning.
duke@435 79 _rescan = false;
duke@435 80 _locked_seen = 0;
duke@435 81 _not_entrant_seen_on_stack = 0;
duke@435 82 }
duke@435 83
duke@435 84 if (PrintMethodFlushing && Verbose) {
duke@435 85 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations);
duke@435 86 }
duke@435 87
duke@435 88 // We want to visit all nmethods after NmethodSweepFraction invocations.
duke@435 89 // If invocation is 1 we do the rest
duke@435 90 int todo = CodeCache::nof_blobs();
duke@435 91 if (_invocations != 1) {
duke@435 92 todo = (CodeCache::nof_blobs() - _seen) / _invocations;
duke@435 93 _invocations--;
duke@435 94 }
duke@435 95
duke@435 96 for(int i = 0; i < todo && _current != NULL; i++) {
duke@435 97 CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current
duke@435 98 if (_current->is_nmethod()) {
duke@435 99 process_nmethod((nmethod *)_current);
duke@435 100 }
duke@435 101 _seen++;
duke@435 102 _current = next;
duke@435 103 }
duke@435 104 // Because we could stop on a codeBlob other than an nmethod we skip forward
duke@435 105 // to the next nmethod (if any). codeBlobs other than nmethods can be freed
duke@435 106 // async to us and make _current invalid while we sleep.
duke@435 107 while (_current != NULL && !_current->is_nmethod()) {
duke@435 108 _current = CodeCache::next(_current);
duke@435 109 }
duke@435 110
duke@435 111 if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) {
duke@435 112 // we've completed a scan without making progress but there were
duke@435 113 // nmethods we were unable to process either because they were
duke@435 114 // locked or were still on stack. We don't have to aggresively
duke@435 115 // clean them up so just stop scanning. We could scan once more
duke@435 116 // but that complicates the control logic and it's unlikely to
duke@435 117 // matter much.
duke@435 118 if (PrintMethodFlushing) {
duke@435 119 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
duke@435 120 }
duke@435 121 }
kvn@1637 122
kvn@1637 123 if (UseCodeCacheFlushing) {
kvn@1637 124 if (!CodeCache::needs_flushing()) {
kvn@1637 125 // In a safepoint, no race with setters
kvn@1637 126 _advise_to_sweep = 0;
kvn@1637 127 }
kvn@1637 128
kvn@1637 129 if (was_full()) {
kvn@1637 130 // There was some progress so attempt to restart the compiler
kvn@1637 131 jlong now = os::javaTimeMillis();
kvn@1637 132 jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000;
kvn@1637 133 jlong curr_interval = now - _last_was_full;
kvn@1637 134 if ((!CodeCache::needs_flushing()) && (curr_interval > max_interval)) {
kvn@1637 135 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
kvn@1637 136 set_was_full(false);
kvn@1637 137
kvn@1637 138 // Update the _last_was_full time so we can tell how fast the
kvn@1637 139 // code cache is filling up
kvn@1637 140 _last_was_full = os::javaTimeMillis();
kvn@1637 141
kvn@1637 142 if (PrintMethodFlushing) {
kvn@1637 143 tty->print_cr("### sweeper: Live blobs:" UINT32_FORMAT "/Free code cache:" SIZE_FORMAT " bytes, restarting compiler",
kvn@1637 144 CodeCache::nof_blobs(), CodeCache::unallocated_capacity());
kvn@1637 145 }
kvn@1637 146 if (LogCompilation && (xtty != NULL)) {
kvn@1637 147 ttyLocker ttyl;
kvn@1637 148 xtty->begin_elem("restart_compiler live_blobs='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
kvn@1637 149 CodeCache::nof_blobs(), CodeCache::unallocated_capacity());
kvn@1637 150 xtty->stamp();
kvn@1637 151 xtty->end_elem();
kvn@1637 152 }
kvn@1637 153 }
kvn@1637 154 }
kvn@1637 155 }
duke@435 156 }
duke@435 157
duke@435 158
duke@435 159 void NMethodSweeper::process_nmethod(nmethod *nm) {
duke@435 160 // Skip methods that are currently referenced by the VM
duke@435 161 if (nm->is_locked_by_vm()) {
duke@435 162 // But still remember to clean-up inline caches for alive nmethods
duke@435 163 if (nm->is_alive()) {
duke@435 164 // Clean-up all inline caches that points to zombie/non-reentrant methods
duke@435 165 nm->cleanup_inline_caches();
duke@435 166 } else {
duke@435 167 _locked_seen++;
duke@435 168 }
duke@435 169 return;
duke@435 170 }
duke@435 171
duke@435 172 if (nm->is_zombie()) {
duke@435 173 // If it is first time, we see nmethod then we mark it. Otherwise,
duke@435 174 // we reclame it. When we have seen a zombie method twice, we know that
duke@435 175 // there are no inline caches that referes to it.
duke@435 176 if (nm->is_marked_for_reclamation()) {
duke@435 177 assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
ysr@1376 178 if (PrintMethodFlushing && Verbose) {
kvn@1637 179 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
ysr@1376 180 }
duke@435 181 nm->flush();
duke@435 182 } else {
ysr@1376 183 if (PrintMethodFlushing && Verbose) {
kvn@1637 184 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
ysr@1376 185 }
duke@435 186 nm->mark_for_reclamation();
duke@435 187 _rescan = true;
duke@435 188 }
duke@435 189 } else if (nm->is_not_entrant()) {
duke@435 190 // If there is no current activations of this method on the
duke@435 191 // stack we can safely convert it to a zombie method
duke@435 192 if (nm->can_not_entrant_be_converted()) {
ysr@1376 193 if (PrintMethodFlushing && Verbose) {
kvn@1637 194 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
ysr@1376 195 }
duke@435 196 nm->make_zombie();
duke@435 197 _rescan = true;
duke@435 198 } else {
duke@435 199 // Still alive, clean up its inline caches
duke@435 200 nm->cleanup_inline_caches();
duke@435 201 // we coudn't transition this nmethod so don't immediately
duke@435 202 // request a rescan. If this method stays on the stack for a
duke@435 203 // long time we don't want to keep rescanning at every safepoint.
duke@435 204 _not_entrant_seen_on_stack++;
duke@435 205 }
duke@435 206 } else if (nm->is_unloaded()) {
duke@435 207 // Unloaded code, just make it a zombie
ysr@1376 208 if (PrintMethodFlushing && Verbose)
kvn@1637 209 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm);
ysr@1376 210 if (nm->is_osr_method()) {
duke@435 211 // No inline caches will ever point to osr methods, so we can just remove it
duke@435 212 nm->flush();
duke@435 213 } else {
duke@435 214 nm->make_zombie();
duke@435 215 _rescan = true;
duke@435 216 }
duke@435 217 } else {
duke@435 218 assert(nm->is_alive(), "should be alive");
kvn@1637 219
kvn@1637 220 if (UseCodeCacheFlushing) {
kvn@1637 221 if ((nm->method()->code() != nm) && !(nm->is_locked_by_vm()) && !(nm->is_osr_method()) &&
kvn@1637 222 (_traversals > _was_full_traversal+2) && (((uint)nm->compile_id()) < _highest_marked) &&
kvn@1637 223 CodeCache::needs_flushing()) {
kvn@1637 224 // This method has not been called since the forced cleanup happened
kvn@1637 225 nm->make_not_entrant();
kvn@1637 226 }
kvn@1637 227 }
kvn@1637 228
duke@435 229 // Clean-up all inline caches that points to zombie/non-reentrant methods
duke@435 230 nm->cleanup_inline_caches();
duke@435 231 }
duke@435 232 }
kvn@1637 233
kvn@1637 234 // Code cache unloading: when compilers notice the code cache is getting full,
kvn@1637 235 // they will call a vm op that comes here. This code attempts to speculatively
kvn@1637 236 // unload the oldest half of the nmethods (based on the compile job id) by
kvn@1637 237 // saving the old code in a list in the CodeCache. Then
kvn@1637 238 // execution resumes. If a method so marked is not called by the second
kvn@1637 239 // safepoint from the current one, the nmethod will be marked non-entrant and
kvn@1637 240 // got rid of by normal sweeping. If the method is called, the methodOop's
kvn@1637 241 // _code field is restored and the methodOop/nmethod
kvn@1637 242 // go back to their normal state.
kvn@1637 243 void NMethodSweeper::handle_full_code_cache(bool is_full) {
kvn@1637 244 // Only the first one to notice can advise us to start early cleaning
kvn@1637 245 if (!is_full){
kvn@1637 246 jint old = Atomic::cmpxchg( 1, &_advise_to_sweep, 0 );
kvn@1637 247 if (old != 0) {
kvn@1637 248 return;
kvn@1637 249 }
kvn@1637 250 }
kvn@1637 251
kvn@1637 252 if (is_full) {
kvn@1637 253 // Since code cache is full, immediately stop new compiles
kvn@1637 254 bool did_set = CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation);
kvn@1637 255 if (!did_set) {
kvn@1637 256 // only the first to notice can start the cleaning,
kvn@1637 257 // others will go back and block
kvn@1637 258 return;
kvn@1637 259 }
kvn@1637 260 set_was_full(true);
kvn@1637 261
kvn@1637 262 // If we run out within MinCodeCacheFlushingInterval of the last unload time, give up
kvn@1637 263 jlong now = os::javaTimeMillis();
kvn@1637 264 jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000;
kvn@1637 265 jlong curr_interval = now - _last_was_full;
kvn@1637 266 if (curr_interval < max_interval) {
kvn@1637 267 _rescan = true;
kvn@1637 268 if (PrintMethodFlushing) {
kvn@1637 269 tty->print_cr("### handle full too often, turning off compiler");
kvn@1637 270 }
kvn@1637 271 if (LogCompilation && (xtty != NULL)) {
kvn@1637 272 ttyLocker ttyl;
kvn@1637 273 xtty->begin_elem("disable_compiler flushing_interval='" UINT64_FORMAT "' live_blobs='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
kvn@1637 274 curr_interval/1000, CodeCache::nof_blobs(), CodeCache::unallocated_capacity());
kvn@1637 275 xtty->stamp();
kvn@1637 276 xtty->end_elem();
kvn@1637 277 }
kvn@1637 278 return;
kvn@1637 279 }
kvn@1637 280 }
kvn@1637 281
kvn@1637 282 VM_HandleFullCodeCache op(is_full);
kvn@1637 283 VMThread::execute(&op);
kvn@1637 284
kvn@1637 285 // rescan again as soon as possible
kvn@1637 286 _rescan = true;
kvn@1637 287 }
kvn@1637 288
kvn@1637 289 void NMethodSweeper::speculative_disconnect_nmethods(bool is_full) {
kvn@1637 290 // If there was a race in detecting full code cache, only run
kvn@1637 291 // one vm op for it or keep the compiler shut off
kvn@1637 292
kvn@1637 293 debug_only(jlong start = os::javaTimeMillis();)
kvn@1637 294
kvn@1637 295 if ((!was_full()) && (is_full)) {
kvn@1637 296 if (!CodeCache::needs_flushing()) {
kvn@1637 297 if (PrintMethodFlushing) {
kvn@1637 298 tty->print_cr("### sweeper: Live blobs:" UINT32_FORMAT "/Free code cache:" SIZE_FORMAT " bytes, restarting compiler",
kvn@1637 299 CodeCache::nof_blobs(), CodeCache::unallocated_capacity());
kvn@1637 300 }
kvn@1637 301 if (LogCompilation && (xtty != NULL)) {
kvn@1637 302 ttyLocker ttyl;
kvn@1637 303 xtty->begin_elem("restart_compiler live_blobs='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
kvn@1637 304 CodeCache::nof_blobs(), CodeCache::unallocated_capacity());
kvn@1637 305 xtty->stamp();
kvn@1637 306 xtty->end_elem();
kvn@1637 307 }
kvn@1637 308 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
kvn@1637 309 return;
kvn@1637 310 }
kvn@1637 311 }
kvn@1637 312
kvn@1637 313 // Traverse the code cache trying to dump the oldest nmethods
kvn@1637 314 uint curr_max_comp_id = CompileBroker::get_compilation_id();
kvn@1637 315 uint flush_target = ((curr_max_comp_id - _highest_marked) >> 1) + _highest_marked;
kvn@1637 316 if (PrintMethodFlushing && Verbose) {
kvn@1637 317 tty->print_cr("### Cleaning code cache: Live blobs:" UINT32_FORMAT "/Free code cache:" SIZE_FORMAT " bytes",
kvn@1637 318 CodeCache::nof_blobs(), CodeCache::unallocated_capacity());
kvn@1637 319 }
kvn@1637 320 if (LogCompilation && (xtty != NULL)) {
kvn@1637 321 ttyLocker ttyl;
kvn@1637 322 xtty->begin_elem("start_cleaning_code_cache live_blobs='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
kvn@1637 323 CodeCache::nof_blobs(), CodeCache::unallocated_capacity());
kvn@1637 324 xtty->stamp();
kvn@1637 325 xtty->end_elem();
kvn@1637 326 }
kvn@1637 327
kvn@1637 328 nmethod* nm = CodeCache::alive_nmethod(CodeCache::first());
kvn@1637 329 jint disconnected = 0;
kvn@1637 330 jint made_not_entrant = 0;
kvn@1637 331 while ((nm != NULL)){
kvn@1637 332 uint curr_comp_id = nm->compile_id();
kvn@1637 333
kvn@1637 334 // OSR methods cannot be flushed like this. Also, don't flush native methods
kvn@1637 335 // since they are part of the JDK in most cases
kvn@1637 336 if (nm->is_in_use() && (!nm->is_osr_method()) && (!nm->is_locked_by_vm()) &&
kvn@1637 337 (!nm->is_native_method()) && ((curr_comp_id < flush_target))) {
kvn@1637 338
kvn@1637 339 if ((nm->method()->code() == nm)) {
kvn@1637 340 // This method has not been previously considered for
kvn@1637 341 // unloading or it was restored already
kvn@1637 342 CodeCache::speculatively_disconnect(nm);
kvn@1637 343 disconnected++;
kvn@1637 344 } else if (nm->is_speculatively_disconnected()) {
kvn@1637 345 // This method was previously considered for preemptive unloading and was not called since then
kvn@1637 346 nm->method()->invocation_counter()->decay();
kvn@1637 347 nm->method()->backedge_counter()->decay();
kvn@1637 348 nm->make_not_entrant();
kvn@1637 349 made_not_entrant++;
kvn@1637 350 }
kvn@1637 351
kvn@1637 352 if (curr_comp_id > _highest_marked) {
kvn@1637 353 _highest_marked = curr_comp_id;
kvn@1637 354 }
kvn@1637 355 }
kvn@1637 356 nm = CodeCache::alive_nmethod(CodeCache::next(nm));
kvn@1637 357 }
kvn@1637 358
kvn@1637 359 if (LogCompilation && (xtty != NULL)) {
kvn@1637 360 ttyLocker ttyl;
kvn@1637 361 xtty->begin_elem("stop_cleaning_code_cache disconnected='" UINT32_FORMAT "' made_not_entrant='" UINT32_FORMAT "' live_blobs='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
kvn@1637 362 disconnected, made_not_entrant, CodeCache::nof_blobs(), CodeCache::unallocated_capacity());
kvn@1637 363 xtty->stamp();
kvn@1637 364 xtty->end_elem();
kvn@1637 365 }
kvn@1637 366
kvn@1637 367 // Shut off compiler. Sweeper will run exiting from this safepoint
kvn@1637 368 // and turn it back on if it clears enough space
kvn@1637 369 if (was_full()) {
kvn@1637 370 _last_was_full = os::javaTimeMillis();
kvn@1637 371 CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation);
kvn@1637 372 }
kvn@1637 373
kvn@1637 374 // After two more traversals the sweeper will get rid of unrestored nmethods
kvn@1637 375 _was_full_traversal = _traversals;
kvn@1637 376 #ifdef ASSERT
kvn@1637 377 jlong end = os::javaTimeMillis();
kvn@1637 378 if(PrintMethodFlushing && Verbose) {
kvn@1637 379 tty->print_cr("### sweeper: unload time: " INT64_FORMAT, end-start);
kvn@1637 380 }
kvn@1637 381 #endif
kvn@1637 382 }

mercurial