1 | package test.article2.recs.plugins.scheduling.exp1; |
---|
2 | |
---|
3 | import gridsim.dcworms.DCWormsTags; |
---|
4 | |
---|
5 | import java.io.FileNotFoundException; |
---|
6 | import java.io.IOException; |
---|
7 | import java.util.ArrayList; |
---|
8 | import java.util.HashMap; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Map; |
---|
11 | import java.util.MissingResourceException; |
---|
12 | |
---|
13 | import schedframe.events.scheduling.SchedulingEvent; |
---|
14 | import schedframe.resources.ResourceStatus; |
---|
15 | import schedframe.resources.computing.ComputingNode; |
---|
16 | import schedframe.resources.computing.ComputingResource; |
---|
17 | import schedframe.resources.computing.Core; |
---|
18 | import schedframe.resources.computing.Processor; |
---|
19 | import schedframe.resources.computing.profiles.energy.airthroughput.StandardAirThroughputStateName; |
---|
20 | import schedframe.resources.computing.profiles.energy.airthroughput.UserAirThroughputStateName; |
---|
21 | import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; |
---|
22 | import schedframe.resources.units.ProcessingElements; |
---|
23 | import schedframe.resources.units.ResourceUnit; |
---|
24 | import schedframe.resources.units.ResourceUnitName; |
---|
25 | import schedframe.resources.units.StandardResourceUnitName; |
---|
26 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
27 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
28 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
29 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
30 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
31 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
32 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
33 | import schedframe.scheduling.queue.TaskQueue; |
---|
34 | import schedframe.scheduling.queue.TaskQueueList; |
---|
35 | import schedframe.scheduling.tasks.TaskInterface; |
---|
36 | import test.article2.recs.plugins.scheduling.RecsSP; |
---|
37 | |
---|
38 | public class RecsInOffSP extends RecsSP { |
---|
39 | |
---|
40 | private static int START_ID = 9; |
---|
41 | private static int END_ID = 17; |
---|
42 | |
---|
43 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
44 | ResourceManager resManager, ModuleList modules) { |
---|
45 | |
---|
46 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
47 | SchedulingPlan plan = new SchedulingPlan(); |
---|
48 | // choose 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 | //case TIMER: |
---|
54 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
55 | TaskQueue q = queues.get(0); |
---|
56 | |
---|
57 | List<ComputingNode> notSelectedNodes = resourceManager.getComputingNodes(); |
---|
58 | // check all tasks in queue |
---|
59 | for (int i = 0; i < q.size(); i++) { |
---|
60 | TaskInterface<?> task = q.get(i); |
---|
61 | // if status of the tasks in READY |
---|
62 | if (task.getStatus() == DCWormsTags.READY) { |
---|
63 | ComputingNode node = chooseProvider(resourceManager, task); |
---|
64 | if (node != null) { |
---|
65 | node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON); |
---|
66 | notSelectedNodes.remove(node); |
---|
67 | addToSchedulingPlan(plan, task, chooseResourcesForExecution(node, task)); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | turnOffIdleNodes(resourceManager.getComputingNodes()); |
---|
72 | adjustOtherFans(notSelectedNodes); |
---|
73 | break; |
---|
74 | } |
---|
75 | return plan; |
---|
76 | } |
---|
77 | |
---|
78 | private ComputingNode chooseProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
79 | List<ComputingNode> nodes = filterNodes(resourceManager.getComputingNodes(), task); |
---|
80 | for(ComputingNode node: nodes){ |
---|
81 | Integer id = Integer.parseInt(node.getName().split("_")[1]); |
---|
82 | if((id >= START_ID && id <= END_ID)){ |
---|
83 | if(node.getStatus() == ResourceStatus.UNAVAILABLE) { |
---|
84 | node.getPowerInterface().setPowerState(StandardPowerStateName.ON); |
---|
85 | } |
---|
86 | return node; |
---|
87 | } |
---|
88 | |
---|
89 | } |
---|
90 | return null; |
---|
91 | } |
---|
92 | |
---|
93 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
94 | ComputingNode node, TaskInterface<?> task) { |
---|
95 | |
---|
96 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
97 | |
---|
98 | int cpuRequest; |
---|
99 | try { |
---|
100 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
101 | } catch (NoSuchFieldException e) { |
---|
102 | cpuRequest = 0; |
---|
103 | } |
---|
104 | |
---|
105 | if (cpuRequest != 0) { |
---|
106 | |
---|
107 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
108 | |
---|
109 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
110 | for (int i = 0; i < cores.size(); i++) { |
---|
111 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
112 | //choosenResources.add(cores.get(i)); |
---|
113 | cpuRequest--; |
---|
114 | } |
---|
115 | } |
---|
116 | if (cpuRequest > 0) { |
---|
117 | //return null; |
---|
118 | } |
---|
119 | choosenResources.add(node.getProcessors().get(0)); |
---|
120 | ProcessingElements pe = new ProcessingElements(); |
---|
121 | pe.addAll(choosenResources); |
---|
122 | map.put(StandardResourceUnitName.PE, pe); |
---|
123 | return map; |
---|
124 | } |
---|
125 | return null; |
---|
126 | } |
---|
127 | |
---|
128 | private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){ |
---|
129 | List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>(); |
---|
130 | for (ComputingNode node : nodes) { |
---|
131 | int cpuRequest; |
---|
132 | try { |
---|
133 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
134 | } catch (NoSuchFieldException e) { |
---|
135 | cpuRequest = 0; |
---|
136 | } |
---|
137 | |
---|
138 | if (cpuRequest != 0) { |
---|
139 | |
---|
140 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
141 | if (cores.size() < cpuRequest) { |
---|
142 | continue; |
---|
143 | } |
---|
144 | |
---|
145 | int freeCores = 0; |
---|
146 | for(Core core: cores){ |
---|
147 | if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE) |
---|
148 | freeCores++; |
---|
149 | } |
---|
150 | |
---|
151 | if(freeCores != cores.size()) |
---|
152 | continue; |
---|
153 | |
---|
154 | try { |
---|
155 | if(!getExecutiveness(createExecutivenessQuery(task, node))) |
---|
156 | continue; |
---|
157 | } catch (FileNotFoundException e) { |
---|
158 | continue; |
---|
159 | } catch (IOException e) { |
---|
160 | continue; |
---|
161 | } catch (MissingResourceException e){ |
---|
162 | continue; |
---|
163 | } |
---|
164 | filteredNodes.add(node); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | return filteredNodes; |
---|
169 | } |
---|
170 | |
---|
171 | private void turnOffIdleNodes(List<ComputingNode> nodes){ |
---|
172 | for(ComputingNode node : nodes){ |
---|
173 | Processor proc = node.getProcessors().get(0); |
---|
174 | int freeCores = 0; |
---|
175 | for(Core core: proc.getCores()){ |
---|
176 | if(core.getStatus() == ResourceStatus.FREE) |
---|
177 | freeCores++; |
---|
178 | } |
---|
179 | |
---|
180 | if(freeCores == proc.getCores().size()) |
---|
181 | node.getPowerInterface().setPowerState(StandardPowerStateName.OFF); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | private void adjustOtherFans(List<ComputingNode> nodes){ |
---|
186 | for(ComputingNode node : nodes){ |
---|
187 | if(node.getFreeProcessorsNumber() == node.getProcessorsNumber()){ |
---|
188 | node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_OFF); |
---|
189 | } else { |
---|
190 | node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON); |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|