[477] | 1 | package schedframe.scheduling; |
---|
| 2 | |
---|
| 3 | import eduni.simjava.Sim_event; |
---|
| 4 | import eduni.simjava.Sim_port; |
---|
| 5 | import eduni.simjava.Sim_system; |
---|
| 6 | import gridsim.GridSim; |
---|
| 7 | import gridsim.GridSimCore; |
---|
| 8 | import gridsim.GridSimTags; |
---|
| 9 | import gridsim.IO_data; |
---|
[493] | 10 | import gridsim.dcworms.DCWormsTags; |
---|
[477] | 11 | |
---|
| 12 | import java.util.ArrayList; |
---|
| 13 | import java.util.List; |
---|
| 14 | import java.util.Map; |
---|
| 15 | import java.util.Properties; |
---|
| 16 | |
---|
| 17 | import schedframe.Initializable; |
---|
| 18 | import schedframe.PluginConfiguration; |
---|
[1447] | 19 | import schedframe.events.EventType; |
---|
[477] | 20 | import schedframe.events.scheduling.SchedulingEventType; |
---|
| 21 | import schedframe.resources.Resource; |
---|
| 22 | import schedframe.resources.ResourceStatus; |
---|
| 23 | import schedframe.resources.ResourceType; |
---|
| 24 | import schedframe.resources.computing.ComputingResource; |
---|
| 25 | import schedframe.resources.providers.LocalSystem; |
---|
| 26 | import schedframe.resources.units.ResourceUnit; |
---|
| 27 | import schedframe.resources.units.ResourceUnitName; |
---|
| 28 | import schedframe.scheduling.manager.resources.ManagedComputingResources; |
---|
| 29 | import schedframe.scheduling.manager.resources.ManagedResources; |
---|
| 30 | import schedframe.scheduling.policy.AbstractManagementSystem; |
---|
[481] | 31 | import schedframe.scheduling.queue.QueueDescription; |
---|
[477] | 32 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 33 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
| 34 | |
---|
| 35 | public class Scheduler extends GridSimCore implements Resource, Initializable{ |
---|
| 36 | |
---|
| 37 | protected ResourceStatus status; |
---|
| 38 | protected ResourceType type; |
---|
| 39 | |
---|
| 40 | protected Scheduler parent; |
---|
| 41 | protected List<Scheduler> children; |
---|
| 42 | |
---|
| 43 | protected AbstractManagementSystem managementSystem; |
---|
| 44 | |
---|
| 45 | protected ManagedComputingResources compResources; |
---|
| 46 | |
---|
| 47 | public Scheduler(AbstractManagementSystem manSys, ResourceType schedType, ManagedResources managedResources) throws Exception { |
---|
| 48 | super(manSys.getName(), 1); |
---|
| 49 | this.managementSystem = manSys; |
---|
| 50 | this.compResources = new ManagedComputingResources(); |
---|
| 51 | this.status = ResourceStatus.AVAILABLE; |
---|
| 52 | this.children = new ArrayList<Scheduler>(1); |
---|
| 53 | this.type = schedType; |
---|
| 54 | init(managedResources); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | public void init(ManagedResources managedResources){ |
---|
[970] | 58 | if(managedResources != null) |
---|
| 59 | addResources(managedResources.getComputingResources()); |
---|
[477] | 60 | managementSystem.init(this, managedResources); |
---|
| 61 | |
---|
| 62 | //required to terminate all scheduling entities after the end of simulation |
---|
| 63 | Integer schedulerIdObj = new Integer(get_id()); |
---|
| 64 | List<Integer> schedRes = GridSim.getGridResourceList(); |
---|
| 65 | schedRes.add(schedulerIdObj); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | public void addChild(Scheduler child) { |
---|
| 69 | child.setParent(this); |
---|
| 70 | if (children == null) { |
---|
| 71 | children = new ArrayList<Scheduler>(1); |
---|
| 72 | } |
---|
| 73 | children.add(child); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public List<Scheduler> getChildren() { |
---|
| 77 | return children; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | public void setParent(Scheduler newParent) { |
---|
| 81 | this.parent = newParent; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public Scheduler getParent() { |
---|
| 85 | return parent; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /*public void addResource(ComputingResource resource) { |
---|
| 89 | compResources.add(resource); |
---|
| 90 | }*/ |
---|
| 91 | |
---|
| 92 | public void addResources(List<ComputingResource> res) { |
---|
| 93 | for(ComputingResource resource:res){ |
---|
| 94 | compResources.add(resource); |
---|
| 95 | resource.setScheduler(this); |
---|
| 96 | for(ComputingResource child: resource.filterDescendants(new Properties())){ |
---|
| 97 | child.setScheduler(this); |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | public ManagedComputingResources getCompResources() { |
---|
| 103 | return compResources; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | public void body() { |
---|
| 107 | |
---|
| 108 | PluginConfiguration pluginConfig = managementSystem.getSchedulingPluginConfiguration(); |
---|
| 109 | if (pluginConfig != null) { |
---|
[1447] | 110 | Map<EventType, Object> events = pluginConfig.getServedEvents(); |
---|
[477] | 111 | if (events != null) { |
---|
| 112 | Object obj = events.get(SchedulingEventType.TIMER); |
---|
| 113 | if (obj != null) { |
---|
| 114 | int delay = (Integer) obj; |
---|
[481] | 115 | send(this.get_id(), delay, DCWormsTags.TIMER); |
---|
[477] | 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | // Process events until END_OF_SIMULATION is received from the |
---|
| 121 | // GridSimShutdown Entity |
---|
| 122 | Sim_event ev = new Sim_event(); |
---|
| 123 | sim_get_next(ev); |
---|
| 124 | boolean run = true; |
---|
| 125 | while (Sim_system.running() && run) { |
---|
| 126 | // sim_get_next(ev); |
---|
| 127 | // if the simulation finishes then exit the loop |
---|
| 128 | if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) { |
---|
| 129 | // managemetnSystem_.setEndSimulation(); |
---|
| 130 | run = false; |
---|
| 131 | break; |
---|
| 132 | } |
---|
| 133 | // process the received event |
---|
| 134 | processRequest(ev); |
---|
| 135 | sim_get_next(ev); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | protected void processRequest(Sim_event ev) { |
---|
| 140 | switch (ev.get_tag()) { |
---|
| 141 | |
---|
| 142 | case GridSimTags.GRIDLET_SUBMIT: |
---|
[1362] | 143 | processWorkloadUnitSubmit(ev); |
---|
[477] | 144 | break; |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | case GridSimTags.GRIDLET_RETURN: |
---|
[494] | 148 | processWorkloadUnitReturn(ev); |
---|
[477] | 149 | break; |
---|
| 150 | |
---|
| 151 | default: |
---|
| 152 | processOtherRequest(ev); |
---|
| 153 | break; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | protected void processOtherRequest(Sim_event ev) { |
---|
| 158 | switch (ev.get_tag()) { |
---|
[481] | 159 | case DCWormsTags.QUERY_RESOURCE_DESC: |
---|
[477] | 160 | SchedulerDescription desc = new SchedulerDescription(new LocalSystem(get_name(), null, null)); |
---|
| 161 | Map<ResourceUnitName, List<ResourceUnit>> units = managementSystem.getResourceManager().getSharedResourceUnits(); |
---|
| 162 | desc.addResourceUnitList(units); |
---|
| 163 | desc.addQueuesDescription(getQueuesDescription()); |
---|
| 164 | |
---|
| 165 | IO_data data = new IO_data(desc, 0, ev.get_src()); |
---|
[481] | 166 | send(ev.get_src(), GridSimTags.SCHEDULE_NOW, DCWormsTags.QUERY_RESOURCE_DESC_RESULT, data); |
---|
[477] | 167 | break; |
---|
| 168 | |
---|
| 169 | default: |
---|
| 170 | managementSystem.processEvent(ev); |
---|
| 171 | break; |
---|
| 172 | } |
---|
| 173 | } |
---|
[509] | 174 | |
---|
[494] | 175 | protected void processWorkloadUnitReturn(Sim_event ev) { |
---|
[478] | 176 | WorkloadUnit job = (WorkloadUnit) ev.get_data(); |
---|
[477] | 177 | managementSystem.notifyReturnedWorkloadUnit(job); |
---|
| 178 | } |
---|
| 179 | |
---|
[1362] | 180 | protected void processWorkloadUnitSubmit(Sim_event ev) { |
---|
[478] | 181 | WorkloadUnit job = (WorkloadUnit) ev.get_data(); |
---|
[1362] | 182 | managementSystem.notifySubmittedWorkloadUnit(job); |
---|
[477] | 183 | } |
---|
| 184 | |
---|
| 185 | public void send(String entityName, double delay, int tag, Object data){ |
---|
| 186 | super.send(entityName, delay, tag, data); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | public void send(int destId, double delay, int tag){ |
---|
| 190 | super.send(destId, delay, tag); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | public void send(int destId, double delay, int tag, Object data){ |
---|
| 194 | super.send(destId, delay, tag, data); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | public void send(Sim_port destPort, double delay, int tag, Object data) { |
---|
| 198 | super.send(destPort, delay, tag, data); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | public void sendInternal(double delay, int tag, Object data) { |
---|
| 202 | this.send(this.get_id(), delay, tag, data); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | public Sim_port getOutputPort() { |
---|
| 206 | return output; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /*public Scheduler getScheduler(String resourceName){ |
---|
| 210 | ComputingResource resourceWithName = null; |
---|
| 211 | for(int i = 0 ; i < compResources.size() && resourceWithName == null; i++){ |
---|
| 212 | ComputingResource resource = compResources.get(i); |
---|
| 213 | if(resource.getName().equals(resourceName)) |
---|
| 214 | resourceWithName = resource; |
---|
| 215 | else |
---|
| 216 | try { |
---|
| 217 | resourceWithName = resource.getDescendantByName(resourceName); |
---|
| 218 | } catch (ResourceException e) { |
---|
| 219 | return null; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | if(resourceWithName == null) |
---|
| 223 | return null; |
---|
| 224 | List<ComputingResource> children = resourceWithName.getChildren(); |
---|
| 225 | Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>(); |
---|
| 226 | for(ComputingResource child:children) { |
---|
| 227 | childrenSchedulers.add(child.getScheduler()); |
---|
| 228 | } |
---|
| 229 | Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
| 230 | while(childrenSchedulers.size() != 1){ |
---|
| 231 | childrenSchedulers = new HashSet<Scheduler>(); |
---|
| 232 | for(Scheduler s: tempChildrenSchedulers){ |
---|
| 233 | childrenSchedulers.add(s.getParent()); |
---|
| 234 | } |
---|
| 235 | tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
| 236 | } |
---|
| 237 | Iterator<Scheduler> it = childrenSchedulers.iterator(); |
---|
| 238 | Scheduler potentialScheduler = it.next(); |
---|
| 239 | if(potentialScheduler.getResources().containsAll(children)) |
---|
| 240 | return potentialScheduler; |
---|
| 241 | return null; |
---|
| 242 | |
---|
| 243 | }*/ |
---|
| 244 | |
---|
| 245 | public List<QueueDescription> getQueuesDescription(){ |
---|
| 246 | List<QueueDescription> queues = new ArrayList<QueueDescription>(); |
---|
[481] | 247 | for(TaskQueue queue: managementSystem.getQueues()){ |
---|
[477] | 248 | QueueDescription qd; |
---|
| 249 | try { |
---|
| 250 | qd = new QueueDescription(queue.getName(), queue.getPriority(), queue.supportReservations(), getQueueLoad(queue.getName())); |
---|
| 251 | queues.add(qd); |
---|
| 252 | } catch (NoSuchFieldException e) {; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | return queues; |
---|
| 256 | |
---|
| 257 | } |
---|
| 258 | private long getQueueLoad(String queueName) throws NoSuchFieldException{ |
---|
| 259 | Map<String, Integer> queue_size = managementSystem.getQueuesSize(); |
---|
| 260 | long load = 0; |
---|
| 261 | if(queueName == null){ |
---|
| 262 | for(String queue: queue_size.keySet()){ |
---|
| 263 | load += queue_size.get(queue); |
---|
| 264 | } |
---|
| 265 | return load; |
---|
| 266 | } |
---|
| 267 | else if(queue_size.containsKey(queueName)) |
---|
| 268 | return queue_size.get(queueName); |
---|
| 269 | else |
---|
| 270 | throw new NoSuchFieldException("Queue " + queueName + |
---|
| 271 | " is not available in resource " + get_name()); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | public ResourceType getType() { |
---|
| 275 | return this.type; |
---|
| 276 | } |
---|
| 277 | |
---|
[895] | 278 | public String getFullName() { |
---|
| 279 | if(parent!= null){ |
---|
[1587] | 280 | return parent.getFullName() + "/" + this.get_name(); |
---|
[895] | 281 | } else |
---|
[1587] | 282 | return this.get_name(); |
---|
[895] | 283 | } |
---|
| 284 | |
---|
[477] | 285 | public ResourceStatus getStatus() { |
---|
| 286 | return status; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | public void setStatus(ResourceStatus newStatus) { |
---|
| 290 | status = newStatus; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | public void getResourceDescription(){ |
---|
| 294 | |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | public void initiate() { |
---|
| 298 | |
---|
| 299 | } |
---|
| 300 | } |
---|