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.util;
014    
015    import java.io.FileOutputStream;
016    import java.io.IOException;
017    import java.io.PrintStream;
018    import org.jikesrvm.VM;
019    import org.jikesrvm.adaptive.controller.Controller;
020    import org.jikesrvm.compilers.common.CompiledMethod;
021    import org.jikesrvm.compilers.opt.driver.CompilationPlan;
022    
023    /**
024     * This class provides advice file used by compile replay experiments
025     * Right now this class is basically duplicate part of the AOSLogging
026     * class.
027     */
028    public class AOSGenerator {
029    
030      /*
031       * The output file stream, where all log messages will go
032       */
033      private static PrintStream log;
034    
035      /*
036      * Record that the AOS logging has been booted.
037      * Needed to allow fast exit from reporting to ensure
038      * that when no class is specified to be run but "-help" is specified,
039      * don't want null pointer exception to occur!
040      */
041      private static boolean booted = false;
042    
043      // variable used to avoid recursive calls
044      private static boolean recording = false;
045    
046      /**
047       * Return whether AOS logging has booted.
048       * @return whether AOS logging has booted
049       */
050      public static boolean booted() {
051        return booted;
052      }
053    
054      /**
055       * Called from ControllerThread.run to initialize the logging subsystem
056       */
057      public static void boot() {
058        VM.sysWrite("AOS generation booted\n");
059        try {
060          log = new PrintStream(new FileOutputStream(Controller.options.COMPILATION_ADVICE_FILE_OUTPUT));
061        } catch (IOException e) {
062          VM.sysWrite("IOException caught in AOSGenerator.java while trying to create and start log file.\n");
063          VM.sysWrite("Please check for file permission problems\n");
064        }
065        booted = true;
066        recording = false;
067      }
068    
069      ////////////////////////////////////////////////////////////////
070      // Logging level 2
071      ////////////////////////////////////////////////////////////////
072    
073      /**
074       * This method logs the successful completion of an adaptively
075       * selected recompilation
076       * @param plan the Compilation plan being executed.
077       */
078      public static void reCompilationWithOpt(CompilationPlan plan) {
079        if (!booted) return;
080        synchronized (log) {
081          log.println(plan.method.getDeclaringClass().getDescriptor() +
082                      " " +
083                      plan.method.getName() +
084                      " " +
085                      plan.method.getDescriptor() +
086                      " 3 " +
087                      /*it's always compiler*/
088                      plan.options.getOptLevel());
089        }
090      }
091    
092      public static void baseCompilationCompleted(CompiledMethod cm) {
093        if (recording || (!booted)) return;
094        synchronized (log) {
095          recording = true;
096          log.println(cm.getMethod().getDeclaringClass().getDescriptor() +
097                      " " +
098                      cm.getMethod().getName() +
099                      " " +
100                      cm.getMethod().getDescriptor() +
101                      " " +
102                      cm.getCompilerType() +
103                      " " +
104                      /*it's always baseline compiler*/
105                      "-1");
106          recording = false;
107        }
108      }
109    }