aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "code/compiledIC.hpp" aoqi@0: #include "code/icBuffer.hpp" aoqi@0: #include "code/nmethod.hpp" aoqi@0: #include "compiler/compileBroker.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/method.hpp" aoqi@0: #include "runtime/atomic.hpp" aoqi@0: #include "runtime/compilationPolicy.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "runtime/sweeper.hpp" goetz@6911: #include "runtime/thread.inline.hpp" aoqi@0: #include "runtime/vm_operations.hpp" aoqi@0: #include "trace/tracing.hpp" aoqi@0: #include "utilities/events.hpp" aoqi@0: #include "utilities/ticks.inline.hpp" aoqi@0: #include "utilities/xmlstream.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: aoqi@0: #define SWEEP(nm) record_sweep(nm, __LINE__) aoqi@0: // Sweeper logging code aoqi@0: class SweeperRecord { aoqi@0: public: aoqi@0: int traversal; aoqi@0: int invocation; aoqi@0: int compile_id; aoqi@0: long traversal_mark; aoqi@0: int state; aoqi@0: const char* kind; aoqi@0: address vep; aoqi@0: address uep; aoqi@0: int line; aoqi@0: aoqi@0: void print() { aoqi@0: tty->print_cr("traversal = %d invocation = %d compile_id = %d %s uep = " PTR_FORMAT " vep = " aoqi@0: PTR_FORMAT " state = %d traversal_mark %d line = %d", aoqi@0: traversal, aoqi@0: invocation, aoqi@0: compile_id, aoqi@0: kind == NULL ? "" : kind, aoqi@0: uep, aoqi@0: vep, aoqi@0: state, aoqi@0: traversal_mark, aoqi@0: line); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: static int _sweep_index = 0; aoqi@0: static SweeperRecord* _records = NULL; aoqi@0: aoqi@0: void NMethodSweeper::report_events(int id, address entry) { aoqi@0: if (_records != NULL) { aoqi@0: for (int i = _sweep_index; i < SweeperLogEntries; i++) { aoqi@0: if (_records[i].uep == entry || aoqi@0: _records[i].vep == entry || aoqi@0: _records[i].compile_id == id) { aoqi@0: _records[i].print(); aoqi@0: } aoqi@0: } aoqi@0: for (int i = 0; i < _sweep_index; i++) { aoqi@0: if (_records[i].uep == entry || aoqi@0: _records[i].vep == entry || aoqi@0: _records[i].compile_id == id) { aoqi@0: _records[i].print(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NMethodSweeper::report_events() { aoqi@0: if (_records != NULL) { aoqi@0: for (int i = _sweep_index; i < SweeperLogEntries; i++) { aoqi@0: // skip empty records aoqi@0: if (_records[i].vep == NULL) continue; aoqi@0: _records[i].print(); aoqi@0: } aoqi@0: for (int i = 0; i < _sweep_index; i++) { aoqi@0: // skip empty records aoqi@0: if (_records[i].vep == NULL) continue; aoqi@0: _records[i].print(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NMethodSweeper::record_sweep(nmethod* nm, int line) { aoqi@0: if (_records != NULL) { aoqi@0: _records[_sweep_index].traversal = _traversals; aoqi@0: _records[_sweep_index].traversal_mark = nm->_stack_traversal_mark; aoqi@0: _records[_sweep_index].invocation = _sweep_fractions_left; aoqi@0: _records[_sweep_index].compile_id = nm->compile_id(); aoqi@0: _records[_sweep_index].kind = nm->compile_kind(); aoqi@0: _records[_sweep_index].state = nm->_state; aoqi@0: _records[_sweep_index].vep = nm->verified_entry_point(); aoqi@0: _records[_sweep_index].uep = nm->entry_point(); aoqi@0: _records[_sweep_index].line = line; aoqi@0: _sweep_index = (_sweep_index + 1) % SweeperLogEntries; aoqi@0: } aoqi@0: } aoqi@0: #else aoqi@0: #define SWEEP(nm) aoqi@0: #endif aoqi@0: aoqi@0: nmethod* NMethodSweeper::_current = NULL; // Current nmethod aoqi@0: long NMethodSweeper::_traversals = 0; // Stack scan count, also sweep ID. aoqi@0: long NMethodSweeper::_total_nof_code_cache_sweeps = 0; // Total number of full sweeps of the code cache aoqi@0: long NMethodSweeper::_time_counter = 0; // Virtual time used to periodically invoke sweeper aoqi@0: long NMethodSweeper::_last_sweep = 0; // Value of _time_counter when the last sweep happened aoqi@0: int NMethodSweeper::_seen = 0; // Nof. nmethod we have currently processed in current pass of CodeCache aoqi@0: int NMethodSweeper::_flushed_count = 0; // Nof. nmethods flushed in current sweep aoqi@0: int NMethodSweeper::_zombified_count = 0; // Nof. nmethods made zombie in current sweep aoqi@0: int NMethodSweeper::_marked_for_reclamation_count = 0; // Nof. nmethods marked for reclaim in current sweep aoqi@0: aoqi@0: volatile bool NMethodSweeper::_should_sweep = true; // Indicates if we should invoke the sweeper aoqi@0: volatile int NMethodSweeper::_sweep_fractions_left = 0; // Nof. invocations left until we are completed with this pass aoqi@0: volatile int NMethodSweeper::_sweep_started = 0; // Flag to control conc sweeper aoqi@0: volatile int NMethodSweeper::_bytes_changed = 0; // Counts the total nmethod size if the nmethod changed from: aoqi@0: // 1) alive -> not_entrant aoqi@0: // 2) not_entrant -> zombie aoqi@0: // 3) zombie -> marked_for_reclamation aoqi@0: int NMethodSweeper::_hotness_counter_reset_val = 0; aoqi@0: aoqi@0: long NMethodSweeper::_total_nof_methods_reclaimed = 0; // Accumulated nof methods flushed aoqi@0: long NMethodSweeper::_total_nof_c2_methods_reclaimed = 0; // Accumulated nof methods flushed aoqi@0: size_t NMethodSweeper::_total_flushed_size = 0; // Total number of bytes flushed from the code cache aoqi@0: Tickspan NMethodSweeper::_total_time_sweeping; // Accumulated time sweeping aoqi@0: Tickspan NMethodSweeper::_total_time_this_sweep; // Total time this sweep aoqi@0: Tickspan NMethodSweeper::_peak_sweep_time; // Peak time for a full sweep aoqi@0: Tickspan NMethodSweeper::_peak_sweep_fraction_time; // Peak time sweeping one fraction aoqi@0: aoqi@0: aoqi@0: aoqi@0: class MarkActivationClosure: public CodeBlobClosure { aoqi@0: public: aoqi@0: virtual void do_code_blob(CodeBlob* cb) { aoqi@0: if (cb->is_nmethod()) { aoqi@0: nmethod* nm = (nmethod*)cb; aoqi@0: nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val()); aoqi@0: // If we see an activation belonging to a non_entrant nmethod, we mark it. aoqi@0: if (nm->is_not_entrant()) { aoqi@0: nm->mark_as_seen_on_stack(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: static MarkActivationClosure mark_activation_closure; aoqi@0: aoqi@0: class SetHotnessClosure: public CodeBlobClosure { aoqi@0: public: aoqi@0: virtual void do_code_blob(CodeBlob* cb) { aoqi@0: if (cb->is_nmethod()) { aoqi@0: nmethod* nm = (nmethod*)cb; aoqi@0: nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val()); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: static SetHotnessClosure set_hotness_closure; aoqi@0: aoqi@0: aoqi@0: int NMethodSweeper::hotness_counter_reset_val() { aoqi@0: if (_hotness_counter_reset_val == 0) { aoqi@0: _hotness_counter_reset_val = (ReservedCodeCacheSize < M) ? 1 : (ReservedCodeCacheSize / M) * 2; aoqi@0: } aoqi@0: return _hotness_counter_reset_val; aoqi@0: } aoqi@0: bool NMethodSweeper::sweep_in_progress() { aoqi@0: return (_current != NULL); aoqi@0: } aoqi@0: aoqi@0: // Scans the stacks of all Java threads and marks activations of not-entrant methods. aoqi@0: // No need to synchronize access, since 'mark_active_nmethods' is always executed at a aoqi@0: // safepoint. aoqi@0: void NMethodSweeper::mark_active_nmethods() { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint"); aoqi@0: // If we do not want to reclaim not-entrant or zombie methods there is no need aoqi@0: // to scan stacks aoqi@0: if (!MethodFlushing) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Increase time so that we can estimate when to invoke the sweeper again. aoqi@0: _time_counter++; aoqi@0: aoqi@0: // Check for restart aoqi@0: assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid"); aoqi@0: if (!sweep_in_progress()) { aoqi@0: _seen = 0; aoqi@0: _sweep_fractions_left = NmethodSweepFraction; aoqi@0: _current = CodeCache::first_nmethod(); aoqi@0: _traversals += 1; aoqi@0: _total_time_this_sweep = Tickspan(); aoqi@0: aoqi@0: if (PrintMethodFlushing) { aoqi@0: tty->print_cr("### Sweep: stack traversal %d", _traversals); aoqi@0: } aoqi@0: Threads::nmethods_do(&mark_activation_closure); aoqi@0: aoqi@0: } else { aoqi@0: // Only set hotness counter aoqi@0: Threads::nmethods_do(&set_hotness_closure); aoqi@0: } aoqi@0: aoqi@0: OrderAccess::storestore(); aoqi@0: } aoqi@0: /** aoqi@0: * This function invokes the sweeper if at least one of the three conditions is met: aoqi@0: * (1) The code cache is getting full aoqi@0: * (2) There are sufficient state changes in/since the last sweep. aoqi@0: * (3) We have not been sweeping for 'some time' aoqi@0: */ aoqi@0: void NMethodSweeper::possibly_sweep() { aoqi@0: assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode"); aoqi@0: // Only compiler threads are allowed to sweep aoqi@0: if (!MethodFlushing || !sweep_in_progress() || !Thread::current()->is_Compiler_thread()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // If there was no state change while nmethod sweeping, 'should_sweep' will be false. aoqi@0: // This is one of the two places where should_sweep can be set to true. The general aoqi@0: // idea is as follows: If there is enough free space in the code cache, there is no aoqi@0: // need to invoke the sweeper. The following formula (which determines whether to invoke aoqi@0: // the sweeper or not) depends on the assumption that for larger ReservedCodeCacheSizes aoqi@0: // we need less frequent sweeps than for smaller ReservedCodecCacheSizes. Furthermore, aoqi@0: // the formula considers how much space in the code cache is currently used. Here are aoqi@0: // some examples that will (hopefully) help in understanding. aoqi@0: // aoqi@0: // Small ReservedCodeCacheSizes: (e.g., < 16M) We invoke the sweeper every time, since aoqi@0: // the result of the division is 0. This aoqi@0: // keeps the used code cache size small aoqi@0: // (important for embedded Java) aoqi@0: // Large ReservedCodeCacheSize : (e.g., 256M + code cache is 10% full). The formula aoqi@0: // computes: (256 / 16) - 1 = 15 aoqi@0: // As a result, we invoke the sweeper after aoqi@0: // 15 invocations of 'mark_active_nmethods. aoqi@0: // Large ReservedCodeCacheSize: (e.g., 256M + code Cache is 90% full). The formula aoqi@0: // computes: (256 / 16) - 10 = 6. aoqi@0: if (!_should_sweep) { aoqi@0: const int time_since_last_sweep = _time_counter - _last_sweep; aoqi@0: // ReservedCodeCacheSize has an 'unsigned' type. We need a 'signed' type for max_wait_time, aoqi@0: // since 'time_since_last_sweep' can be larger than 'max_wait_time'. If that happens using aoqi@0: // an unsigned type would cause an underflow (wait_until_next_sweep becomes a large positive aoqi@0: // value) that disables the intended periodic sweeps. aoqi@0: const int max_wait_time = ReservedCodeCacheSize / (16 * M); aoqi@0: double wait_until_next_sweep = max_wait_time - time_since_last_sweep - CodeCache::reverse_free_ratio(); aoqi@0: assert(wait_until_next_sweep <= (double)max_wait_time, "Calculation of code cache sweeper interval is incorrect"); aoqi@0: aoqi@0: if ((wait_until_next_sweep <= 0.0) || !CompileBroker::should_compile_new_jobs()) { aoqi@0: _should_sweep = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (_should_sweep && _sweep_fractions_left > 0) { aoqi@0: // Only one thread at a time will sweep aoqi@0: jint old = Atomic::cmpxchg( 1, &_sweep_started, 0 ); aoqi@0: if (old != 0) { aoqi@0: return; aoqi@0: } aoqi@0: #ifdef ASSERT aoqi@0: if (LogSweeper && _records == NULL) { aoqi@0: // Create the ring buffer for the logging code aoqi@0: _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC); aoqi@0: memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: if (_sweep_fractions_left > 0) { aoqi@0: sweep_code_cache(); aoqi@0: _sweep_fractions_left--; aoqi@0: } aoqi@0: aoqi@0: // We are done with sweeping the code cache once. aoqi@0: if (_sweep_fractions_left == 0) { aoqi@0: _total_nof_code_cache_sweeps++; aoqi@0: _last_sweep = _time_counter; aoqi@0: // Reset flag; temporarily disables sweeper aoqi@0: _should_sweep = false; aoqi@0: // If there was enough state change, 'possibly_enable_sweeper()' aoqi@0: // sets '_should_sweep' to true aoqi@0: possibly_enable_sweeper(); aoqi@0: // Reset _bytes_changed only if there was enough state change. _bytes_changed aoqi@0: // can further increase by calls to 'report_state_change'. aoqi@0: if (_should_sweep) { aoqi@0: _bytes_changed = 0; aoqi@0: } aoqi@0: } aoqi@0: // Release work, because another compiler thread could continue. aoqi@0: OrderAccess::release_store((int*)&_sweep_started, 0); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NMethodSweeper::sweep_code_cache() { jcm@8713: ResourceMark rm; aoqi@0: Ticks sweep_start_counter = Ticks::now(); aoqi@0: aoqi@0: _flushed_count = 0; aoqi@0: _zombified_count = 0; aoqi@0: _marked_for_reclamation_count = 0; aoqi@0: aoqi@0: if (PrintMethodFlushing && Verbose) { aoqi@0: tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left); aoqi@0: } aoqi@0: aoqi@0: if (!CompileBroker::should_compile_new_jobs()) { aoqi@0: // If we have turned off compilations we might as well do full sweeps aoqi@0: // in order to reach the clean state faster. Otherwise the sleeping compiler aoqi@0: // threads will slow down sweeping. aoqi@0: _sweep_fractions_left = 1; aoqi@0: } aoqi@0: aoqi@0: // We want to visit all nmethods after NmethodSweepFraction aoqi@0: // invocations so divide the remaining number of nmethods by the aoqi@0: // remaining number of invocations. This is only an estimate since aoqi@0: // the number of nmethods changes during the sweep so the final aoqi@0: // stage must iterate until it there are no more nmethods. aoqi@0: int todo = (CodeCache::nof_nmethods() - _seen) / _sweep_fractions_left; aoqi@0: int swept_count = 0; aoqi@0: aoqi@0: aoqi@0: assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here"); aoqi@0: assert(!CodeCache_lock->owned_by_self(), "just checking"); aoqi@0: aoqi@0: int freed_memory = 0; aoqi@0: { aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: aoqi@0: // The last invocation iterates until there are no more nmethods aoqi@0: for (int i = 0; (i < todo || _sweep_fractions_left == 1) && _current != NULL; i++) { aoqi@0: swept_count++; aoqi@0: if (SafepointSynchronize::is_synchronizing()) { // Safepoint request aoqi@0: if (PrintMethodFlushing && Verbose) { aoqi@0: tty->print_cr("### Sweep at %d out of %d, invocation: %d, yielding to safepoint", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left); aoqi@0: } aoqi@0: MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: aoqi@0: assert(Thread::current()->is_Java_thread(), "should be java thread"); aoqi@0: JavaThread* thread = (JavaThread*)Thread::current(); aoqi@0: ThreadBlockInVM tbivm(thread); aoqi@0: thread->java_suspend_self(); aoqi@0: } aoqi@0: // Since we will give up the CodeCache_lock, always skip ahead aoqi@0: // to the next nmethod. Other blobs can be deleted by other aoqi@0: // threads but nmethods are only reclaimed by the sweeper. aoqi@0: nmethod* next = CodeCache::next_nmethod(_current); aoqi@0: aoqi@0: // Now ready to process nmethod and give up CodeCache_lock aoqi@0: { aoqi@0: MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: freed_memory += process_nmethod(_current); aoqi@0: } aoqi@0: _seen++; aoqi@0: _current = next; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: assert(_sweep_fractions_left > 1 || _current == NULL, "must have scanned the whole cache"); aoqi@0: aoqi@0: const Ticks sweep_end_counter = Ticks::now(); aoqi@0: const Tickspan sweep_time = sweep_end_counter - sweep_start_counter; aoqi@0: _total_time_sweeping += sweep_time; aoqi@0: _total_time_this_sweep += sweep_time; aoqi@0: _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time); aoqi@0: _total_flushed_size += freed_memory; aoqi@0: _total_nof_methods_reclaimed += _flushed_count; aoqi@0: aoqi@0: EventSweepCodeCache event(UNTIMED); aoqi@0: if (event.should_commit()) { aoqi@0: event.set_starttime(sweep_start_counter); aoqi@0: event.set_endtime(sweep_end_counter); aoqi@0: event.set_sweepIndex(_traversals); aoqi@0: event.set_sweepFractionIndex(NmethodSweepFraction - _sweep_fractions_left + 1); aoqi@0: event.set_sweptCount(swept_count); aoqi@0: event.set_flushedCount(_flushed_count); aoqi@0: event.set_markedCount(_marked_for_reclamation_count); aoqi@0: event.set_zombifiedCount(_zombified_count); aoqi@0: event.commit(); aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: if(PrintMethodFlushing) { aoqi@0: tty->print_cr("### sweeper: sweep time(%d): " aoqi@0: INT64_FORMAT, _sweep_fractions_left, (jlong)sweep_time.value()); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: if (_sweep_fractions_left == 1) { aoqi@0: _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep); aoqi@0: log_sweep("finished"); aoqi@0: } aoqi@0: aoqi@0: // Sweeper is the only case where memory is released, check here if it aoqi@0: // is time to restart the compiler. Only checking if there is a certain aoqi@0: // amount of free memory in the code cache might lead to re-enabling aoqi@0: // compilation although no memory has been released. For example, there are aoqi@0: // cases when compilation was disabled although there is 4MB (or more) free aoqi@0: // memory in the code cache. The reason is code cache fragmentation. Therefore, aoqi@0: // it only makes sense to re-enable compilation if we have actually freed memory. aoqi@0: // Note that typically several kB are released for sweeping 16MB of the code aoqi@0: // cache. As a result, 'freed_memory' > 0 to restart the compiler. aoqi@0: if (!CompileBroker::should_compile_new_jobs() && (freed_memory > 0)) { aoqi@0: CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation); aoqi@0: log_sweep("restart_compiler"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This function updates the sweeper statistics that keep track of nmethods aoqi@0: * state changes. If there is 'enough' state change, the sweeper is invoked aoqi@0: * as soon as possible. There can be data races on _bytes_changed. The data aoqi@0: * races are benign, since it does not matter if we loose a couple of bytes. aoqi@0: * In the worst case we call the sweeper a little later. Also, we are guaranteed aoqi@0: * to invoke the sweeper if the code cache gets full. aoqi@0: */ aoqi@0: void NMethodSweeper::report_state_change(nmethod* nm) { aoqi@0: _bytes_changed += nm->total_size(); aoqi@0: possibly_enable_sweeper(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Function determines if there was 'enough' state change in the code cache to invoke aoqi@0: * the sweeper again. Currently, we determine 'enough' as more than 1% state change in aoqi@0: * the code cache since the last sweep. aoqi@0: */ aoqi@0: void NMethodSweeper::possibly_enable_sweeper() { aoqi@0: double percent_changed = ((double)_bytes_changed / (double)ReservedCodeCacheSize) * 100; aoqi@0: if (percent_changed > 1.0) { aoqi@0: _should_sweep = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class NMethodMarker: public StackObj { aoqi@0: private: aoqi@0: CompilerThread* _thread; aoqi@0: public: aoqi@0: NMethodMarker(nmethod* nm) { aoqi@0: _thread = CompilerThread::current(); aoqi@0: if (!nm->is_zombie() && !nm->is_unloaded()) { aoqi@0: // Only expose live nmethods for scanning aoqi@0: _thread->set_scanned_nmethod(nm); aoqi@0: } aoqi@0: } aoqi@0: ~NMethodMarker() { aoqi@0: _thread->set_scanned_nmethod(NULL); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void NMethodSweeper::release_nmethod(nmethod *nm) { aoqi@0: // Clean up any CompiledICHolders aoqi@0: { aoqi@0: ResourceMark rm; aoqi@0: MutexLocker ml_patch(CompiledIC_lock); aoqi@0: RelocIterator iter(nm); aoqi@0: while (iter.next()) { aoqi@0: if (iter.type() == relocInfo::virtual_call_type) { aoqi@0: CompiledIC::cleanup_call_site(iter.virtual_call_reloc()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: nm->flush(); aoqi@0: } aoqi@0: aoqi@0: int NMethodSweeper::process_nmethod(nmethod *nm) { aoqi@0: assert(!CodeCache_lock->owned_by_self(), "just checking"); aoqi@0: aoqi@0: int freed_memory = 0; aoqi@0: // Make sure this nmethod doesn't get unloaded during the scan, aoqi@0: // since safepoints may happen during acquired below locks. aoqi@0: NMethodMarker nmm(nm); aoqi@0: SWEEP(nm); aoqi@0: aoqi@0: // Skip methods that are currently referenced by the VM aoqi@0: if (nm->is_locked_by_vm()) { aoqi@0: // But still remember to clean-up inline caches for alive nmethods aoqi@0: if (nm->is_alive()) { aoqi@0: // Clean inline caches that point to zombie/non-entrant methods aoqi@0: MutexLocker cl(CompiledIC_lock); aoqi@0: nm->cleanup_inline_caches(); aoqi@0: SWEEP(nm); aoqi@0: } aoqi@0: return freed_memory; aoqi@0: } aoqi@0: aoqi@0: if (nm->is_zombie()) { aoqi@0: // If it is the first time we see nmethod then we mark it. Otherwise, aoqi@0: // we reclaim it. When we have seen a zombie method twice, we know that aoqi@0: // there are no inline caches that refer to it. aoqi@0: if (nm->is_marked_for_reclamation()) { aoqi@0: assert(!nm->is_locked_by_vm(), "must not flush locked nmethods"); aoqi@0: if (PrintMethodFlushing && Verbose) { aoqi@0: tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm); aoqi@0: } aoqi@0: freed_memory = nm->total_size(); aoqi@0: if (nm->is_compiled_by_c2()) { aoqi@0: _total_nof_c2_methods_reclaimed++; aoqi@0: } aoqi@0: release_nmethod(nm); aoqi@0: _flushed_count++; aoqi@0: } else { aoqi@0: if (PrintMethodFlushing && Verbose) { aoqi@0: tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm); aoqi@0: } aoqi@0: nm->mark_for_reclamation(); aoqi@0: // Keep track of code cache state change aoqi@0: _bytes_changed += nm->total_size(); aoqi@0: _marked_for_reclamation_count++; aoqi@0: SWEEP(nm); aoqi@0: } aoqi@0: } else if (nm->is_not_entrant()) { aoqi@0: // If there are no current activations of this method on the aoqi@0: // stack we can safely convert it to a zombie method thartmann@8075: if (nm->can_convert_to_zombie()) { aoqi@0: if (PrintMethodFlushing && Verbose) { aoqi@0: tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm); aoqi@0: } thartmann@8073: // Clear ICStubs to prevent back patching stubs of zombie or unloaded thartmann@8073: // nmethods during the next safepoint (see ICStub::finalize). thartmann@8073: MutexLocker cl(CompiledIC_lock); thartmann@8073: nm->clear_ic_stubs(); aoqi@0: // Code cache state change is tracked in make_zombie() aoqi@0: nm->make_zombie(); aoqi@0: _zombified_count++; aoqi@0: SWEEP(nm); aoqi@0: } else { aoqi@0: // Still alive, clean up its inline caches aoqi@0: MutexLocker cl(CompiledIC_lock); aoqi@0: nm->cleanup_inline_caches(); aoqi@0: SWEEP(nm); aoqi@0: } aoqi@0: } else if (nm->is_unloaded()) { aoqi@0: // Unloaded code, just make it a zombie aoqi@0: if (PrintMethodFlushing && Verbose) { aoqi@0: tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm); aoqi@0: } aoqi@0: if (nm->is_osr_method()) { aoqi@0: SWEEP(nm); aoqi@0: // No inline caches will ever point to osr methods, so we can just remove it aoqi@0: freed_memory = nm->total_size(); aoqi@0: if (nm->is_compiled_by_c2()) { aoqi@0: _total_nof_c2_methods_reclaimed++; aoqi@0: } aoqi@0: release_nmethod(nm); aoqi@0: _flushed_count++; aoqi@0: } else { thartmann@8075: { thartmann@8075: // Clean ICs of unloaded nmethods as well because they may reference other thartmann@8075: // unloaded nmethods that may be flushed earlier in the sweeper cycle. thartmann@8075: MutexLocker cl(CompiledIC_lock); thartmann@8075: nm->cleanup_inline_caches(); thartmann@8075: } aoqi@0: // Code cache state change is tracked in make_zombie() aoqi@0: nm->make_zombie(); aoqi@0: _zombified_count++; aoqi@0: SWEEP(nm); aoqi@0: } aoqi@0: } else { aoqi@0: if (UseCodeCacheFlushing) { aoqi@0: if (!nm->is_locked_by_vm() && !nm->is_osr_method() && !nm->is_native_method()) { aoqi@0: // Do not make native methods and OSR-methods not-entrant aoqi@0: nm->dec_hotness_counter(); aoqi@0: // Get the initial value of the hotness counter. This value depends on the aoqi@0: // ReservedCodeCacheSize aoqi@0: int reset_val = hotness_counter_reset_val(); aoqi@0: int time_since_reset = reset_val - nm->hotness_counter(); aoqi@0: double threshold = -reset_val + (CodeCache::reverse_free_ratio() * NmethodSweepActivity); aoqi@0: // The less free space in the code cache we have - the bigger reverse_free_ratio() is. aoqi@0: // I.e., 'threshold' increases with lower available space in the code cache and a higher aoqi@0: // NmethodSweepActivity. If the current hotness counter - which decreases from its initial aoqi@0: // value until it is reset by stack walking - is smaller than the computed threshold, the aoqi@0: // corresponding nmethod is considered for removal. aoqi@0: if ((NmethodSweepActivity > 0) && (nm->hotness_counter() < threshold) && (time_since_reset > 10)) { aoqi@0: // A method is marked as not-entrant if the method is aoqi@0: // 1) 'old enough': nm->hotness_counter() < threshold aoqi@0: // 2) The method was in_use for a minimum amount of time: (time_since_reset > 10) aoqi@0: // The second condition is necessary if we are dealing with very small code cache aoqi@0: // sizes (e.g., <10m) and the code cache size is too small to hold all hot methods. aoqi@0: // The second condition ensures that methods are not immediately made not-entrant aoqi@0: // after compilation. aoqi@0: nm->make_not_entrant(); aoqi@0: // Code cache state change is tracked in make_not_entrant() aoqi@0: if (PrintMethodFlushing && Verbose) { aoqi@0: tty->print_cr("### Nmethod %d/" PTR_FORMAT "made not-entrant: hotness counter %d/%d threshold %f", aoqi@0: nm->compile_id(), nm, nm->hotness_counter(), reset_val, threshold); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // Clean-up all inline caches that point to zombie/non-reentrant methods aoqi@0: MutexLocker cl(CompiledIC_lock); aoqi@0: nm->cleanup_inline_caches(); aoqi@0: SWEEP(nm); aoqi@0: } aoqi@0: return freed_memory; aoqi@0: } aoqi@0: aoqi@0: // Print out some state information about the current sweep and the aoqi@0: // state of the code cache if it's requested. aoqi@0: void NMethodSweeper::log_sweep(const char* msg, const char* format, ...) { aoqi@0: if (PrintMethodFlushing) { jcm@8713: ResourceMark rm; aoqi@0: stringStream s; aoqi@0: // Dump code cache state into a buffer before locking the tty, aoqi@0: // because log_state() will use locks causing lock conflicts. aoqi@0: CodeCache::log_state(&s); aoqi@0: aoqi@0: ttyLocker ttyl; aoqi@0: tty->print("### sweeper: %s ", msg); aoqi@0: if (format != NULL) { aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: tty->vprint(format, ap); aoqi@0: va_end(ap); aoqi@0: } aoqi@0: tty->print_cr("%s", s.as_string()); aoqi@0: } aoqi@0: aoqi@0: if (LogCompilation && (xtty != NULL)) { jcm@8713: ResourceMark rm; aoqi@0: stringStream s; aoqi@0: // Dump code cache state into a buffer before locking the tty, aoqi@0: // because log_state() will use locks causing lock conflicts. aoqi@0: CodeCache::log_state(&s); aoqi@0: aoqi@0: ttyLocker ttyl; aoqi@0: xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count()); aoqi@0: if (format != NULL) { aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: xtty->vprint(format, ap); aoqi@0: va_end(ap); aoqi@0: } aoqi@0: xtty->print("%s", s.as_string()); aoqi@0: xtty->stamp(); aoqi@0: xtty->end_elem(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void NMethodSweeper::print() { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print_cr("Code cache sweeper statistics:"); aoqi@0: tty->print_cr(" Total sweep time: %1.0lfms", (double)_total_time_sweeping.value()/1000000); aoqi@0: tty->print_cr(" Total number of full sweeps: %ld", _total_nof_code_cache_sweeps); aoqi@0: tty->print_cr(" Total number of flushed methods: %ld(%ld C2 methods)", _total_nof_methods_reclaimed, aoqi@0: _total_nof_c2_methods_reclaimed); aoqi@0: tty->print_cr(" Total size of flushed methods: " SIZE_FORMAT "kB", _total_flushed_size/K); aoqi@0: }