1 | package experiments.e2dc2013.recs.plugins.scheduling.exp2; |
---|
2 | |
---|
3 | import experiments.e2dc2013.recs.plugins.scheduling.RecsSP; |
---|
4 | import gridsim.dcworms.DCWormsTags; |
---|
5 | |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.HashMap; |
---|
8 | import java.util.List; |
---|
9 | import java.util.Map; |
---|
10 | import java.util.Random; |
---|
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.computing.Core; |
---|
17 | import schedframe.resources.computing.profiles.energy.airthroughput.StandardAirflowStateName; |
---|
18 | import schedframe.resources.computing.profiles.energy.airthroughput.CustomAirflowStateName; |
---|
19 | import schedframe.resources.units.ProcessingElements; |
---|
20 | import schedframe.resources.units.ResourceUnit; |
---|
21 | import schedframe.resources.units.ResourceUnitName; |
---|
22 | import schedframe.resources.units.StandardResourceUnitName; |
---|
23 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
24 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
25 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
26 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
27 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
28 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
29 | import schedframe.scheduling.plugin.ModuleList; |
---|
30 | import schedframe.scheduling.queue.TaskQueue; |
---|
31 | import schedframe.scheduling.queue.TaskQueueList; |
---|
32 | import schedframe.scheduling.tasks.TaskInterface; |
---|
33 | |
---|
34 | public class RecsRandomSP extends RecsSP { |
---|
35 | |
---|
36 | private Random rand = new Random(5); |
---|
37 | |
---|
38 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
39 | ResourceManager resManager, ModuleList modules) { |
---|
40 | |
---|
41 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
42 | SchedulingPlan plan = new SchedulingPlan(); |
---|
43 | // choose the events types to serve. |
---|
44 | // Different actions for different events are possible. |
---|
45 | switch (event.getType()) { |
---|
46 | case START_TASK_EXECUTION: |
---|
47 | case TASK_FINISHED: |
---|
48 | //case TIMER: |
---|
49 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
50 | TaskQueue q = queues.get(0); |
---|
51 | |
---|
52 | List<Node> notSelectedNodes = resourceManager.getNodes(); |
---|
53 | // check all tasks in queue |
---|
54 | for (int i = 0; i < q.size(); i++) { |
---|
55 | TaskInterface<?> task = q.get(i); |
---|
56 | // if status of the tasks in READY |
---|
57 | if (task.getStatus() == DCWormsTags.READY) { |
---|
58 | Node node = chooseRandomProvider(resourceManager, task); |
---|
59 | if (node != null) { |
---|
60 | node.getAirflowInterface().setAirflowState(StandardAirflowStateName.ON); |
---|
61 | notSelectedNodes.remove(node); |
---|
62 | addToSchedulingPlan(plan, task, chooseResourcesForExecution(node, task)); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | adjustOtherFans(notSelectedNodes); |
---|
67 | break; |
---|
68 | } |
---|
69 | return plan; |
---|
70 | } |
---|
71 | |
---|
72 | private Node chooseRandomProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
73 | List<Node> nodes = filterNodes(resourceManager.getNodes(), task); |
---|
74 | if(nodes.size() > 0) |
---|
75 | return randomNode(nodes); |
---|
76 | return null; |
---|
77 | } |
---|
78 | |
---|
79 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
80 | Node node, TaskInterface<?> task) { |
---|
81 | |
---|
82 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
83 | |
---|
84 | int cpuRequest; |
---|
85 | try { |
---|
86 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
87 | } catch (NoSuchFieldException e) { |
---|
88 | cpuRequest = 0; |
---|
89 | } |
---|
90 | |
---|
91 | if (cpuRequest != 0) { |
---|
92 | |
---|
93 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
94 | |
---|
95 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
96 | for (int i = 0; i < cores.size(); i++) { |
---|
97 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
98 | //choosenResources.add(cores.get(i)); |
---|
99 | cpuRequest--; |
---|
100 | } |
---|
101 | } |
---|
102 | if (cpuRequest > 0) { |
---|
103 | //return null; |
---|
104 | } |
---|
105 | choosenResources.add(node.getProcessors().get(0)); |
---|
106 | ProcessingElements pe = new ProcessingElements(); |
---|
107 | pe.addAll(choosenResources); |
---|
108 | map.put(StandardResourceUnitName.PE, pe); |
---|
109 | return map; |
---|
110 | } |
---|
111 | return null; |
---|
112 | } |
---|
113 | |
---|
114 | private List<Node> filterNodes(List<Node> nodes, TaskInterface<?> task){ |
---|
115 | List<Node> filteredNodes = new ArrayList<Node>(); |
---|
116 | for (Node node : nodes) { |
---|
117 | int cpuRequest; |
---|
118 | try { |
---|
119 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
120 | } catch (NoSuchFieldException e) { |
---|
121 | cpuRequest = 0; |
---|
122 | } |
---|
123 | |
---|
124 | if (cpuRequest != 0) { |
---|
125 | |
---|
126 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
127 | if (cores.size() < cpuRequest) { |
---|
128 | continue; |
---|
129 | } |
---|
130 | |
---|
131 | int freeCores = 0; |
---|
132 | for(Core core: cores){ |
---|
133 | if(core.getStatus() == ResourceStatus.FREE) |
---|
134 | freeCores++; |
---|
135 | } |
---|
136 | |
---|
137 | if(freeCores != cores.size()) |
---|
138 | continue; |
---|
139 | |
---|
140 | filteredNodes.add(node); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | return filteredNodes; |
---|
145 | } |
---|
146 | |
---|
147 | private Node randomNode(List<Node> nodes){ |
---|
148 | return nodes.get(rand.nextInt(nodes.size())); |
---|
149 | } |
---|
150 | |
---|
151 | private void adjustOtherFans(List<Node> nodes){ |
---|
152 | for(Node node : nodes){ |
---|
153 | if(node.getFreeProcessorsNumber() == node.getProcessorsNumber()){ |
---|
154 | node.getAirflowInterface().setAirflowState(StandardAirflowStateName.OFF); |
---|
155 | } else if(new JobRegistryImpl(node.getFullName()).getRunningTasks().size() > 1) |
---|
156 | node.getAirflowInterface().setAirflowState(new CustomAirflowStateName("FAN_ON_TURBO")); |
---|
157 | else |
---|
158 | node.getAirflowInterface().setAirflowState(StandardAirflowStateName.ON); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|