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.List; |
---|
9 | import java.util.Properties; |
---|
10 | import java.util.Random; |
---|
11 | |
---|
12 | import schedframe.resources.PowerState; |
---|
13 | import schedframe.scheduling.TaskInterface; |
---|
14 | import schedframe.scheduling.events.SchedulingEvent; |
---|
15 | import schedframe.scheduling.events.TaskFinishedEvent; |
---|
16 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
17 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
18 | import test.rewolucja.GSSIMJobInterface; |
---|
19 | import test.rewolucja.energy.profile.PStateType; |
---|
20 | import test.rewolucja.resources.ProcessingElements; |
---|
21 | import test.rewolucja.resources.ResourceStatus; |
---|
22 | import test.rewolucja.resources.ResourceType; |
---|
23 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
24 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
25 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
26 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
27 | import test.rewolucja.resources.physical.implementation.Processor; |
---|
28 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
29 | import test.rewolucja.scheduling.UsedResourceList; |
---|
30 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
31 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
32 | import test.rewolucja.scheduling.queue.Queue; |
---|
33 | import test.rewolucja.scheduling.queue.QueueList; |
---|
34 | |
---|
35 | public class FCFSPreferedRandomClusterLocalPlugin extends BaseLocalPlugin { |
---|
36 | |
---|
37 | private Random rand; |
---|
38 | private boolean init = true; |
---|
39 | |
---|
40 | int scenario = 3; |
---|
41 | String prefered = null; |
---|
42 | |
---|
43 | public FCFSPreferedRandomClusterLocalPlugin() { |
---|
44 | rand = new Random(5); |
---|
45 | } |
---|
46 | |
---|
47 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
48 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
49 | |
---|
50 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
51 | |
---|
52 | if( init) |
---|
53 | { |
---|
54 | List<Processor> cpus = resourceManager.getProcessors(); |
---|
55 | |
---|
56 | for( Processor cpu : cpus) |
---|
57 | cpu.getPowerInterface().setPState( PStateType.P0); |
---|
58 | |
---|
59 | init = false; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
65 | // chose the events types to serve. |
---|
66 | // Different actions for different events are possible. |
---|
67 | switch (event.getType()) { |
---|
68 | case TASK_FINISHED: |
---|
69 | switch( scenario) |
---|
70 | { |
---|
71 | case 2: |
---|
72 | case 3: |
---|
73 | TaskFinishedEvent finEvent = (TaskFinishedEvent) event; |
---|
74 | SubmittedTask subTask = jobRegistry.getSubmittedTask(finEvent.getJobId(), finEvent.getTaskId()); |
---|
75 | UsedResourceList<ResourceHistoryItem> usedResourcesList = subTask.getUsedResources(); |
---|
76 | ProcessingElements pes = (ProcessingElements)usedResourcesList.getLast().getResourceUnits().get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
77 | |
---|
78 | for( ComputingResource cr : pes) |
---|
79 | { |
---|
80 | ((Processor)cr).getPowerInterface().setPowerState( PowerState.OFF); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | case START_TASK_EXECUTION: |
---|
85 | // our tasks are placed only in first queue (see |
---|
86 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
87 | Queue q = queues.get(0); |
---|
88 | // check all tasks in queue |
---|
89 | |
---|
90 | for (int i = 0; i < q.size(); i++) { |
---|
91 | GSSIMJobInterface<?> job = q.get(i); |
---|
92 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
93 | // if status of the tasks in READY |
---|
94 | if (task.getStatus() == Gridlet.READY) { |
---|
95 | |
---|
96 | ComputingNode node = chooseRandomProvider(resourceManager, ResourceStatus.FREE, task); |
---|
97 | |
---|
98 | if (node != null) { |
---|
99 | List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task); |
---|
100 | |
---|
101 | int type = Integer.parseInt( task.getJobId()) % 4; |
---|
102 | String model = cpus.get(0).getComputingNode().getCategory().getName(); |
---|
103 | //System.out.println(type + " -> " + model); |
---|
104 | |
---|
105 | addToSchedulingPlan(plan, task, cpus); |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | switch( scenario) |
---|
110 | { |
---|
111 | case 0: break; |
---|
112 | case 1: break; |
---|
113 | case 2: |
---|
114 | node = chooseRandomProvider(resourceManager, ResourceStatus.UNAVAILABLE, task); |
---|
115 | if( node != null) |
---|
116 | { |
---|
117 | List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.UNAVAILABLE, task); |
---|
118 | for( Processor cpu: cpus) |
---|
119 | cpu.getPowerInterface().setPowerState( PowerState.ON); |
---|
120 | |
---|
121 | i--; |
---|
122 | } |
---|
123 | break; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | switch( scenario) |
---|
130 | { |
---|
131 | case 0: break; |
---|
132 | case 1: |
---|
133 | |
---|
134 | for( Processor cpu : resourceManager.getProcessors()) |
---|
135 | { |
---|
136 | switch( cpu.getStatus()) |
---|
137 | { |
---|
138 | case FREE: cpu.getPowerInterface().setPState( PStateType.P3); break; |
---|
139 | case PENDING: cpu.getPowerInterface().setPState( PStateType.P0); break; |
---|
140 | } |
---|
141 | |
---|
142 | } |
---|
143 | break; |
---|
144 | |
---|
145 | case 2: |
---|
146 | for( Processor cpu : resourceManager.getProcessors()) |
---|
147 | { |
---|
148 | switch( cpu.getStatus()) |
---|
149 | { |
---|
150 | case FREE: cpu.getPowerInterface().setPowerState( PowerState.OFF); break; |
---|
151 | } |
---|
152 | } |
---|
153 | break; |
---|
154 | |
---|
155 | default: break; |
---|
156 | } |
---|
157 | |
---|
158 | break; |
---|
159 | } |
---|
160 | return plan; |
---|
161 | } |
---|
162 | |
---|
163 | private List<ComputingNode> findSuitableNodes(String model, int cpuRequest, ResourceStatus status, List<ComputingNode> nodes){ |
---|
164 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
165 | for(ComputingNode node: nodes) |
---|
166 | { |
---|
167 | if( (model == null || node.getCategory().getName().equals(model))) |
---|
168 | { |
---|
169 | @SuppressWarnings("unchecked") |
---|
170 | List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status); |
---|
171 | |
---|
172 | if( cpus.size() >= cpuRequest) |
---|
173 | avNodes.add(node); |
---|
174 | } |
---|
175 | } |
---|
176 | return avNodes; |
---|
177 | } |
---|
178 | |
---|
179 | private int getCpuRequest( TaskInterface<?> task) |
---|
180 | { |
---|
181 | int cpuRequest; |
---|
182 | |
---|
183 | try { |
---|
184 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
185 | } catch (NoSuchFieldException e) { |
---|
186 | cpuRequest = 1; |
---|
187 | } |
---|
188 | |
---|
189 | return cpuRequest; |
---|
190 | } |
---|
191 | |
---|
192 | private ComputingNode chooseRandomProvider(ClusterResourceManager resourceManager, ResourceStatus status, TaskInterface<?> task) { |
---|
193 | |
---|
194 | String preferedNode = null; |
---|
195 | |
---|
196 | int cpuRequest = getCpuRequest(task); |
---|
197 | |
---|
198 | List<ComputingNode> nodes = null; |
---|
199 | |
---|
200 | switch( scenario) |
---|
201 | { |
---|
202 | case 3: |
---|
203 | int type = Integer.parseInt( task.getJobId()) % 4; |
---|
204 | switch( type) |
---|
205 | { |
---|
206 | case 0: preferedNode = "B"; break; |
---|
207 | default: preferedNode = "A"; break; |
---|
208 | } |
---|
209 | break; |
---|
210 | default: |
---|
211 | preferedNode = prefered; |
---|
212 | break; |
---|
213 | |
---|
214 | } |
---|
215 | |
---|
216 | nodes = findSuitableNodes(preferedNode, cpuRequest, status, resourceManager.getComputingNodes()); |
---|
217 | switch( nodes.size()) |
---|
218 | { |
---|
219 | case 0: break; |
---|
220 | case 1: return nodes.get(0); |
---|
221 | default: |
---|
222 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
223 | ComputingNode node = nodes.get(nodeIdx); |
---|
224 | return node; |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | if( preferedNode != null) |
---|
229 | { |
---|
230 | nodes = findSuitableNodes( getUnprefered(preferedNode), cpuRequest, status, resourceManager.getComputingNodes()); |
---|
231 | switch( nodes.size()) |
---|
232 | { |
---|
233 | case 0: break; |
---|
234 | case 1: return nodes.get(0); |
---|
235 | default: |
---|
236 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
237 | ComputingNode node = nodes.get(nodeIdx); |
---|
238 | return node; |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | return null; |
---|
243 | } |
---|
244 | |
---|
245 | public String getName() { |
---|
246 | return getClass().getName(); |
---|
247 | } |
---|
248 | |
---|
249 | public void init(Properties properties) { |
---|
250 | // no extra initialization is expected. |
---|
251 | } |
---|
252 | |
---|
253 | private List<Processor> chooseProcessorsForExecution( |
---|
254 | ComputingNode node, ResourceStatus status, TaskInterface<?> task) { |
---|
255 | |
---|
256 | int cpuRequest = getCpuRequest(task); |
---|
257 | |
---|
258 | List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status); |
---|
259 | |
---|
260 | return cpus.subList(0, cpuRequest); |
---|
261 | } |
---|
262 | |
---|
263 | private String getUnprefered( String preferedNode) |
---|
264 | { |
---|
265 | if( preferedNode.equals("A")) |
---|
266 | return "B"; |
---|
267 | |
---|
268 | if( preferedNode.equals("B")) |
---|
269 | return "A"; |
---|
270 | |
---|
271 | return null; |
---|
272 | } |
---|
273 | |
---|
274 | } |
---|
275 | |
---|