use of org.jikesrvm.adaptive.controller.ControllerPlan in project JikesRVM by JikesRVM.
the class OnStackReplacementEvent method process.
/**
* This function will generate a controller plan and
* inserted in the recompilation queue.
*/
@Override
public void process() {
CompiledMethod compiledMethod = CompiledMethods.getCompiledMethod(CMID);
NormalMethod todoMethod = (NormalMethod) compiledMethod.getMethod();
double priority;
OptOptions options;
OptimizationPlanElement[] optimizationPlan;
ControllerPlan oldPlan = ControllerMemory.findLatestPlan(todoMethod);
if (oldPlan != null) {
CompilationPlan oldCompPlan = oldPlan.getCompPlan();
priority = oldPlan.getPriority();
options = oldCompPlan.options;
optimizationPlan = oldCompPlan.optimizationPlan;
} else {
priority = 5.0;
options = (OptOptions) RuntimeCompiler.options;
optimizationPlan = (OptimizationPlanElement[]) RuntimeCompiler.optimizationPlan;
}
CompilationPlan compPlan = new CompilationPlan(todoMethod, optimizationPlan, null, options);
OnStackReplacementPlan plan = new OnStackReplacementPlan(this.suspendedThread, compPlan, this.CMID, this.whereFrom, this.tsFromFPoff, this.ypTakenFPoff, priority);
Controller.compilationQueue.insert(priority, plan);
AOSLogging.logger.logOsrEvent("OSR inserts compilation plan successfully!");
// do not hold the reference anymore.
suspendedThread = null;
CMID = 0;
}
use of org.jikesrvm.adaptive.controller.ControllerPlan in project JikesRVM by JikesRVM.
the class InvocationCounts method counterTripped.
/**
* Called from baseline compiled code when a method's invocation counter
* becomes negative and thus must be handled
*
* @param id the compiled method id
*/
@Entrypoint
static synchronized void counterTripped(int id) {
// set counter to max int to avoid lots of redundant calls.
counts[id] = 0x7fffffff;
if (processed[id])
return;
processed[id] = true;
CompiledMethod cm = CompiledMethods.getCompiledMethod(id);
if (cm == null)
return;
if (VM.VerifyAssertions)
VM._assert(cm.getCompilerType() == CompiledMethod.BASELINE);
NormalMethod m = (NormalMethod) cm.getMethod();
CompilationPlan compPlan = new CompilationPlan(m, _optPlan, null, _options);
ControllerPlan cp = // 2.0 is a bogus number....
new ControllerPlan(compPlan, Controller.controllerClock, id, 2.0, 2.0, 2.0);
cp.execute();
}
use of org.jikesrvm.adaptive.controller.ControllerPlan in project JikesRVM by JikesRVM.
the class RuntimeCompiler method compile.
/**
* Compile a Java method when it is first invoked.
* @param method the method to compile
* @return its compiled method.
*/
public static CompiledMethod compile(NormalMethod method) {
if (VM.BuildForAdaptiveSystem) {
CompiledMethod cm;
if (!Controller.enabled) {
// System still early in boot process; compile with baseline compiler
cm = baselineCompile(method);
ControllerMemory.incrementNumBase();
} else {
if (Controller.options.optIRC()) {
if (// will only run once: don't bother optimizing
method.isClassInitializer() || // multiple pending (undelivered) exceptions [--DL]
RVMThread.getCurrentThread().getExceptionRegisters().getInUse()) {
// compile with baseline compiler
cm = baselineCompile(method);
ControllerMemory.incrementNumBase();
} else {
// compile with opt compiler
AOSInstrumentationPlan instrumentationPlan = new AOSInstrumentationPlan(Controller.options, method);
CompilationPlan compPlan = new CompilationPlan(method, (OptimizationPlanElement[]) optimizationPlan, instrumentationPlan, (OptOptions) options);
cm = optCompileWithFallBack(method, compPlan);
}
} else {
if ((Controller.options.BACKGROUND_RECOMPILATION && !Controller.options.ENABLE_PRECOMPILE)) {
// must be an initial compilation: compile with baseline compiler
// or if recompilation with OSR.
cm = baselineCompile(method);
ControllerMemory.incrementNumBase();
} else {
if (CompilerAdviceAttribute.hasAdvice()) {
CompilerAdviceAttribute attr = CompilerAdviceAttribute.getCompilerAdviceInfo(method);
if (attr.getCompiler() != CompiledMethod.OPT) {
cm = fallback(method);
AOSLogging.logger.recordCompileTime(cm, 0.0);
return cm;
}
int newCMID = -2;
CompilationPlan compPlan;
if (Controller.options.counters()) {
// for invocation counter, we only use one optimization level
compPlan = InvocationCounts.createCompilationPlan(method);
} else {
// for now there is not two options for sampling, so
// we don't have to use: if (Controller.options.sampling())
compPlan = Controller.recompilationStrategy.createCompilationPlan(method, attr.getOptLevel(), null);
}
AOSLogging.logger.recompilationStarted(compPlan);
newCMID = recompileWithOpt(compPlan);
cm = newCMID == -1 ? null : CompiledMethods.getCompiledMethod(newCMID);
if (newCMID == -1) {
AOSLogging.logger.recompilationAborted(compPlan);
} else if (newCMID > 0) {
AOSLogging.logger.recompilationCompleted(compPlan);
}
if (cm == null) {
// if recompilation is aborted
cm = baselineCompile(method);
ControllerMemory.incrementNumBase();
}
} else {
// check to see if there is a compilation plan for this method.
ControllerPlan plan = ControllerMemory.findLatestPlan(method);
if (plan == null || plan.getStatus() != ControllerPlan.IN_PROGRESS) {
// initial compilation or some other funny state: compile with baseline compiler
cm = baselineCompile(method);
ControllerMemory.incrementNumBase();
} else {
cm = plan.doRecompile();
if (cm == null) {
// opt compilation aborted for some reason.
cm = baselineCompile(method);
}
}
}
}
}
}
if ((Controller.options.ENABLE_ADVICE_GENERATION) && (cm.getCompilerType() == CompiledMethod.BASELINE) && Controller.enabled) {
AOSGenerator.baseCompilationCompleted(cm);
}
AOSLogging.logger.recordCompileTime(cm, 0.0);
return cm;
} else {
return baselineCompile(method);
}
}
use of org.jikesrvm.adaptive.controller.ControllerPlan in project JikesRVM by JikesRVM.
the class OSRProfiler method invalidateState.
// invalidate an execution state
private static synchronized void invalidateState(ExecutionState state) {
// step 1: invalidate the compiled method with this OSR assumption
// how does this affect the performance?
CompiledMethod mostRecentlyCompiledMethod = CompiledMethods.getCompiledMethod(state.cmid);
if (VM.VerifyAssertions) {
VM._assert(mostRecentlyCompiledMethod.getMethod() == state.meth);
}
// be invalidated in more than one thread at the same time
if (mostRecentlyCompiledMethod != state.meth.getCurrentCompiledMethod()) {
return;
}
// make sure the compiled method is an opt one
if (!(mostRecentlyCompiledMethod instanceof OptCompiledMethod)) {
return;
}
// reset the compiled method to null first, if other thread invokes
// this method before following opt recompilation, it can avoid OSR
state.meth.invalidateCompiledMethod(mostRecentlyCompiledMethod);
// a list of state from callee -> caller
if (VM.TraceOnStackReplacement) {
VM.sysWriteln("OSR " + OSRProfiler.invalidations + " : " + state.bcIndex + "@" + state.meth);
}
// simply reset the compiled method to null is not good
// for long run loops, because invalidate may cause
// the method falls back to the baseline again...
// NOW, we look for the previous compilation plan, and reuse
// the compilation plan.
boolean recmplsucc = false;
if (Controller.enabled) {
CompilationPlan cmplplan = null;
if (Controller.options.ENABLE_PRECOMPILE && CompilerAdviceAttribute.hasAdvice()) {
CompilerAdviceAttribute attr = CompilerAdviceAttribute.getCompilerAdviceInfo(state.meth);
if (VM.VerifyAssertions) {
VM._assert(attr.getCompiler() == CompiledMethod.OPT);
}
if (Controller.options.counters()) {
// for invocation counter, we only use one optimization level
cmplplan = InvocationCounts.createCompilationPlan(state.meth);
} else {
// for now there is not two options for sampling, so
// we don't have to use: if (Controller.options.sampling())
cmplplan = Controller.recompilationStrategy.createCompilationPlan(state.meth, attr.getOptLevel(), null);
}
} else {
ControllerPlan ctrlplan = ControllerMemory.findMatchingPlan(mostRecentlyCompiledMethod);
if (ctrlplan != null) {
cmplplan = ctrlplan.getCompPlan();
}
}
if (cmplplan != null) {
if (VM.VerifyAssertions) {
VM._assert(cmplplan.getMethod() == state.meth);
}
// for invalidated method, we do not perform OSR guarded inlining anymore.
// the Options object may be shared by several methods,
// we have to reset it back
boolean savedOsr = cmplplan.options.OSR_GUARDED_INLINING;
cmplplan.options.OSR_GUARDED_INLINING = false;
int newcmid = RuntimeCompiler.recompileWithOpt(cmplplan);
cmplplan.options.OSR_GUARDED_INLINING = savedOsr;
if (newcmid != -1) {
AOSLogging.logger.debug("recompiling state with opt succeeded " + state.cmid);
AOSLogging.logger.debug("new cmid " + newcmid);
// transfer hotness to the new cmid
double oldSamples = Controller.methodSamples.getData(state.cmid);
Controller.methodSamples.reset(state.cmid);
Controller.methodSamples.augmentData(newcmid, oldSamples);
recmplsucc = true;
if (VM.TraceOnStackReplacement) {
VM.sysWriteln(" recompile " + state.meth + " at -O" + cmplplan.options.getOptLevel());
}
}
}
}
if (!recmplsucc) {
int newcmid = RuntimeCompiler.recompileWithOpt(state.meth);
if (newcmid == -1) {
if (VM.TraceOnStackReplacement) {
VM.sysWriteln(" opt recompilation failed!");
}
state.meth.invalidateCompiledMethod(mostRecentlyCompiledMethod);
}
}
if (VM.TraceOnStackReplacement) {
VM.sysWriteln(" opt recompilation done!");
}
}
use of org.jikesrvm.adaptive.controller.ControllerPlan in project JikesRVM by JikesRVM.
the class SpecialCompiler method optCompile.
/**
* <ol>
* <li>generate prologue PSEUDO_bytecode from the state.
* <li>make new bytecodes with prologue.
* <li>set method's bytecode to specialized one.
* <li>adjust exception map, line number map.
* <li>compile the method.
* <li>restore bytecode, exception, linenumber map to the original one.
* </ol>
*
* @param state the execution state for the compilation
* @return the compiled method produced by the optimizing compiler
*/
public static CompiledMethod optCompile(ExecutionState state) {
NormalMethod method = state.getMethod();
if (VM.TraceOnStackReplacement) {
VM.sysWriteln("OPT : starts compiling " + method);
}
ControllerPlan latestPlan = ControllerMemory.findLatestPlan(method);
OptOptions _options = null;
if (latestPlan != null) {
_options = latestPlan.getCompPlan().options.dup();
} else {
// no previous compilation plan, a long run loop promoted from baseline.
// this only happens when testing, not in real code
_options = new OptOptions();
_options.setOptLevel(0);
}
// disable OSR points in specialized method
_options.OSR_GUARDED_INLINING = false;
CompilationPlan compPlan = new CompilationPlan(method, (OptimizationPlanElement[]) RuntimeCompiler.optimizationPlan, null, _options);
// it is also necessary to recompile the current method
// without OSR.
/* generate prologue bytes */
byte[] prologue = state.generatePrologue();
int prosize = prologue.length;
method.setForOsrSpecialization(prologue, state.getMaxStackHeight());
int[] oldStartPCs = null;
int[] oldEndPCs = null;
int[] oldHandlerPCs = null;
/* adjust exception table. */
{
// if (VM.TraceOnStackReplacement) {
// VM.sysWriteln("OPT adjust exception table.");
// }
ExceptionHandlerMap exceptionHandlerMap = method.getExceptionHandlerMap();
if (exceptionHandlerMap != null) {
oldStartPCs = exceptionHandlerMap.getStartPC();
oldEndPCs = exceptionHandlerMap.getEndPC();
oldHandlerPCs = exceptionHandlerMap.getHandlerPC();
int n = oldStartPCs.length;
int[] newStartPCs = new int[n];
System.arraycopy(oldStartPCs, 0, newStartPCs, 0, n);
exceptionHandlerMap.setStartPC(newStartPCs);
int[] newEndPCs = new int[n];
System.arraycopy(oldEndPCs, 0, newEndPCs, 0, n);
exceptionHandlerMap.setEndPC(newEndPCs);
int[] newHandlerPCs = new int[n];
System.arraycopy(oldHandlerPCs, 0, newHandlerPCs, 0, n);
exceptionHandlerMap.setHandlerPC(newHandlerPCs);
for (int i = 0; i < n; i++) {
newStartPCs[i] += prosize;
newEndPCs[i] += prosize;
newHandlerPCs[i] += prosize;
}
}
}
CompiledMethod newCompiledMethod = RuntimeCompiler.recompileWithOptOnStackSpecialization(compPlan);
// restore original bytecode, exception table, and line number table
method.finalizeOsrSpecialization();
{
ExceptionHandlerMap exceptionHandlerMap = method.getExceptionHandlerMap();
if (exceptionHandlerMap != null) {
exceptionHandlerMap.setStartPC(oldStartPCs);
exceptionHandlerMap.setEndPC(oldEndPCs);
exceptionHandlerMap.setHandlerPC(oldHandlerPCs);
}
}
// reverse back to the baseline
if (newCompiledMethod == null) {
if (VM.TraceOnStackReplacement) {
VM.sysWriteln("OPT : fialed, because compilation in progress, " + "fall back to baseline");
}
return baselineCompile(state);
}
// mark the method is a specialized one
newCompiledMethod.setSpecialForOSR();
if (VM.TraceOnStackReplacement) {
VM.sysWriteln("OPT : done");
VM.sysWriteln();
}
return newCompiledMethod;
}
Aggregations