src/share/vm/runtime/sweeper.cpp

Thu, 10 May 2012 15:44:19 +0200

author
nloodin
date
Thu, 10 May 2012 15:44:19 +0200
changeset 3783
7432b9db36ff
parent 3572
cfdfbeac0a5b
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

7165755: OS Information much longer on linux than other platforms
Reviewed-by: sla, dholmes

duke@435 1 /*
trims@2708 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "code/codeCache.hpp"
stefank@2314 27 #include "code/nmethod.hpp"
stefank@2314 28 #include "compiler/compileBroker.hpp"
stefank@2314 29 #include "memory/resourceArea.hpp"
stefank@2314 30 #include "oops/methodOop.hpp"
stefank@2314 31 #include "runtime/atomic.hpp"
stefank@2314 32 #include "runtime/compilationPolicy.hpp"
stefank@2314 33 #include "runtime/mutexLocker.hpp"
stefank@2314 34 #include "runtime/os.hpp"
stefank@2314 35 #include "runtime/sweeper.hpp"
stefank@2314 36 #include "runtime/vm_operations.hpp"
stefank@2314 37 #include "utilities/events.hpp"
stefank@2314 38 #include "utilities/xmlstream.hpp"
duke@435 39
never@2916 40 #ifdef ASSERT
never@2916 41
never@2916 42 #define SWEEP(nm) record_sweep(nm, __LINE__)
never@2916 43 // Sweeper logging code
never@2916 44 class SweeperRecord {
never@2916 45 public:
never@2916 46 int traversal;
never@2916 47 int invocation;
never@2916 48 int compile_id;
never@2916 49 long traversal_mark;
never@2916 50 int state;
never@2916 51 const char* kind;
never@2916 52 address vep;
never@2916 53 address uep;
never@2916 54 int line;
never@2916 55
never@2916 56 void print() {
never@2916 57 tty->print_cr("traversal = %d invocation = %d compile_id = %d %s uep = " PTR_FORMAT " vep = "
never@2916 58 PTR_FORMAT " state = %d traversal_mark %d line = %d",
never@2916 59 traversal,
never@2916 60 invocation,
never@2916 61 compile_id,
never@2916 62 kind == NULL ? "" : kind,
never@2916 63 uep,
never@2916 64 vep,
never@2916 65 state,
never@2916 66 traversal_mark,
never@2916 67 line);
never@2916 68 }
never@2916 69 };
never@2916 70
never@2916 71 static int _sweep_index = 0;
never@2916 72 static SweeperRecord* _records = NULL;
never@2916 73
never@2916 74 void NMethodSweeper::report_events(int id, address entry) {
never@2916 75 if (_records != NULL) {
never@2916 76 for (int i = _sweep_index; i < SweeperLogEntries; i++) {
never@2916 77 if (_records[i].uep == entry ||
never@2916 78 _records[i].vep == entry ||
never@2916 79 _records[i].compile_id == id) {
never@2916 80 _records[i].print();
never@2916 81 }
never@2916 82 }
never@2916 83 for (int i = 0; i < _sweep_index; i++) {
never@2916 84 if (_records[i].uep == entry ||
never@2916 85 _records[i].vep == entry ||
never@2916 86 _records[i].compile_id == id) {
never@2916 87 _records[i].print();
never@2916 88 }
never@2916 89 }
never@2916 90 }
never@2916 91 }
never@2916 92
never@2916 93 void NMethodSweeper::report_events() {
never@2916 94 if (_records != NULL) {
never@2916 95 for (int i = _sweep_index; i < SweeperLogEntries; i++) {
never@2916 96 // skip empty records
never@2916 97 if (_records[i].vep == NULL) continue;
never@2916 98 _records[i].print();
never@2916 99 }
never@2916 100 for (int i = 0; i < _sweep_index; i++) {
never@2916 101 // skip empty records
never@2916 102 if (_records[i].vep == NULL) continue;
never@2916 103 _records[i].print();
never@2916 104 }
never@2916 105 }
never@2916 106 }
never@2916 107
never@2916 108 void NMethodSweeper::record_sweep(nmethod* nm, int line) {
never@2916 109 if (_records != NULL) {
never@2916 110 _records[_sweep_index].traversal = _traversals;
never@2916 111 _records[_sweep_index].traversal_mark = nm->_stack_traversal_mark;
never@2916 112 _records[_sweep_index].invocation = _invocations;
never@2916 113 _records[_sweep_index].compile_id = nm->compile_id();
never@2916 114 _records[_sweep_index].kind = nm->compile_kind();
never@2916 115 _records[_sweep_index].state = nm->_state;
never@2916 116 _records[_sweep_index].vep = nm->verified_entry_point();
never@2916 117 _records[_sweep_index].uep = nm->entry_point();
never@2916 118 _records[_sweep_index].line = line;
never@2916 119
never@2916 120 _sweep_index = (_sweep_index + 1) % SweeperLogEntries;
never@2916 121 }
never@2916 122 }
never@2916 123 #else
never@2916 124 #define SWEEP(nm)
never@2916 125 #endif
never@2916 126
never@2916 127
duke@435 128 long NMethodSweeper::_traversals = 0; // No. of stack traversals performed
never@1970 129 nmethod* NMethodSweeper::_current = NULL; // Current nmethod
never@1999 130 int NMethodSweeper::_seen = 0 ; // No. of nmethods we have currently processed in current pass of CodeCache
never@1999 131
never@1999 132 volatile int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass
never@1999 133 volatile int NMethodSweeper::_sweep_started = 0; // Whether a sweep is in progress.
duke@435 134
duke@435 135 jint NMethodSweeper::_locked_seen = 0;
duke@435 136 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
duke@435 137 bool NMethodSweeper::_rescan = false;
never@1893 138 bool NMethodSweeper::_do_sweep = false;
kvn@1637 139 bool NMethodSweeper::_was_full = false;
kvn@1637 140 jint NMethodSweeper::_advise_to_sweep = 0;
kvn@1637 141 jlong NMethodSweeper::_last_was_full = 0;
kvn@1637 142 uint NMethodSweeper::_highest_marked = 0;
kvn@1637 143 long NMethodSweeper::_was_full_traversal = 0;
duke@435 144
jrose@1424 145 class MarkActivationClosure: public CodeBlobClosure {
jrose@1424 146 public:
jrose@1424 147 virtual void do_code_blob(CodeBlob* cb) {
jrose@1424 148 // If we see an activation belonging to a non_entrant nmethod, we mark it.
jrose@1424 149 if (cb->is_nmethod() && ((nmethod*)cb)->is_not_entrant()) {
jrose@1424 150 ((nmethod*)cb)->mark_as_seen_on_stack();
jrose@1424 151 }
jrose@1424 152 }
jrose@1424 153 };
jrose@1424 154 static MarkActivationClosure mark_activation_closure;
jrose@1424 155
never@1893 156 void NMethodSweeper::scan_stacks() {
duke@435 157 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
duke@435 158 if (!MethodFlushing) return;
never@1893 159 _do_sweep = true;
duke@435 160
duke@435 161 // No need to synchronize access, since this is always executed at a
duke@435 162 // safepoint. If we aren't in the middle of scan and a rescan
never@1893 163 // hasn't been requested then just return. If UseCodeCacheFlushing is on and
never@1893 164 // code cache flushing is in progress, don't skip sweeping to help make progress
never@1893 165 // clearing space in the code cache.
never@1893 166 if ((_current == NULL && !_rescan) && !(UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs())) {
never@1893 167 _do_sweep = false;
never@1893 168 return;
never@1893 169 }
duke@435 170
duke@435 171 // Make sure CompiledIC_lock in unlocked, since we might update some
duke@435 172 // inline caches. If it is, we just bail-out and try later.
duke@435 173 if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return;
duke@435 174
duke@435 175 // Check for restart
duke@435 176 assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
duke@435 177 if (_current == NULL) {
duke@435 178 _seen = 0;
duke@435 179 _invocations = NmethodSweepFraction;
never@1893 180 _current = CodeCache::first_nmethod();
duke@435 181 _traversals += 1;
duke@435 182 if (PrintMethodFlushing) {
duke@435 183 tty->print_cr("### Sweep: stack traversal %d", _traversals);
duke@435 184 }
jrose@1424 185 Threads::nmethods_do(&mark_activation_closure);
duke@435 186
duke@435 187 // reset the flags since we started a scan from the beginning.
duke@435 188 _rescan = false;
duke@435 189 _locked_seen = 0;
duke@435 190 _not_entrant_seen_on_stack = 0;
duke@435 191 }
duke@435 192
kvn@1637 193 if (UseCodeCacheFlushing) {
kvn@1637 194 if (!CodeCache::needs_flushing()) {
never@1893 195 // scan_stacks() runs during a safepoint, no race with setters
kvn@1637 196 _advise_to_sweep = 0;
kvn@1637 197 }
kvn@1637 198
kvn@1637 199 if (was_full()) {
kvn@1637 200 // There was some progress so attempt to restart the compiler
kvn@1637 201 jlong now = os::javaTimeMillis();
kvn@1637 202 jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000;
kvn@1637 203 jlong curr_interval = now - _last_was_full;
kvn@1637 204 if ((!CodeCache::needs_flushing()) && (curr_interval > max_interval)) {
kvn@1637 205 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
kvn@1637 206 set_was_full(false);
kvn@1637 207
kvn@1637 208 // Update the _last_was_full time so we can tell how fast the
kvn@1637 209 // code cache is filling up
kvn@1637 210 _last_was_full = os::javaTimeMillis();
kvn@1637 211
never@1999 212 log_sweep("restart_compiler");
kvn@1637 213 }
kvn@1637 214 }
kvn@1637 215 }
duke@435 216 }
duke@435 217
never@1893 218 void NMethodSweeper::possibly_sweep() {
never@1999 219 assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode");
never@1893 220 if ((!MethodFlushing) || (!_do_sweep)) return;
never@1893 221
never@1893 222 if (_invocations > 0) {
never@1893 223 // Only one thread at a time will sweep
never@1893 224 jint old = Atomic::cmpxchg( 1, &_sweep_started, 0 );
never@1893 225 if (old != 0) {
never@1893 226 return;
never@1893 227 }
never@2916 228 #ifdef ASSERT
never@2916 229 if (LogSweeper && _records == NULL) {
never@2916 230 // Create the ring buffer for the logging code
never@2916 231 _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries);
never@2916 232 memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
never@2916 233 }
never@2916 234 #endif
never@1999 235 if (_invocations > 0) {
never@1999 236 sweep_code_cache();
never@1999 237 _invocations--;
never@1999 238 }
never@1999 239 _sweep_started = 0;
never@1893 240 }
never@1893 241 }
never@1893 242
never@1893 243 void NMethodSweeper::sweep_code_cache() {
never@1893 244 #ifdef ASSERT
never@1893 245 jlong sweep_start;
never@1999 246 if (PrintMethodFlushing) {
never@1893 247 sweep_start = os::javaTimeMillis();
never@1893 248 }
never@1893 249 #endif
never@1893 250 if (PrintMethodFlushing && Verbose) {
never@1999 251 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _invocations);
never@1893 252 }
never@1893 253
never@1999 254 // We want to visit all nmethods after NmethodSweepFraction
never@1999 255 // invocations so divide the remaining number of nmethods by the
never@1999 256 // remaining number of invocations. This is only an estimate since
never@1999 257 // the number of nmethods changes during the sweep so the final
never@1999 258 // stage must iterate until it there are no more nmethods.
never@1999 259 int todo = (CodeCache::nof_nmethods() - _seen) / _invocations;
never@1893 260
never@1893 261 assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here");
never@1893 262 assert(!CodeCache_lock->owned_by_self(), "just checking");
never@1893 263
never@1893 264 {
never@1893 265 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1893 266
never@1999 267 // The last invocation iterates until there are no more nmethods
never@1999 268 for (int i = 0; (i < todo || _invocations == 1) && _current != NULL; i++) {
iveresov@3572 269 if (SafepointSynchronize::is_synchronizing()) { // Safepoint request
iveresov@3572 270 if (PrintMethodFlushing && Verbose) {
iveresov@3572 271 tty->print_cr("### Sweep at %d out of %d, invocation: %d, yielding to safepoint", _seen, CodeCache::nof_nmethods(), _invocations);
iveresov@3572 272 }
iveresov@3572 273 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1893 274
iveresov@3572 275 assert(Thread::current()->is_Java_thread(), "should be java thread");
iveresov@3572 276 JavaThread* thread = (JavaThread*)Thread::current();
iveresov@3572 277 ThreadBlockInVM tbivm(thread);
iveresov@3572 278 thread->java_suspend_self();
iveresov@3572 279 }
never@1999 280 // Since we will give up the CodeCache_lock, always skip ahead
never@1999 281 // to the next nmethod. Other blobs can be deleted by other
never@1999 282 // threads but nmethods are only reclaimed by the sweeper.
never@1970 283 nmethod* next = CodeCache::next_nmethod(_current);
never@1893 284
never@1893 285 // Now ready to process nmethod and give up CodeCache_lock
never@1893 286 {
never@1893 287 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1970 288 process_nmethod(_current);
never@1893 289 }
never@1893 290 _seen++;
never@1893 291 _current = next;
never@1893 292 }
never@1893 293 }
never@1893 294
never@1999 295 assert(_invocations > 1 || _current == NULL, "must have scanned the whole cache");
never@1999 296
never@1893 297 if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) {
never@1893 298 // we've completed a scan without making progress but there were
never@1893 299 // nmethods we were unable to process either because they were
never@1893 300 // locked or were still on stack. We don't have to aggresively
never@1893 301 // clean them up so just stop scanning. We could scan once more
never@1893 302 // but that complicates the control logic and it's unlikely to
never@1893 303 // matter much.
never@1893 304 if (PrintMethodFlushing) {
never@1893 305 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
never@1893 306 }
never@1893 307 }
never@1893 308
never@1893 309 #ifdef ASSERT
never@1893 310 if(PrintMethodFlushing) {
never@1893 311 jlong sweep_end = os::javaTimeMillis();
never@1893 312 tty->print_cr("### sweeper: sweep time(%d): " INT64_FORMAT, _invocations, sweep_end - sweep_start);
never@1893 313 }
never@1893 314 #endif
never@1999 315
never@1999 316 if (_invocations == 1) {
never@1999 317 log_sweep("finished");
never@1999 318 }
never@1893 319 }
never@1893 320
never@2916 321 class NMethodMarker: public StackObj {
never@2916 322 private:
never@2916 323 CompilerThread* _thread;
never@2916 324 public:
never@2916 325 NMethodMarker(nmethod* nm) {
never@2916 326 _thread = CompilerThread::current();
never@2916 327 _thread->set_scanned_nmethod(nm);
never@2916 328 }
never@2916 329 ~NMethodMarker() {
never@2916 330 _thread->set_scanned_nmethod(NULL);
never@2916 331 }
never@2916 332 };
never@2916 333
duke@435 334
duke@435 335 void NMethodSweeper::process_nmethod(nmethod *nm) {
never@1893 336 assert(!CodeCache_lock->owned_by_self(), "just checking");
never@1893 337
never@2916 338 // Make sure this nmethod doesn't get unloaded during the scan,
never@2916 339 // since the locks acquired below might safepoint.
never@2916 340 NMethodMarker nmm(nm);
never@2916 341
never@2916 342 SWEEP(nm);
never@2916 343
duke@435 344 // Skip methods that are currently referenced by the VM
duke@435 345 if (nm->is_locked_by_vm()) {
duke@435 346 // But still remember to clean-up inline caches for alive nmethods
duke@435 347 if (nm->is_alive()) {
duke@435 348 // Clean-up all inline caches that points to zombie/non-reentrant methods
never@1893 349 MutexLocker cl(CompiledIC_lock);
duke@435 350 nm->cleanup_inline_caches();
never@2916 351 SWEEP(nm);
duke@435 352 } else {
duke@435 353 _locked_seen++;
never@2916 354 SWEEP(nm);
duke@435 355 }
duke@435 356 return;
duke@435 357 }
duke@435 358
duke@435 359 if (nm->is_zombie()) {
duke@435 360 // If it is first time, we see nmethod then we mark it. Otherwise,
duke@435 361 // we reclame it. When we have seen a zombie method twice, we know that
never@1999 362 // there are no inline caches that refer to it.
duke@435 363 if (nm->is_marked_for_reclamation()) {
duke@435 364 assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
ysr@1376 365 if (PrintMethodFlushing && Verbose) {
kvn@1637 366 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
ysr@1376 367 }
never@1893 368 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@435 369 nm->flush();
duke@435 370 } else {
ysr@1376 371 if (PrintMethodFlushing && Verbose) {
kvn@1637 372 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
ysr@1376 373 }
duke@435 374 nm->mark_for_reclamation();
duke@435 375 _rescan = true;
never@2916 376 SWEEP(nm);
duke@435 377 }
duke@435 378 } else if (nm->is_not_entrant()) {
duke@435 379 // If there is no current activations of this method on the
duke@435 380 // stack we can safely convert it to a zombie method
duke@435 381 if (nm->can_not_entrant_be_converted()) {
ysr@1376 382 if (PrintMethodFlushing && Verbose) {
kvn@1637 383 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
ysr@1376 384 }
duke@435 385 nm->make_zombie();
duke@435 386 _rescan = true;
never@2916 387 SWEEP(nm);
duke@435 388 } else {
duke@435 389 // Still alive, clean up its inline caches
never@1893 390 MutexLocker cl(CompiledIC_lock);
duke@435 391 nm->cleanup_inline_caches();
duke@435 392 // we coudn't transition this nmethod so don't immediately
duke@435 393 // request a rescan. If this method stays on the stack for a
never@1893 394 // long time we don't want to keep rescanning the code cache.
duke@435 395 _not_entrant_seen_on_stack++;
never@2916 396 SWEEP(nm);
duke@435 397 }
duke@435 398 } else if (nm->is_unloaded()) {
duke@435 399 // Unloaded code, just make it a zombie
ysr@1376 400 if (PrintMethodFlushing && Verbose)
kvn@1637 401 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm);
ysr@1376 402 if (nm->is_osr_method()) {
duke@435 403 // No inline caches will ever point to osr methods, so we can just remove it
never@1893 404 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@2916 405 SWEEP(nm);
duke@435 406 nm->flush();
duke@435 407 } else {
duke@435 408 nm->make_zombie();
duke@435 409 _rescan = true;
never@2916 410 SWEEP(nm);
duke@435 411 }
duke@435 412 } else {
duke@435 413 assert(nm->is_alive(), "should be alive");
kvn@1637 414
kvn@1637 415 if (UseCodeCacheFlushing) {
kvn@1637 416 if ((nm->method()->code() != nm) && !(nm->is_locked_by_vm()) && !(nm->is_osr_method()) &&
kvn@1637 417 (_traversals > _was_full_traversal+2) && (((uint)nm->compile_id()) < _highest_marked) &&
kvn@1637 418 CodeCache::needs_flushing()) {
kvn@1637 419 // This method has not been called since the forced cleanup happened
kvn@1637 420 nm->make_not_entrant();
kvn@1637 421 }
kvn@1637 422 }
kvn@1637 423
duke@435 424 // Clean-up all inline caches that points to zombie/non-reentrant methods
never@1893 425 MutexLocker cl(CompiledIC_lock);
duke@435 426 nm->cleanup_inline_caches();
never@2916 427 SWEEP(nm);
duke@435 428 }
duke@435 429 }
kvn@1637 430
kvn@1637 431 // Code cache unloading: when compilers notice the code cache is getting full,
kvn@1637 432 // they will call a vm op that comes here. This code attempts to speculatively
kvn@1637 433 // unload the oldest half of the nmethods (based on the compile job id) by
kvn@1637 434 // saving the old code in a list in the CodeCache. Then
never@1893 435 // execution resumes. If a method so marked is not called by the second sweeper
never@1893 436 // stack traversal after the current one, the nmethod will be marked non-entrant and
kvn@1637 437 // got rid of by normal sweeping. If the method is called, the methodOop's
kvn@1637 438 // _code field is restored and the methodOop/nmethod
kvn@1637 439 // go back to their normal state.
kvn@1637 440 void NMethodSweeper::handle_full_code_cache(bool is_full) {
kvn@1637 441 // Only the first one to notice can advise us to start early cleaning
kvn@1637 442 if (!is_full){
kvn@1637 443 jint old = Atomic::cmpxchg( 1, &_advise_to_sweep, 0 );
kvn@1637 444 if (old != 0) {
kvn@1637 445 return;
kvn@1637 446 }
kvn@1637 447 }
kvn@1637 448
kvn@1637 449 if (is_full) {
kvn@1637 450 // Since code cache is full, immediately stop new compiles
kvn@1637 451 bool did_set = CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation);
kvn@1637 452 if (!did_set) {
kvn@1637 453 // only the first to notice can start the cleaning,
kvn@1637 454 // others will go back and block
kvn@1637 455 return;
kvn@1637 456 }
kvn@1637 457 set_was_full(true);
kvn@1637 458
kvn@1637 459 // If we run out within MinCodeCacheFlushingInterval of the last unload time, give up
kvn@1637 460 jlong now = os::javaTimeMillis();
kvn@1637 461 jlong max_interval = (jlong)MinCodeCacheFlushingInterval * (jlong)1000;
kvn@1637 462 jlong curr_interval = now - _last_was_full;
kvn@1637 463 if (curr_interval < max_interval) {
kvn@1637 464 _rescan = true;
never@1999 465 log_sweep("disable_compiler", "flushing_interval='" UINT64_FORMAT "'",
never@1999 466 curr_interval/1000);
kvn@1637 467 return;
kvn@1637 468 }
kvn@1637 469 }
kvn@1637 470
kvn@1637 471 VM_HandleFullCodeCache op(is_full);
kvn@1637 472 VMThread::execute(&op);
kvn@1637 473
kvn@1637 474 // rescan again as soon as possible
kvn@1637 475 _rescan = true;
kvn@1637 476 }
kvn@1637 477
kvn@1637 478 void NMethodSweeper::speculative_disconnect_nmethods(bool is_full) {
kvn@1637 479 // If there was a race in detecting full code cache, only run
kvn@1637 480 // one vm op for it or keep the compiler shut off
kvn@1637 481
kvn@1637 482 debug_only(jlong start = os::javaTimeMillis();)
kvn@1637 483
kvn@1637 484 if ((!was_full()) && (is_full)) {
kvn@1637 485 if (!CodeCache::needs_flushing()) {
never@1999 486 log_sweep("restart_compiler");
kvn@1637 487 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
kvn@1637 488 return;
kvn@1637 489 }
kvn@1637 490 }
kvn@1637 491
kvn@1637 492 // Traverse the code cache trying to dump the oldest nmethods
kvn@1637 493 uint curr_max_comp_id = CompileBroker::get_compilation_id();
kvn@1637 494 uint flush_target = ((curr_max_comp_id - _highest_marked) >> 1) + _highest_marked;
never@1999 495 log_sweep("start_cleaning");
kvn@1637 496
kvn@1637 497 nmethod* nm = CodeCache::alive_nmethod(CodeCache::first());
kvn@1637 498 jint disconnected = 0;
kvn@1637 499 jint made_not_entrant = 0;
kvn@1637 500 while ((nm != NULL)){
kvn@1637 501 uint curr_comp_id = nm->compile_id();
kvn@1637 502
kvn@1637 503 // OSR methods cannot be flushed like this. Also, don't flush native methods
kvn@1637 504 // since they are part of the JDK in most cases
kvn@1637 505 if (nm->is_in_use() && (!nm->is_osr_method()) && (!nm->is_locked_by_vm()) &&
kvn@1637 506 (!nm->is_native_method()) && ((curr_comp_id < flush_target))) {
kvn@1637 507
kvn@1637 508 if ((nm->method()->code() == nm)) {
kvn@1637 509 // This method has not been previously considered for
kvn@1637 510 // unloading or it was restored already
kvn@1637 511 CodeCache::speculatively_disconnect(nm);
kvn@1637 512 disconnected++;
kvn@1637 513 } else if (nm->is_speculatively_disconnected()) {
kvn@1637 514 // This method was previously considered for preemptive unloading and was not called since then
iveresov@2138 515 CompilationPolicy::policy()->delay_compilation(nm->method());
kvn@1637 516 nm->make_not_entrant();
kvn@1637 517 made_not_entrant++;
kvn@1637 518 }
kvn@1637 519
kvn@1637 520 if (curr_comp_id > _highest_marked) {
kvn@1637 521 _highest_marked = curr_comp_id;
kvn@1637 522 }
kvn@1637 523 }
kvn@1637 524 nm = CodeCache::alive_nmethod(CodeCache::next(nm));
kvn@1637 525 }
kvn@1637 526
never@1999 527 log_sweep("stop_cleaning",
never@1999 528 "disconnected='" UINT32_FORMAT "' made_not_entrant='" UINT32_FORMAT "'",
never@1999 529 disconnected, made_not_entrant);
kvn@1637 530
never@1893 531 // Shut off compiler. Sweeper will start over with a new stack scan and
never@1893 532 // traversal cycle and turn it back on if it clears enough space.
kvn@1637 533 if (was_full()) {
kvn@1637 534 _last_was_full = os::javaTimeMillis();
kvn@1637 535 CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation);
kvn@1637 536 }
kvn@1637 537
kvn@1637 538 // After two more traversals the sweeper will get rid of unrestored nmethods
kvn@1637 539 _was_full_traversal = _traversals;
kvn@1637 540 #ifdef ASSERT
kvn@1637 541 jlong end = os::javaTimeMillis();
kvn@1637 542 if(PrintMethodFlushing && Verbose) {
kvn@1637 543 tty->print_cr("### sweeper: unload time: " INT64_FORMAT, end-start);
kvn@1637 544 }
kvn@1637 545 #endif
kvn@1637 546 }
never@1999 547
never@1999 548
never@1999 549 // Print out some state information about the current sweep and the
never@1999 550 // state of the code cache if it's requested.
never@1999 551 void NMethodSweeper::log_sweep(const char* msg, const char* format, ...) {
never@1999 552 if (PrintMethodFlushing) {
iveresov@2764 553 stringStream s;
iveresov@2764 554 // Dump code cache state into a buffer before locking the tty,
iveresov@2764 555 // because log_state() will use locks causing lock conflicts.
iveresov@2764 556 CodeCache::log_state(&s);
iveresov@2764 557
never@1999 558 ttyLocker ttyl;
never@1999 559 tty->print("### sweeper: %s ", msg);
never@1999 560 if (format != NULL) {
never@1999 561 va_list ap;
never@1999 562 va_start(ap, format);
never@1999 563 tty->vprint(format, ap);
never@1999 564 va_end(ap);
never@1999 565 }
iveresov@2764 566 tty->print_cr(s.as_string());
never@1999 567 }
never@1999 568
never@1999 569 if (LogCompilation && (xtty != NULL)) {
iveresov@2764 570 stringStream s;
iveresov@2764 571 // Dump code cache state into a buffer before locking the tty,
iveresov@2764 572 // because log_state() will use locks causing lock conflicts.
iveresov@2764 573 CodeCache::log_state(&s);
iveresov@2764 574
never@1999 575 ttyLocker ttyl;
never@2001 576 xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count());
never@1999 577 if (format != NULL) {
never@1999 578 va_list ap;
never@1999 579 va_start(ap, format);
never@1999 580 xtty->vprint(format, ap);
never@1999 581 va_end(ap);
never@1999 582 }
iveresov@2764 583 xtty->print(s.as_string());
never@1999 584 xtty->stamp();
never@1999 585 xtty->end_elem();
never@1999 586 }
never@1999 587 }

mercurial