1 | package test.fips.models.xeon_fpga; |
---|
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 dcworms.schedframe.scheduling.ExecTask; |
---|
10 | import schedframe.events.scheduling.SchedulingEvent; |
---|
11 | import schedframe.resources.ResourceStatus; |
---|
12 | import schedframe.resources.StandardResourceType; |
---|
13 | import schedframe.resources.computing.ComputingResource; |
---|
14 | import schedframe.resources.computing.Core; |
---|
15 | import schedframe.resources.computing.Node; |
---|
16 | import schedframe.resources.computing.Processor; |
---|
17 | import schedframe.resources.computing.profiles.energy.ResourceEvent; |
---|
18 | import schedframe.resources.computing.profiles.energy.ResourceEventType; |
---|
19 | import schedframe.resources.units.ProcessingElements; |
---|
20 | import schedframe.resources.units.ResourceUnit; |
---|
21 | import schedframe.resources.units.ResourceUnitName; |
---|
22 | import schedframe.resources.units.StandardResourceUnitName; |
---|
23 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
24 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
25 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
26 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
27 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
28 | import schedframe.scheduling.plugin.ModuleList; |
---|
29 | import schedframe.scheduling.queue.TaskQueue; |
---|
30 | import schedframe.scheduling.queue.TaskQueueList; |
---|
31 | import schedframe.scheduling.tasks.TaskInterface; |
---|
32 | import simulator.DataCenterWorkloadSimulator; |
---|
33 | import eduni.simjava.Sim_system; |
---|
34 | import example.localplugin.BaseLocalSchedulingPlugin; |
---|
35 | import gridsim.dcworms.DCWormsTags; |
---|
36 | |
---|
37 | public class FCFSBF_RandomPlugin extends BaseLocalSchedulingPlugin { |
---|
38 | |
---|
39 | private Random rand; |
---|
40 | |
---|
41 | public FCFSBF_RandomPlugin() { |
---|
42 | rand = new Random(175); |
---|
43 | } |
---|
44 | |
---|
45 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
46 | ResourceManager resManager, ModuleList modules) { |
---|
47 | |
---|
48 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
49 | List<Node> nodes = resourceManager.getNodes(); |
---|
50 | SchedulingPlan plan = new SchedulingPlan(); |
---|
51 | |
---|
52 | // choose the events types to serve. |
---|
53 | // Different actions for different events are possible. |
---|
54 | switch (event.getType()) { |
---|
55 | |
---|
56 | |
---|
57 | case START_TASK_EXECUTION: |
---|
58 | case TASK_FINISHED: |
---|
59 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
60 | TaskQueue q = queues.get(0); |
---|
61 | // check all tasks in queue |
---|
62 | for (int i = 0; i < q.size(); i++) { |
---|
63 | TaskInterface<?> task = q.get(i); |
---|
64 | // if status of the tasks in READY |
---|
65 | if (task.getStatus() == DCWormsTags.READY) { |
---|
66 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(nodes, task); |
---|
67 | if (choosenResources != null) { |
---|
68 | addToSchedulingPlan(plan, task, choosenResources); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | break; |
---|
74 | |
---|
75 | } |
---|
76 | return plan; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
81 | List<Node> nodes, TaskInterface<?> task) { |
---|
82 | |
---|
83 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(1); |
---|
84 | |
---|
85 | int cpuRequest; |
---|
86 | try { |
---|
87 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
88 | } catch (NoSuchFieldException e) { |
---|
89 | cpuRequest = 0; |
---|
90 | } |
---|
91 | |
---|
92 | List<Node> filteredNodes = filterNodes(nodes, cpuRequest); |
---|
93 | if(filteredNodes.size()==0) |
---|
94 | return null; |
---|
95 | Node node = chooseRandomNode(filteredNodes); |
---|
96 | while(node.getFreeProcessors().size() < cpuRequest){ |
---|
97 | node = chooseRandomNode(nodes); |
---|
98 | } |
---|
99 | |
---|
100 | if (cpuRequest != 0) { |
---|
101 | |
---|
102 | if (node.getFreeProcessors().size() < cpuRequest) { |
---|
103 | return null; |
---|
104 | } |
---|
105 | |
---|
106 | List<Processor> cpus = node.getProcessors(); |
---|
107 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(cpuRequest); |
---|
108 | for (int i = 0; i < cpus.size() && cpuRequest > 0; i++) { |
---|
109 | if (cpus.get(i).getStatus() == ResourceStatus.FREE) { |
---|
110 | choosenResources.add(cpus.get(i)); |
---|
111 | cpuRequest--; |
---|
112 | } |
---|
113 | } |
---|
114 | if (cpuRequest > 0) { |
---|
115 | return null; |
---|
116 | } |
---|
117 | |
---|
118 | ProcessingElements pe = new ProcessingElements(); |
---|
119 | pe.addAll(choosenResources); |
---|
120 | map.put(StandardResourceUnitName.PE, pe); |
---|
121 | return map; |
---|
122 | } |
---|
123 | |
---|
124 | return null; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | private List<Node> filterNodes(List<Node> nodes, int cpuRequest){ |
---|
129 | List<Node> filteredNodes = new ArrayList<Node>(); |
---|
130 | |
---|
131 | for (Node node : nodes) { |
---|
132 | |
---|
133 | if (cpuRequest != 0) { |
---|
134 | |
---|
135 | List<Processor> cpus = node.getProcessors(); |
---|
136 | if (cpus.size() < cpuRequest) { |
---|
137 | if(cpus.size() == 0){ |
---|
138 | if(node.getProcessors().size() < cpuRequest) |
---|
139 | continue; |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | int freeCpus = 0; |
---|
144 | for(Processor cpu: cpus){ |
---|
145 | if(cpu.getStatus() == ResourceStatus.FREE) |
---|
146 | freeCpus++; |
---|
147 | } |
---|
148 | |
---|
149 | if(freeCpus < cpuRequest) |
---|
150 | continue; |
---|
151 | |
---|
152 | filteredNodes.add(node); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | return filteredNodes; |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | private Node chooseRandomNode(List<Node> nodes) { |
---|
161 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
162 | return nodes.get(nodeIdx); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | } |
---|
167 | |
---|