[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | import gridsim.gssim.ResourceHistoryItem; |
---|
| 5 | import gridsim.gssim.SubmittedTask; |
---|
| 6 | |
---|
| 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.Collections; |
---|
| 9 | import java.util.Comparator; |
---|
| 10 | import java.util.HashMap; |
---|
| 11 | import java.util.List; |
---|
| 12 | import java.util.Map; |
---|
| 13 | import java.util.Properties; |
---|
| 14 | |
---|
| 15 | import schedframe.resources.units.Memory; |
---|
| 16 | import schedframe.resources.units.ResourceUnit; |
---|
| 17 | import schedframe.scheduling.TaskInterface; |
---|
| 18 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 19 | import schedframe.scheduling.events.TaskFinishedEvent; |
---|
| 20 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 21 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 22 | import test.rewolucja.GSSIMJobInterface; |
---|
| 23 | import test.rewolucja.energy.profile.PStateType; |
---|
| 24 | import test.rewolucja.resources.ProcessingElements; |
---|
| 25 | import test.rewolucja.resources.ResourceStatus; |
---|
| 26 | import test.rewolucja.resources.ResourceType; |
---|
| 27 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 28 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 29 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 30 | import test.rewolucja.resources.physical.implementation.CPU; |
---|
| 31 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
| 32 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 33 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 34 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
[163] | 35 | import test.rewolucja.scheduling.queue.Queue; |
---|
[104] | 36 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 37 | |
---|
| 38 | public class FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 39 | |
---|
| 40 | public FCFSConsolidationClusterLocalPlugin () { |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 44 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
| 45 | |
---|
| 46 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 47 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 48 | // chose the events types to serve. |
---|
| 49 | // Different actions for different events are possible. |
---|
| 50 | switch (event.getType()) { |
---|
| 51 | case START_TASK_EXECUTION: |
---|
| 52 | case TASK_FINISHED: |
---|
| 53 | // our tasks are placed only in first queue (see |
---|
| 54 | // BaseLocalPlugin.placeTasksInQueues() method) |
---|
[163] | 55 | Queue q = queues.get(0); |
---|
[104] | 56 | // check all tasks in queue |
---|
| 57 | |
---|
| 58 | for (int i = 0; i < q.size(); i++) { |
---|
| 59 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 60 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 61 | // if status of the tasks in READY |
---|
| 62 | if (task.getStatus() == Gridlet.READY) { |
---|
| 63 | |
---|
| 64 | Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 65 | if (choosenResources != null) { |
---|
| 66 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | break; |
---|
| 72 | } |
---|
| 73 | return plan; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 77 | |
---|
| 78 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
| 79 | nodes = findSuitableNodes(task, nodes); |
---|
| 80 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
| 81 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
| 82 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
| 83 | } |
---|
| 84 | }); |
---|
| 85 | if(nodes.size() > 0) |
---|
| 86 | { |
---|
| 87 | ComputingNode node = nodes.get(0); |
---|
| 88 | HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>(); |
---|
| 89 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
| 90 | int cpuRequest; |
---|
| 91 | try { |
---|
| 92 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 93 | } catch (NoSuchFieldException e) { |
---|
| 94 | cpuRequest = 1; |
---|
| 95 | } |
---|
| 96 | for (int i = 0; i < node.getProcessors().size() && cpuRequest > 0; i++) { |
---|
| 97 | if (node.getProcessors().get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 98 | choosenResources.add(node.getProcessors().get(i)); |
---|
| 99 | cpuRequest--; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | ProcessingElements result = new ProcessingElements(node.getName()); |
---|
| 103 | result.addAll(choosenResources); |
---|
| 104 | map.put(ResourceParameterName.PROCESSINGELEMENTS, result); |
---|
| 105 | |
---|
| 106 | int memoryRequest; |
---|
| 107 | try { |
---|
| 108 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
| 109 | } catch (NoSuchFieldException e) { |
---|
| 110 | memoryRequest = 0; |
---|
| 111 | } |
---|
| 112 | if (memoryRequest != 0) { |
---|
| 113 | Memory memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
| 114 | map.put(ResourceParameterName.MEMORY, memory); |
---|
| 115 | } |
---|
| 116 | return map; |
---|
| 117 | } else |
---|
| 118 | return null; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, List<ComputingNode> nodes){ |
---|
| 122 | int cpuRequest; |
---|
| 123 | try { |
---|
| 124 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 125 | } catch (NoSuchFieldException e) { |
---|
| 126 | cpuRequest = 1; |
---|
| 127 | } |
---|
| 128 | int memoryRequest; |
---|
| 129 | try { |
---|
| 130 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
| 131 | } catch (NoSuchFieldException e) { |
---|
| 132 | memoryRequest = 0; |
---|
| 133 | } |
---|
| 134 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
| 135 | for(ComputingNode node: nodes){ |
---|
| 136 | if(node.getFreeProcessorsNumber() >= cpuRequest && node.getFreeMemory() >= memoryRequest){ |
---|
| 137 | avNodes.add(node); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | return avNodes; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | public String getName() { |
---|
| 144 | return getClass().getName(); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | public void init(Properties properties) { |
---|
| 148 | // no extra initialization is expected. |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | } |
---|