| 1 | package de.uka.ipd.sdq.simucomframework.resources; |
| 2 | |
| 3 | import org.apache.log4j.Logger; |
| 4 | |
| 5 | import de.uka.ipd.sdq.reliability.core.FailureStatistics; |
| 6 | import de.uka.ipd.sdq.scheduler.IActiveResource; |
| 7 | import de.uka.ipd.sdq.scheduler.ISchedulableProcess; |
| 8 | import de.uka.ipd.sdq.simucomframework.SimuComSimProcess; |
| 9 | import de.uka.ipd.sdq.simucomframework.exceptions.FailureException; |
| 10 | import de.uka.ipd.sdq.simucomframework.exceptions.ThroughputZeroOrNegativeException; |
| 11 | import de.uka.ipd.sdq.simucomframework.model.SimuComModel; |
| 12 | import de.uka.ipd.sdq.simucomframework.variables.StackContext; |
| 13 | import de.uka.ipd.sdq.simucomframework.variables.converter.NumberConverter; |
| 14 | |
| 15 | /** |
| 16 | * Realizes a LinkingResource. Adds the latency time to the |
| 17 | * passed demand in {@link #consumeResource(SimuComSimProcess, double)}, |
| 18 | * and they is loaded by latency + demand / throughput. |
| 19 | * |
| 20 | * @author hauck, brosch, merkle |
| 21 | * |
| 22 | */ |
| 23 | public class SimulatedLinkingResource extends AbstractScheduledResource { |
| 24 | |
| 25 | protected static Logger logger = Logger |
| 26 | .getLogger(SimulatedLinkingResource.class.getName()); |
| 27 | |
| 28 | private String throughput; |
| 29 | private String latencySpec; |
| 30 | private static long resourceId = 1; |
| 31 | private String id; |
| 32 | |
| 33 | private double totalDemandedTime; |
| 34 | private boolean utilizationSet = false; |
| 35 | |
| 36 | // private SimpleTimeSpanSensor demandedTimeSensor; |
| 37 | // private OverallUtilisationSensor utilisationSensor; |
| 38 | |
| 39 | public SimulatedLinkingResource(String id, SimuComModel simuComModel, |
| 40 | String typeID, String resourceContainerID, String resourceTypeID, |
| 41 | String description, String d, String latencySpec, |
| 42 | Double failureProbability) { |
| 43 | super(simuComModel, typeID, resourceContainerID, resourceTypeID, |
| 44 | description, SchedulingStrategy.FCFS, 1, false); |
| 45 | this.id = id; |
| 46 | this.latencySpec = latencySpec; |
| 47 | this.throughput = d; |
| 48 | this.failureProbability = failureProbability; |
| 49 | this.canFail = (simuComModel.getConfiguration().getSimulateFailures() && this.failureProbability > 0.0); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | public String getId() { |
| 54 | return id; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected IActiveResource createActiveResource(SimuComModel simuComModel) { |
| 59 | // this.demandedTimeSensor = new SimpleTimeSpanSensor(simuComModel, |
| 60 | // "Demanded time at " + description); |
| 61 | IActiveResource aResource = getModel().getSchedulingFactory() |
| 62 | .createSimFCFSResource(SchedulingStrategy.FCFS.toString(), |
| 63 | getNextResourceId()); |
| 64 | |
| 65 | // utilisationSensor = new OverallUtilisationSensor(simuComModel, |
| 66 | // "Utilisation of " + typeID + " " + description); |
| 67 | return aResource; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected double calculateDemand(double demand) { |
| 72 | double calculatedThroughput = NumberConverter.toDouble(StackContext |
| 73 | .evaluateStatic(throughput)); |
| 74 | if (calculatedThroughput <= 0) { |
| 75 | throw new ThroughputZeroOrNegativeException( |
| 76 | "Throughput at resource " + getName() |
| 77 | + " was less or equal zero"); |
| 78 | } |
| 79 | |
| 80 | double result = NumberConverter.toDouble(StackContext |
| 81 | .evaluateStatic(latencySpec)) |
| 82 | + demand / calculatedThroughput; |
| 83 | logger.debug("A network load of " + result + " has been determined."); |
| 84 | |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void consumeResource(SimuComSimProcess process, int resourceServiceID, |
| 90 | double abstractDemand) { |
| 91 | |
| 92 | if (abstractDemand <= 0) { |
| 93 | // Do nothing. |
| 94 | // TODO throw an exception or add a warning? |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | // If the resource can fail, simulate a failure with the given |
| 100 | // probability. |
| 101 | // This works for communication link resources (LAN), but only if the |
| 102 | // "simulate linking resources" option is activated. Otherwise, the |
| 103 | // commlink failure is triggered out of the OAW generated code. |
| 104 | if (canFail) { |
| 105 | if (Math.random() < failureProbability) { |
| 106 | FailureException |
| 107 | .raise(FailureStatistics.getInstance() |
| 108 | .getInternalNetworkFailureType(id, |
| 109 | this.resourceTypeID)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // throw new |
| 114 | // RuntimeException("Not supported in this branch of the simulation's code"); |
| 115 | // registerProcessWindows(process, aResource); |
| 116 | // logger.info("Demanding " + abstractDemand); |
| 117 | double concreteDemand = calculateDemand(abstractDemand); |
| 118 | // logger.info("Recording " + concreteDemand); |
| 119 | fireDemand(concreteDemand); |
| 120 | this.totalDemandedTime += concreteDemand; |
| 121 | aResource.process(process, resourceServiceID, concreteDemand); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public double getRemainingDemandForProcess(SimuComSimProcess thread) { |
| 126 | return aResource.getRemainingDemand(thread); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void updateDemand(SimuComSimProcess thread, double demand) { |
| 131 | aResource.updateDemand(thread, demand); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public IActiveResource getScheduledResource() { |
| 136 | return aResource; |
| 137 | // return null; |
| 138 | } |
| 139 | |
| 140 | private void registerProcessWindows(ISchedulableProcess process, |
| 141 | IActiveResource resource) { |
| 142 | /* |
| 143 | * if (resourceConf != null) { ProcessConfiguration processConf = |
| 144 | * ConfigurationFactory.eINSTANCE .createProcessConfiguration(); |
| 145 | * processConf.setName(process.getId()); |
| 146 | * processConf.setPriority(PriorityClass.DEFAULT); |
| 147 | * processConf.setReplicas(1); ProcessWithPriority p = |
| 148 | * (ProcessWithPriority) ISchedulingFactory.eINSTANCE |
| 149 | * .createRunningProcess(process, processConf, resourceConf); |
| 150 | * |
| 151 | * resource.registerProcess(p); } |
| 152 | */ |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void activateResource() { |
| 157 | aResource.start(); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void deactivateResource() { |
| 162 | if (utilizationSet == false) { |
| 163 | // this.utilisationSensor.setTotalResourceDemand(totalDemandedTime, |
| 164 | // 1); |
| 165 | utilizationSet = true; |
| 166 | } |
| 167 | aResource.stop(); |
| 168 | } |
| 169 | |
| 170 | public static String getNextResourceId() { |
| 171 | return "NETWORK_" + Long.toString(resourceId++); |
| 172 | } |
| 173 | } |