| 1 | package de.uka.ipd.sdq.simucomframework.resources; |
| 2 | |
| 3 | import java.util.ArrayDeque; |
| 4 | import java.util.Deque; |
| 5 | |
| 6 | import de.uka.ipd.sdq.reliability.core.FailureStatistics; |
| 7 | import de.uka.ipd.sdq.scheduler.IPassiveResource; |
| 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.scheduler.resources.AbstractSimResource; |
| 12 | import de.uka.ipd.sdq.scheduler.resources.passive.PassiveResourceObservee; |
| 13 | import de.uka.ipd.sdq.scheduler.resources.passive.SimAbstractPassiveResource; |
| 14 | import de.uka.ipd.sdq.scheduler.sensors.IPassiveResourceSensor; |
| 15 | import de.uka.ipd.sdq.simucomframework.exceptions.FailureException; |
| 16 | |
| 17 | /** |
| 18 | * @author Fabro |
| 19 | * |
| 20 | * Simulates a simple passive resource. |
| 21 | * |
| 22 | * Note: This class intentionally does not extend |
| 23 | * {@link SimAbstractPassiveResource}, because that abstract class is |
| 24 | * intended for passive resources that are accessed by EXACT schedulers |
| 25 | * (e.g., specific Windows, Linux Scheduler). |
| 26 | * |
| 27 | * TODO: comment |
| 28 | * |
| 29 | * @param <M> |
| 30 | * the type of the simulation model. |
| 31 | */ |
| 32 | public class SimSimpleFairPassiveResource extends AbstractSimResource implements |
| 33 | IPassiveResource { |
| 34 | |
| 35 | protected Deque<SimpleWaitingProcess> waiting_queue; |
| 36 | private SchedulerModel myModel; |
| 37 | private int available; |
| 38 | private String passiveResourceID; |
| 39 | private String assemblyContextID; |
| 40 | private boolean simulateFailures; |
| 41 | |
| 42 | // provides observer functionality to this resource |
| 43 | private PassiveResourceObservee observee; |
| 44 | |
| 45 | public SimSimpleFairPassiveResource(SchedulerModel model, int capacity, String name, String passiveResourceID, |
| 46 | String assemblyContextID, String combinedID, boolean simulateFailures) { |
| 47 | super(model, capacity, name, combinedID); |
| 48 | this.waiting_queue = new ArrayDeque<SimpleWaitingProcess>(); |
| 49 | this.myModel = model; |
| 50 | this.passiveResourceID = passiveResourceID; |
| 51 | this.assemblyContextID = assemblyContextID; |
| 52 | this.observee = new PassiveResourceObservee(); |
| 53 | this.available = capacity; |
| 54 | this.simulateFailures = simulateFailures; |
| 55 | } |
| 56 | |
| 57 | private boolean canProceed(ISchedulableProcess process, int num) { |
| 58 | return (waiting_queue.isEmpty() || waiting_queue.peek().getProcess() |
| 59 | .equals(process)) |
| 60 | && num <= available; |
| 61 | } |
| 62 | |
| 63 | private void grantAccess(ISchedulableProcess process, int num) { |
| 64 | LoggingWrapper.log("Process " + process + " acquires " + num + " of " |
| 65 | + this); |
| 66 | this.available -= num; |
| 67 | observee.fireAquire(process, num); |
| 68 | assert this.available >= 0 : "More resource than available have been acquired!"; |
| 69 | } |
| 70 | |
| 71 | public boolean acquire(ISchedulableProcess sched_process, int num, |
| 72 | boolean timeout, double timeoutValue) { |
| 73 | |
| 74 | // AM: Copied from AbstractActiveResource: If simulation is stopped, |
| 75 | // allow all processes to finish |
| 76 | if (!myModel.getSimulationControl().isRunning()) { |
| 77 | // Do nothing, but allows calling process to complete |
| 78 | return true; |
| 79 | } |
| 80 | // TODO |
| 81 | // Do we need some logic here to check if the simulation has stopped? |
| 82 | // In this case, this method should not block, but return in order to |
| 83 | // allow processes to complete |
| 84 | observee.fireRequest(sched_process, num); |
| 85 | if (canProceed(sched_process, num)) { |
| 86 | grantAccess(sched_process, num); |
| 87 | return true; |
| 88 | } else { |
| 89 | LoggingWrapper.log("Process " + sched_process + " is waiting for " |
| 90 | + num + " of " + this); |
| 91 | SimpleWaitingProcess process = new SimpleWaitingProcess( |
| 92 | myModel, sched_process, num); |
| 93 | processTimeout(timeout, timeoutValue, process); |
| 94 | waiting_queue.add(process); |
| 95 | sched_process.passivate(); |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Schedules a timeout event if a timeout is specified and failures are |
| 102 | * simulated. |
| 103 | * |
| 104 | * @param timeout |
| 105 | * indicates if the acquire request is associated with a timeout |
| 106 | * @param timeoutValue |
| 107 | * the timeout value |
| 108 | * @param process |
| 109 | * the waiting process |
| 110 | */ |
| 111 | private void processTimeout(final boolean timeout, |
| 112 | final double timeoutValue, final SimpleWaitingProcess process) { |
| 113 | if (!simulateFailures || !timeout) { |
| 114 | return; |
| 115 | } |
| 116 | if (timeoutValue == 0.0) { |
| 117 | FailureException.raise(FailureStatistics.getInstance() |
| 118 | .getResourceTimeoutFailureType(this.assemblyContextID, |
| 119 | this.passiveResourceID)); |
| 120 | } |
| 121 | if (timeoutValue > 0.0) { |
| 122 | PassiveResourceTimeoutEvent event = new PassiveResourceTimeoutEvent(myModel, this, process); |
| 123 | event.schedule(process, timeoutValue); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Retrieves the passive resource ID. |
| 129 | * |
| 130 | * @return the passive resource ID |
| 131 | */ |
| 132 | protected String getPassiveResourceID() { |
| 133 | return passiveResourceID; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Retrieves the assembly context ID. |
| 138 | * |
| 139 | * @return the assembly context ID |
| 140 | */ |
| 141 | protected String getAssemblyContextID() { |
| 142 | return assemblyContextID; |
| 143 | } |
| 144 | |
| 145 | public void release(ISchedulableProcess sched_process, int num) { |
| 146 | |
| 147 | // AM: Copied from AbstractActiveResource: If simulation is stopped, |
| 148 | // allow all processes to finish |
| 149 | if (!myModel.getSimulationControl().isRunning()) { |
| 150 | // Do nothing, but allows calling process to complete |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | LoggingWrapper.log("Process " + sched_process + " releases " + num |
| 155 | + " of " + this); |
| 156 | this.available += num; |
| 157 | observee.fireRelease(sched_process, num); |
| 158 | notifyWaitingProcesses(); |
| 159 | } |
| 160 | |
| 161 | private void notifyWaitingProcesses() { |
| 162 | SimpleWaitingProcess waitingProcess = waiting_queue.peek(); |
| 163 | while (waitingProcess != null |
| 164 | && canProceed(waitingProcess.getProcess(), waitingProcess |
| 165 | .getNumRequested())) { |
| 166 | grantAccess(waitingProcess.getProcess(), waitingProcess |
| 167 | .getNumRequested()); |
| 168 | waiting_queue.remove(); |
| 169 | waitingProcess.getProcess().activate(); |
| 170 | waitingProcess = waiting_queue.peek(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | public void addObserver(IPassiveResourceSensor observer) { |
| 175 | observee.addObserver(observer); |
| 176 | } |
| 177 | |
| 178 | public void removeObserver(IPassiveResourceSensor observer) { |
| 179 | observee.removeObserver(observer); |
| 180 | } |
| 181 | |
| 182 | public int getAvailable() { |
| 183 | return available; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Determines if a given process is currently waiting to acquire this |
| 188 | * resource. |
| 189 | * |
| 190 | * @param process |
| 191 | * the process |
| 192 | * @return TRUE if the process is waiting to acquire the resource; FALSE |
| 193 | * otherwise |
| 194 | */ |
| 195 | public boolean isWaiting(final SimpleWaitingProcess process) { |
| 196 | return waiting_queue.contains(process); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Removes a waiting process from the queue. |
| 201 | * |
| 202 | * @param process |
| 203 | * the process to remove |
| 204 | */ |
| 205 | public void remove(final SimpleWaitingProcess process) { |
| 206 | waiting_queue.remove(process); |
| 207 | } |
| 208 | } |