001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.adaptive.controller;
014    
015    import org.jikesrvm.adaptive.recompilation.CompilerDNA;
016    import org.jikesrvm.classloader.NormalMethod;
017    import org.jikesrvm.compilers.common.CompiledMethod;
018    
019    /**
020     * Represents the recompilation choice of simply recompiling the
021     * method in question at a particular opt-level.  The cost is the
022     * expected compilation time at that level, and the benefit is the
023     * execution improvement of executing at that level.
024     */
025    class RecompileOptChoice extends RecompilationChoice {
026    
027      /**
028       * The opt level associated with this recompilation choice
029       */
030      private int thisChoiceOptLevel;
031    
032      /**
033       * The "compiler" (see CompilerDNA) that is associated with this choice
034       */
035      private int thisChoiceCompiler;
036    
037      /**
038       * Constructor
039       * @param level the opt level associated with this choice
040       */
041      RecompileOptChoice(int level) {
042        thisChoiceOptLevel = level;
043        thisChoiceCompiler = CompilerDNA.getCompilerConstant(level);
044      }
045    
046      @Override
047      double getCost(NormalMethod meth) {
048        return CompilerDNA.estimateCompileTime(getCompiler(), meth);
049      }
050    
051      @Override
052      double getFutureExecutionTime(int prevCompiler, double futureTimeForMethod) {
053        double rtFactor = CompilerDNA.getBenefitRatio(prevCompiler, getCompiler());
054        return futureTimeForMethod / rtFactor;
055      }
056    
057      /**
058       * {@inheritDoc}
059       * In this case, simply create a plan to recompile at level {@link #thisChoiceOptLevel}.
060       */
061      @Override
062      ControllerPlan makeControllerPlan(CompiledMethod cmpMethod, int prevCompiler, double prevTimeForMethod,
063                                           double bestActionTime, double expectedCompilationTime) {
064        double speedup = CompilerDNA.getBenefitRatio(prevCompiler, getCompiler());
065        double priority = prevTimeForMethod - bestActionTime;
066        return Controller.recompilationStrategy.
067            createControllerPlan(cmpMethod.getMethod(),
068                                 thisChoiceOptLevel,
069                                 null,
070                                 cmpMethod.getId(),
071                                 speedup,
072                                 expectedCompilationTime,
073                                 priority);
074      }
075    
076      /**
077       * How should this choice be displayed?
078       */
079      @Override
080      public String toString() {
081        return "O" + getOptLevel();
082      }
083    
084      /**
085       * Which opt-level is associated with this choice?
086       * @return the opt-level for this choice
087       */
088      int getOptLevel() {
089        return thisChoiceOptLevel;
090      }
091    
092      /**
093       * Which "compiler" is associated with this choice?
094       * @return the integer representing the compiler for this choice
095       * @see CompilerDNA#getCompilerConstant(int)
096       */
097      int getCompiler() {
098        return thisChoiceCompiler;
099      }
100    }