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