source: DCWoRMS/branches/coolemall/src/test/fips/models/xeon_fpga/FCFSBF_RandomPlugin.java @ 1600

Revision 1600, 4.9 KB checked in by wojtekp, 8 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.fips.models.xeon_fpga;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Random;
8
9import dcworms.schedframe.scheduling.ExecTask;
10import schedframe.events.scheduling.SchedulingEvent;
11import schedframe.resources.ResourceStatus;
12import schedframe.resources.StandardResourceType;
13import schedframe.resources.computing.ComputingResource;
14import schedframe.resources.computing.Core;
15import schedframe.resources.computing.Node;
16import schedframe.resources.computing.Processor;
17import schedframe.resources.computing.profiles.energy.ResourceEvent;
18import schedframe.resources.computing.profiles.energy.ResourceEventType;
19import schedframe.resources.units.ProcessingElements;
20import schedframe.resources.units.ResourceUnit;
21import schedframe.resources.units.ResourceUnitName;
22import schedframe.resources.units.StandardResourceUnitName;
23import schedframe.scheduling.manager.resources.ClusterResourceManager;
24import schedframe.scheduling.manager.resources.ResourceManager;
25import schedframe.scheduling.manager.tasks.JobRegistry;
26import schedframe.scheduling.plan.SchedulingPlanInterface;
27import schedframe.scheduling.plan.impl.SchedulingPlan;
28import schedframe.scheduling.plugin.ModuleList;
29import schedframe.scheduling.queue.TaskQueue;
30import schedframe.scheduling.queue.TaskQueueList;
31import schedframe.scheduling.tasks.TaskInterface;
32import simulator.DataCenterWorkloadSimulator;
33import eduni.simjava.Sim_system;
34import example.localplugin.BaseLocalSchedulingPlugin;
35import gridsim.dcworms.DCWormsTags;
36
37public 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
Note: See TracBrowser for help on using the repository browser.