1 | package de.uka.ipd.sdq.scheduler.resources.active; |
2 | |
3 | import java.util.Hashtable; |
4 | import java.util.Map.Entry; |
5 | |
6 | import de.uka.ipd.sdq.probfunction.math.util.MathTools; |
7 | import de.uka.ipd.sdq.scheduler.IRunningProcess; |
8 | import de.uka.ipd.sdq.scheduler.ISchedulableProcess; |
9 | import de.uka.ipd.sdq.scheduler.LoggingWrapper; |
10 | import de.uka.ipd.sdq.scheduler.SchedulerModel; |
11 | import de.uka.ipd.sdq.simulation.abstractsimengine.AbstractSimEventDelegator; |
12 | |
13 | public class SimProcessorSharingResource extends AbstractActiveResource { |
14 | |
15 | /** |
16 | * The minimum amount of time used for scheduling an event |
17 | */ |
18 | static double JIFFY = 1e-9; |
19 | |
20 | private class ProcessingFinishedEvent extends AbstractSimEventDelegator<ISchedulableProcess> { |
21 | |
22 | public ProcessingFinishedEvent(SchedulerModel model) { |
23 | super(model, ProcessingFinishedEvent.class.getName()); |
24 | } |
25 | |
26 | @Override |
27 | public void eventRoutine(ISchedulableProcess process) { |
28 | ISchedulableProcess last = process; |
29 | toNow(); |
30 | running_processes.remove(last); |
31 | // fire changes of the queue length only, if there is one single core. |
32 | // With more cores, one cannot say on which core a job has been processed. |
33 | if (getCapacity() == 1) { |
34 | fireStateChange(running_processes.size(), 0); |
35 | } |
36 | fireDemandCompleted(last); |
37 | LoggingWrapper.log(last + " finished."); |
38 | scheduleNextEvent(); |
39 | last.activate(); |
40 | } |
41 | |
42 | } |
43 | |
44 | private ProcessingFinishedEvent processingFinished; |
45 | private Hashtable<ISchedulableProcess,Double> running_processes = new Hashtable<ISchedulableProcess, Double>(); |
46 | private double last_time; |
47 | |
48 | public SimProcessorSharingResource(SchedulerModel model, String name, String id, int i) { |
49 | super(model, i, name, id); |
50 | this.processingFinished = new ProcessingFinishedEvent(model); |
51 | } |
52 | |
53 | public void scheduleNextEvent() { |
54 | ISchedulableProcess shortest = null; |
55 | for (ISchedulableProcess process : running_processes.keySet()) { |
56 | if (shortest == null || running_processes.get(shortest) > running_processes.get(process)){ |
57 | shortest = process; |
58 | } |
59 | } |
60 | processingFinished.removeEvent(); |
61 | if (shortest!=null){ |
62 | double remainingTime = running_processes.get(shortest) * getSpeed(); |
63 | |
64 | // avoid trouble caused by rounding issues |
65 | remainingTime = remainingTime < JIFFY ? 0.0 : remainingTime; |
66 | |
67 | assert remainingTime >= 0 : "Remaining time ("+ remainingTime +")small than zero!"; |
68 | |
69 | processingFinished.schedule(shortest, remainingTime); |
70 | } |
71 | } |
72 | |
73 | |
74 | private void toNow() { |
75 | double now = getModel().getSimulationControl().getCurrentSimulationTime(); |
76 | double passed_time = now - last_time; |
77 | if (MathTools.less(0, passed_time)){ |
78 | passed_time /= getSpeed(); |
79 | for (Entry<ISchedulableProcess,Double> e : running_processes.entrySet()) { |
80 | double rem = e.getValue() - passed_time; |
81 | e.setValue(rem); |
82 | } |
83 | } |
84 | last_time = now; |
85 | } |
86 | |
87 | |
88 | private double getSpeed() { |
89 | double speed = (double)running_processes.size() / (double)getCapacity(); |
90 | return speed < 1.0 ? 1.0 : speed; |
91 | } |
92 | |
93 | |
94 | public void start() { |
95 | } |
96 | |
97 | |
98 | @Override |
99 | protected void dequeue(ISchedulableProcess process) { |
100 | } |
101 | |
102 | |
103 | @Override |
104 | protected void doProcessing(ISchedulableProcess process, int resourceServiceID, double demand) { |
105 | toNow(); |
106 | LoggingWrapper.log("PS: " + process + " demands " + demand); |
107 | if(demand < JIFFY) |
108 | { |
109 | demand = JIFFY; |
110 | LoggingWrapper.log("PS: " + process + " demand was increased to match JIFFY " + demand); |
111 | } |
112 | |
113 | running_processes.put(process, demand); |
114 | // fire changes of the queue length only, if there is one single core. |
115 | // With more cores, one cannot say on which core a job has been processed. |
116 | if (getCapacity() == 1) { |
117 | fireStateChange(running_processes.size(), 0); |
118 | } |
119 | scheduleNextEvent(); |
120 | process.passivate(); |
121 | } |
122 | |
123 | |
124 | @Override |
125 | public double getRemainingDemand(ISchedulableProcess process) { |
126 | if (!running_processes.contains(process)) { |
127 | return 0.0; |
128 | } |
129 | toNow(); |
130 | return running_processes.get(process); |
131 | } |
132 | |
133 | @Override |
134 | public void updateDemand(ISchedulableProcess process, double demand) { |
135 | boolean updated = false; |
136 | for (Entry<ISchedulableProcess,Double> e : running_processes.entrySet()) { |
137 | if (e.getKey().equals(process)) { |
138 | if (Double.isNaN(demand)) { |
139 | // TODO PM: Should an exception be thrown here? At least, a log entry should be written. |
140 | System.out.println(); |
141 | } |
142 | e.setValue(demand); |
143 | updated = true; |
144 | break; |
145 | } |
146 | } |
147 | if (updated == false) { |
148 | throw new RuntimeException("COULD NOT UPDATE PROCESS!"); |
149 | } |
150 | scheduleNextEvent(); |
151 | } |
152 | |
153 | @Override |
154 | protected void enqueue(ISchedulableProcess process) { |
155 | } |
156 | |
157 | public void registerProcess(IRunningProcess runningProcess) { |
158 | } |
159 | |
160 | public int getQueueLengthFor(SimResourceInstance simResourceInstance) { |
161 | return this.running_processes.size(); |
162 | } |
163 | |
164 | public void stop() { |
165 | } |
166 | |
167 | } |