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.PowerState; |
---|
14 | import schedframe.resources.units.Memory; |
---|
15 | import schedframe.resources.units.ResourceUnit; |
---|
16 | import schedframe.scheduling.TaskInterface; |
---|
17 | import schedframe.scheduling.events.SchedulingEvent; |
---|
18 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
19 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
20 | import test.rewolucja.GSSIMJobInterface; |
---|
21 | import test.rewolucja.resources.ProcessingElements; |
---|
22 | import test.rewolucja.resources.ResourceStatus; |
---|
23 | import test.rewolucja.resources.ResourceType; |
---|
24 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
25 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
26 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
27 | import test.rewolucja.resources.physical.implementation.CPU; |
---|
28 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
29 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
30 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
31 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
32 | import test.rewolucja.scheduling.queue.GSSIMQueue; |
---|
33 | import test.rewolucja.scheduling.queue.QueueList; |
---|
34 | |
---|
35 | public class FCFSClusterLocalPlugin extends BaseLocalPlugin { |
---|
36 | |
---|
37 | public FCFSClusterLocalPlugin() { |
---|
38 | } |
---|
39 | |
---|
40 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
41 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
42 | |
---|
43 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
44 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
45 | // chose the events types to serve. |
---|
46 | // Different actions for different events are possible. |
---|
47 | switch (event.getType()) { |
---|
48 | case START_TASK_EXECUTION: |
---|
49 | case TASK_FINISHED: |
---|
50 | case TIMER: |
---|
51 | // our tasks are placed only in first queue (see |
---|
52 | // BaseLocalPlugin.placeTasksInQueues() method) |
---|
53 | GSSIMQueue q = queues.get(0); |
---|
54 | // check all tasks in queue |
---|
55 | |
---|
56 | |
---|
57 | for (int i = 0; i < q.size(); i++) { |
---|
58 | GSSIMJobInterface<?> job = q.get(i); |
---|
59 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
60 | // if status of the tasks in READY |
---|
61 | if (task.getStatus() == Gridlet.READY) { |
---|
62 | |
---|
63 | SubmittedTask subTask = (SubmittedTask) task; |
---|
64 | |
---|
65 | /****************3 ways to schedule task****************/ |
---|
66 | |
---|
67 | //1. Choosing particular resources to perform execution |
---|
68 | Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, subTask); |
---|
69 | if (choosenResources != null) { |
---|
70 | addToSchedulingPlan(plan, task, choosenResources); |
---|
71 | } |
---|
72 | |
---|
73 | //2. Choosing resource scheduler/provider to submit task. If the given resource doesn't contains/isn't |
---|
74 | //a scheduler, random resources from the given resource will be chosen in order to perform execution |
---|
75 | /*String provName = chooseProviderForExecution(resourceManager); |
---|
76 | if (provName != null) { |
---|
77 | addToSchedulingPlan(plan, task, provName); |
---|
78 | }*/ |
---|
79 | |
---|
80 | //3. Scheduler will choose random resources to perform execution |
---|
81 | //addToSchedulingPlan(plan, task); |
---|
82 | |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | break; |
---|
87 | } |
---|
88 | return plan; |
---|
89 | } |
---|
90 | |
---|
91 | private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution( |
---|
92 | ClusterResourceManager resourceManager, ExecTaskInterface task) { |
---|
93 | |
---|
94 | HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>(); |
---|
95 | |
---|
96 | int cpuRequest; |
---|
97 | try { |
---|
98 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
99 | } catch (NoSuchFieldException e) { |
---|
100 | cpuRequest = 1; |
---|
101 | } |
---|
102 | |
---|
103 | if (cpuRequest != 0) { |
---|
104 | List<ComputingResource> choosenResources = null; |
---|
105 | List<CPU> processors = null; |
---|
106 | processors = resourceManager.getProcessors(); |
---|
107 | if (processors.size() < cpuRequest) { |
---|
108 | // log.warn("Task requires more cpus than is availiable in this resource."); |
---|
109 | return null; |
---|
110 | } |
---|
111 | |
---|
112 | choosenResources = new ArrayList<ComputingResource>(); |
---|
113 | |
---|
114 | for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { |
---|
115 | if (processors.get(i).getStatus() == ResourceStatus.FREE) { |
---|
116 | choosenResources.add(processors.get(i)); |
---|
117 | cpuRequest--; |
---|
118 | } |
---|
119 | } |
---|
120 | if (cpuRequest > 0) { |
---|
121 | // log.info("Task " + task.getJobId() + "_" + task.getId() + |
---|
122 | // " requires more cpus than is availiable in this moment."); |
---|
123 | return null; |
---|
124 | } |
---|
125 | |
---|
126 | ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName()); |
---|
127 | result.addAll(choosenResources); |
---|
128 | map.put(ResourceParameterName.PROCESSINGELEMENTS, result); |
---|
129 | |
---|
130 | } |
---|
131 | int memoryRequest; |
---|
132 | try { |
---|
133 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
134 | } catch (NoSuchFieldException e) { |
---|
135 | memoryRequest = 0; |
---|
136 | } |
---|
137 | if (memoryRequest != 0) { |
---|
138 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
139 | |
---|
140 | Memory memory = null; |
---|
141 | for (ComputingNode node : nodes) { |
---|
142 | |
---|
143 | if (node.getFreeMemory() >= memoryRequest) { |
---|
144 | memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
145 | } else |
---|
146 | return null; |
---|
147 | } |
---|
148 | map.put(ResourceParameterName.MEMORY, memory); |
---|
149 | } |
---|
150 | |
---|
151 | return map; |
---|
152 | } |
---|
153 | |
---|
154 | private String chooseProviderForExecution(ResourceManagerInterface unitsManager) { |
---|
155 | List<ComputingResource> processingElements; |
---|
156 | Properties properties = new Properties(); |
---|
157 | properties.setProperty("type", ResourceType.COMPUTING_NODE.toString()); |
---|
158 | // properties.setProperty("status", ResourceStatus.FREE.toString()); |
---|
159 | processingElements = (List<ComputingResource>) unitsManager.filterResources(properties); |
---|
160 | return processingElements.get(0).getName(); |
---|
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 | } |
---|