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

Revision 1600, 4.6 KB checked in by wojtekp, 8 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.fips.models.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<Processor> cpus = resourceManager.getProcessors();
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(cpus, 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<Processor> cpus, 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
93
94               
95                if (cpuRequest != 0) {
96
97
98
99                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(cpuRequest);                               
100                        for (int i = 0; i < cpus.size() && cpuRequest > 0; i++) {
101                                if (cpus.get(i).getStatus() == ResourceStatus.FREE) {
102                                        choosenResources.add(cpus.get(i));
103                                        cpuRequest--;
104                                }
105                        }
106                        if (cpuRequest > 0) {
107                                return null;
108                        }
109
110                        ProcessingElements pe = new ProcessingElements();
111                        pe.addAll(choosenResources);
112                        map.put(StandardResourceUnitName.PE, pe);
113                        return map;
114                }
115
116                return null;
117        }
118
119
120        private List<Node> filterNodes(List<Node> nodes, int cpuRequest){
121                List<Node> filteredNodes = new ArrayList<Node>();
122
123                for (Node node : nodes) {
124
125                        if (cpuRequest != 0) {
126
127                                List<Processor> cpus = node.getProcessors();
128                                if (cpus.size() < cpuRequest) {
129                                        if(cpus.size() == 0){
130                                                if(node.getProcessors().size() < cpuRequest)
131                                                        continue;
132                                        }
133                                }
134
135                                int freeCpus = 0;
136                                for(Processor cpu: cpus){
137                                        if(cpu.getStatus() == ResourceStatus.FREE)
138                                                freeCpus++;
139                                }
140                               
141                                if(freeCpus < cpuRequest)
142                                        continue;
143                               
144                                filteredNodes.add(node);
145                        }
146                }
147               
148                return filteredNodes;
149        }
150       
151
152        private Node chooseRandomNode(List<Node> nodes) {
153                int nodeIdx = rand.nextInt(nodes.size());
154                return nodes.get(nodeIdx);
155        }
156
157
158}
159
Note: See TracBrowser for help on using the repository browser.