[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.HashMap; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Map; |
---|
| 11 | |
---|
| 12 | import schedframe.events.scheduling.SchedulingEvent; |
---|
| 13 | import schedframe.resources.ResourceStatus; |
---|
| 14 | import schedframe.resources.computing.ComputingNode; |
---|
| 15 | import schedframe.resources.computing.ComputingResource; |
---|
| 16 | import schedframe.resources.units.Memory; |
---|
| 17 | import schedframe.resources.units.ProcessingElements; |
---|
| 18 | import schedframe.resources.units.ResourceUnit; |
---|
| 19 | import schedframe.resources.units.ResourceUnitName; |
---|
| 20 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 21 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
| 22 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 23 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 24 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 25 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
| 26 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 27 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 28 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 29 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 30 | |
---|
[525] | 31 | public class FCFSBF_ConsolidationClusterPlugin extends BaseLocalSchedulingPlugin { |
---|
[477] | 32 | |
---|
[525] | 33 | public FCFSBF_ConsolidationClusterPlugin () { |
---|
[477] | 34 | } |
---|
| 35 | |
---|
[481] | 36 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
[477] | 37 | ResourceManager resManager, ModuleList modules) { |
---|
| 38 | |
---|
| 39 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 40 | SchedulingPlan plan = new SchedulingPlan(); |
---|
[574] | 41 | // choose the events types to serve. |
---|
[477] | 42 | // Different actions for different events are possible. |
---|
| 43 | switch (event.getType()) { |
---|
| 44 | case START_TASK_EXECUTION: |
---|
| 45 | case TASK_FINISHED: |
---|
| 46 | // our tasks are placed only in first queue (see |
---|
[531] | 47 | // BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
[477] | 48 | TaskQueue q = queues.get(0); |
---|
| 49 | // check all tasks in queue |
---|
| 50 | |
---|
| 51 | for (int i = 0; i < q.size(); i++) { |
---|
[513] | 52 | TaskInterface<?> task = q.get(i); |
---|
[477] | 53 | // if status of the tasks in READY |
---|
[481] | 54 | if (task.getStatus() == DCWormsTags.READY) { |
---|
[477] | 55 | |
---|
| 56 | Map<ResourceUnitName, ResourceUnit> choosenResources = null; |
---|
| 57 | try { |
---|
| 58 | choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 59 | } catch (NoSuchFieldException e) { |
---|
| 60 | // TODO Auto-generated catch block |
---|
| 61 | e.printStackTrace(); |
---|
| 62 | } |
---|
| 63 | if (choosenResources != null) { |
---|
| 64 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | break; |
---|
| 70 | } |
---|
| 71 | return plan; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) throws NoSuchFieldException { |
---|
| 75 | |
---|
| 76 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 77 | nodes = findSuitableNodes(task, nodes); |
---|
| 78 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 79 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 80 | return node1.getResourceCharacteristic().getParameters().get("category").get(0).getContent().compareTo(node2.getResourceCharacteristic().getParameters().get("category").get(0).getContent()); |
---|
| 81 | } |
---|
| 82 | }); |
---|
| 83 | if(nodes.size() > 0) |
---|
| 84 | { |
---|
| 85 | ComputingNode node = nodes.get(0); |
---|
| 86 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
| 87 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
| 88 | int cpuRequest; |
---|
| 89 | try { |
---|
| 90 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 91 | } catch (NoSuchFieldException e) { |
---|
[531] | 92 | cpuRequest = 0; |
---|
[477] | 93 | } |
---|
| 94 | for (int i = 0; i < node.getProcessors().size() && cpuRequest > 0; i++) { |
---|
| 95 | if (node.getProcessors().get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 96 | choosenResources.add(node.getProcessors().get(i)); |
---|
| 97 | cpuRequest--; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | ProcessingElements result = new ProcessingElements(node.getName()); |
---|
| 101 | result.addAll(choosenResources); |
---|
| 102 | map.put(StandardResourceUnitName.PE, result); |
---|
| 103 | |
---|
| 104 | int memoryRequest; |
---|
| 105 | try { |
---|
| 106 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
| 107 | } catch (NoSuchFieldException e) { |
---|
| 108 | memoryRequest = 0; |
---|
| 109 | } |
---|
| 110 | if (memoryRequest != 0) { |
---|
| 111 | Memory memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
| 112 | map.put(StandardResourceUnitName.MEMORY, memory); |
---|
| 113 | } |
---|
| 114 | return map; |
---|
| 115 | } else |
---|
| 116 | return null; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, List<ComputingNode> nodes) throws NoSuchFieldException{ |
---|
| 120 | int cpuRequest; |
---|
| 121 | try { |
---|
| 122 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 123 | } catch (NoSuchFieldException e) { |
---|
| 124 | cpuRequest = 1; |
---|
| 125 | } |
---|
| 126 | int memoryRequest; |
---|
| 127 | try { |
---|
| 128 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
| 129 | } catch (NoSuchFieldException e) { |
---|
| 130 | memoryRequest = 0; |
---|
| 131 | } |
---|
| 132 | List<ComputingNode> suitableNodes = new ArrayList<ComputingNode>(); |
---|
| 133 | for(ComputingNode node: nodes){ |
---|
| 134 | if(node.getFreeProcessorsNumber() >= cpuRequest && node.getFreeMemory() >= memoryRequest){ |
---|
| 135 | suitableNodes.add(node); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | return suitableNodes; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | } |
---|