[477] | 1 | package schedframe; |
---|
| 2 | |
---|
| 3 | import gridsim.GridSim; |
---|
| 4 | |
---|
[1207] | 5 | import java.util.ArrayList; |
---|
[477] | 6 | import java.util.HashSet; |
---|
| 7 | import java.util.Iterator; |
---|
| 8 | import java.util.LinkedList; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Set; |
---|
| 11 | |
---|
| 12 | import schedframe.exceptions.ResourceException; |
---|
[1396] | 13 | import schedframe.resources.ResourceHistoryItem; |
---|
[477] | 14 | import schedframe.resources.computing.ComputingResource; |
---|
| 15 | import schedframe.scheduling.Scheduler; |
---|
| 16 | |
---|
[1207] | 17 | public class SimulatedEnvironment { |
---|
[477] | 18 | |
---|
| 19 | protected static Scheduler scheduler; |
---|
| 20 | protected static List<ComputingResource> computingResources; |
---|
[512] | 21 | protected List<Initializable> toInit; |
---|
[490] | 22 | protected Set<String> compResLayers; |
---|
[1396] | 23 | protected static List<ResourceHistoryItem> compResHistory = new ArrayList<ResourceHistoryItem>(); |
---|
[477] | 24 | |
---|
[1207] | 25 | public SimulatedEnvironment(Scheduler logicalStructure, List<ComputingResource> compResources){ |
---|
[477] | 26 | scheduler = logicalStructure; |
---|
| 27 | computingResources = compResources; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | public Scheduler getScheduler() { |
---|
| 31 | return scheduler; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | public List<ComputingResource> getComputingResources() { |
---|
| 35 | return computingResources; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | public static ComputingResource getComputingResourceByName(String resourceName) throws ResourceException { |
---|
| 39 | ComputingResource resourceWithName = null; |
---|
| 40 | for (int i = 0; i < computingResources.size() && resourceWithName == null; i++) { |
---|
| 41 | ComputingResource resource = computingResources.get(i); |
---|
[1207] | 42 | if (resource.getFullName().equals(resourceName)) |
---|
[477] | 43 | resourceWithName = resource; |
---|
| 44 | else |
---|
| 45 | resourceWithName = resource.getDescendantByName(resourceName); |
---|
| 46 | } |
---|
| 47 | return resourceWithName; |
---|
| 48 | } |
---|
| 49 | |
---|
[768] | 50 | private static Scheduler getSchedulerByName(String schedulerName) { |
---|
[477] | 51 | Scheduler schedulerWithName = null; |
---|
| 52 | |
---|
| 53 | if (scheduler.getName().compareTo(schedulerName) == 0) |
---|
| 54 | schedulerWithName = scheduler; |
---|
| 55 | else if (scheduler.getChildren() != null) { |
---|
| 56 | LinkedList<Scheduler> toExamine = new LinkedList<Scheduler>(); |
---|
| 57 | toExamine.push(scheduler); |
---|
| 58 | |
---|
| 59 | while (!toExamine.isEmpty() && schedulerWithName != null) { |
---|
| 60 | Scheduler scheduler = toExamine.pop(); |
---|
[768] | 61 | List<Scheduler> schedulers = scheduler.getChildren(); |
---|
[477] | 62 | |
---|
| 63 | int numberOfSched = schedulers.size(); |
---|
| 64 | for (int i = 0; i < numberOfSched; i++) { |
---|
| 65 | Scheduler schedulerChild = schedulers.get(i); |
---|
| 66 | if(scheduler.getName().equals(schedulerName)){ |
---|
| 67 | schedulerWithName = schedulerChild; |
---|
| 68 | break; |
---|
| 69 | } else |
---|
| 70 | toExamine.addLast(schedulerChild); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | return schedulerWithName; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | public static Scheduler getScheduler(String resName){ |
---|
| 78 | if(GridSim.getEntityId(resName) != -1){ |
---|
[768] | 79 | return getSchedulerByName(resName); |
---|
[477] | 80 | } |
---|
| 81 | ComputingResource resourceWithName = null; |
---|
| 82 | try { |
---|
| 83 | resourceWithName = getComputingResourceByName(resName); |
---|
| 84 | } catch (ResourceException e) { |
---|
| 85 | } |
---|
| 86 | /*for(int i = 0 ; i < computingResources.size() && resourceWithName == null; i++){ |
---|
| 87 | ComputingResource resource = computingResources.get(i); |
---|
| 88 | if(resource.getName().equals(resName)) |
---|
| 89 | resourceWithName = resource; |
---|
| 90 | else |
---|
| 91 | try { |
---|
| 92 | resourceWithName = resource.getDescendantByName(resName); |
---|
| 93 | } catch (ResourceException e) { |
---|
| 94 | return null; |
---|
| 95 | } |
---|
| 96 | }*/ |
---|
| 97 | if(resourceWithName == null) |
---|
| 98 | return null; |
---|
| 99 | List<ComputingResource> children = resourceWithName.getChildren(); |
---|
| 100 | Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>(); |
---|
| 101 | if(children.isEmpty()) |
---|
| 102 | return null; |
---|
| 103 | for(ComputingResource child: children) { |
---|
| 104 | childrenSchedulers.add(child.getScheduler()); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
| 108 | while(childrenSchedulers.size() > 1){ |
---|
| 109 | childrenSchedulers = new HashSet<Scheduler>(); |
---|
| 110 | for(Scheduler s: tempChildrenSchedulers){ |
---|
| 111 | childrenSchedulers.add(s.getParent()); |
---|
| 112 | } |
---|
| 113 | tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
| 114 | } |
---|
| 115 | Iterator<Scheduler> it = childrenSchedulers.iterator(); |
---|
| 116 | Scheduler potentialScheduler = it.next(); |
---|
| 117 | if(potentialScheduler.getCompResources().containsAll(children)) |
---|
| 118 | return potentialScheduler; |
---|
| 119 | return null; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | public static ComputingResource getCommonComputingResourceParent(List<ComputingResource> compResources){ |
---|
| 123 | if(compResources.size() == 0) |
---|
| 124 | return null; |
---|
| 125 | Set<ComputingResource> candidates = new HashSet<ComputingResource>(compResources); |
---|
| 126 | while(candidates.size() > 1){ |
---|
| 127 | Set<ComputingResource> parents = new HashSet<ComputingResource>(); |
---|
| 128 | Iterator<ComputingResource> it = candidates.iterator(); |
---|
| 129 | while(it.hasNext()){ |
---|
| 130 | ComputingResource compRes = it.next(); |
---|
| 131 | if(compRes.getParent() != null){ |
---|
| 132 | parents.add(compRes.getParent()); |
---|
| 133 | } |
---|
| 134 | it.remove(); |
---|
| 135 | } |
---|
| 136 | candidates.addAll(parents); |
---|
| 137 | } |
---|
| 138 | return candidates.toArray(new ComputingResource[0])[0]; |
---|
| 139 | } |
---|
| 140 | |
---|
[512] | 141 | public void setInitList(List<Initializable> toI) { |
---|
[477] | 142 | toInit = toI; |
---|
| 143 | } |
---|
| 144 | |
---|
[512] | 145 | public List<Initializable> getToInit() { |
---|
[477] | 146 | return toInit; |
---|
| 147 | } |
---|
| 148 | |
---|
[490] | 149 | public Set<String> getComputingResourceLayers() { |
---|
| 150 | return compResLayers; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | public void setCompResLayers(Set<String> compResLayers) { |
---|
| 154 | this.compResLayers = compResLayers; |
---|
| 155 | } |
---|
| 156 | |
---|
[1207] | 157 | public static void traceResource(long timestamp, String resourceName, String operation, String paramter){ |
---|
[1396] | 158 | ResourceHistoryItem rhc = new ResourceHistoryItem(timestamp, resourceName, operation, paramter); |
---|
[1207] | 159 | compResHistory.add(rhc); |
---|
| 160 | } |
---|
| 161 | |
---|
[1396] | 162 | public static List<ResourceHistoryItem> getCompResHistory() { |
---|
[1207] | 163 | return compResHistory; |
---|
| 164 | } |
---|
[477] | 165 | } |
---|