1 | package example.localplugin; |
---|
2 | |
---|
3 | import gridsim.Gridlet; |
---|
4 | import gridsim.gssim.SubmittedTask; |
---|
5 | import gssim.schedframe.scheduling.ExecTaskInterface; |
---|
6 | |
---|
7 | import java.util.ArrayList; |
---|
8 | import java.util.HashMap; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Map; |
---|
11 | import java.util.Properties; |
---|
12 | |
---|
13 | import schedframe.resources.units.Memory; |
---|
14 | import schedframe.resources.units.ResourceUnit; |
---|
15 | import schedframe.scheduling.TaskInterface; |
---|
16 | import schedframe.scheduling.events.SchedulingEvent; |
---|
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.interfaces.ResourceManagerInterface; |
---|
24 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
25 | import test.rewolucja.resources.physical.implementation.CPU; |
---|
26 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
27 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
28 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
29 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
30 | import test.rewolucja.scheduling.queue.GSSIMQueue; |
---|
31 | import test.rewolucja.scheduling.queue.QueueList; |
---|
32 | |
---|
33 | public class FCFS_EBFLocalPlugin extends BaseLocalPlugin { |
---|
34 | |
---|
35 | public FCFS_EBFLocalPlugin() { |
---|
36 | } |
---|
37 | |
---|
38 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
39 | ResourceManagerInterface resourceManager, ModuleList modules) { |
---|
40 | |
---|
41 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
42 | // chose the events types to serve. |
---|
43 | // Different actions for different events are possible. |
---|
44 | switch (event.getType()) { |
---|
45 | case START_TASK_EXECUTION: |
---|
46 | case TASK_FINISHED: |
---|
47 | // our tasks are placed only in first queue (see |
---|
48 | // BaseLocalPlugin.placeTasksInQueues() method) |
---|
49 | GSSIMQueue q = queues.get(0); |
---|
50 | // check all tasks in queue |
---|
51 | |
---|
52 | for (int i = 0; i < q.size(); i++) { |
---|
53 | GSSIMJobInterface<?> job = q.get(i); |
---|
54 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
55 | // if status of the tasks in READY |
---|
56 | if (task.getStatus() == Gridlet.READY) { |
---|
57 | |
---|
58 | SubmittedTask subTask = (SubmittedTask) task; |
---|
59 | // DataCenterResourceManager dcrm = |
---|
60 | // (DataCenterResourceManager)unitsManager; |
---|
61 | // List<CPU> cpus = dcrm.getProcessors(); |
---|
62 | // cpus.get(0).getPowerInterface().setFrequency(100); |
---|
63 | Map<ResourceParameterName, ResourceUnit> choosenResources = null; |
---|
64 | choosenResources = chooseResourcesForExecution(resourceManager, subTask); |
---|
65 | // String name = |
---|
66 | // chooseProviderForExecution(resourceManager); |
---|
67 | if (choosenResources != null) { |
---|
68 | // q.remove(i); |
---|
69 | // i--; |
---|
70 | addToSchedulingPlan(plan, task, choosenResources); |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|
76 | return plan; |
---|
77 | } |
---|
78 | |
---|
79 | public HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution( |
---|
80 | ResourceManagerInterface unitsManager, ExecTaskInterface task) { |
---|
81 | |
---|
82 | HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>(); |
---|
83 | |
---|
84 | int cpuRequest; |
---|
85 | try { |
---|
86 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
87 | } catch (NoSuchFieldException e) { |
---|
88 | cpuRequest = 1; |
---|
89 | } |
---|
90 | |
---|
91 | if (cpuRequest != 0) { |
---|
92 | List<ComputingResource> choosenResources = null; |
---|
93 | List<ComputingResource> processingElements = null; |
---|
94 | try { |
---|
95 | Properties properties = new Properties(); |
---|
96 | properties.setProperty("type", ResourceType.CPU.toString()); |
---|
97 | // properties.setProperty("status", |
---|
98 | // ResourceStatus.FREE.toString()); |
---|
99 | processingElements = (List<ComputingResource>) unitsManager.filterResources(properties); |
---|
100 | } catch (Exception e) { |
---|
101 | // TODO Auto-generated catch block |
---|
102 | e.printStackTrace(); |
---|
103 | } |
---|
104 | if (processingElements.size() < cpuRequest) { |
---|
105 | // log.warn("Task requires more cpus than is availiable in this resource."); |
---|
106 | return null; |
---|
107 | } |
---|
108 | |
---|
109 | choosenResources = new ArrayList<ComputingResource>(); |
---|
110 | |
---|
111 | for (int i = 0; i < processingElements.size() && cpuRequest > 0; i++) { |
---|
112 | if (processingElements.get(i).getStatus() == ResourceStatus.FREE) { |
---|
113 | choosenResources.add(processingElements.get(i)); |
---|
114 | cpuRequest--; |
---|
115 | CPU cpu = (CPU)processingElements.get(i); |
---|
116 | //cpu.getParent().getPowerProfile().setPowerState(PowerState.OFF); |
---|
117 | } |
---|
118 | } |
---|
119 | if (cpuRequest > 0) { |
---|
120 | // log.info("Task " + task.getJobId() + "_" + task.getId() + |
---|
121 | // " requires more cpus than is availiable in this moment."); |
---|
122 | return null; |
---|
123 | } |
---|
124 | |
---|
125 | ProcessingElements result = new ProcessingElements(processingElements.get(0).getParent().getName()); |
---|
126 | result.addAll(choosenResources); |
---|
127 | map.put(ResourceParameterName.PROCESSINGELEMENTS, result); |
---|
128 | |
---|
129 | } |
---|
130 | int memoryRequest; |
---|
131 | try { |
---|
132 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
133 | } catch (NoSuchFieldException e) { |
---|
134 | memoryRequest = 0; |
---|
135 | } |
---|
136 | if (memoryRequest != 0) { |
---|
137 | List<ComputingNode> nodes = null; |
---|
138 | try { |
---|
139 | Properties properties = new Properties(); |
---|
140 | properties.setProperty("type", ResourceType.COMPUTING_NODE.toString()); |
---|
141 | nodes = (List<ComputingNode>) unitsManager.filterResources(properties); |
---|
142 | } catch (Exception e) { |
---|
143 | // TODO Auto-generated catch block |
---|
144 | e.printStackTrace(); |
---|
145 | } |
---|
146 | Memory memory = null; |
---|
147 | for (ComputingNode node : nodes) { |
---|
148 | |
---|
149 | if (node.getFreeMemory() >= memoryRequest) { |
---|
150 | memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
151 | } else |
---|
152 | return null; |
---|
153 | } |
---|
154 | map.put(ResourceParameterName.MEMORY, memory); |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | return map; |
---|
159 | } |
---|
160 | |
---|
161 | public String getName() { |
---|
162 | return getClass().getName(); |
---|
163 | } |
---|
164 | |
---|
165 | public void init(Properties properties) { |
---|
166 | // no extra initialization is expected. |
---|
167 | } |
---|
168 | |
---|
169 | } |
---|