annotate src/share/vm/runtime/sweeper.cpp @ 5770:908afcc9d1cb

8029091: Bug in calculation of code cache sweeping interval Summary: Use signed data type so that no underflow can happen Reviewed-by: kvn, roland
author anoll
date Tue, 17 Dec 2013 08:31:06 +0100
parents 86e6d691f2e1
children 6aa49042b101
rev   line source
duke@0 1 /*
sla@4802 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "code/codeCache.hpp"
coleenp@3602 27 #include "code/compiledIC.hpp"
coleenp@3602 28 #include "code/icBuffer.hpp"
stefank@1879 29 #include "code/nmethod.hpp"
stefank@1879 30 #include "compiler/compileBroker.hpp"
stefank@1879 31 #include "memory/resourceArea.hpp"
coleenp@3602 32 #include "oops/method.hpp"
stefank@1879 33 #include "runtime/atomic.hpp"
stefank@1879 34 #include "runtime/compilationPolicy.hpp"
stefank@1879 35 #include "runtime/mutexLocker.hpp"
stefank@1879 36 #include "runtime/os.hpp"
stefank@1879 37 #include "runtime/sweeper.hpp"
stefank@1879 38 #include "runtime/vm_operations.hpp"
sla@4802 39 #include "trace/tracing.hpp"
stefank@1879 40 #include "utilities/events.hpp"
mgronlun@5696 41 #include "utilities/ticks.inline.hpp"
stefank@1879 42 #include "utilities/xmlstream.hpp"
duke@0 43
never@2481 44 #ifdef ASSERT
never@2481 45
never@2481 46 #define SWEEP(nm) record_sweep(nm, __LINE__)
never@2481 47 // Sweeper logging code
never@2481 48 class SweeperRecord {
never@2481 49 public:
never@2481 50 int traversal;
never@2481 51 int invocation;
never@2481 52 int compile_id;
never@2481 53 long traversal_mark;
never@2481 54 int state;
never@2481 55 const char* kind;
never@2481 56 address vep;
never@2481 57 address uep;
never@2481 58 int line;
never@2481 59
never@2481 60 void print() {
never@2481 61 tty->print_cr("traversal = %d invocation = %d compile_id = %d %s uep = " PTR_FORMAT " vep = "
never@2481 62 PTR_FORMAT " state = %d traversal_mark %d line = %d",
never@2481 63 traversal,
never@2481 64 invocation,
never@2481 65 compile_id,
never@2481 66 kind == NULL ? "" : kind,
never@2481 67 uep,
never@2481 68 vep,
never@2481 69 state,
never@2481 70 traversal_mark,
never@2481 71 line);
never@2481 72 }
never@2481 73 };
never@2481 74
never@2481 75 static int _sweep_index = 0;
never@2481 76 static SweeperRecord* _records = NULL;
never@2481 77
never@2481 78 void NMethodSweeper::report_events(int id, address entry) {
never@2481 79 if (_records != NULL) {
never@2481 80 for (int i = _sweep_index; i < SweeperLogEntries; i++) {
never@2481 81 if (_records[i].uep == entry ||
never@2481 82 _records[i].vep == entry ||
never@2481 83 _records[i].compile_id == id) {
never@2481 84 _records[i].print();
never@2481 85 }
never@2481 86 }
never@2481 87 for (int i = 0; i < _sweep_index; i++) {
never@2481 88 if (_records[i].uep == entry ||
never@2481 89 _records[i].vep == entry ||
never@2481 90 _records[i].compile_id == id) {
never@2481 91 _records[i].print();
never@2481 92 }
never@2481 93 }
never@2481 94 }
never@2481 95 }
never@2481 96
never@2481 97 void NMethodSweeper::report_events() {
never@2481 98 if (_records != NULL) {
never@2481 99 for (int i = _sweep_index; i < SweeperLogEntries; i++) {
never@2481 100 // skip empty records
never@2481 101 if (_records[i].vep == NULL) continue;
never@2481 102 _records[i].print();
never@2481 103 }
never@2481 104 for (int i = 0; i < _sweep_index; i++) {
never@2481 105 // skip empty records
never@2481 106 if (_records[i].vep == NULL) continue;
never@2481 107 _records[i].print();
never@2481 108 }
never@2481 109 }
never@2481 110 }
never@2481 111
never@2481 112 void NMethodSweeper::record_sweep(nmethod* nm, int line) {
never@2481 113 if (_records != NULL) {
never@2481 114 _records[_sweep_index].traversal = _traversals;
never@2481 115 _records[_sweep_index].traversal_mark = nm->_stack_traversal_mark;
anoll@5664 116 _records[_sweep_index].invocation = _sweep_fractions_left;
never@2481 117 _records[_sweep_index].compile_id = nm->compile_id();
never@2481 118 _records[_sweep_index].kind = nm->compile_kind();
never@2481 119 _records[_sweep_index].state = nm->_state;
never@2481 120 _records[_sweep_index].vep = nm->verified_entry_point();
never@2481 121 _records[_sweep_index].uep = nm->entry_point();
never@2481 122 _records[_sweep_index].line = line;
never@2481 123 _sweep_index = (_sweep_index + 1) % SweeperLogEntries;
never@2481 124 }
never@2481 125 }
never@2481 126 #else
never@2481 127 #define SWEEP(nm)
never@2481 128 #endif
never@2481 129
anoll@5664 130 nmethod* NMethodSweeper::_current = NULL; // Current nmethod
anoll@5664 131 long NMethodSweeper::_traversals = 0; // Stack scan count, also sweep ID.
anoll@5664 132 long NMethodSweeper::_time_counter = 0; // Virtual time used to periodically invoke sweeper
anoll@5664 133 long NMethodSweeper::_last_sweep = 0; // Value of _time_counter when the last sweep happened
anoll@5664 134 int NMethodSweeper::_seen = 0; // Nof. nmethod we have currently processed in current pass of CodeCache
anoll@5664 135 int NMethodSweeper::_flushed_count = 0; // Nof. nmethods flushed in current sweep
anoll@5664 136 int NMethodSweeper::_zombified_count = 0; // Nof. nmethods made zombie in current sweep
anoll@5664 137 int NMethodSweeper::_marked_for_reclamation_count = 0; // Nof. nmethods marked for reclaim in current sweep
never@2481 138
anoll@5664 139 volatile bool NMethodSweeper::_should_sweep = true; // Indicates if we should invoke the sweeper
anoll@5664 140 volatile int NMethodSweeper::_sweep_fractions_left = 0; // Nof. invocations left until we are completed with this pass
anoll@5664 141 volatile int NMethodSweeper::_sweep_started = 0; // Flag to control conc sweeper
anoll@5664 142 volatile int NMethodSweeper::_bytes_changed = 0; // Counts the total nmethod size if the nmethod changed from:
anoll@5664 143 // 1) alive -> not_entrant
anoll@5664 144 // 2) not_entrant -> zombie
anoll@5664 145 // 3) zombie -> marked_for_reclamation
duke@0 146
anoll@5664 147 int NMethodSweeper::_total_nof_methods_reclaimed = 0; // Accumulated nof methods flushed
mgronlun@5696 148 Tickspan NMethodSweeper::_total_time_sweeping; // Accumulated time sweeping
mgronlun@5696 149 Tickspan NMethodSweeper::_total_time_this_sweep; // Total time this sweep
mgronlun@5696 150 Tickspan NMethodSweeper::_peak_sweep_time; // Peak time for a full sweep
mgronlun@5696 151 Tickspan NMethodSweeper::_peak_sweep_fraction_time; // Peak time sweeping one fraction
anoll@5664 152 int NMethodSweeper::_hotness_counter_reset_val = 0;
anoll@5357 153
sla@4802 154
jrose@989 155 class MarkActivationClosure: public CodeBlobClosure {
jrose@989 156 public:
jrose@989 157 virtual void do_code_blob(CodeBlob* cb) {
anoll@5357 158 if (cb->is_nmethod()) {
anoll@5357 159 nmethod* nm = (nmethod*)cb;
anoll@5357 160 nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
anoll@5357 161 // If we see an activation belonging to a non_entrant nmethod, we mark it.
anoll@5357 162 if (nm->is_not_entrant()) {
anoll@5357 163 nm->mark_as_seen_on_stack();
anoll@5357 164 }
jrose@989 165 }
jrose@989 166 }
jrose@989 167 };
jrose@989 168 static MarkActivationClosure mark_activation_closure;
jrose@989 169
anoll@5357 170 class SetHotnessClosure: public CodeBlobClosure {
anoll@5357 171 public:
anoll@5357 172 virtual void do_code_blob(CodeBlob* cb) {
anoll@5357 173 if (cb->is_nmethod()) {
anoll@5357 174 nmethod* nm = (nmethod*)cb;
anoll@5357 175 nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
anoll@5357 176 }
anoll@5357 177 }
anoll@5357 178 };
anoll@5357 179 static SetHotnessClosure set_hotness_closure;
anoll@5357 180
anoll@5357 181
anoll@5357 182 int NMethodSweeper::hotness_counter_reset_val() {
anoll@5357 183 if (_hotness_counter_reset_val == 0) {
anoll@5357 184 _hotness_counter_reset_val = (ReservedCodeCacheSize < M) ? 1 : (ReservedCodeCacheSize / M) * 2;
anoll@5357 185 }
anoll@5357 186 return _hotness_counter_reset_val;
anoll@5357 187 }
neliasso@4603 188 bool NMethodSweeper::sweep_in_progress() {
neliasso@4603 189 return (_current != NULL);
neliasso@4603 190 }
neliasso@4603 191
anoll@5357 192 // Scans the stacks of all Java threads and marks activations of not-entrant methods.
anoll@5357 193 // No need to synchronize access, since 'mark_active_nmethods' is always executed at a
anoll@5357 194 // safepoint.
anoll@5357 195 void NMethodSweeper::mark_active_nmethods() {
duke@0 196 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
anoll@5357 197 // If we do not want to reclaim not-entrant or zombie methods there is no need
anoll@5357 198 // to scan stacks
anoll@5357 199 if (!MethodFlushing) {
anoll@5357 200 return;
anoll@5357 201 }
duke@0 202
anoll@5664 203 // Increase time so that we can estimate when to invoke the sweeper again.
anoll@5664 204 _time_counter++;
anoll@5664 205
duke@0 206 // Check for restart
duke@0 207 assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
anoll@5664 208 if (!sweep_in_progress()) {
anoll@5664 209 _seen = 0;
anoll@5664 210 _sweep_fractions_left = NmethodSweepFraction;
anoll@5664 211 _current = CodeCache::first_nmethod();
anoll@5664 212 _traversals += 1;
mgronlun@5696 213 _total_time_this_sweep = Tickspan();
sla@4802 214
duke@0 215 if (PrintMethodFlushing) {
duke@0 216 tty->print_cr("### Sweep: stack traversal %d", _traversals);
duke@0 217 }
jrose@989 218 Threads::nmethods_do(&mark_activation_closure);
duke@0 219
anoll@5357 220 } else {
anoll@5357 221 // Only set hotness counter
anoll@5357 222 Threads::nmethods_do(&set_hotness_closure);
duke@0 223 }
duke@0 224
anoll@5357 225 OrderAccess::storestore();
duke@0 226 }
anoll@5664 227 /**
anoll@5664 228 * This function invokes the sweeper if at least one of the three conditions is met:
anoll@5664 229 * (1) The code cache is getting full
anoll@5664 230 * (2) There are sufficient state changes in/since the last sweep.
anoll@5664 231 * (3) We have not been sweeping for 'some time'
anoll@5664 232 */
never@1458 233 void NMethodSweeper::possibly_sweep() {
never@1564 234 assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode");
anoll@5679 235 // Only compiler threads are allowed to sweep
anoll@5679 236 if (!MethodFlushing || !sweep_in_progress() || !Thread::current()->is_Compiler_thread()) {
anoll@5357 237 return;
anoll@5357 238 }
never@1458 239
anoll@5664 240 // If there was no state change while nmethod sweeping, 'should_sweep' will be false.
anoll@5664 241 // This is one of the two places where should_sweep can be set to true. The general
anoll@5664 242 // idea is as follows: If there is enough free space in the code cache, there is no
anoll@5664 243 // need to invoke the sweeper. The following formula (which determines whether to invoke
anoll@5664 244 // the sweeper or not) depends on the assumption that for larger ReservedCodeCacheSizes
anoll@5664 245 // we need less frequent sweeps than for smaller ReservedCodecCacheSizes. Furthermore,
anoll@5664 246 // the formula considers how much space in the code cache is currently used. Here are
anoll@5664 247 // some examples that will (hopefully) help in understanding.
anoll@5664 248 //
anoll@5664 249 // Small ReservedCodeCacheSizes: (e.g., < 16M) We invoke the sweeper every time, since
anoll@5664 250 // the result of the division is 0. This
anoll@5664 251 // keeps the used code cache size small
anoll@5664 252 // (important for embedded Java)
anoll@5664 253 // Large ReservedCodeCacheSize : (e.g., 256M + code cache is 10% full). The formula
anoll@5664 254 // computes: (256 / 16) - 1 = 15
anoll@5664 255 // As a result, we invoke the sweeper after
anoll@5664 256 // 15 invocations of 'mark_active_nmethods.
anoll@5664 257 // Large ReservedCodeCacheSize: (e.g., 256M + code Cache is 90% full). The formula
anoll@5664 258 // computes: (256 / 16) - 10 = 6.
anoll@5664 259 if (!_should_sweep) {
anoll@5770 260 const int time_since_last_sweep = _time_counter - _last_sweep;
anoll@5770 261 // ReservedCodeCacheSize has an 'unsigned' type. We need a 'signed' type for max_wait_time,
anoll@5770 262 // since 'time_since_last_sweep' can be larger than 'max_wait_time'. If that happens using
anoll@5770 263 // an unsigned type would cause an underflow (wait_until_next_sweep becomes a large positive
anoll@5770 264 // value) that disables the intended periodic sweeps.
anoll@5770 265 const int max_wait_time = ReservedCodeCacheSize / (16 * M);
anoll@5770 266 double wait_until_next_sweep = max_wait_time - time_since_last_sweep - CodeCache::reverse_free_ratio();
anoll@5770 267 assert(wait_until_next_sweep <= (double)max_wait_time, "Calculation of code cache sweeper interval is incorrect");
anoll@5664 268
anoll@5664 269 if ((wait_until_next_sweep <= 0.0) || !CompileBroker::should_compile_new_jobs()) {
anoll@5664 270 _should_sweep = true;
anoll@5664 271 }
anoll@5664 272 }
anoll@5664 273
anoll@5664 274 if (_should_sweep && _sweep_fractions_left > 0) {
never@1458 275 // Only one thread at a time will sweep
never@1458 276 jint old = Atomic::cmpxchg( 1, &_sweep_started, 0 );
never@1458 277 if (old != 0) {
never@1458 278 return;
never@1458 279 }
never@2481 280 #ifdef ASSERT
never@2481 281 if (LogSweeper && _records == NULL) {
never@2481 282 // Create the ring buffer for the logging code
zgu@3465 283 _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
never@2481 284 memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
never@2481 285 }
never@2481 286 #endif
anoll@5664 287
anoll@5664 288 if (_sweep_fractions_left > 0) {
never@1564 289 sweep_code_cache();
anoll@5664 290 _sweep_fractions_left--;
anoll@5664 291 }
anoll@5664 292
anoll@5664 293 // We are done with sweeping the code cache once.
anoll@5664 294 if (_sweep_fractions_left == 0) {
anoll@5664 295 _last_sweep = _time_counter;
anoll@5664 296 // Reset flag; temporarily disables sweeper
anoll@5664 297 _should_sweep = false;
anoll@5664 298 // If there was enough state change, 'possibly_enable_sweeper()'
anoll@5664 299 // sets '_should_sweep' to true
anoll@5664 300 possibly_enable_sweeper();
anoll@5664 301 // Reset _bytes_changed only if there was enough state change. _bytes_changed
anoll@5664 302 // can further increase by calls to 'report_state_change'.
anoll@5664 303 if (_should_sweep) {
anoll@5664 304 _bytes_changed = 0;
anoll@5664 305 }
never@1564 306 }
never@1564 307 _sweep_started = 0;
never@1458 308 }
never@1458 309 }
never@1458 310
never@1458 311 void NMethodSweeper::sweep_code_cache() {
mgronlun@5696 312 Ticks sweep_start_counter = Ticks::now();
sla@4802 313
anoll@5664 314 _flushed_count = 0;
anoll@5664 315 _zombified_count = 0;
anoll@5664 316 _marked_for_reclamation_count = 0;
sla@4802 317
never@1458 318 if (PrintMethodFlushing && Verbose) {
anoll@5664 319 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left);
never@1458 320 }
never@1458 321
neliasso@4603 322 if (!CompileBroker::should_compile_new_jobs()) {
neliasso@4603 323 // If we have turned off compilations we might as well do full sweeps
neliasso@4603 324 // in order to reach the clean state faster. Otherwise the sleeping compiler
anoll@5357 325 // threads will slow down sweeping.
anoll@5664 326 _sweep_fractions_left = 1;
neliasso@4603 327 }
neliasso@4603 328
never@1564 329 // We want to visit all nmethods after NmethodSweepFraction
never@1564 330 // invocations so divide the remaining number of nmethods by the
never@1564 331 // remaining number of invocations. This is only an estimate since
never@1564 332 // the number of nmethods changes during the sweep so the final
never@1564 333 // stage must iterate until it there are no more nmethods.
anoll@5664 334 int todo = (CodeCache::nof_nmethods() - _seen) / _sweep_fractions_left;
anoll@5299 335 int swept_count = 0;
never@1458 336
anoll@5357 337
never@1458 338 assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here");
never@1458 339 assert(!CodeCache_lock->owned_by_self(), "just checking");
never@1458 340
anoll@5357 341 int freed_memory = 0;
never@1458 342 {
never@1458 343 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1458 344
never@1564 345 // The last invocation iterates until there are no more nmethods
anoll@5664 346 for (int i = 0; (i < todo || _sweep_fractions_left == 1) && _current != NULL; i++) {
anoll@5299 347 swept_count++;
iveresov@3137 348 if (SafepointSynchronize::is_synchronizing()) { // Safepoint request
iveresov@3137 349 if (PrintMethodFlushing && Verbose) {
anoll@5664 350 tty->print_cr("### Sweep at %d out of %d, invocation: %d, yielding to safepoint", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left);
iveresov@3137 351 }
iveresov@3137 352 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1458 353
iveresov@3137 354 assert(Thread::current()->is_Java_thread(), "should be java thread");
iveresov@3137 355 JavaThread* thread = (JavaThread*)Thread::current();
iveresov@3137 356 ThreadBlockInVM tbivm(thread);
iveresov@3137 357 thread->java_suspend_self();
iveresov@3137 358 }
never@1564 359 // Since we will give up the CodeCache_lock, always skip ahead
never@1564 360 // to the next nmethod. Other blobs can be deleted by other
never@1564 361 // threads but nmethods are only reclaimed by the sweeper.
never@1535 362 nmethod* next = CodeCache::next_nmethod(_current);
never@1458 363
never@1458 364 // Now ready to process nmethod and give up CodeCache_lock
never@1458 365 {
never@1458 366 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
anoll@5357 367 freed_memory += process_nmethod(_current);
never@1458 368 }
never@1458 369 _seen++;
never@1458 370 _current = next;
never@1458 371 }
never@1458 372 }
never@1458 373
anoll@5664 374 assert(_sweep_fractions_left > 1 || _current == NULL, "must have scanned the whole cache");
never@1458 375
mgronlun@5696 376 const Ticks sweep_end_counter = Ticks::now();
mgronlun@5696 377 const Tickspan sweep_time = sweep_end_counter - sweep_start_counter;
sla@4802 378 _total_time_sweeping += sweep_time;
sla@4802 379 _total_time_this_sweep += sweep_time;
sla@4802 380 _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
sla@4802 381 _total_nof_methods_reclaimed += _flushed_count;
sla@4802 382
sla@4802 383 EventSweepCodeCache event(UNTIMED);
sla@4802 384 if (event.should_commit()) {
sla@4802 385 event.set_starttime(sweep_start_counter);
sla@4802 386 event.set_endtime(sweep_end_counter);
sla@4802 387 event.set_sweepIndex(_traversals);
anoll@5664 388 event.set_sweepFractionIndex(NmethodSweepFraction - _sweep_fractions_left + 1);
anoll@5299 389 event.set_sweptCount(swept_count);
sla@4802 390 event.set_flushedCount(_flushed_count);
anoll@5664 391 event.set_markedCount(_marked_for_reclamation_count);
sla@4802 392 event.set_zombifiedCount(_zombified_count);
sla@4802 393 event.commit();
sla@4802 394 }
sla@4802 395
never@1458 396 #ifdef ASSERT
never@1458 397 if(PrintMethodFlushing) {
mgronlun@5696 398 tty->print_cr("### sweeper: sweep time(%d): "
mgronlun@5696 399 INT64_FORMAT, _sweep_fractions_left, (jlong)sweep_time.value());
never@1458 400 }
never@1458 401 #endif
never@1564 402
anoll@5664 403 if (_sweep_fractions_left == 1) {
sla@4802 404 _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep);
never@1564 405 log_sweep("finished");
never@1564 406 }
neliasso@4603 407
anoll@5357 408 // Sweeper is the only case where memory is released, check here if it
anoll@5357 409 // is time to restart the compiler. Only checking if there is a certain
anoll@5357 410 // amount of free memory in the code cache might lead to re-enabling
anoll@5357 411 // compilation although no memory has been released. For example, there are
anoll@5357 412 // cases when compilation was disabled although there is 4MB (or more) free
anoll@5357 413 // memory in the code cache. The reason is code cache fragmentation. Therefore,
anoll@5357 414 // it only makes sense to re-enable compilation if we have actually freed memory.
anoll@5357 415 // Note that typically several kB are released for sweeping 16MB of the code
anoll@5357 416 // cache. As a result, 'freed_memory' > 0 to restart the compiler.
anoll@5664 417 if (!CompileBroker::should_compile_new_jobs() && (freed_memory > 0)) {
neliasso@4603 418 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
neliasso@4603 419 log_sweep("restart_compiler");
neliasso@4603 420 }
never@1458 421 }
never@1458 422
anoll@5664 423 /**
anoll@5664 424 * This function updates the sweeper statistics that keep track of nmethods
anoll@5664 425 * state changes. If there is 'enough' state change, the sweeper is invoked
anoll@5664 426 * as soon as possible. There can be data races on _bytes_changed. The data
anoll@5664 427 * races are benign, since it does not matter if we loose a couple of bytes.
anoll@5664 428 * In the worst case we call the sweeper a little later. Also, we are guaranteed
anoll@5664 429 * to invoke the sweeper if the code cache gets full.
anoll@5664 430 */
anoll@5664 431 void NMethodSweeper::report_state_change(nmethod* nm) {
anoll@5664 432 _bytes_changed += nm->total_size();
anoll@5664 433 possibly_enable_sweeper();
anoll@5664 434 }
anoll@5664 435
anoll@5664 436 /**
anoll@5664 437 * Function determines if there was 'enough' state change in the code cache to invoke
anoll@5664 438 * the sweeper again. Currently, we determine 'enough' as more than 1% state change in
anoll@5664 439 * the code cache since the last sweep.
anoll@5664 440 */
anoll@5664 441 void NMethodSweeper::possibly_enable_sweeper() {
anoll@5664 442 double percent_changed = ((double)_bytes_changed / (double)ReservedCodeCacheSize) * 100;
anoll@5664 443 if (percent_changed > 1.0) {
anoll@5664 444 _should_sweep = true;
anoll@5664 445 }
anoll@5664 446 }
anoll@5664 447
never@2481 448 class NMethodMarker: public StackObj {
never@2481 449 private:
never@2481 450 CompilerThread* _thread;
never@2481 451 public:
never@2481 452 NMethodMarker(nmethod* nm) {
never@2481 453 _thread = CompilerThread::current();
coleenp@3602 454 if (!nm->is_zombie() && !nm->is_unloaded()) {
coleenp@3602 455 // Only expose live nmethods for scanning
anoll@5357 456 _thread->set_scanned_nmethod(nm);
anoll@5357 457 }
coleenp@3602 458 }
never@2481 459 ~NMethodMarker() {
never@2481 460 _thread->set_scanned_nmethod(NULL);
never@2481 461 }
never@2481 462 };
never@2481 463
coleenp@3602 464 void NMethodSweeper::release_nmethod(nmethod *nm) {
coleenp@3602 465 // Clean up any CompiledICHolders
coleenp@3602 466 {
coleenp@3602 467 ResourceMark rm;
coleenp@3602 468 MutexLocker ml_patch(CompiledIC_lock);
coleenp@3602 469 RelocIterator iter(nm);
coleenp@3602 470 while (iter.next()) {
coleenp@3602 471 if (iter.type() == relocInfo::virtual_call_type) {
coleenp@3602 472 CompiledIC::cleanup_call_site(iter.virtual_call_reloc());
coleenp@3602 473 }
coleenp@3602 474 }
coleenp@3602 475 }
coleenp@3602 476
coleenp@3602 477 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
coleenp@3602 478 nm->flush();
coleenp@3602 479 }
duke@0 480
anoll@5357 481 int NMethodSweeper::process_nmethod(nmethod *nm) {
never@1458 482 assert(!CodeCache_lock->owned_by_self(), "just checking");
never@1458 483
anoll@5357 484 int freed_memory = 0;
never@2481 485 // Make sure this nmethod doesn't get unloaded during the scan,
anoll@5357 486 // since safepoints may happen during acquired below locks.
never@2481 487 NMethodMarker nmm(nm);
never@2481 488 SWEEP(nm);
never@2481 489
duke@0 490 // Skip methods that are currently referenced by the VM
duke@0 491 if (nm->is_locked_by_vm()) {
duke@0 492 // But still remember to clean-up inline caches for alive nmethods
duke@0 493 if (nm->is_alive()) {
anoll@5357 494 // Clean inline caches that point to zombie/non-entrant methods
never@1458 495 MutexLocker cl(CompiledIC_lock);
duke@0 496 nm->cleanup_inline_caches();
never@2481 497 SWEEP(nm);
duke@0 498 }
anoll@5357 499 return freed_memory;
duke@0 500 }
duke@0 501
duke@0 502 if (nm->is_zombie()) {
anoll@5357 503 // If it is the first time we see nmethod then we mark it. Otherwise,
anoll@5357 504 // we reclaim it. When we have seen a zombie method twice, we know that
never@1564 505 // there are no inline caches that refer to it.
duke@0 506 if (nm->is_marked_for_reclamation()) {
duke@0 507 assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
ysr@941 508 if (PrintMethodFlushing && Verbose) {
kvn@1202 509 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
ysr@941 510 }
anoll@5357 511 freed_memory = nm->total_size();
coleenp@3602 512 release_nmethod(nm);
sla@4802 513 _flushed_count++;
duke@0 514 } else {
ysr@941 515 if (PrintMethodFlushing && Verbose) {
kvn@1202 516 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
ysr@941 517 }
duke@0 518 nm->mark_for_reclamation();
anoll@5664 519 // Keep track of code cache state change
anoll@5664 520 _bytes_changed += nm->total_size();
anoll@5664 521 _marked_for_reclamation_count++;
never@2481 522 SWEEP(nm);
duke@0 523 }
duke@0 524 } else if (nm->is_not_entrant()) {
anoll@5357 525 // If there are no current activations of this method on the
duke@0 526 // stack we can safely convert it to a zombie method
duke@0 527 if (nm->can_not_entrant_be_converted()) {
ysr@941 528 if (PrintMethodFlushing && Verbose) {
kvn@1202 529 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
ysr@941 530 }
anoll@5664 531 // Code cache state change is tracked in make_zombie()
duke@0 532 nm->make_zombie();
sla@4802 533 _zombified_count++;
never@2481 534 SWEEP(nm);
duke@0 535 } else {
duke@0 536 // Still alive, clean up its inline caches
never@1458 537 MutexLocker cl(CompiledIC_lock);
duke@0 538 nm->cleanup_inline_caches();
never@2481 539 SWEEP(nm);
duke@0 540 }
duke@0 541 } else if (nm->is_unloaded()) {
duke@0 542 // Unloaded code, just make it a zombie
anoll@5357 543 if (PrintMethodFlushing && Verbose) {
kvn@1202 544 tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm);
anoll@5357 545 }
ysr@941 546 if (nm->is_osr_method()) {
coleenp@3602 547 SWEEP(nm);
duke@0 548 // No inline caches will ever point to osr methods, so we can just remove it
anoll@5357 549 freed_memory = nm->total_size();
coleenp@3602 550 release_nmethod(nm);
sla@4802 551 _flushed_count++;
duke@0 552 } else {
anoll@5664 553 // Code cache state change is tracked in make_zombie()
duke@0 554 nm->make_zombie();
sla@4802 555 _zombified_count++;
never@2481 556 SWEEP(nm);
duke@0 557 }
duke@0 558 } else {
kvn@1202 559 if (UseCodeCacheFlushing) {
anoll@5357 560 if (!nm->is_locked_by_vm() && !nm->is_osr_method() && !nm->is_native_method()) {
anoll@5357 561 // Do not make native methods and OSR-methods not-entrant
anoll@5357 562 nm->dec_hotness_counter();
anoll@5357 563 // Get the initial value of the hotness counter. This value depends on the
anoll@5357 564 // ReservedCodeCacheSize
anoll@5357 565 int reset_val = hotness_counter_reset_val();
anoll@5357 566 int time_since_reset = reset_val - nm->hotness_counter();
anoll@5357 567 double threshold = -reset_val + (CodeCache::reverse_free_ratio() * NmethodSweepActivity);
anoll@5357 568 // The less free space in the code cache we have - the bigger reverse_free_ratio() is.
anoll@5357 569 // I.e., 'threshold' increases with lower available space in the code cache and a higher
anoll@5357 570 // NmethodSweepActivity. If the current hotness counter - which decreases from its initial
anoll@5357 571 // value until it is reset by stack walking - is smaller than the computed threshold, the
anoll@5357 572 // corresponding nmethod is considered for removal.
anoll@5357 573 if ((NmethodSweepActivity > 0) && (nm->hotness_counter() < threshold) && (time_since_reset > 10)) {
anoll@5357 574 // A method is marked as not-entrant if the method is
anoll@5357 575 // 1) 'old enough': nm->hotness_counter() < threshold
anoll@5357 576 // 2) The method was in_use for a minimum amount of time: (time_since_reset > 10)
anoll@5357 577 // The second condition is necessary if we are dealing with very small code cache
anoll@5357 578 // sizes (e.g., <10m) and the code cache size is too small to hold all hot methods.
anoll@5357 579 // The second condition ensures that methods are not immediately made not-entrant
anoll@5357 580 // after compilation.
anoll@5357 581 nm->make_not_entrant();
anoll@5664 582 // Code cache state change is tracked in make_not_entrant()
anoll@5664 583 if (PrintMethodFlushing && Verbose) {
anoll@5664 584 tty->print_cr("### Nmethod %d/" PTR_FORMAT "made not-entrant: hotness counter %d/%d threshold %f",
anoll@5664 585 nm->compile_id(), nm, nm->hotness_counter(), reset_val, threshold);
anoll@5664 586 }
anoll@5357 587 }
kvn@1202 588 }
kvn@1202 589 }
anoll@5357 590 // Clean-up all inline caches that point to zombie/non-reentrant methods
never@1458 591 MutexLocker cl(CompiledIC_lock);
duke@0 592 nm->cleanup_inline_caches();
never@2481 593 SWEEP(nm);
duke@0 594 }
anoll@5357 595 return freed_memory;
duke@0 596 }
kvn@1202 597
never@1564 598 // Print out some state information about the current sweep and the
never@1564 599 // state of the code cache if it's requested.
never@1564 600 void NMethodSweeper::log_sweep(const char* msg, const char* format, ...) {
never@1564 601 if (PrintMethodFlushing) {
iveresov@2329 602 stringStream s;
iveresov@2329 603 // Dump code cache state into a buffer before locking the tty,
iveresov@2329 604 // because log_state() will use locks causing lock conflicts.
iveresov@2329 605 CodeCache::log_state(&s);
iveresov@2329 606
never@1564 607 ttyLocker ttyl;
never@1564 608 tty->print("### sweeper: %s ", msg);
never@1564 609 if (format != NULL) {
never@1564 610 va_list ap;
never@1564 611 va_start(ap, format);
never@1564 612 tty->vprint(format, ap);
never@1564 613 va_end(ap);
never@1564 614 }
iveresov@2329 615 tty->print_cr(s.as_string());
never@1564 616 }
never@1564 617
never@1564 618 if (LogCompilation && (xtty != NULL)) {
iveresov@2329 619 stringStream s;
iveresov@2329 620 // Dump code cache state into a buffer before locking the tty,
iveresov@2329 621 // because log_state() will use locks causing lock conflicts.
iveresov@2329 622 CodeCache::log_state(&s);
iveresov@2329 623
never@1564 624 ttyLocker ttyl;
never@1566 625 xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count());
never@1564 626 if (format != NULL) {
never@1564 627 va_list ap;
never@1564 628 va_start(ap, format);
never@1564 629 xtty->vprint(format, ap);
never@1564 630 va_end(ap);
never@1564 631 }
iveresov@2329 632 xtty->print(s.as_string());
never@1564 633 xtty->stamp();
never@1564 634 xtty->end_elem();
never@1564 635 }
never@1564 636 }