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.Collections; |
---|
9 | import java.util.Comparator; |
---|
10 | import java.util.List; |
---|
11 | import java.util.Properties; |
---|
12 | |
---|
13 | import schedframe.resources.PowerState; |
---|
14 | import schedframe.scheduling.TaskInterface; |
---|
15 | import schedframe.scheduling.events.SchedulingEvent; |
---|
16 | import schedframe.scheduling.events.TaskFinishedEvent; |
---|
17 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
18 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
19 | import test.rewolucja.GSSIMJobInterface; |
---|
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 FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin { |
---|
36 | |
---|
37 | private int scenario = 1; |
---|
38 | |
---|
39 | public FCFSConsolidationClusterLocalPlugin () { |
---|
40 | } |
---|
41 | |
---|
42 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
43 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
44 | |
---|
45 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
46 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
47 | // chose the events types to serve. |
---|
48 | // Different actions for different events are possible. |
---|
49 | switch (event.getType()) { |
---|
50 | case TASK_FINISHED: |
---|
51 | if( scenario == 1) |
---|
52 | { |
---|
53 | TaskFinishedEvent finEvent = (TaskFinishedEvent) event; |
---|
54 | SubmittedTask subTask = jobRegistry.getSubmittedTask(finEvent.getJobId(), finEvent.getTaskId()); |
---|
55 | UsedResourceList<ResourceHistoryItem> usedResourcesList = subTask.getUsedResources(); |
---|
56 | ProcessingElements pes = (ProcessingElements)usedResourcesList.getLast().getResourceUnits().get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
57 | |
---|
58 | ComputingNode cn = ((Processor)pes.get(0)).getComputingNode(); |
---|
59 | if( cn.getFreeProcessors().size() == cn.getProcessorsNumber()) |
---|
60 | cn.getPowerInterface().setPowerState( PowerState.OFF); |
---|
61 | } |
---|
62 | case START_TASK_EXECUTION: |
---|
63 | // our tasks are placed only in first queue (see |
---|
64 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
65 | Queue q = queues.get(0); |
---|
66 | // check all tasks in queue |
---|
67 | |
---|
68 | for (int i = 0; i < q.size(); i++) { |
---|
69 | GSSIMJobInterface<?> job = q.get(i); |
---|
70 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
71 | // if status of the tasks in READY |
---|
72 | if (task.getStatus() == Gridlet.READY) { |
---|
73 | |
---|
74 | ComputingNode node = chooseResourcesForExecution(resourceManager, PowerState.ON, task); |
---|
75 | |
---|
76 | if (node != null) { |
---|
77 | List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task); |
---|
78 | //System.out.println("Uruchamiam na zasobie: " + node.getName()); |
---|
79 | addToSchedulingPlan(plan, task, cpus); |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | node = chooseResourcesForExecution(resourceManager, PowerState.OFF, task); |
---|
84 | |
---|
85 | if( node != null) |
---|
86 | { |
---|
87 | //System.out.println("Wlaczam wezel: " + node.getName()); |
---|
88 | node.getPowerInterface().setPowerState( PowerState.ON); |
---|
89 | i--; |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | switch( scenario) |
---|
96 | { |
---|
97 | case 0: break; |
---|
98 | case 1: |
---|
99 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
100 | for( ComputingNode node : nodes) |
---|
101 | if( node.getFreeProcessors().size() == node.getProcessorsNumber()) |
---|
102 | { |
---|
103 | //System.out.println("Wylaczam zasob" + node.getName()); |
---|
104 | node.getPowerInterface().setPowerState( PowerState.OFF); |
---|
105 | //System.out.println("Zasob w stanie: " + node.getPowerInterface().getPowerState()); |
---|
106 | } |
---|
107 | break; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | break; |
---|
112 | } |
---|
113 | |
---|
114 | return plan; |
---|
115 | } |
---|
116 | |
---|
117 | private ComputingNode chooseResourcesForExecution(ClusterResourceManager resourceManager, PowerState status, TaskInterface<?> task) { |
---|
118 | |
---|
119 | //System.out.println("Szukam zasobow w stanie: " +status); |
---|
120 | |
---|
121 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
122 | nodes = findSuitableNodes(task, status, nodes); |
---|
123 | |
---|
124 | Collections.sort(nodes, new Comparator<ComputingNode>(){ |
---|
125 | public int compare(ComputingNode node1, ComputingNode node2){ |
---|
126 | return node1.getCategory().getName().compareTo(node2.getCategory().getName()); |
---|
127 | } |
---|
128 | }); |
---|
129 | |
---|
130 | if(nodes.size() > 0) |
---|
131 | return nodes.get(0); |
---|
132 | |
---|
133 | return null; |
---|
134 | } |
---|
135 | |
---|
136 | private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, PowerState status, List<ComputingNode> nodes){ |
---|
137 | |
---|
138 | int cpuRequest = getCpuRequest(task); |
---|
139 | |
---|
140 | List<ComputingNode> suitableNodes = new ArrayList<ComputingNode>(); |
---|
141 | |
---|
142 | for(ComputingNode node: nodes) |
---|
143 | { |
---|
144 | if( node.getPowerInterface().getPowerState() == status) |
---|
145 | switch( status) |
---|
146 | { |
---|
147 | case ON: |
---|
148 | if(node.getFreeProcessorsNumber() >= cpuRequest) |
---|
149 | { |
---|
150 | suitableNodes.add(node); |
---|
151 | } |
---|
152 | break; |
---|
153 | case OFF: |
---|
154 | if( node.getProcessorsNumber() >= cpuRequest) |
---|
155 | suitableNodes.add(node); |
---|
156 | break; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | return suitableNodes; |
---|
161 | } |
---|
162 | |
---|
163 | public String getName() { |
---|
164 | return getClass().getName(); |
---|
165 | } |
---|
166 | |
---|
167 | public void init(Properties properties) { |
---|
168 | // no extra initialization is expected. |
---|
169 | } |
---|
170 | |
---|
171 | private List<Processor> chooseProcessorsForExecution( |
---|
172 | ComputingNode node, ResourceStatus status, TaskInterface<?> task) { |
---|
173 | |
---|
174 | int cpuRequest = getCpuRequest(task); |
---|
175 | |
---|
176 | List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status); |
---|
177 | |
---|
178 | return cpus.subList(0, cpuRequest); |
---|
179 | } |
---|
180 | |
---|
181 | private int getCpuRequest( TaskInterface<?> task) |
---|
182 | { |
---|
183 | int cpuRequest; |
---|
184 | |
---|
185 | try { |
---|
186 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
187 | } catch (NoSuchFieldException e) { |
---|
188 | cpuRequest = 1; |
---|
189 | } |
---|
190 | |
---|
191 | return cpuRequest; |
---|
192 | } |
---|
193 | } |
---|