src/share/vm/runtime/sweeper.cpp

Thu, 11 Jul 2013 01:11:52 -0700

author
roland
date
Thu, 11 Jul 2013 01:11:52 -0700
changeset 5385
ec173c8f3739
parent 5237
f2110083203d
child 5734
ab274453d37f
permissions
-rw-r--r--

Merge

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

mercurial