nmethod.cpp revision 1410
1204N/A * Copyright 1997-2010 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A#
include "incls/_precompiled.incl" 0N/A// Only bother with this argument setup if dtrace is available 0N/A const char*,
int,
const char*,
int,
const char*,
int,
void*,
size_t);
0N/A char*,
int,
char*,
int,
char*,
int);
0N/A#
else // ndef DTRACE_ENABLED 0N/A//--------------------------------------------------------------------------------- 0N/A// NMethod statistics 0N/A// They are printed under various flags, including: 0N/A// PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation. 0N/A// (In the latter two cases, they like other stats are printed to the log only.) 0N/A// These variables are put into one block to reduce relocations 0N/A// and make it simpler to print from the debugger. 0N/A tty->
print_cr(
"PcDesc Statistics: %d queries, %.2f comparisons per query",
0N/A tty->
print_cr(
" caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d",
0N/A//--------------------------------------------------------------------------------- 0N/A// The _unwind_handler is a special marker address, which says that 0N/A// for given exception oop and address, the frame should be removed 0N/A// as the tuple cannot be caught in the nmethod 0N/A// private method for handling exception cache 0N/A// These methods are private, and used to manipulate the exception cache 0N/A//----------------------------------------------------------------------------- 0N/A// Helper used by both find_pc_desc methods. 0N/A // reset the cache by filling it with benign (non-null) values 0N/A // In order to prevent race conditions do not load cache elements 0N/A // repeatedly, but use a local copy: 0N/A // Step one: Check the most recently returned value. 0N/A // Step two: Check the LRU cache. 0N/A // Update the LRU cache by shifting pc_desc forward: 0N/A // Note: Do not update _last_pc_desc. It fronts for the LRU cache. 0N/A// adjust pcs_size so that it is a multiple of both oopSize and 0N/A// sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple 0N/A// of oopSize, then 2*sizeof(PcDesc) is) 0N/A//----------------------------------------------------------------------------- 0N/A // find the previous and next entry of ec 0N/A// public method for accessing the exception cache 0N/A// These are the public access methods. 0N/A // We never grab a lock to read the exception cache, so we may 0N/A // have false negatives. This is okay, as it can only happen during 0N/A // the first few exception lookups for a given nmethod. 0N/A // There are potential race conditions during exception cache updates, so we 0N/A // must own the ExceptionCache_lock before doing ANY modifications. Because 605N/A // we don't lock during reads, it is possible to have several threads attempt 0N/A // to update the cache with the same data. We need to check for already inserted 0N/A // copies of the current data before adding it. 0N/A//-------------end of code for ExceptionCache-------------- 0N/A// %%% This variable is no longer used? 116N/A#
endif // def HAVE_DTRACE_H 0N/A // To make dependency checking during class loading fast, record 0N/A // the nmethod dependencies in the classes it is dependent on. 0N/A // This allows the dependency checking code to simply walk the 0N/A // class hierarchy above the loaded class, checking only nmethods 0N/A // which are dependent on those classes. The slow way is to 0N/A // check every nmethod for dependencies which makes it linear in 0N/A // the number of methods compiled. For applications with a lot 0N/A // classes the slow way is too slow. 0N/A if (
klass ==
NULL)
continue;
// ignore things like evol_method 0N/A // record this nmethod as dependent on this klass 0N/A// For native wrappers 0N/A // We have no exception handler or deopt handler make the 0N/A // values something that will never match a pc like the nmethod vtable entry 116N/A#
endif // def HAVE_DTRACE_H 0N/A // This output goes directly to the tty, not the compiler log. 0N/A // To enable tools to match it up with the compilation activity, 0N/A // be sure to tag this tty output with the compile ID. 0N/A // print the header part first 0N/A // then print the requested information 116N/A // We have no exception handler or deopt handler make the 116N/A // values something that will never match a pc like the nmethod vtable entry 116N/A // This output goes directly to the tty, not the compiler log. 116N/A // To enable tools to match it up with the compilation activity, 116N/A // be sure to tag this tty output with the compile ID. 116N/A // print the header part first 116N/A // then print the requested information 116N/A#
endif // def HAVE_DTRACE_H 0N/A // Always leave some room in the CodeCache for I2C/C2I adapters 116N/A#
endif // def HAVE_DTRACE_H 0N/A // Exception handler and deopt handler are in the stub section 0N/A // Copy contents of ScopeDescRecorder to nmethod 0N/A // Copy contents of ExceptionHandlerTable to nmethod 0N/A // we use the information of entry points to find out if a method is 0N/A // static or non static 0N/A " entry points must be same for static methods and vice versa");
0N/A // Note: Do not verify in here as the CodeCache_lock is 0N/A // taken which would conflict with the CompiledIC_lock 0N/A // which taken during the verification of call sites. 0N/A // (was bug - gri 10/25/99) 0N/A// Print a short set of xml attributes to identify this nmethod. The 0N/A// output should be embedded in some other element. 0N/A// Print out more verbose output usually for a newly created nmethod. 0N/A // Print a little tag line that looks like +PrintCompilation output: 0N/A // print the header part first 0N/A // then print the requested information 0N/A // If the method is not entrant or zombie then a JMP is plastered over the 0N/A // first few bytes. If an oop in the old code was there, that oop 0N/A // should not get GC'd. Skip the first few bytes of oops on 0N/A // not-entrant methods. 0N/A // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 0N/A // This means that the low_boundary is going to be a little too high. 0N/A // This shouldn't matter, since oops of non-entrant methods are never used. 0N/A // In fact, why are we bothering to look at oops in a non-entrant method?? 0N/A // Find all calls in an nmethod, and clear the ones that points to zombie methods 0N/A // Ok, to lookup references to zombies here 0N/A // Clean inline caches pointing to both zombie and not_entrant methods 0N/A // Clean inline caches pointing to both zombie and not_entrant methods 989N/A// This is a private interface with the sweeper. 0N/A// Tell if a non-entrant method can be converted to a zombie (i.e., there is no activations on the stack) 0N/A // Since the nmethod sweeper only does partial sweep the sweeper's traversal 0N/A // count can be greater than the stack traversal count before it hits the 0N/A // nmethod for the second time. 0N/A // Could be gated by ProfileTraps, but do not bother... 0N/A // Since this nmethod is being unloaded, make sure that dependencies 0N/A // recorded in instanceKlasses get flushed and pass non-NULL closure to 0N/A // indicate that this work is being done during a GC. 0N/A // A non-NULL is_alive closure indicates that this is being called during GC. 0N/A // Break cycle between nmethod & method 941N/A // Unlink the osr method, so we do not look this up again 0N/A // If _method is already NULL the methodOop is about to be unloaded, 0N/A // so we don't have to break the cycle. Note that it is possible to 0N/A // have the methodOop live here, in case we unload the nmethod because 0N/A // it is pointing to some oop (other than the methodOop) being unloaded. 0N/A // OSR methods point to the methodOop, but the methodOop does not 0N/A // Make the class unloaded - i.e., change state and notify sweeper 0N/A // Transitioning directly from live to unloaded -- so 0N/A // we need to force a cache clean-up; remember this 0N/A // The methodOop is gone at this point 989N/A //set_scavenge_root_link(NULL); // done by prune_scavenge_root_nmethods 0N/A // Remove from list of active nmethods 0N/A // Set entry as invalid 0N/A// Common functionality for both make_not_entrant and make_zombie 0N/A // Make sure the nmethod is not flushed in case of a safepoint in code below. 1206N/A // If the method is already zombie there is nothing to do 1109N/A // invalidate osr nmethod before acquiring the patching lock since 1109N/A // they both acquire leaf locks and we don't want a deadlock. 1109N/A // This logic is equivalent to the logic below for patching the 1109N/A // verified entry point of regular methods. 1109N/A // this effectively makes the osr nmethod not entrant 0N/A // Enter critical section. Does not block for safepoint. 1109N/A // another thread already performed this transition so nothing 1109N/A // to do, but return false to indicate this. 0N/A // The caller can be calling the method statically or through an inline 0N/A // When the nmethod becomes zombie it is no longer alive so the 0N/A // dependencies must be flushed. nmethods in the not_entrant 0N/A // state will be flushed later when the transition to zombie 0N/A // happens or they get unloaded. 0N/A }
// leave critical region under Patching_lock 0N/A // Make sweeper aware that there is a zombie method that needs to be removed 0N/A // not_entrant only stuff 1206N/A // It's a true state change, so mark the method as decompiled. 1206N/A // Do it only for transition from alive. 0N/A // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload event 0N/A // and it hasn't already been reported for this nmethod then report it now. 0N/A // (the event may have been reported earilier if the GC marked it for unloading). 0N/A // Zombie only stuff 0N/A // Check whether method got unloaded at a safepoint before this, 0N/A // if so we can skip the flushing steps below 0N/A // Remove nmethod from method. 0N/A // We need to check if both the _code and _from_compiled_code_entry_point 0N/A // refer to this nmethod because there is a race in setting these two fields 0N/A // in methodOop as seen in bugid 4947125. 0N/A // If the vep() points to the zombie nmethod, the memory for the nmethod 0N/A // could be flushed and the compiler and vtable stubs could still call 0N/A // Note that there are no valid oops in the nmethod anymore. 0N/A // completely deallocate this method 0N/A // We need to deallocate any ExceptionCache data. 0N/A // Note that we do not need to grab the nmethod lock for this, it 0N/A // better be thread safe if we're disposing of it! 0N/A// Notify all classes this nmethod is dependent on that it is no 0N/A// longer dependent. This should only be called in two situations. 0N/A// First, when a nmethod transitions to a zombie all dependents need 0N/A// to be clear. Since zombification happens at a safepoint there's no 0N/A// synchronization issues. The second place is a little more tricky. 0N/A// During phase 1 of mark sweep class unloading may happen and as a 0N/A// result some nmethods may get unloaded. In this case the flushing 0N/A// of dependencies must happen during phase 1 since after GC any 0N/A// dependencies in the unloaded nmethod won't be updated, so 0N/A// traversing the dependency information in unsafe. In that case this 0N/A// function is called with a non-NULL argument and this function only 0N/A// notifies instanceKlasses that are reachable 0N/A "is_alive is non-NULL if and only if we are called during GC");
0N/A if (
klass ==
NULL)
continue;
// ignore things like evol_method 0N/A // During GC the is_alive closure is non-NULL, and is used to 0N/A // determine liveness of dependees that need to be updated. 0N/A// If this oop is not live, the nmethod can be unloaded. 0N/A // The oop should be kept alive 989N/A // If ScavengeRootsInCode is true, an nmethod might be unloaded 989N/A // simply because one of its constant oops has gone dead. 989N/A // No actual classes need to be unloaded in order for this to occur. 0N/A// ------------------------------------------------------------------ 0N/A// post_compiled_method_load_event 0N/A// new method for install_code() path 0N/A// Transfer information from compilation to jvmti 0N/A // If a JVMTI agent has enabled the CompiledMethodUnload event then 0N/A // post the event. Sometime later this nmethod will be made a zombie by 0N/A // the sweeper but the methodOop will not be valid at that point. 0N/A // The JVMTI CompiledMethodUnload event can be enabled or disabled at 0N/A // any time. As the nmethod is being unloaded now we mark it has 0N/A // having the unload event reported - this will ensure that we don't 0N/A // attempt to report the event in the unlikely scenario where the 0N/A // event is enabled at the time the nmethod is made a zombie. 0N/A// GC to unload an nmethod if it contains otherwise unreachable 0N/A // Make sure the oop's ready to receive visitors 0N/A "should not call follow on zombie or unloaded nmethod");
0N/A // If the method is not entrant then a JMP is plastered over the 0N/A // first few bytes. If an oop in the old code was there, that oop 0N/A // should not get GC'd. Skip the first few bytes of oops on 0N/A // not-entrant methods. 0N/A // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 0N/A // (See comment above.) 0N/A // The RedefineClasses() API can cause the class unloading invariant 0N/A // Also, leave a debugging breadcrumb in local flag. 0N/A // This set of the unloading_occurred flag is done before the 0N/A // call to post_compiled_method_unload() so that the unloading 0N/A // of this nmethod is reported. 0N/A // If class unloading occurred we first iterate over all inline caches and 0N/A // clear ICs where the cached oop is referring to an unloaded klass or method. 0N/A // The remaining live cached oops will be traversed in the relocInfo::oop_type 0N/A // The only exception is compiledICHolder oops which may 0N/A // yet be marked below. (We check this further below). 1409N/A "cached oop in IC should be cleared");
0N/A // In this loop, we must only traverse those oops directly embedded in 0N/A // the code. Other oops (oop_index>0) are seen as part of scopes_oops. 0N/A "oop must be found in exactly one place");
0N/A // This nmethod was not unloaded; check below that all CompiledICs 0N/A // refer to marked oops. 0N/A "Found unmarked ic_oop in reachable nmethod");
941N/A// This method is called twice during GC -- once while 941N/A// tracing the "active" nmethods on thread stacks during 941N/A// the (strong) marking phase, and then again when walking 941N/A// the code cache contents during the weak roots processing 941N/A// phase. The two uses are distinguished by means of the 994N/A// 'do_strong_roots_only' flag, which is true in the first 941N/A// case. We want to walk the weak roots in the nmethod 941N/A// only in the second case. The weak roots in the nmethod 941N/A// are the oops in the ExceptionCache and the InlineCache 0N/A // make sure the oops ready to receive visitors 0N/A "should not call follow on zombie or unloaded nmethod");
0N/A // If the method is not entrant or zombie then a JMP is plastered over the 0N/A // first few bytes. If an oop in the old code was there, that oop 0N/A // should not get GC'd. Skip the first few bytes of oops on 0N/A // not-entrant methods. 0N/A // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump. 0N/A // (See comment above.) 941N/A // weak roots processing phase -- update ExceptionCache oops 941N/A }
// Else strong roots phase -- skip oops in ExceptionCache 0N/A // In this loop, we must only follow those oops directly embedded in 0N/A // the code. Other oops (oop_index>0) are seen as part of scopes_oops. 941N/A "oop must be found in exactly one place");
989N/A // This includes oop constants not inlined in the code stream. 989N/A// An nmethod is "marked" if its _mark_link is set non-null. 989N/A// Even if it is the end of the linked list, it will have a non-null link value, 989N/A// as long as it is on the list. 989N/A// This code must be MP safe, because it is used from parallel GC passes. 989N/A // Claim this nmethod for this thread to mark. 989N/A // Atomically append this nmethod (now claimed) to the head of the list: 989N/A // Mark was clear when we first saw this guy. 989N/A // On fall through, another racing thread marked this nmethod before we did. 989N/A // We use cmpxchg_ptr instead of regular assignment here because the user 989N/A // may fork a bunch of threads, and we need them all to see the same state. 0N/A// Method that knows how to preserve outgoing arguments at call. This method must be 0N/A// called with a frame corresponding to a Java invoke 0N/A // must be sorted and unique; we do a binary search in find_pc_desc() 0N/A "must start with a sentinel");
0N/A "must end with a sentinel");
1135N/A // Search for MethodHandle invokes and tag the nmethod. 0N/A // Adjust the final sentinel downward. 0N/A // Fill any rounding gaps with copies of the last record. 0N/A // The following assert could fail if sizeof(PcDesc) is not 0N/A // an integral multiple of oopSize (the rounding term). 0N/A // If it fails, change the logic to always allocate a multiple 0N/A // of sizeof(PcDesc), and fill unused words with copies of *last_pc. 0N/A// Finds a PcDesc with real-pc equal to "pc" 0N/A return NULL;
// PC is wildly out of range 0N/A // Check the PcDesc cache if it contains the desired PcDesc 0N/A // (This as an almost 100% hit rate.) 0N/A // Fallback algorithm: quasi-linear search for the PcDesc 0N/A // Find the last pc_offset less than the given offset. 0N/A // The successor must be the required match, if there is a match at all. 0N/A // (Use a fixed radix to avoid expensive affine pointer arithmetic.) 0N/A /* invariant on lower..upper during the following search: */ \
0N/A // Use the last successful return as a split point. 0N/A // Take giant steps at first (4096, then 256, then 16, then 1) 0N/A // Sneak up on the value with a linear search of length ~16. 0N/A // wholesale check of all dependencies 0N/A // What has happened: 0N/A // 1) a new class dependee has been added 0N/A // 2) dependee and all its super classes have been marked 0N/A // Evaluate only relevant dependencies. 0N/A // RC_TRACE macro has an embedded ResourceMark 0N/A (
"Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)",
0N/A// Called from mark_for_deoptimization, when dependee is invalidated. 0N/A // a zombie may never be patched 0N/A // Exception happened outside inline-cache check code => we are inside 0N/A // an active nmethod => use cpc to determine a return address 1250N/A // Let the normal error handling report the exception 0N/A // make sure you didn't forget to adjust the filler fields 0N/A//------------------------------------------------------------------------------------------- 0N/A// QQQ might we make this work from a frame?? 1204N/A// ----------------------------------------------------------------------------- 1204N/A// nmethod::get_deopt_original_pc 1204N/A// Return the original PC for the given PC if: 1204N/A// (a) the given PC belongs to a nmethod and 0N/A// ----------------------------------------------------------------------------- 1135N/A// ----------------------------------------------------------------------------- 0N/A // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant 0N/A // Make sure all the entry points are correctly aligned for patching. 0N/A // This code does not work in release mode since 0N/A // owns_lock only is available in debug mode. 0N/A if( !
method() )
return;
// Runtime stubs have no scope 0N/A // iterate through all interrupt point 0N/A // and verify the debug information is valid. 0N/A //verify_interrupt_point(iter.addr()); 0N/A // Right now there is no way to find out which entries support 0N/A // an interrupt point. It would be nice if we had this 0N/A // information in a table. 0N/A// ----------------------------------------------------------------------------- 989N/A // Actually look inside, to verify the claim that it's clean. 989N/A fatal(
"found an unadvertised bad non-perm oop in the code cache");
0N/A// Printing operations 0N/A // Find the first pc desc for all scopes in the code and print it. 0N/A// Return a the last scope in (begin..end] 0N/A // First, find an oopmap in (begin, end]. 0N/A // We use the odd half-closed interval so that oop maps and scope descs 0N/A // which are tied to the byte after a call are printed with the call itself. 100N/A // Print any debug info present at this pc. 0N/A // Print relocation information