1 | package test.article.recs.plugins.scheduling; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.HashMap; |
---|
5 | import java.util.List; |
---|
6 | import java.util.Map; |
---|
7 | import java.util.Random; |
---|
8 | |
---|
9 | import schedframe.events.scheduling.SchedulingEvent; |
---|
10 | import schedframe.resources.ResourceStatus; |
---|
11 | import schedframe.resources.computing.ComputingNode; |
---|
12 | import schedframe.resources.computing.ComputingResource; |
---|
13 | import schedframe.resources.computing.Core; |
---|
14 | import schedframe.resources.units.ProcessingElements; |
---|
15 | import schedframe.resources.units.ResourceUnit; |
---|
16 | import schedframe.resources.units.ResourceUnitName; |
---|
17 | import schedframe.resources.units.StandardResourceUnitName; |
---|
18 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
19 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
20 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
21 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
22 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
23 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
24 | import schedframe.scheduling.queue.TaskQueue; |
---|
25 | import schedframe.scheduling.queue.TaskQueueList; |
---|
26 | import schedframe.scheduling.tasks.TaskInterface; |
---|
27 | import example.localplugin.BaseLocalSchedulingPlugin; |
---|
28 | import gridsim.dcworms.DCWormsTags; |
---|
29 | |
---|
30 | public class RecsExclusivenessRandomSP extends BaseLocalSchedulingPlugin { |
---|
31 | |
---|
32 | private Random rand = new Random(5); |
---|
33 | |
---|
34 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
35 | ResourceManager resManager, ModuleList modules) { |
---|
36 | |
---|
37 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
38 | SchedulingPlan plan = new SchedulingPlan(); |
---|
39 | // choose the events types to serve. |
---|
40 | // Different actions for different events are possible. |
---|
41 | switch (event.getType()) { |
---|
42 | case START_TASK_EXECUTION: |
---|
43 | case TASK_FINISHED: |
---|
44 | //case TIMER: |
---|
45 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
46 | TaskQueue q = queues.get(0); |
---|
47 | // check all tasks in queue |
---|
48 | |
---|
49 | for (int i = 0; i < q.size(); i++) { |
---|
50 | TaskInterface<?> task = q.get(i); |
---|
51 | // if status of the tasks in READY |
---|
52 | if (task.getStatus() == DCWormsTags.READY) { |
---|
53 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
54 | if (choosenResources != null) { |
---|
55 | addToSchedulingPlan(plan, task, choosenResources); |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | break; |
---|
61 | } |
---|
62 | return plan; |
---|
63 | } |
---|
64 | |
---|
65 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
66 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
67 | |
---|
68 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(); |
---|
69 | |
---|
70 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
71 | List<ComputingNode> avNodes = filterNodes(nodes, task); |
---|
72 | if(avNodes.size() == 0) |
---|
73 | return null; |
---|
74 | ComputingNode node = randomNode(avNodes); |
---|
75 | |
---|
76 | int cpuRequest; |
---|
77 | try { |
---|
78 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
79 | } catch (NoSuchFieldException e) { |
---|
80 | cpuRequest = 0; |
---|
81 | } |
---|
82 | |
---|
83 | if (cpuRequest != 0) { |
---|
84 | |
---|
85 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
86 | |
---|
87 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(); |
---|
88 | for (int i = 0; i < cores.size() && cpuRequest > 0; i++) { |
---|
89 | if (cores.get(i).getStatus() == ResourceStatus.FREE) { |
---|
90 | choosenResources.add(cores.get(i)); |
---|
91 | cpuRequest--; |
---|
92 | } |
---|
93 | } |
---|
94 | if (cpuRequest > 0) { |
---|
95 | return null; |
---|
96 | } |
---|
97 | ProcessingElements pe = new ProcessingElements(); |
---|
98 | pe.addAll(choosenResources); |
---|
99 | map.put(StandardResourceUnitName.PE, pe); |
---|
100 | return map; |
---|
101 | } |
---|
102 | return null; |
---|
103 | } |
---|
104 | |
---|
105 | private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){ |
---|
106 | List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>(); |
---|
107 | for (ComputingNode node : nodes) { |
---|
108 | int cpuRequest; |
---|
109 | try { |
---|
110 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
111 | } catch (NoSuchFieldException e) { |
---|
112 | cpuRequest = 0; |
---|
113 | } |
---|
114 | |
---|
115 | if (cpuRequest != 0) { |
---|
116 | |
---|
117 | List<Core> cores = node.getProcessors().get(0).getCores(); |
---|
118 | if (cores.size() < cpuRequest) { |
---|
119 | continue; |
---|
120 | } |
---|
121 | |
---|
122 | int freeCores = 0; |
---|
123 | for(Core core: cores){ |
---|
124 | if(core.getStatus() == ResourceStatus.FREE) |
---|
125 | freeCores++; |
---|
126 | } |
---|
127 | |
---|
128 | if(freeCores != cores.size()) |
---|
129 | continue; |
---|
130 | |
---|
131 | filteredNodes.add(node); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | return filteredNodes; |
---|
136 | } |
---|
137 | |
---|
138 | private ComputingNode randomNode(List<ComputingNode> nodes){ |
---|
139 | return nodes.get(rand.nextInt(nodes.size())); |
---|
140 | } |
---|
141 | } |
---|