1 | package example.localplugin; |
---|
2 | |
---|
3 | import gridsim.Gridlet; |
---|
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 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
31 | |
---|
32 | public class FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin { |
---|
33 | |
---|
34 | public FCFSConsolidationClusterLocalPlugin () { |
---|
35 | } |
---|
36 | |
---|
37 | public SchedulingPlanInterface schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
38 | ResourceManager resManager, ModuleList modules) { |
---|
39 | |
---|
40 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
41 | SchedulingPlan plan = new SchedulingPlan(); |
---|
42 | // chose the events types to serve. |
---|
43 | // Different actions for different events are possible. |
---|
44 | switch (event.getType()) { |
---|
45 | case START_TASK_EXECUTION: |
---|
46 | case TASK_FINISHED: |
---|
47 | // our tasks are placed only in first queue (see |
---|
48 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
49 | TaskQueue q = queues.get(0); |
---|
50 | // check all tasks in queue |
---|
51 | |
---|
52 | for (int i = 0; i < q.size(); i++) { |
---|
53 | WorkloadUnit<?> job = q.get(i); |
---|
54 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
55 | // if status of the tasks in READY |
---|
56 | if (task.getStatus() == Gridlet.READY) { |
---|
57 | |
---|
58 | Map<ResourceUnitName, ResourceUnit> choosenResources = null; |
---|
59 | try { |
---|
60 | choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
61 | } catch (NoSuchFieldException e) { |
---|
62 | // TODO Auto-generated catch block |
---|
63 | e.printStackTrace(); |
---|
64 | } |
---|
65 | if (choosenResources != null) { |
---|
66 | addToSchedulingPlan(plan, task, choosenResources); |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | break; |
---|
72 | } |
---|
73 | return plan; |
---|
74 | } |
---|
75 | |
---|
76 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) throws NoSuchFieldException { |
---|
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.getResourceCharacteristic().getParameters().get("category").get(0).getContent().compareTo(node2.getResourceCharacteristic().getParameters().get("category").get(0).getContent()); |
---|
83 | } |
---|
84 | }); |
---|
85 | if(nodes.size() > 0) |
---|
86 | { |
---|
87 | ComputingNode node = nodes.get(0); |
---|
88 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, 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(StandardResourceUnitName.PE, 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(StandardResourceUnitName.MEMORY, memory); |
---|
115 | } |
---|
116 | return map; |
---|
117 | } else |
---|
118 | return null; |
---|
119 | } |
---|
120 | |
---|
121 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, List<ComputingNode> nodes) throws NoSuchFieldException{ |
---|
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> suitableNodes = new ArrayList<ComputingNode>(); |
---|
135 | for(ComputingNode node: nodes){ |
---|
136 | if(node.getFreeProcessorsNumber() >= cpuRequest && node.getFreeMemory() >= memoryRequest){ |
---|
137 | suitableNodes.add(node); |
---|
138 | } |
---|
139 | } |
---|
140 | return suitableNodes; |
---|
141 | } |
---|
142 | |
---|
143 | public String getName() { |
---|
144 | return getClass().getName(); |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|