1 | package de.uka.ipd.sdq.scheduler.timeslice.impl; |
2 | |
3 | import de.uka.ipd.sdq.probfunction.math.util.MathTools; |
4 | import de.uka.ipd.sdq.scheduler.timeslice.ITimeSlice; |
5 | |
6 | |
7 | public class QuantumTimeSlice implements ITimeSlice { |
8 | |
9 | protected double remaining_time; |
10 | protected double timeslice; |
11 | protected int remaining_quanta; |
12 | protected int quanta; // Windows default : 6 |
13 | protected int min_quanta; // Windows default : 2 |
14 | |
15 | public QuantumTimeSlice(double timeslice, int quanta, int min_quanta) { |
16 | super(); |
17 | |
18 | assert timeslice > 0 : "Timeslice must be larger than 0."; |
19 | assert quanta > 0 : "Quanta must be larger than 0."; |
20 | assert min_quanta > 0 : "Min Quanta must be larger than 0."; |
21 | |
22 | this.timeslice = timeslice; |
23 | this.quanta = quanta; |
24 | this.min_quanta = min_quanta; |
25 | this.remaining_time = 0; |
26 | this.remaining_quanta = 0; |
27 | } |
28 | |
29 | protected QuantumTimeSlice(){ |
30 | this.timeslice = 0; |
31 | this.remaining_time = 0; |
32 | this.remaining_quanta = 0; |
33 | } |
34 | |
35 | |
36 | public boolean isFinished() { |
37 | return MathTools.equalsDouble(remaining_time, 0.0); |
38 | } |
39 | |
40 | |
41 | public void substractTime(double time) { |
42 | remaining_time -= time; |
43 | |
44 | double half = timeslice / 2; |
45 | if (remaining_time <= half && (remaining_time + time) > half){ |
46 | remaining_quanta -= (quanta / 2); |
47 | if (MathTools.equalsDouble(remaining_time, 0)){ |
48 | remaining_quanta = 0; |
49 | } |
50 | } |
51 | assert MathTools.lessOrEqual(0.0, remaining_time) : "Timeslice exceeded: " + remaining_time; |
52 | } |
53 | |
54 | |
55 | public void reset() { |
56 | remaining_time = timeslice; |
57 | remaining_quanta = quanta; |
58 | } |
59 | |
60 | |
61 | public void fullReset() { |
62 | remaining_time = timeslice; |
63 | remaining_quanta = quanta; |
64 | } |
65 | |
66 | |
67 | public double getRemainingTime() { |
68 | return remaining_time; |
69 | } |
70 | |
71 | |
72 | public void punish(int penalty) { |
73 | remaining_quanta--; |
74 | } |
75 | |
76 | |
77 | public void setExpired() { |
78 | this.remaining_time = 0; |
79 | this.remaining_quanta = 0; |
80 | |
81 | } |
82 | |
83 | @Override |
84 | public ITimeSlice clone() { |
85 | QuantumTimeSlice cts = new QuantumTimeSlice(); |
86 | cts.timeslice = this.timeslice; |
87 | cts.quanta = this.quanta; |
88 | cts.remaining_time = this.remaining_time; |
89 | cts.remaining_quanta = this.remaining_quanta; |
90 | return cts; |
91 | } |
92 | |
93 | |
94 | public void halfReset() { |
95 | int factor = 0; |
96 | if (remaining_quanta > 4) |
97 | factor = 2; |
98 | else if (remaining_quanta > 1) |
99 | factor = 1; |
100 | remaining_time = timeslice/2 * factor; |
101 | } |
102 | |
103 | public void updateTimeForBoosting() { |
104 | if (remaining_quanta < min_quanta) { |
105 | remaining_time = 0; |
106 | } |
107 | } |
108 | |
109 | public void updateTimeForScheduling() { |
110 | } |
111 | } |