| 1 | package de.uka.ipd.sdq.simucomframework.usage; |
| 2 | |
| 3 | import org.apache.log4j.Logger; |
| 4 | |
| 5 | import de.uka.ipd.sdq.probespec.framework.RequestContext; |
| 6 | import de.uka.ipd.sdq.probespec.framework.garbagecollection.IRegionBasedGarbageCollector; |
| 7 | import de.uka.ipd.sdq.reliability.core.FailureStatistics; |
| 8 | import de.uka.ipd.sdq.simucomframework.Context; |
| 9 | import de.uka.ipd.sdq.simucomframework.ReliabilitySensorHelper; |
| 10 | import de.uka.ipd.sdq.simucomframework.SimuComSimProcess; |
| 11 | import de.uka.ipd.sdq.simucomframework.exceptions.FailureException; |
| 12 | import de.uka.ipd.sdq.simucomframework.model.SimuComModel; |
| 13 | |
| 14 | /** |
| 15 | * A closed workload user is a user which performs the typical closed workload |
| 16 | * cycle: execute, think, execute, ... |
| 17 | * |
| 18 | * @author Steffen Becker |
| 19 | * |
| 20 | */ |
| 21 | public class ClosedWorkloadUser extends SimuComSimProcess implements IUser { |
| 22 | |
| 23 | private static Logger logger = Logger.getLogger(ClosedWorkloadUser.class |
| 24 | .getName()); |
| 25 | |
| 26 | private IScenarioRunner scenarioRunner; |
| 27 | private String thinkTime; |
| 28 | private String usageScenarioId; |
| 29 | |
| 30 | // private static int USERCOUNT = 0; |
| 31 | private int runCount = 0; |
| 32 | |
| 33 | private IRegionBasedGarbageCollector<RequestContext> blackboardGarbageCollector; |
| 34 | |
| 35 | /** |
| 36 | * Constructor of the closed workload user |
| 37 | * |
| 38 | * @param owner |
| 39 | * The model this user belongs to |
| 40 | * @param name |
| 41 | * The users name |
| 42 | * @param scenarioRunner |
| 43 | * The scenario runner determining the users behaviour |
| 44 | * @param thinkTimeSpec |
| 45 | * A stoex which determines the users think time |
| 46 | * @param usageScenarioId |
| 47 | * the id of the corresponding UsageScenario PCM model element |
| 48 | */ |
| 49 | public ClosedWorkloadUser(final SimuComModel owner, final String name, |
| 50 | final IScenarioRunner scenarioRunner, final String thinkTimeSpec, |
| 51 | final String usageScenarioId) { |
| 52 | super(owner, name); |
| 53 | this.scenarioRunner = scenarioRunner; |
| 54 | this.thinkTime = thinkTimeSpec; |
| 55 | this.blackboardGarbageCollector = owner.getProbeSpecContext().getBlackboardGarbageCollector(); |
| 56 | this.usageScenarioId = usageScenarioId; |
| 57 | } |
| 58 | |
| 59 | protected long sessionId; |
| 60 | |
| 61 | /* |
| 62 | * (non-Javadoc) |
| 63 | * |
| 64 | * @see desmoj.core.simulator.SimProcess#lifeCycle() |
| 65 | */ |
| 66 | @Override |
| 67 | protected void internalLifeCycle() { |
| 68 | |
| 69 | // Repeat usage scenario runs as long as simulation is running: |
| 70 | while (getModel().getSimulationControl().isRunning()) { |
| 71 | // update session id |
| 72 | updateNewSessionID(); |
| 73 | |
| 74 | try { |
| 75 | if (getModel().getConfiguration().getSimulateFailures()) { |
| 76 | FailureStatistics.getInstance().increaseRunCount(); |
| 77 | FailureStatistics.getInstance().printRunCount(logger); |
| 78 | } |
| 79 | blackboardGarbageCollector.enterRegion(getRequestContext() |
| 80 | .rootContext()); |
| 81 | scenarioRunner(this); |
| 82 | if (getModel().getConfiguration().getSimulateFailures()) { |
| 83 | ReliabilitySensorHelper.recordScenarioRunResultSuccess( |
| 84 | getModel(), getRequestContext(), usageScenarioId); |
| 85 | } |
| 86 | } catch (FailureException exception) { |
| 87 | if (getModel().getConfiguration().getSimulateFailures()) { |
| 88 | FailureStatistics.getInstance() |
| 89 | .increaseUnhandledFailureCounter( |
| 90 | exception.getFailureType(), |
| 91 | currentSessionId); |
| 92 | ReliabilitySensorHelper.recordScenarioRunResultFailure( |
| 93 | getModel(), exception |
| 94 | .getFailureType(), getRequestContext(), |
| 95 | usageScenarioId); |
| 96 | } |
| 97 | } finally { |
| 98 | // Increase measurements counter manually as usage scenario run |
| 99 | // is not finished: |
| 100 | this.getModel().increaseMainMeasurementsCount(); |
| 101 | |
| 102 | blackboardGarbageCollector.leaveRegion(getRequestContext() |
| 103 | .rootContext()); |
| 104 | runCount++; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Mark user as finished: |
| 109 | // USERCOUNT--; |
| 110 | |
| 111 | // Print failure statistics after last user is finished: |
| 112 | // if (USERCOUNT == 0) { |
| 113 | // if (this.getModel().getConfig().getSimulateFailures()) { |
| 114 | // FailureStatistics.getInstance().printFailureStatistics(logger); |
| 115 | // } |
| 116 | // } |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * (non-Javadoc) |
| 121 | * |
| 122 | * @see |
| 123 | * de.uka.ipd.sdq.simucomframework.usage.IScenarioRunner#scenarioRunner( |
| 124 | * desmoj.core.simulator.SimProcess) |
| 125 | */ |
| 126 | public void scenarioRunner(SimuComSimProcess thread) { |
| 127 | double thinkTime = (Double) Context.evaluateStatic(this.thinkTime, |
| 128 | Double.class, null); |
| 129 | this.hold(thinkTime); |
| 130 | this.scenarioRunner.scenarioRunner(thread); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * (non-Javadoc) |
| 135 | * |
| 136 | * @see de.uka.ipd.sdq.simucomframework.usage.IUser#startUserLife() |
| 137 | */ |
| 138 | public void startUserLife() { |
| 139 | logger.debug(this.getName() + " started! I'm alive!!!"); |
| 140 | // USERCOUNT++; |
| 141 | this.scheduleAt(0); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * {@inheritDoc} |
| 146 | * <p> |
| 147 | * A closed workload user runs through his lifecycle several times in |
| 148 | * series. Each time the same {@link SimuComSimProcess} is used. In order to be |
| 149 | * able to distinguish between the different runs, the request context id |
| 150 | * gets concatenated with the run count. The run counts represents how often |
| 151 | * the lifecycle has been executed. Thus the returned ID is unique for each |
| 152 | * run through the lifecycle. |
| 153 | */ |
| 154 | @Override |
| 155 | public RequestContext getRequestContext() { |
| 156 | return super.getRequestContext().append("." + runCount); |
| 157 | } |
| 158 | |
| 159 | } |