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