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.resources.PowerState; |
---|
12 | import schedframe.scheduling.TaskInterface; |
---|
13 | import schedframe.scheduling.events.SchedulingEvent; |
---|
14 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
15 | import test.rewolucja.GSSIMJobInterface; |
---|
16 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
17 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
18 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
19 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
20 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
21 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
22 | import test.rewolucja.scheduling.queue.Queue; |
---|
23 | import test.rewolucja.scheduling.queue.QueueList; |
---|
24 | |
---|
25 | public class FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin { |
---|
26 | |
---|
27 | private int scenario = 1; |
---|
28 | |
---|
29 | public FCFSConsolidationClusterLocalPlugin () { |
---|
30 | } |
---|
31 | |
---|
32 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
33 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
34 | |
---|
35 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
36 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
37 | // chose the events types to serve. |
---|
38 | // Different actions for different events are possible. |
---|
39 | switch (event.getType()) { |
---|
40 | case START_TASK_EXECUTION: |
---|
41 | case TASK_FINISHED: |
---|
42 | // our tasks are placed only in first queue (see |
---|
43 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
44 | Queue q = queues.get(0); |
---|
45 | // check all tasks in queue |
---|
46 | |
---|
47 | for (int i = 0; i < q.size(); i++) { |
---|
48 | GSSIMJobInterface<?> job = q.get(i); |
---|
49 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
50 | // if status of the tasks in READY |
---|
51 | if (task.getStatus() == Gridlet.READY) { |
---|
52 | |
---|
53 | ComputingNode node = chooseResourcesForExecution(resourceManager, PowerState.ON, task); |
---|
54 | if (node != null) { |
---|
55 | System.out.println("Uruchamiam na zasobie: " + node.getName()); |
---|
56 | addToSchedulingPlan(plan, task, node.getName()); |
---|
57 | } |
---|
58 | else |
---|
59 | { |
---|
60 | node = chooseResourcesForExecution(resourceManager, PowerState.OFF, task); |
---|
61 | |
---|
62 | if( node != null) |
---|
63 | { |
---|
64 | System.out.println("Wlaczam wezel: " + node.getName()); |
---|
65 | node.getPowerInterface().setPowerState( PowerState.ON); |
---|
66 | i--; |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | switch( scenario) |
---|
73 | { |
---|
74 | case 0: break; |
---|
75 | case 1: |
---|
76 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
77 | for( ComputingNode node : nodes) |
---|
78 | if( node.getFreeProcessors().size() == node.getProcessorsNumber()) |
---|
79 | { |
---|
80 | node.getPowerInterface().setPowerState( PowerState.OFF); |
---|
81 | } |
---|
82 | break; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | break; |
---|
87 | } |
---|
88 | return plan; |
---|
89 | } |
---|
90 | |
---|
91 | private ComputingNode chooseResourcesForExecution(ClusterResourceManager resourceManager, PowerState status, TaskInterface<?> task) { |
---|
92 | |
---|
93 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
94 | nodes = findSuitableNodes(task, status, nodes); |
---|
95 | |
---|
96 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
97 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
98 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
99 | } |
---|
100 | }); |
---|
101 | |
---|
102 | if(nodes.size() > 0) |
---|
103 | return nodes.get(0); |
---|
104 | |
---|
105 | return null; |
---|
106 | } |
---|
107 | |
---|
108 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, PowerState status, List<ComputingNode> nodes){ |
---|
109 | int cpuRequest; |
---|
110 | try { |
---|
111 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
112 | } catch (NoSuchFieldException e) { |
---|
113 | cpuRequest = 1; |
---|
114 | } |
---|
115 | |
---|
116 | List<ComputingNode> suitableNodes = new ArrayList<ComputingNode>(); |
---|
117 | for(ComputingNode node: nodes) |
---|
118 | { |
---|
119 | switch( status) |
---|
120 | { |
---|
121 | case ON: |
---|
122 | if(node.getFreeProcessorsNumber() >= cpuRequest) |
---|
123 | suitableNodes.add(node); |
---|
124 | break; |
---|
125 | case OFF: |
---|
126 | if( node.getProcessorsNumber() >= cpuRequest) |
---|
127 | suitableNodes.add(node); |
---|
128 | break; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | return suitableNodes; |
---|
133 | } |
---|
134 | |
---|
135 | public String getName() { |
---|
136 | return getClass().getName(); |
---|
137 | } |
---|
138 | |
---|
139 | public void init(Properties properties) { |
---|
140 | // no extra initialization is expected. |
---|
141 | } |
---|
142 | |
---|
143 | } |
---|