1 | package de.uka.ipd.sdq.probespec.framework.calculator; |
2 | |
3 | import java.util.Vector; |
4 | |
5 | import javax.measure.Measure; |
6 | import javax.measure.quantity.Duration; |
7 | import javax.measure.quantity.Quantity; |
8 | import javax.measure.unit.Unit; |
9 | |
10 | import de.uka.ipd.sdq.probespec.framework.ProbeSample; |
11 | import de.uka.ipd.sdq.probespec.framework.ProbeSetSample; |
12 | import de.uka.ipd.sdq.probespec.framework.ProbeSpecContext; |
13 | import de.uka.ipd.sdq.probespec.framework.ProbeType; |
14 | import de.uka.ipd.sdq.probespec.framework.exceptions.CalculatorException; |
15 | import de.uka.ipd.sdq.probespec.framework.matching.IMatchRule; |
16 | import de.uka.ipd.sdq.probespec.framework.matching.ProbeTypeMatchRule; |
17 | |
18 | /** |
19 | * Calculates the waiting time for resources in environments where the stop of |
20 | * the waiting period cannot be observed directly. Rather the following values |
21 | * (respectively events) should be observable. |
22 | * <ul> |
23 | * <li><code>start</code> - "request for processing"-event</li> |
24 | * <li><code>stop</code> - "end of processing"-event (Notice: This is different |
25 | * from the waiting period stop)</li> |
26 | * <li><code>demand</code> - the demanded time</li> |
27 | * </ul> |
28 | * The waiting time results from calculating |
29 | * <code>(stop - start) - demand </code>. |
30 | * |
31 | * @author pmerkle |
32 | * |
33 | */ |
34 | public class DemandBasedWaitingTimeCalculator extends WaitingTimeCalculator { |
35 | |
36 | public DemandBasedWaitingTimeCalculator(ProbeSpecContext ctx, Integer startWaitingProbeSetID, |
37 | Integer stopProcessingProbeSetID) { |
38 | super(ctx, startWaitingProbeSetID, stopProcessingProbeSetID); |
39 | } |
40 | |
41 | @SuppressWarnings("unchecked") |
42 | @Override |
43 | protected Vector<Measure<?, ? extends Quantity>> calculate( |
44 | ProbeSetSample start, ProbeSetSample end) |
45 | throws CalculatorException { |
46 | // Obtain demand. The demand of start and end should be equal! |
47 | ProbeSample<Double, Duration> demandSample = obtainDemandProbeSample(start); |
48 | if (demandSample == null) { |
49 | throw new CalculatorException( |
50 | "Could not access demand probe sample."); |
51 | } |
52 | |
53 | // Obtain processing time |
54 | Vector<Measure<?, ? extends Quantity>> timeSpanResultTuple = super |
55 | .calculate(start, end); |
56 | Measure<Double, Duration> processingTimeSpanMeasure = (Measure<Double, Duration>) timeSpanResultTuple |
57 | .get(0); |
58 | Measure<Double, Duration> endTimeMeasure = (Measure<Double, Duration>) timeSpanResultTuple |
59 | .get(1); |
60 | |
61 | // Calculate waiting time |
62 | Unit<Duration> unit = processingTimeSpanMeasure.getUnit(); |
63 | double demand = demandSample.getMeasure().doubleValue(unit); |
64 | double processingTime = processingTimeSpanMeasure.doubleValue(unit); |
65 | double waitingTime = processingTime - demand; |
66 | if (waitingTime < 0) { |
67 | // final double threshold = -0.000001; |
68 | // if (waitingTime < threshold) { |
69 | // throw new RuntimeException( |
70 | // "Calculated negative waiting time. This should not happen!"); |
71 | // } else { |
72 | waitingTime = 0; |
73 | // } |
74 | } |
75 | |
76 | // Create result tuple |
77 | Measure<Double, Duration> waitingTimeMeasure = Measure.valueOf( |
78 | waitingTime, unit); |
79 | Vector<Measure<?, ? extends Quantity>> resultTuple = new Vector<Measure<?, ? extends Quantity>>(); |
80 | resultTuple.add(waitingTimeMeasure); |
81 | resultTuple.add(endTimeMeasure); |
82 | |
83 | return resultTuple; |
84 | } |
85 | |
86 | @SuppressWarnings("unchecked") |
87 | private ProbeSample<Double, Duration> obtainDemandProbeSample( |
88 | ProbeSetSample probeSetSample) { |
89 | IMatchRule[] rules = new IMatchRule[1]; |
90 | rules[0] = new ProbeTypeMatchRule(ProbeType.RESOURCE_DEMAND); |
91 | Vector<ProbeSample<?, ? extends Quantity>> result = probeSetSample |
92 | .getProbeSamples(rules); |
93 | |
94 | if (result != null && result.size() > 0) |
95 | return (ProbeSample<Double, Duration>) result.get(0); |
96 | |
97 | return null; |
98 | } |
99 | |
100 | } |