001    /*
002    Galois, a framework to exploit amorphous data-parallelism in irregular
003    programs.
004    
005    Copyright (C) 2010, The University of Texas at Austin. All rights reserved.
006    UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS SOFTWARE
007    AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR ANY
008    PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF PERFORMANCE, AND ANY
009    WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE.
010    NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF THE
011    SOFTWARE OR DOCUMENTATION. Under no circumstances shall University be liable
012    for incidental, special, indirect, direct or consequential damages or loss of
013    profits, interruption of business, or related expenses which may arise from use
014    of Software or Documentation, including but not limited to those resulting from
015    defects in Software and/or Documentation, or loss or inaccuracy of data of any
016    kind.
017    
018    File: Launcher.java 
019    
020    */
021    
022    
023    
024    package util;
025    
026    import java.util.Random;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import util.ThreadTimer.Tick;
032    
033    /**
034     * Resets state when multiple runs of the same piece of code are executed.
035     * 
036     *
037     */
038    public class Launcher {
039      private static Launcher instance;
040      private boolean isFirstRun;
041      private Tick start;
042      private Tick stop;
043      private final List<Statistics> stats;
044      private final List<Runnable> callbacks;
045      private final Random random;
046    
047      private Launcher() {
048        isFirstRun = true;
049        stats = new ArrayList<Statistics>();
050        callbacks = new ArrayList<Runnable>();
051    
052        random = new Random();
053      }
054    
055      /**
056       * Marks beginning of timed section. If there are multiple calls to this
057       * method within the same run, honor the latest call. This method also
058       * performs a full garbage collection.
059       */
060      public void startTiming() {
061        System.gc();
062        if (stop == null)
063          start = ThreadTimer.tick();
064      }
065    
066      /**
067       * Marks end of timed section. If there are multiple calls to this method
068       * within the same run, honor the earliest call.
069       */
070      public void stopTiming() {
071        if (stop == null)
072          stop = ThreadTimer.tick();
073      }
074    
075      /**
076       * 
077       * @return  true if this is the first run
078       */
079      public boolean isFirstRun() {
080        return isFirstRun;
081      }
082    
083      /**
084       * Returns the wall-time of the most recently completed run with or without
085       * garbage collection time included
086       * 
087       * @param withoutGc true if garbage collection time should be excluded from result
088       * @return   wall-time in milliseconds of the run
089       */
090      public long elapsedTime(boolean withoutGc) {
091        return start.elapsedTime(withoutGc, stop);
092      }
093    
094      /**
095       * Resets the launcher to its initial configuration in preparation for the
096       * next run. All previously accumulated statistics will be cleared.
097       */
098      public void reset() {
099        stop = null;
100        start = null;
101        isFirstRun = false;
102        for (Runnable r : callbacks) {
103          r.run();
104        }
105        callbacks.clear();
106        stats.clear();
107      }
108    
109      /**
110       * @return  the accumulated statistics for the most recently completed run
111       */
112      public List<Statistics> getStatistics() {
113        return stats;
114      }
115    
116      /**
117       * Adds a callback to be called when launcher is reset.
118       * 
119       * @param callback  callback to called on reset
120       */
121      public void onRestart(Runnable callback) {
122        callbacks.add(callback);
123      }
124    
125      /**
126       * Adds statistics to current run
127       * 
128       * @param stat  the statistics to add
129       */
130      public void addStats(Statistics stat) {
131        if (stat != null)
132          stats.add(stat);
133      }
134    
135      /**
136       * Returns the random number generator for this run
137       * 
138       * @param seed  the random number generator seed
139       * @return      a random number generator
140       */
141      public Random getRandom(int seed) {
142        random.setSeed(seed);
143        return random;
144      }
145    
146      /**
147       * @return  an instance of the launcher
148       */
149      public static Launcher getLauncher() {
150        if (instance == null)
151          instance = new Launcher();
152        return instance;
153      }
154    }