| 1 | package de.uka.ipd.sdq.prototype.framework; |
| 2 | |
| 3 | import java.util.concurrent.atomic.AtomicLong; |
| 4 | |
| 5 | import de.uka.ipd.sdq.prototype.framework.utils.RunProperties; |
| 6 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
| 7 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
| 8 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanSensor; |
| 9 | |
| 10 | /** |
| 11 | * Abstract class for running both closed and open workload users. |
| 12 | * |
| 13 | * For closed workloads, this class represents a single user. |
| 14 | * For open workloads, only one instance of this class represents the usage scenario and spawns |
| 15 | * a new thread each time the interarrival time has passed. |
| 16 | * |
| 17 | * @author Steffen, martens |
| 18 | * |
| 19 | */ |
| 20 | public abstract class AbstractScenarioThread extends Thread implements IStopable { |
| 21 | protected org.apache.log4j.Logger logger = org.apache.log4j.Logger |
| 22 | .getLogger(AbstractScenarioThread.class); |
| 23 | |
| 24 | private static AtomicLong measurementTotalCount = null; |
| 25 | protected long maxMeasurementCount = -1; |
| 26 | |
| 27 | ExperimentRun experimentRun = null; |
| 28 | protected boolean shouldContinue = true; |
| 29 | |
| 30 | protected String scenarioName; |
| 31 | |
| 32 | private TimeSpanSensor timeSpanSensor; |
| 33 | |
| 34 | static { |
| 35 | /** |
| 36 | * Total measurement count of all active usage scenarios (thus static). |
| 37 | * Is reset to 0 in the constructor by all scenarios, so that the counting |
| 38 | * starts after the construction of all threads. */ |
| 39 | measurementTotalCount = new AtomicLong(0); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Initialise thread and perform warmup runs. Number of warmup runs can be configured |
| 44 | * with -u option, or is 1000 as the default. |
| 45 | * |
| 46 | * @param expRun |
| 47 | * @param timeSpanSensor |
| 48 | * @param runProps |
| 49 | */ |
| 50 | public AbstractScenarioThread ( |
| 51 | Experiment exp, |
| 52 | ExperimentRun expRun, |
| 53 | String scenarioName, |
| 54 | RunProperties runProps) |
| 55 | { |
| 56 | this.experimentRun = expRun; |
| 57 | this.scenarioName = scenarioName; |
| 58 | this.timeSpanSensor = ExperimentManager.createOrReuseTimeSpanSensor(scenarioName); |
| 59 | |
| 60 | |
| 61 | if (runProps.hasOption("m")) |
| 62 | { |
| 63 | maxMeasurementCount = Integer.parseInt(runProps.getOptionValue('m')); |
| 64 | } |
| 65 | |
| 66 | int warmupRuns = 1000; |
| 67 | if (runProps.hasOption("u")) |
| 68 | { |
| 69 | warmupRuns = Integer.parseInt(runProps.getOptionValue('u')); |
| 70 | } |
| 71 | logger.info("Warmup - Cyles: "+warmupRuns); |
| 72 | for (int i=0; i<warmupRuns; i++) |
| 73 | { |
| 74 | logger.info("Warmup started, cycle: "+i); |
| 75 | getScenarioRunner(runProps).run(); |
| 76 | } |
| 77 | logger.info("Warmup finished"); |
| 78 | |
| 79 | // reset number of measurements to 0 |
| 80 | measurementTotalCount = new AtomicLong(0); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | public void run() { |
| 85 | while (shouldContinue) { |
| 86 | logger.debug("Starting my scenario"); |
| 87 | |
| 88 | try { |
| 89 | runAndMeasureUsageScenarioIteration(); |
| 90 | } catch (Exception ex) { |
| 91 | ex.printStackTrace(); |
| 92 | shouldContinue = false; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | protected abstract void runAndMeasureUsageScenarioIteration(); |
| 99 | |
| 100 | /** |
| 101 | * FIXME: duplicate, see ExperimentManager! |
| 102 | * |
| 103 | * @param start |
| 104 | */ |
| 105 | protected void takeScenarioMeasurement(long start) { |
| 106 | long now = System.nanoTime(); |
| 107 | double measuredTimeSpan = (now - start) / Math.pow(10, 9); |
| 108 | |
| 109 | experimentRun.addTimeSpanMeasurement(timeSpanSensor, |
| 110 | now / Math.pow(10, 9), measuredTimeSpan); |
| 111 | logger.debug("Finished my scenario"); |
| 112 | |
| 113 | long value = measurementTotalCount.incrementAndGet(); |
| 114 | logger.debug("Execution of scenario iteration no "+value+" took: " + measuredTimeSpan |
| 115 | + " seconds"); |
| 116 | |
| 117 | if (maxMeasurementCount > 0 && value >= maxMeasurementCount && shouldContinue) { |
| 118 | logger.info("Reached maximum measurement count"); |
| 119 | shouldContinue = false; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | |
| 124 | } |
| 125 | |
| 126 | public void requestStop() { |
| 127 | shouldContinue = false; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Return a new instance of the usage scenario to be executed. |
| 132 | * @param runProps |
| 133 | * @return |
| 134 | */ |
| 135 | protected abstract Runnable getScenarioRunner(RunProperties runProps); |
| 136 | } |