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.List; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import schedframe.scheduling.TaskInterface; |
---|
12 | import schedframe.scheduling.events.SchedulingEvent; |
---|
13 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
14 | import test.rewolucja.GSSIMJobInterface; |
---|
15 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
16 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
17 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
18 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
19 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
20 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
21 | import test.rewolucja.scheduling.queue.GSSIMQueue; |
---|
22 | import test.rewolucja.scheduling.queue.QueueList; |
---|
23 | |
---|
24 | public class FCFSEnergyAwareClusterLocalPlugin extends BaseLocalPlugin { |
---|
25 | |
---|
26 | public FCFSEnergyAwareClusterLocalPlugin () { |
---|
27 | } |
---|
28 | |
---|
29 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
30 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
31 | |
---|
32 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
33 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
34 | // chose the events types to serve. |
---|
35 | // Different actions for different events are possible. |
---|
36 | switch (event.getType()) { |
---|
37 | case START_TASK_EXECUTION: |
---|
38 | case TASK_FINISHED: |
---|
39 | // our tasks are placed only in first queue (see |
---|
40 | // BaseLocalPlugin.placeTasksInQueues() method) |
---|
41 | GSSIMQueue q = queues.get(0); |
---|
42 | // check all tasks in queue |
---|
43 | |
---|
44 | for (int i = 0; i < q.size(); i++) { |
---|
45 | GSSIMJobInterface<?> job = q.get(i); |
---|
46 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
47 | // if status of the tasks in READY |
---|
48 | if (task.getStatus() == Gridlet.READY) { |
---|
49 | |
---|
50 | String nodeName = chooseProviderForExecution(resourceManager, task); |
---|
51 | if (nodeName != null) { |
---|
52 | addToSchedulingPlan(plan, task, nodeName); |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | break; |
---|
58 | } |
---|
59 | return plan; |
---|
60 | } |
---|
61 | |
---|
62 | private String chooseProviderForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
63 | int cpuRequest; |
---|
64 | try { |
---|
65 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
66 | } catch (NoSuchFieldException e) { |
---|
67 | cpuRequest = 1; |
---|
68 | } |
---|
69 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
70 | nodes = findSuitableNodes(cpuRequest, nodes); |
---|
71 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
72 | |
---|
73 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
74 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
75 | |
---|
76 | } |
---|
77 | }); |
---|
78 | |
---|
79 | return nodes.size() == 0 ? null : nodes.get(0).getName(); |
---|
80 | } |
---|
81 | |
---|
82 | private List<ComputingNode> findSuitableNodes(int cpuRequest, List<ComputingNode> nodes){ |
---|
83 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
84 | for(ComputingNode node: nodes){ |
---|
85 | if(node.getFreeProcessorsNumber() >= cpuRequest){ |
---|
86 | avNodes.add(node); |
---|
87 | } |
---|
88 | } |
---|
89 | return avNodes; |
---|
90 | } |
---|
91 | |
---|
92 | public String getName() { |
---|
93 | return getClass().getName(); |
---|
94 | } |
---|
95 | |
---|
96 | public void init(Properties properties) { |
---|
97 | // no extra initialization is expected. |
---|
98 | } |
---|
99 | |
---|
100 | class CategoryComparator implements Comparator<String>{ |
---|
101 | |
---|
102 | public int compare(String cat1, String cat2){ |
---|
103 | return cat1.compareTo(cat2); |
---|
104 | |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|