| 1 | package de.uka.ipd.sdq.scheduler.strategy.impl; |
| 2 | |
| 3 | import java.util.Deque; |
| 4 | |
| 5 | import scheduler.configuration.StarvationBoost; |
| 6 | import de.uka.ipd.sdq.scheduler.LoggingWrapper; |
| 7 | import de.uka.ipd.sdq.scheduler.processes.IActiveProcess; |
| 8 | import de.uka.ipd.sdq.scheduler.processes.impl.PreemptiveProcess; |
| 9 | import de.uka.ipd.sdq.scheduler.queueing.IQueueingStrategy; |
| 10 | import de.uka.ipd.sdq.scheduler.resources.IResourceInstance; |
| 11 | import de.uka.ipd.sdq.scheduler.resources.active.SimActiveResource; |
| 12 | import de.uka.ipd.sdq.scheduler.resources.passive.WaitingProcess; |
| 13 | import de.uka.ipd.sdq.scheduler.strategy.IScheduler; |
| 14 | |
| 15 | public abstract class AbstractScheduler implements IScheduler { |
| 16 | protected SimActiveResource resource; |
| 17 | |
| 18 | protected IQueueingStrategy queueing_strategy; |
| 19 | |
| 20 | private boolean in_front_after_waiting; |
| 21 | |
| 22 | protected double scheduling_interval; |
| 23 | |
| 24 | protected StarvationBoost starvationBoost; |
| 25 | |
| 26 | public AbstractScheduler(SimActiveResource resource, |
| 27 | IQueueingStrategy queueingStrategy, boolean in_front_after_waiting, StarvationBoost starvationBoost) { |
| 28 | super(); |
| 29 | this.resource = resource; |
| 30 | this.queueing_strategy = queueingStrategy; |
| 31 | this.in_front_after_waiting = in_front_after_waiting; |
| 32 | this.starvationBoost = starvationBoost; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | public void forkNewProcess(IActiveProcess process, IResourceInstance current) { |
| 37 | queueing_strategy.forkProcess(process, current, false); |
| 38 | } |
| 39 | |
| 40 | public void registerProcess(IActiveProcess p, IResourceInstance instance) { |
| 41 | queueing_strategy.registerProcess(p,instance); |
| 42 | } |
| 43 | |
| 44 | public void terminateProcess(IActiveProcess process, IResourceInstance current){ |
| 45 | if (process.isRunning()){ |
| 46 | fromRunningToReady(process, current, true); |
| 47 | queueing_strategy.terminateProcess(process); |
| 48 | process.getLastInstance().schedulingInterrupt(0); |
| 49 | } else { |
| 50 | queueing_strategy.terminateProcess(process); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Marks the given process as executing on the specified resource instance. |
| 56 | * |
| 57 | * @param process |
| 58 | * @param instance |
| 59 | */ |
| 60 | protected void fromReadyToRunningOn(IActiveProcess process, |
| 61 | IResourceInstance instance) { |
| 62 | LoggingWrapper.log(" From READY to RUNNING " + process + " on " |
| 63 | + instance); |
| 64 | assert process != null; |
| 65 | assert process.isReady(); |
| 66 | assert queueing_strategy.containsPending(process); |
| 67 | assert !instance.processAssigned(); |
| 68 | |
| 69 | queueing_strategy.removePendingProcess(process); |
| 70 | process.setRunning(); |
| 71 | queueing_strategy.setRunningOn(process, instance); |
| 72 | |
| 73 | instance.assign(process); |
| 74 | process.setLastInstance(instance); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * |
| 79 | * @param process |
| 80 | */ |
| 81 | protected void fromRunningToReady(IActiveProcess process, IResourceInstance current, boolean inFront) { |
| 82 | LoggingWrapper.log(" From RUNNING to READY Process " + process); |
| 83 | assert process.isRunning() : "Process must be in running state to return to pending queue!"; |
| 84 | assert queueing_strategy.runningOn(process).equals( |
| 85 | process.getLastInstance()) : "Inconstistant State of the last instance of the process."; |
| 86 | assert process.getLastInstance().getRunningProcess().equals(process) : "Inconsistent running state!"; |
| 87 | queueing_strategy.removeRunning(process); |
| 88 | stopProcess(process); |
| 89 | process.setReady(); |
| 90 | queueing_strategy.addProcess(process, current, inFront); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | public void fromRunningToWaiting(WaitingProcess waiting_process, |
| 95 | Deque<WaitingProcess> waiting_queue, boolean in_front) { |
| 96 | LoggingWrapper.log(" From RUNNING to WAITING Process " |
| 97 | + waiting_process.getProcess()); |
| 98 | IActiveProcess process = waiting_process.getProcess(); |
| 99 | assert process.isRunning() : "Process must be in running state."; |
| 100 | |
| 101 | queueing_strategy.fromRunningToWaiting(process); |
| 102 | stopProcess(process); |
| 103 | process.setWaiting(); |
| 104 | |
| 105 | if (in_front) { |
| 106 | waiting_queue.addFirst(waiting_process); |
| 107 | } else { |
| 108 | waiting_queue.addLast(waiting_process); |
| 109 | } |
| 110 | |
| 111 | process.getLastInstance().schedulingInterrupt(0); |
| 112 | |
| 113 | queueing_strategy.onSleep(process.getLastInstance()); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | private void stopProcess(IActiveProcess process) { |
| 118 | process.getLastInstance().release(); |
| 119 | process.cancelProceedEvent(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | public void fromWaitingToReady(WaitingProcess waiting_process, |
| 124 | Deque<WaitingProcess> waitingQueue, IResourceInstance current) { |
| 125 | LoggingWrapper.log("From WAITING to READY Process " |
| 126 | + waiting_process.getProcess()); |
| 127 | IActiveProcess process = waiting_process.getProcess(); |
| 128 | assert process.isWaiting() : "Process must be in waiting state"; |
| 129 | |
| 130 | waitingQueue.remove(waiting_process); |
| 131 | process.setReady(); |
| 132 | |
| 133 | ((PreemptiveProcess)process).getTimeslice().updateTimeForScheduling(); |
| 134 | |
| 135 | queueing_strategy.fromWaitingToReady(process, current, in_front_after_waiting); |
| 136 | process.toNow(); |
| 137 | process.update(); |
| 138 | process.getLastInstance().schedulingInterrupt(0); |
| 139 | } |
| 140 | } |