/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "code/codeCache.hpp"
#include "code/nmethod.hpp"
#include "compiler/compileBroker.hpp"
#include "memory/resourceArea.hpp"
#include "oops/methodOop.hpp"
#include "runtime/atomic.hpp"
#include "runtime/compilationPolicy.hpp"
#include "runtime/mutexLocker.hpp"
#include "runtime/sweeper.hpp"
#include "runtime/vm_operations.hpp"
#include "trace/tracing.hpp"
#include "utilities/events.hpp"
#include "utilities/xmlstream.hpp"
#ifdef ASSERT
// Sweeper logging code
class SweeperRecord {
public:
int traversal;
int invocation;
int compile_id;
long traversal_mark;
int state;
const char* kind;
int line;
void print() {
PTR_FORMAT " state = %d traversal_mark %d line = %d",
uep,
vep,
line);
}
};
static int _sweep_index = 0;
for (int i = _sweep_index; i < SweeperLogEntries; i++) {
}
}
for (int i = 0; i < _sweep_index; i++) {
}
}
}
}
for (int i = _sweep_index; i < SweeperLogEntries; i++) {
// skip empty records
}
for (int i = 0; i < _sweep_index; i++) {
// skip empty records
}
}
}
}
}
#else
#endif
int NMethodSweeper::_seen = 0 ; // No. of nmethods we have currently processed in current pass of CodeCache
volatile int NMethodSweeper::_invocations = 0; // No. of invocations left until we are completed with this pass
public:
// If we see an activation belonging to a non_entrant nmethod, we mark it.
}
}
};
if (!MethodFlushing) return;
_do_sweep = true;
// No need to synchronize access, since this is always executed at a
// safepoint. If we aren't in the middle of scan and a rescan
// hasn't been requested then just return. If UseCodeCacheFlushing is on and
// code cache flushing is in progress, don't skip sweeping to help make progress
// clearing space in the code cache.
if ((_current == NULL && !_rescan) && !(UseCodeCacheFlushing && !CompileBroker::should_compile_new_jobs())) {
_do_sweep = false;
return;
}
// Make sure CompiledIC_lock in unlocked, since we might update some
// inline caches. If it is, we just bail-out and try later.
// Check for restart
_seen = 0;
_traversals += 1;
if (PrintMethodFlushing) {
}
// reset the flags since we started a scan from the beginning.
_rescan = false;
_locked_seen = 0;
}
if (UseCodeCacheFlushing) {
if (!CodeCache::needs_flushing()) {
// scan_stacks() runs during a safepoint, no race with setters
_advise_to_sweep = 0;
}
if (was_full()) {
// There was some progress so attempt to restart the compiler
set_was_full(false);
// Update the _last_was_full time so we can tell how fast the
// code cache is filling up
log_sweep("restart_compiler");
}
}
}
}
if ((!MethodFlushing) || (!_do_sweep)) return;
if (_invocations > 0) {
// Only one thread at a time will sweep
if (old != 0) {
return;
}
#ifdef ASSERT
// Create the ring buffer for the logging code
}
#endif
if (_invocations > 0) {
_invocations--;
}
_sweep_started = 0;
}
}
_flushed_count = 0;
_zombified_count = 0;
_marked_count = 0;
if (PrintMethodFlushing && Verbose) {
tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _invocations);
}
// We want to visit all nmethods after NmethodSweepFraction
// invocations so divide the remaining number of nmethods by the
// remaining number of invocations. This is only an estimate since
// the number of nmethods changes during the sweep so the final
// stage must iterate until it there are no more nmethods.
{
// The last invocation iterates until there are no more nmethods
if (PrintMethodFlushing && Verbose) {
tty->print_cr("### Sweep at %d out of %d, invocation: %d, yielding to safepoint", _seen, CodeCache::nof_nmethods(), _invocations);
}
}
// Since we will give up the CodeCache_lock, always skip ahead
// to the next nmethod. Other blobs can be deleted by other
// threads but nmethods are only reclaimed by the sweeper.
// Now ready to process nmethod and give up CodeCache_lock
{
}
_seen++;
}
}
// we've completed a scan without making progress but there were
// nmethods we were unable to process either because they were
// locked or were still on stack. We don't have to aggresively
// clean them up so just stop scanning. We could scan once more
// but that complicates the control logic and it's unlikely to
// matter much.
if (PrintMethodFlushing) {
}
}
if (event.should_commit()) {
}
#ifdef ASSERT
if(PrintMethodFlushing) {
}
#endif
if (_invocations == 1) {
log_sweep("finished");
}
}
private:
public:
}
~NMethodMarker() {
}
};
// Make sure this nmethod doesn't get unloaded during the scan,
// since the locks acquired below might safepoint.
// Skip methods that are currently referenced by the VM
if (nm->is_locked_by_vm()) {
// But still remember to clean-up inline caches for alive nmethods
// Clean-up all inline caches that points to zombie/non-reentrant methods
} else {
_locked_seen++;
}
return;
}
// If it is first time, we see nmethod then we mark it. Otherwise,
// we reclame it. When we have seen a zombie method twice, we know that
// there are no inline caches that refer to it.
if (nm->is_marked_for_reclamation()) {
if (PrintMethodFlushing && Verbose) {
tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
}
} else {
if (PrintMethodFlushing && Verbose) {
tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
}
_rescan = true;
}
} else if (nm->is_not_entrant()) {
// If there is no current activations of this method on the
// stack we can safely convert it to a zombie method
if (nm->can_not_entrant_be_converted()) {
if (PrintMethodFlushing && Verbose) {
tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
}
nm->make_zombie();
_rescan = true;
} else {
// Still alive, clean up its inline caches
// we coudn't transition this nmethod so don't immediately
// request a rescan. If this method stays on the stack for a
// long time we don't want to keep rescanning the code cache.
}
} else if (nm->is_unloaded()) {
// Unloaded code, just make it a zombie
if (PrintMethodFlushing && Verbose)
if (nm->is_osr_method()) {
// No inline caches will ever point to osr methods, so we can just remove it
} else {
nm->make_zombie();
_rescan = true;
}
} else {
if (UseCodeCacheFlushing) {
CodeCache::needs_flushing()) {
// This method has not been called since the forced cleanup happened
nm->make_not_entrant();
}
}
// Clean-up all inline caches that points to zombie/non-reentrant methods
}
}
// Code cache unloading: when compilers notice the code cache is getting full,
// they will call a vm op that comes here. This code attempts to speculatively
// unload the oldest half of the nmethods (based on the compile job id) by
// saving the old code in a list in the CodeCache. Then
// execution resumes. If a method so marked is not called by the second sweeper
// stack traversal after the current one, the nmethod will be marked non-entrant and
// got rid of by normal sweeping. If the method is called, the methodOop's
// go back to their normal state.
// Only the first one to notice can advise us to start early cleaning
if (!is_full){
if (old != 0) {
return;
}
}
if (is_full) {
// Since code cache is full, immediately stop new compiles
if (!did_set){
// only the first to notice can start the cleaning,
// others will go back and block
return;
}
set_was_full(true);
// If we run out within MinCodeCacheFlushingInterval of the last unload time, give up
if (curr_interval < max_interval) {
_rescan = true;
curr_interval/1000);
return;
}
}
// rescan again as soon as possible
_rescan = true;
}
// If there was a race in detecting full code cache, only run
// one vm op for it or keep the compiler shut off
if (!CodeCache::needs_flushing()) {
log_sweep("restart_compiler");
return;
}
}
// Traverse the code cache trying to dump the oldest nmethods
log_sweep("start_cleaning");
// OSR methods cannot be flushed like this. Also, don't flush native methods
// since they are part of the JDK in most cases
// This method has not been previously considered for
// unloading or it was restored already
disconnected++;
} else if (nm->is_speculatively_disconnected()) {
// This method was previously considered for preemptive unloading and was not called since then
nm->make_not_entrant();
}
if (curr_comp_id > _highest_marked) {
}
}
}
log_sweep("stop_cleaning",
// Shut off compiler. Sweeper will start over with a new stack scan and
// traversal cycle and turn it back on if it clears enough space.
if (was_full()) {
}
if (event.should_commit()) {
}
// After two more traversals the sweeper will get rid of unrestored nmethods
#ifdef ASSERT
if(PrintMethodFlushing && Verbose) {
}
#endif
}
// Print out some state information about the current sweep and the
// state of the code cache if it's requested.
if (PrintMethodFlushing) {
stringStream s;
// Dump code cache state into a buffer before locking the tty,
// because log_state() will use locks causing lock conflicts.
}
}
stringStream s;
// Dump code cache state into a buffer before locking the tty,
// because log_state() will use locks causing lock conflicts.
}
}
}