Wed, 18 Sep 2013 07:22:20 +0200
8022883: Assertion failed: sweptCount >= flushedCount + markedCount + zombifiedCount
Summary: Provide correct number of visited nmethods to Tracing
Reviewed-by: kvn, iveresov
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; |
anoll@5734 | 272 | int swept_count = 0; |
never@1893 | 273 | |
never@1893 | 274 | assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here"); |
never@1893 | 275 | assert(!CodeCache_lock->owned_by_self(), "just checking"); |
never@1893 | 276 | |
never@1893 | 277 | { |
never@1893 | 278 | MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); |
never@1893 | 279 | |
never@1999 | 280 | // The last invocation iterates until there are no more nmethods |
never@1999 | 281 | for (int i = 0; (i < todo || _invocations == 1) && _current != NULL; i++) { |
anoll@5734 | 282 | swept_count++; |
iveresov@3572 | 283 | if (SafepointSynchronize::is_synchronizing()) { // Safepoint request |
iveresov@3572 | 284 | if (PrintMethodFlushing && Verbose) { |
iveresov@3572 | 285 | tty->print_cr("### Sweep at %d out of %d, invocation: %d, yielding to safepoint", _seen, CodeCache::nof_nmethods(), _invocations); |
iveresov@3572 | 286 | } |
iveresov@3572 | 287 | MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); |
never@1893 | 288 | |
iveresov@3572 | 289 | assert(Thread::current()->is_Java_thread(), "should be java thread"); |
iveresov@3572 | 290 | JavaThread* thread = (JavaThread*)Thread::current(); |
iveresov@3572 | 291 | ThreadBlockInVM tbivm(thread); |
iveresov@3572 | 292 | thread->java_suspend_self(); |
iveresov@3572 | 293 | } |
never@1999 | 294 | // Since we will give up the CodeCache_lock, always skip ahead |
never@1999 | 295 | // to the next nmethod. Other blobs can be deleted by other |
never@1999 | 296 | // threads but nmethods are only reclaimed by the sweeper. |
never@1970 | 297 | nmethod* next = CodeCache::next_nmethod(_current); |
never@1893 | 298 | |
never@1893 | 299 | // Now ready to process nmethod and give up CodeCache_lock |
never@1893 | 300 | { |
never@1893 | 301 | MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); |
never@1970 | 302 | process_nmethod(_current); |
never@1893 | 303 | } |
never@1893 | 304 | _seen++; |
never@1893 | 305 | _current = next; |
never@1893 | 306 | } |
never@1893 | 307 | } |
never@1893 | 308 | |
never@1999 | 309 | assert(_invocations > 1 || _current == NULL, "must have scanned the whole cache"); |
never@1999 | 310 | |
neliasso@5038 | 311 | if (!sweep_in_progress() && !_resweep && (_locked_seen || _not_entrant_seen_on_stack)) { |
never@1893 | 312 | // we've completed a scan without making progress but there were |
never@1893 | 313 | // nmethods we were unable to process either because they were |
never@1893 | 314 | // locked or were still on stack. We don't have to aggresively |
never@1893 | 315 | // clean them up so just stop scanning. We could scan once more |
never@1893 | 316 | // but that complicates the control logic and it's unlikely to |
never@1893 | 317 | // matter much. |
never@1893 | 318 | if (PrintMethodFlushing) { |
never@1893 | 319 | tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep"); |
never@1893 | 320 | } |
never@1893 | 321 | } |
never@1893 | 322 | |
sla@5237 | 323 | jlong sweep_end_counter = os::elapsed_counter(); |
sla@5237 | 324 | jlong sweep_time = sweep_end_counter - sweep_start_counter; |
sla@5237 | 325 | _total_time_sweeping += sweep_time; |
sla@5237 | 326 | _total_time_this_sweep += sweep_time; |
sla@5237 | 327 | _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time); |
sla@5237 | 328 | _total_nof_methods_reclaimed += _flushed_count; |
sla@5237 | 329 | |
sla@5237 | 330 | EventSweepCodeCache event(UNTIMED); |
sla@5237 | 331 | if (event.should_commit()) { |
sla@5237 | 332 | event.set_starttime(sweep_start_counter); |
sla@5237 | 333 | event.set_endtime(sweep_end_counter); |
sla@5237 | 334 | event.set_sweepIndex(_traversals); |
sla@5237 | 335 | event.set_sweepFractionIndex(NmethodSweepFraction - _invocations + 1); |
anoll@5734 | 336 | event.set_sweptCount(swept_count); |
sla@5237 | 337 | event.set_flushedCount(_flushed_count); |
sla@5237 | 338 | event.set_markedCount(_marked_count); |
sla@5237 | 339 | event.set_zombifiedCount(_zombified_count); |
sla@5237 | 340 | event.commit(); |
sla@5237 | 341 | } |
sla@5237 | 342 | |
never@1893 | 343 | #ifdef ASSERT |
never@1893 | 344 | if(PrintMethodFlushing) { |
sla@5237 | 345 | tty->print_cr("### sweeper: sweep time(%d): " INT64_FORMAT, _invocations, (jlong)sweep_time); |
never@1893 | 346 | } |
never@1893 | 347 | #endif |
never@1999 | 348 | |
never@1999 | 349 | if (_invocations == 1) { |
sla@5237 | 350 | _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep); |
never@1999 | 351 | log_sweep("finished"); |
never@1999 | 352 | } |
neliasso@5038 | 353 | |
neliasso@5038 | 354 | // Sweeper is the only case where memory is released, |
neliasso@5038 | 355 | // check here if it is time to restart the compiler. |
neliasso@5038 | 356 | if (UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs() && !CodeCache::needs_flushing()) { |
neliasso@5038 | 357 | CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation); |
neliasso@5038 | 358 | log_sweep("restart_compiler"); |
neliasso@5038 | 359 | } |
never@1893 | 360 | } |
never@1893 | 361 | |
never@2916 | 362 | class NMethodMarker: public StackObj { |
never@2916 | 363 | private: |
never@2916 | 364 | CompilerThread* _thread; |
never@2916 | 365 | public: |
never@2916 | 366 | NMethodMarker(nmethod* nm) { |
never@2916 | 367 | _thread = CompilerThread::current(); |
coleenp@4037 | 368 | if (!nm->is_zombie() && !nm->is_unloaded()) { |
coleenp@4037 | 369 | // Only expose live nmethods for scanning |
never@2916 | 370 | _thread->set_scanned_nmethod(nm); |
never@2916 | 371 | } |
coleenp@4037 | 372 | } |
never@2916 | 373 | ~NMethodMarker() { |
never@2916 | 374 | _thread->set_scanned_nmethod(NULL); |
never@2916 | 375 | } |
never@2916 | 376 | }; |
never@2916 | 377 | |
coleenp@4037 | 378 | void NMethodSweeper::release_nmethod(nmethod *nm) { |
coleenp@4037 | 379 | // Clean up any CompiledICHolders |
coleenp@4037 | 380 | { |
coleenp@4037 | 381 | ResourceMark rm; |
coleenp@4037 | 382 | MutexLocker ml_patch(CompiledIC_lock); |
coleenp@4037 | 383 | RelocIterator iter(nm); |
coleenp@4037 | 384 | while (iter.next()) { |
coleenp@4037 | 385 | if (iter.type() == relocInfo::virtual_call_type) { |
coleenp@4037 | 386 | CompiledIC::cleanup_call_site(iter.virtual_call_reloc()); |
coleenp@4037 | 387 | } |
coleenp@4037 | 388 | } |
coleenp@4037 | 389 | } |
coleenp@4037 | 390 | |
coleenp@4037 | 391 | MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); |
coleenp@4037 | 392 | nm->flush(); |
coleenp@4037 | 393 | } |
duke@435 | 394 | |
duke@435 | 395 | void NMethodSweeper::process_nmethod(nmethod *nm) { |
never@1893 | 396 | assert(!CodeCache_lock->owned_by_self(), "just checking"); |
never@1893 | 397 | |
never@2916 | 398 | // Make sure this nmethod doesn't get unloaded during the scan, |
never@2916 | 399 | // since the locks acquired below might safepoint. |
never@2916 | 400 | NMethodMarker nmm(nm); |
never@2916 | 401 | |
never@2916 | 402 | SWEEP(nm); |
never@2916 | 403 | |
duke@435 | 404 | // Skip methods that are currently referenced by the VM |
duke@435 | 405 | if (nm->is_locked_by_vm()) { |
duke@435 | 406 | // But still remember to clean-up inline caches for alive nmethods |
duke@435 | 407 | if (nm->is_alive()) { |
duke@435 | 408 | // Clean-up all inline caches that points to zombie/non-reentrant methods |
never@1893 | 409 | MutexLocker cl(CompiledIC_lock); |
duke@435 | 410 | nm->cleanup_inline_caches(); |
never@2916 | 411 | SWEEP(nm); |
duke@435 | 412 | } else { |
duke@435 | 413 | _locked_seen++; |
never@2916 | 414 | SWEEP(nm); |
duke@435 | 415 | } |
duke@435 | 416 | return; |
duke@435 | 417 | } |
duke@435 | 418 | |
duke@435 | 419 | if (nm->is_zombie()) { |
duke@435 | 420 | // If it is first time, we see nmethod then we mark it. Otherwise, |
duke@435 | 421 | // we reclame it. When we have seen a zombie method twice, we know that |
never@1999 | 422 | // there are no inline caches that refer to it. |
duke@435 | 423 | if (nm->is_marked_for_reclamation()) { |
duke@435 | 424 | assert(!nm->is_locked_by_vm(), "must not flush locked nmethods"); |
ysr@1376 | 425 | if (PrintMethodFlushing && Verbose) { |
kvn@1637 | 426 | tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm); |
ysr@1376 | 427 | } |
coleenp@4037 | 428 | release_nmethod(nm); |
sla@5237 | 429 | _flushed_count++; |
duke@435 | 430 | } else { |
ysr@1376 | 431 | if (PrintMethodFlushing && Verbose) { |
kvn@1637 | 432 | tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm); |
ysr@1376 | 433 | } |
duke@435 | 434 | nm->mark_for_reclamation(); |
neliasso@5038 | 435 | _resweep = true; |
sla@5237 | 436 | _marked_count++; |
never@2916 | 437 | SWEEP(nm); |
duke@435 | 438 | } |
duke@435 | 439 | } else if (nm->is_not_entrant()) { |
duke@435 | 440 | // If there is no current activations of this method on the |
duke@435 | 441 | // stack we can safely convert it to a zombie method |
duke@435 | 442 | if (nm->can_not_entrant_be_converted()) { |
ysr@1376 | 443 | if (PrintMethodFlushing && Verbose) { |
kvn@1637 | 444 | tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm); |
ysr@1376 | 445 | } |
duke@435 | 446 | nm->make_zombie(); |
neliasso@5038 | 447 | _resweep = true; |
sla@5237 | 448 | _zombified_count++; |
never@2916 | 449 | SWEEP(nm); |
duke@435 | 450 | } else { |
duke@435 | 451 | // Still alive, clean up its inline caches |
never@1893 | 452 | MutexLocker cl(CompiledIC_lock); |
duke@435 | 453 | nm->cleanup_inline_caches(); |
duke@435 | 454 | // we coudn't transition this nmethod so don't immediately |
duke@435 | 455 | // request a rescan. If this method stays on the stack for a |
never@1893 | 456 | // long time we don't want to keep rescanning the code cache. |
duke@435 | 457 | _not_entrant_seen_on_stack++; |
never@2916 | 458 | SWEEP(nm); |
duke@435 | 459 | } |
duke@435 | 460 | } else if (nm->is_unloaded()) { |
duke@435 | 461 | // Unloaded code, just make it a zombie |
ysr@1376 | 462 | if (PrintMethodFlushing && Verbose) |
kvn@1637 | 463 | tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm); |
sla@5237 | 464 | |
ysr@1376 | 465 | if (nm->is_osr_method()) { |
coleenp@4037 | 466 | SWEEP(nm); |
duke@435 | 467 | // No inline caches will ever point to osr methods, so we can just remove it |
coleenp@4037 | 468 | release_nmethod(nm); |
sla@5237 | 469 | _flushed_count++; |
duke@435 | 470 | } else { |
duke@435 | 471 | nm->make_zombie(); |
neliasso@5038 | 472 | _resweep = true; |
sla@5237 | 473 | _zombified_count++; |
never@2916 | 474 | SWEEP(nm); |
duke@435 | 475 | } |
duke@435 | 476 | } else { |
duke@435 | 477 | assert(nm->is_alive(), "should be alive"); |
kvn@1637 | 478 | |
kvn@1637 | 479 | if (UseCodeCacheFlushing) { |
neliasso@5038 | 480 | if (nm->is_speculatively_disconnected() && !nm->is_locked_by_vm() && !nm->is_osr_method() && |
neliasso@5038 | 481 | (_traversals > _last_flush_traversal_id + 2) && (nm->compile_id() < _highest_marked)) { |
kvn@1637 | 482 | // This method has not been called since the forced cleanup happened |
kvn@1637 | 483 | nm->make_not_entrant(); |
kvn@1637 | 484 | } |
kvn@1637 | 485 | } |
kvn@1637 | 486 | |
duke@435 | 487 | // Clean-up all inline caches that points to zombie/non-reentrant methods |
never@1893 | 488 | MutexLocker cl(CompiledIC_lock); |
duke@435 | 489 | nm->cleanup_inline_caches(); |
never@2916 | 490 | SWEEP(nm); |
duke@435 | 491 | } |
duke@435 | 492 | } |
kvn@1637 | 493 | |
kvn@1637 | 494 | // Code cache unloading: when compilers notice the code cache is getting full, |
kvn@1637 | 495 | // they will call a vm op that comes here. This code attempts to speculatively |
kvn@1637 | 496 | // unload the oldest half of the nmethods (based on the compile job id) by |
kvn@1637 | 497 | // saving the old code in a list in the CodeCache. Then |
never@1893 | 498 | // execution resumes. If a method so marked is not called by the second sweeper |
never@1893 | 499 | // stack traversal after the current one, the nmethod will be marked non-entrant and |
coleenp@4037 | 500 | // got rid of by normal sweeping. If the method is called, the Method*'s |
coleenp@4037 | 501 | // _code field is restored and the Method*/nmethod |
kvn@1637 | 502 | // go back to their normal state. |
kvn@1637 | 503 | void NMethodSweeper::handle_full_code_cache(bool is_full) { |
neliasso@5038 | 504 | |
neliasso@5038 | 505 | if (is_full) { |
neliasso@5038 | 506 | // Since code cache is full, immediately stop new compiles |
neliasso@5038 | 507 | if (CompileBroker::set_should_compile_new_jobs(CompileBroker::stop_compilation)) { |
neliasso@5038 | 508 | log_sweep("disable_compiler"); |
kvn@1637 | 509 | } |
kvn@1637 | 510 | } |
kvn@1637 | 511 | |
neliasso@5038 | 512 | // Make sure only one thread can flush |
neliasso@5038 | 513 | // The token is reset after CodeCacheMinimumFlushInterval in scan stacks, |
neliasso@5038 | 514 | // no need to check the timeout here. |
neliasso@5038 | 515 | jint old = Atomic::cmpxchg( 1, &_flush_token, 0 ); |
neliasso@5038 | 516 | if (old != 0) { |
neliasso@5038 | 517 | return; |
kvn@1637 | 518 | } |
kvn@1637 | 519 | |
kvn@1637 | 520 | VM_HandleFullCodeCache op(is_full); |
kvn@1637 | 521 | VMThread::execute(&op); |
kvn@1637 | 522 | |
neliasso@5038 | 523 | // resweep again as soon as possible |
neliasso@5038 | 524 | _resweep = true; |
kvn@1637 | 525 | } |
kvn@1637 | 526 | |
kvn@1637 | 527 | void NMethodSweeper::speculative_disconnect_nmethods(bool is_full) { |
kvn@1637 | 528 | // If there was a race in detecting full code cache, only run |
kvn@1637 | 529 | // one vm op for it or keep the compiler shut off |
kvn@1637 | 530 | |
sla@5237 | 531 | jlong disconnect_start_counter = os::elapsed_counter(); |
kvn@1637 | 532 | |
neliasso@5038 | 533 | // Traverse the code cache trying to dump the oldest nmethods |
neliasso@5038 | 534 | int curr_max_comp_id = CompileBroker::get_compilation_id(); |
neliasso@5038 | 535 | int flush_target = ((curr_max_comp_id - _dead_compile_ids) / CodeCacheFlushingFraction) + _dead_compile_ids; |
kvn@1637 | 536 | |
never@1999 | 537 | log_sweep("start_cleaning"); |
kvn@1637 | 538 | |
kvn@1637 | 539 | nmethod* nm = CodeCache::alive_nmethod(CodeCache::first()); |
kvn@1637 | 540 | jint disconnected = 0; |
kvn@1637 | 541 | jint made_not_entrant = 0; |
neliasso@5038 | 542 | jint nmethod_count = 0; |
neliasso@5038 | 543 | |
kvn@1637 | 544 | while ((nm != NULL)){ |
neliasso@5038 | 545 | int curr_comp_id = nm->compile_id(); |
kvn@1637 | 546 | |
kvn@1637 | 547 | // OSR methods cannot be flushed like this. Also, don't flush native methods |
kvn@1637 | 548 | // since they are part of the JDK in most cases |
neliasso@5038 | 549 | if (!nm->is_osr_method() && !nm->is_locked_by_vm() && !nm->is_native_method()) { |
kvn@1637 | 550 | |
neliasso@5038 | 551 | // only count methods that can be speculatively disconnected |
neliasso@5038 | 552 | nmethod_count++; |
kvn@1637 | 553 | |
neliasso@5038 | 554 | if (nm->is_in_use() && (curr_comp_id < flush_target)) { |
neliasso@5038 | 555 | if ((nm->method()->code() == nm)) { |
neliasso@5038 | 556 | // This method has not been previously considered for |
neliasso@5038 | 557 | // unloading or it was restored already |
neliasso@5038 | 558 | CodeCache::speculatively_disconnect(nm); |
neliasso@5038 | 559 | disconnected++; |
neliasso@5038 | 560 | } else if (nm->is_speculatively_disconnected()) { |
neliasso@5038 | 561 | // This method was previously considered for preemptive unloading and was not called since then |
neliasso@5038 | 562 | CompilationPolicy::policy()->delay_compilation(nm->method()); |
neliasso@5038 | 563 | nm->make_not_entrant(); |
neliasso@5038 | 564 | made_not_entrant++; |
neliasso@5038 | 565 | } |
neliasso@5038 | 566 | |
neliasso@5038 | 567 | if (curr_comp_id > _highest_marked) { |
neliasso@5038 | 568 | _highest_marked = curr_comp_id; |
neliasso@5038 | 569 | } |
kvn@1637 | 570 | } |
kvn@1637 | 571 | } |
kvn@1637 | 572 | nm = CodeCache::alive_nmethod(CodeCache::next(nm)); |
kvn@1637 | 573 | } |
kvn@1637 | 574 | |
neliasso@5038 | 575 | // remember how many compile_ids wheren't seen last flush. |
neliasso@5038 | 576 | _dead_compile_ids = curr_max_comp_id - nmethod_count; |
neliasso@5038 | 577 | |
never@1999 | 578 | log_sweep("stop_cleaning", |
never@1999 | 579 | "disconnected='" UINT32_FORMAT "' made_not_entrant='" UINT32_FORMAT "'", |
never@1999 | 580 | disconnected, made_not_entrant); |
kvn@1637 | 581 | |
never@1893 | 582 | // Shut off compiler. Sweeper will start over with a new stack scan and |
never@1893 | 583 | // traversal cycle and turn it back on if it clears enough space. |
neliasso@5038 | 584 | if (is_full) { |
neliasso@5038 | 585 | _last_full_flush_time = os::javaTimeMillis(); |
kvn@1637 | 586 | } |
kvn@1637 | 587 | |
sla@5237 | 588 | jlong disconnect_end_counter = os::elapsed_counter(); |
sla@5237 | 589 | jlong disconnect_time = disconnect_end_counter - disconnect_start_counter; |
sla@5237 | 590 | _total_disconnect_time += disconnect_time; |
sla@5237 | 591 | _peak_disconnect_time = MAX2(disconnect_time, _peak_disconnect_time); |
sla@5237 | 592 | |
sla@5237 | 593 | EventCleanCodeCache event(UNTIMED); |
sla@5237 | 594 | if (event.should_commit()) { |
sla@5237 | 595 | event.set_starttime(disconnect_start_counter); |
sla@5237 | 596 | event.set_endtime(disconnect_end_counter); |
sla@5237 | 597 | event.set_disconnectedCount(disconnected); |
sla@5237 | 598 | event.set_madeNonEntrantCount(made_not_entrant); |
sla@5237 | 599 | event.commit(); |
sla@5237 | 600 | } |
sla@5237 | 601 | _number_of_flushes++; |
sla@5237 | 602 | |
kvn@1637 | 603 | // After two more traversals the sweeper will get rid of unrestored nmethods |
neliasso@5038 | 604 | _last_flush_traversal_id = _traversals; |
neliasso@5038 | 605 | _resweep = true; |
kvn@1637 | 606 | #ifdef ASSERT |
sla@5237 | 607 | |
kvn@1637 | 608 | if(PrintMethodFlushing && Verbose) { |
sla@5237 | 609 | tty->print_cr("### sweeper: unload time: " INT64_FORMAT, (jlong)disconnect_time); |
kvn@1637 | 610 | } |
kvn@1637 | 611 | #endif |
kvn@1637 | 612 | } |
never@1999 | 613 | |
never@1999 | 614 | |
never@1999 | 615 | // Print out some state information about the current sweep and the |
never@1999 | 616 | // state of the code cache if it's requested. |
never@1999 | 617 | void NMethodSweeper::log_sweep(const char* msg, const char* format, ...) { |
never@1999 | 618 | if (PrintMethodFlushing) { |
iveresov@2764 | 619 | stringStream s; |
iveresov@2764 | 620 | // Dump code cache state into a buffer before locking the tty, |
iveresov@2764 | 621 | // because log_state() will use locks causing lock conflicts. |
iveresov@2764 | 622 | CodeCache::log_state(&s); |
iveresov@2764 | 623 | |
never@1999 | 624 | ttyLocker ttyl; |
never@1999 | 625 | tty->print("### sweeper: %s ", msg); |
never@1999 | 626 | if (format != NULL) { |
never@1999 | 627 | va_list ap; |
never@1999 | 628 | va_start(ap, format); |
never@1999 | 629 | tty->vprint(format, ap); |
never@1999 | 630 | va_end(ap); |
never@1999 | 631 | } |
iveresov@2764 | 632 | tty->print_cr(s.as_string()); |
never@1999 | 633 | } |
never@1999 | 634 | |
never@1999 | 635 | if (LogCompilation && (xtty != NULL)) { |
iveresov@2764 | 636 | stringStream s; |
iveresov@2764 | 637 | // Dump code cache state into a buffer before locking the tty, |
iveresov@2764 | 638 | // because log_state() will use locks causing lock conflicts. |
iveresov@2764 | 639 | CodeCache::log_state(&s); |
iveresov@2764 | 640 | |
never@1999 | 641 | ttyLocker ttyl; |
never@2001 | 642 | xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count()); |
never@1999 | 643 | if (format != NULL) { |
never@1999 | 644 | va_list ap; |
never@1999 | 645 | va_start(ap, format); |
never@1999 | 646 | xtty->vprint(format, ap); |
never@1999 | 647 | va_end(ap); |
never@1999 | 648 | } |
iveresov@2764 | 649 | xtty->print(s.as_string()); |
never@1999 | 650 | xtty->stamp(); |
never@1999 | 651 | xtty->end_elem(); |
never@1999 | 652 | } |
never@1999 | 653 | } |