[477] | 1 | package example.localplugin; |
---|
| 2 | |
---|
[493] | 3 | import gridsim.dcworms.DCWormsTags; |
---|
[477] | 4 | |
---|
| 5 | import java.util.ArrayList; |
---|
| 6 | import java.util.Collections; |
---|
| 7 | import java.util.Comparator; |
---|
| 8 | import java.util.List; |
---|
| 9 | |
---|
| 10 | import schedframe.events.scheduling.SchedulingEvent; |
---|
| 11 | import schedframe.resources.computing.ComputingNode; |
---|
| 12 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
| 13 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 14 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 15 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 16 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
| 17 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 18 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 19 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 20 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 21 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
| 22 | |
---|
| 23 | public class FCFSEnergyAwareClusterLocalPlugin extends BaseLocalSchedulingPlugin { |
---|
| 24 | |
---|
| 25 | public FCFSEnergyAwareClusterLocalPlugin () { |
---|
| 26 | } |
---|
| 27 | |
---|
[481] | 28 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
[477] | 29 | ResourceManager resManager, ModuleList modules) { |
---|
| 30 | |
---|
| 31 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 32 | SchedulingPlan plan = new SchedulingPlan(); |
---|
| 33 | // chose the events types to serve. |
---|
| 34 | // Different actions for different events are possible. |
---|
| 35 | switch (event.getType()) { |
---|
| 36 | case START_TASK_EXECUTION: |
---|
| 37 | case TASK_FINISHED: |
---|
| 38 | // our tasks are placed only in first queue (see |
---|
| 39 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
| 40 | TaskQueue q = queues.get(0); |
---|
| 41 | // check all tasks in queue |
---|
| 42 | |
---|
| 43 | for (int i = 0; i < q.size(); i++) { |
---|
[478] | 44 | WorkloadUnit job = q.get(i); |
---|
[477] | 45 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 46 | // if status of the tasks in READY |
---|
[481] | 47 | if (task.getStatus() == DCWormsTags.READY) { |
---|
[477] | 48 | |
---|
| 49 | String nodeName = chooseProviderForExecution(resourceManager, task); |
---|
| 50 | if (nodeName != null) { |
---|
| 51 | addToSchedulingPlan(plan, task, nodeName); |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | break; |
---|
| 57 | } |
---|
| 58 | return plan; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | private String chooseProviderForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 62 | int cpuRequest; |
---|
| 63 | try { |
---|
| 64 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 65 | } catch (NoSuchFieldException e) { |
---|
| 66 | cpuRequest = 1; |
---|
| 67 | } |
---|
| 68 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 69 | nodes = findSuitableNodes(cpuRequest, nodes); |
---|
| 70 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 71 | |
---|
| 72 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 73 | return node1.getResourceCharacteristic().getParameters().get("category").get(0).getContent().compareTo(node2.getResourceCharacteristic().getParameters().get("category").get(0).getContent()); |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | }); |
---|
| 77 | |
---|
| 78 | return nodes.size() == 0 ? null : nodes.get(0).getName(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | private List<ComputingNode> findSuitableNodes(int cpuRequest, List<ComputingNode> nodes){ |
---|
| 82 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
| 83 | for(ComputingNode node: nodes){ |
---|
| 84 | if(node.getFreeProcessorsNumber() >= cpuRequest){ |
---|
| 85 | avNodes.add(node); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | return avNodes; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public String getName() { |
---|
| 92 | return getClass().getName(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | class CategoryComparator implements Comparator<String>{ |
---|
| 96 | |
---|
| 97 | public int compare(String cat1, String cat2){ |
---|
| 98 | return cat1.compareTo(cat2); |
---|
| 99 | |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|