annotate src/share/vm/runtime/sweeper.cpp @ 7949:f22b5be95347

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