1 | package de.uka.ipd.sdq.scheduler.sensors.impl; |
2 | |
3 | import de.uka.ipd.sdq.scheduler.SchedulerModel; |
4 | import de.uka.ipd.sdq.scheduler.processes.IActiveProcess; |
5 | import de.uka.ipd.sdq.scheduler.processes.PROCESS_STATE; |
6 | import de.uka.ipd.sdq.scheduler.sensors.IProcessStateSensor; |
7 | |
8 | public class RunTimeSensor implements IProcessStateSensor { |
9 | |
10 | private SchedulerModel model; |
11 | private PROCESS_STATE last_state; |
12 | private double lastUpdateTime; |
13 | private double otherTime; |
14 | private double runTime; |
15 | private IActiveProcess process; |
16 | |
17 | public RunTimeSensor(SchedulerModel model, IActiveProcess process) { |
18 | this.model = model; |
19 | this.lastUpdateTime = 0; |
20 | this.process = process; |
21 | this.last_state = process.getState(); |
22 | reset(); |
23 | } |
24 | |
25 | public void reset() { |
26 | runTime = 0; |
27 | otherTime = 0; |
28 | this.lastUpdateTime = model.getSimulationControl().getCurrentSimulationTime(); |
29 | } |
30 | |
31 | public double getRunTime(){ |
32 | if (process.getState() == PROCESS_STATE.RUNNING){ |
33 | double currentTime = model.getSimulationControl().getCurrentSimulationTime(); |
34 | double passedTime = currentTime - lastUpdateTime; |
35 | runTime += passedTime; |
36 | lastUpdateTime = currentTime; |
37 | } |
38 | return runTime; |
39 | } |
40 | |
41 | public double getNotRunTime(){ |
42 | if (process.getState() != PROCESS_STATE.RUNNING){ |
43 | double currentTime = model.getSimulationControl().getCurrentSimulationTime(); |
44 | double passedTime = currentTime - lastUpdateTime; |
45 | otherTime += passedTime; |
46 | lastUpdateTime = currentTime; |
47 | } |
48 | return otherTime; |
49 | } |
50 | |
51 | public void update(PROCESS_STATE new_state) { |
52 | double currentTime = model.getSimulationControl().getCurrentSimulationTime(); |
53 | double passedTime = currentTime - lastUpdateTime; |
54 | |
55 | // Process was running, but is finished now |
56 | if (last_state == PROCESS_STATE.RUNNING |
57 | && new_state != PROCESS_STATE.RUNNING) { |
58 | runTime += passedTime; |
59 | lastUpdateTime = currentTime; |
60 | } |
61 | |
62 | // Process was not running, but starts now |
63 | if (last_state != PROCESS_STATE.RUNNING |
64 | && new_state == PROCESS_STATE.RUNNING) { |
65 | otherTime += passedTime; |
66 | lastUpdateTime = currentTime; |
67 | } |
68 | last_state = new_state; |
69 | } |
70 | } |