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