1 | package example.localplugin; |
---|
2 | |
---|
3 | import gridsim.dcworms.DCWormsTags; |
---|
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.Node; |
---|
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 | |
---|
31 | public class Cluster_FCFSBF_ConsolidationPlugin extends BaseLocalSchedulingPlugin { |
---|
32 | |
---|
33 | public Cluster_FCFSBF_ConsolidationPlugin () { |
---|
34 | } |
---|
35 | |
---|
36 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
37 | ResourceManager resManager, ModuleList modules) { |
---|
38 | |
---|
39 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
40 | SchedulingPlan plan = new SchedulingPlan(); |
---|
41 | // choose the events types to serve. |
---|
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 |
---|
47 | // BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
48 | TaskQueue q = queues.get(0); |
---|
49 | // check all tasks in queue |
---|
50 | |
---|
51 | for (int i = 0; i < q.size(); i++) { |
---|
52 | TaskInterface<?> task = q.get(i); |
---|
53 | // if status of the tasks in READY |
---|
54 | if (task.getStatus() == DCWormsTags.READY) { |
---|
55 | |
---|
56 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
57 | if (choosenResources != null) { |
---|
58 | addToSchedulingPlan(plan, task, choosenResources); |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | break; |
---|
64 | } |
---|
65 | return plan; |
---|
66 | } |
---|
67 | |
---|
68 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task){ |
---|
69 | |
---|
70 | List<Node> nodes = resourceManager.getNodes(); |
---|
71 | nodes = findSuitableNodes(task, nodes); |
---|
72 | Collections.sort(nodes, new Comparator<Node>(){ |
---|
73 | public int compare(Node node1, Node node2){ |
---|
74 | return node1.getResourceCharacteristic().getParameters().get("category").get(0).getContent().compareTo(node2.getResourceCharacteristic().getParameters().get("category").get(0).getContent()); |
---|
75 | } |
---|
76 | }); |
---|
77 | if(nodes.size() > 0) |
---|
78 | { |
---|
79 | Node node = nodes.get(0); |
---|
80 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
81 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
82 | int cpuRequest; |
---|
83 | try { |
---|
84 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
85 | } catch (NoSuchFieldException e) { |
---|
86 | cpuRequest = 0; |
---|
87 | } |
---|
88 | for (int i = 0; i < node.getProcessors().size() && cpuRequest > 0; i++) { |
---|
89 | if (node.getProcessors().get(i).getStatus() == ResourceStatus.FREE) { |
---|
90 | choosenResources.add(node.getProcessors().get(i)); |
---|
91 | cpuRequest--; |
---|
92 | } |
---|
93 | } |
---|
94 | ProcessingElements result = new ProcessingElements(node.getFullName()); |
---|
95 | result.addAll(choosenResources); |
---|
96 | map.put(StandardResourceUnitName.PE, result); |
---|
97 | |
---|
98 | int memoryRequest; |
---|
99 | try { |
---|
100 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
101 | } catch (NoSuchFieldException e) { |
---|
102 | memoryRequest = 0; |
---|
103 | } |
---|
104 | if (memoryRequest != 0) { |
---|
105 | Memory memory; |
---|
106 | try { |
---|
107 | memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
108 | map.put(StandardResourceUnitName.MEMORY, memory); |
---|
109 | } catch (NoSuchFieldException e) { |
---|
110 | return null; |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|
114 | return map; |
---|
115 | } else |
---|
116 | return null; |
---|
117 | } |
---|
118 | |
---|
119 | private List<Node> findSuitableNodes(TaskInterface<?> task, List<Node> nodes) { |
---|
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<Node> suitableNodes = new ArrayList<Node>(); |
---|
133 | for(Node node: nodes){ |
---|
134 | try{ |
---|
135 | if(node.getFreeProcessorsNumber() >= cpuRequest && node.getFreeMemory() >= memoryRequest){ |
---|
136 | suitableNodes.add(node); |
---|
137 | } |
---|
138 | } catch (NoSuchFieldException e){ |
---|
139 | |
---|
140 | } |
---|
141 | } |
---|
142 | return suitableNodes; |
---|
143 | } |
---|
144 | |
---|
145 | } |
---|