1 | package de.uka.ipd.sdq.scheduler.strategy.impl; |
2 | |
3 | import scheduler.configuration.StarvationBoost; |
4 | import de.uka.ipd.sdq.scheduler.ISchedulableProcess; |
5 | import de.uka.ipd.sdq.scheduler.SchedulerModel; |
6 | import de.uka.ipd.sdq.scheduler.priority.IPriority; |
7 | import de.uka.ipd.sdq.scheduler.priority.IPriorityUpdateStrategy; |
8 | import de.uka.ipd.sdq.scheduler.priority.update.SetToBaseUpdate; |
9 | import de.uka.ipd.sdq.scheduler.processes.IActiveProcess; |
10 | import de.uka.ipd.sdq.scheduler.processes.impl.ProcessWithPriority; |
11 | import de.uka.ipd.sdq.scheduler.queueing.IQueueingStrategy; |
12 | import de.uka.ipd.sdq.scheduler.resources.IResourceInstance; |
13 | import de.uka.ipd.sdq.scheduler.resources.active.SimActiveResource; |
14 | import de.uka.ipd.sdq.scheduler.resources.active.SimResourceInstance; |
15 | |
16 | public class PreemptiveScheduler extends AbstractScheduler { |
17 | |
18 | private SchedulerModel model; |
19 | |
20 | public PreemptiveScheduler(SchedulerModel model, SimActiveResource resource, |
21 | IQueueingStrategy queueingStrategy, boolean in_front_after_waiting, |
22 | double scheduling_interval, StarvationBoost starvationBoost) { |
23 | super(resource, queueingStrategy, in_front_after_waiting, starvationBoost); |
24 | this.model = model; |
25 | this.scheduling_interval = scheduling_interval; |
26 | } |
27 | |
28 | |
29 | public void schedule(IResourceInstance instance) { |
30 | if (instance.isScheduling()) |
31 | return; |
32 | |
33 | // Balance the runqueue of this instance with the runqueues of other |
34 | // instances. This might change the state of the instance's runqueue. |
35 | // So, the next runnable process can only be determined after the |
36 | // balancing was finished. |
37 | queueing_strategy.activelyBalance(instance); |
38 | |
39 | // get the currently scheduled process for the instance. |
40 | ProcessWithPriority running_process = (ProcessWithPriority) instance |
41 | .getRunningProcess(); |
42 | |
43 | // Update the timing variables and priority of the process. Possibly |
44 | // pending events of the process are canceled. |
45 | toNow(running_process); |
46 | |
47 | if (running_process == null){ |
48 | } else if ( running_process.getTimeslice().isFinished()) { |
49 | unschedule(running_process, false, instance); |
50 | } else { |
51 | unschedule(running_process, true, instance); |
52 | } |
53 | scheduleNextProcess(instance); |
54 | scheduleNextEvent(instance); |
55 | } |
56 | |
57 | |
58 | private void scheduleNextProcess(ProcessWithPriority next_process, IResourceInstance instance) { |
59 | if (next_process != null) { |
60 | next_process.toNow(); |
61 | next_process.update(); |
62 | fromReadyToRunningOn(next_process, instance); |
63 | } |
64 | } |
65 | |
66 | private void scheduleNextProcess(IResourceInstance instance) { |
67 | lookForStarvingProcessesAndApplyStarvationBoost(instance); |
68 | |
69 | ProcessWithPriority next_process = (ProcessWithPriority) queueing_strategy.getNextProcessFor(instance); |
70 | scheduleNextProcess(next_process, instance); |
71 | } |
72 | |
73 | private void lookForStarvingProcessesAndApplyStarvationBoost(IResourceInstance instance) { |
74 | if(starvationBoost != null){ |
75 | for (IActiveProcess p : queueing_strategy.getStarvingProcesses(instance, starvationBoost.getStarvationLimit())){ |
76 | applyStarvationBoost((ProcessWithPriority)p); |
77 | } |
78 | |
79 | } |
80 | |
81 | } |
82 | |
83 | private void applyStarvationBoost(ProcessWithPriority p) { |
84 | p.setToStaticPriorityWithBonus(starvationBoost.getBoost()); |
85 | IPriorityUpdateStrategy priorityUpdateStrategy = new SetToBaseUpdate(starvationBoost.getDurationInTimeslices()); |
86 | p.setPriorityUpdateStrategy(priorityUpdateStrategy); |
87 | } |
88 | |
89 | |
90 | private void toNow(ProcessWithPriority process) { |
91 | if (process != null){ |
92 | process.toNow(); |
93 | } |
94 | } |
95 | |
96 | private void unschedule(ProcessWithPriority running_process, |
97 | boolean next_has_higher_priority, IResourceInstance current) { |
98 | if (running_process != null) { |
99 | if (running_process.getTimeslice().isFinished()){ |
100 | running_process.update(); |
101 | fromRunningToReady(running_process, current, false); |
102 | running_process.getTimeslice().fullReset(); |
103 | } else { |
104 | fromRunningToReady(running_process, current, next_has_higher_priority); |
105 | } |
106 | if (running_process.getRunQueue().getCurrentLoad() == 1){ |
107 | running_process.getRunQueue().resetStarvationInfo(); |
108 | } |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * pOne > pTwo ? |
114 | * |
115 | * @param pTwo |
116 | * @return |
117 | */ |
118 | private boolean hasHigherPriority(ProcessWithPriority pOne, |
119 | ProcessWithPriority pTwo) { |
120 | if (pOne == null) |
121 | return false; |
122 | if (pTwo == null) |
123 | return true; |
124 | IPriority prio_one = pOne.getDynamicPriority(); |
125 | IPriority prio_two = pTwo.getDynamicPriority(); |
126 | return prio_one.greaterThan(prio_two); |
127 | } |
128 | |
129 | public void scheduleNextEvent(IResourceInstance instance) { |
130 | double time = model.getSimulationControl().getCurrentSimulationTime(); |
131 | ProcessWithPriority running = (ProcessWithPriority) instance |
132 | .getRunningProcess(); |
133 | if (running != null) { |
134 | running.toNow(); |
135 | double remainingTime = running.getTimeslice().getRemainingTime(); |
136 | double currentDemand = running.getCurrentDemand(); |
137 | if ( currentDemand < remainingTime ) |
138 | running.scheduleProceedEvent(this); |
139 | else |
140 | instance.scheduleSchedulingEvent(remainingTime); |
141 | } else { |
142 | if (!queueing_strategy.isIdle(instance)) |
143 | instance.scheduleSchedulingEvent(0); |
144 | else |
145 | instance.scheduleSchedulingEvent(scheduling_interval); |
146 | } |
147 | } |
148 | |
149 | |
150 | public boolean isIdle(IResourceInstance instance) { |
151 | return queueing_strategy.isIdle(instance); |
152 | } |
153 | |
154 | |
155 | public double getInterval() { |
156 | return scheduling_interval; |
157 | } |
158 | |
159 | @Override |
160 | public void terminateProcess(IActiveProcess process, IResourceInstance current) { |
161 | super.terminateProcess(process, current); |
162 | ISchedulableProcess sProcess = process.getSchedulableProcess(); |
163 | if (sProcess.isFinished() |
164 | // do NOT remove the originally defined processes as they |
165 | // serve as prototypes for all spawned processes. |
166 | && sProcess.getRootProcess() != sProcess){ |
167 | this.resource.unregisterProcess(process); |
168 | } |
169 | } |
170 | |
171 | |
172 | public int getQueueLengthFor(SimResourceInstance simResourceInstance) { |
173 | return this.queueing_strategy.getQueueLengthFor(simResourceInstance); |
174 | } |
175 | |
176 | |
177 | } |