source: DCWoRMS/trunk/src/test/article/recs/plugins/scheduling/RecsExclusivenessDFSSP.java @ 679

Revision 679, 5.0 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.article.recs.plugins.scheduling;
2
3import gridsim.dcworms.DCWormsTags;
4
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9import java.util.Random;
10
11import schedframe.events.scheduling.SchedulingEvent;
12import schedframe.resources.ResourceStatus;
13import schedframe.resources.computing.ComputingNode;
14import schedframe.resources.computing.ComputingResource;
15import schedframe.resources.computing.Core;
16import schedframe.resources.computing.Processor;
17import schedframe.resources.units.ProcessingElements;
18import schedframe.resources.units.ResourceUnit;
19import schedframe.resources.units.ResourceUnitName;
20import schedframe.resources.units.StandardResourceUnitName;
21import schedframe.scheduling.manager.resources.ClusterResourceManager;
22import schedframe.scheduling.manager.resources.ResourceManager;
23import schedframe.scheduling.manager.tasks.JobRegistry;
24import schedframe.scheduling.plan.SchedulingPlanInterface;
25import schedframe.scheduling.plan.impl.SchedulingPlan;
26import schedframe.scheduling.plugin.grid.ModuleList;
27import schedframe.scheduling.queue.TaskQueue;
28import schedframe.scheduling.queue.TaskQueueList;
29import schedframe.scheduling.tasks.TaskInterface;
30import test.article.recs.utils.RecsProcessorPowerInterface;
31
32public class RecsExclusivenessDFSSP extends RecsSP {
33
34        private Random rand = new Random(5);
35       
36        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
37                        ResourceManager resManager, ModuleList modules) {
38
39                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
40                SchedulingPlan plan = new SchedulingPlan();
41                // our tasks are placed only in first queue (see
42                // BaseLocalSchedulingPlugin.placeJobsInQueues() method)
43                TaskQueue q = queues.get(0);
44                // choose the events types to serve.
45                // Different actions for different events are possible.
46                switch (event.getType()) {
47               
48                case START_TASK_EXECUTION:
49                case TASK_FINISHED:
50                        // check all tasks in queue
51                        for (int i = 0; i < q.size(); i++) {
52                                TaskInterface<?> task = q.get(i);
53                                initApplicationType(task);
54                               
55                                // if status of the tasks in READY
56                                if (task.getStatus() == DCWormsTags.READY) {
57
58                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
59                                        if (choosenResources  != null) {
60                                                addToSchedulingPlan(plan, task, choosenResources);
61                                        }
62                                }
63                        }
64                        adjustFrequency(resourceManager.getProcessors());
65                }
66                return plan;
67        }
68       
69        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
70                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
71
72                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
73               
74                List<ComputingNode> nodes = resourceManager.getComputingNodes();
75                List<ComputingNode> avNodes = filterNodes(nodes, task);
76                if(avNodes.size() == 0)
77                        return null;
78                ComputingNode node = randomNode(avNodes);
79
80                int cpuRequest;
81                try {
82                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
83                } catch (NoSuchFieldException e) {
84                        cpuRequest = 0;
85                }
86
87                if (cpuRequest != 0) {
88
89                        List<Core> cores = node.getProcessors().get(0).getCores();
90
91                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
92                        for (int i = 0; i < cores.size() && cpuRequest > 0; i++) {
93                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
94                                        choosenResources.add(cores.get(i));
95                                        cpuRequest--;
96                                }
97                        }
98                        if (cpuRequest > 0) {
99                                return null;
100                        }
101                        ProcessingElements pe = new ProcessingElements();
102                        pe.addAll(choosenResources);
103                        map.put(StandardResourceUnitName.PE, pe);
104                        return map;
105                }
106                return null;
107        }
108       
109        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
110                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
111                for (ComputingNode node : nodes) {
112                        int cpuRequest;
113                        try {
114                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
115                        } catch (NoSuchFieldException e) {
116                                cpuRequest = 0;
117                        }
118
119                        if (cpuRequest != 0) {
120
121                                List<Core> cores = node.getProcessors().get(0).getCores();
122                                if (cores.size() < cpuRequest) {
123                                        continue;
124                                }
125
126                                int freeCores = 0;
127                                for(Core core: cores){
128                                        if(core.getStatus() == ResourceStatus.FREE)
129                                                freeCores++;
130                                }
131                               
132                                if(freeCores != cores.size())
133                                        continue;
134                               
135                                filteredNodes.add(node);
136                        }
137                }
138               
139                return filteredNodes;
140        }
141       
142        private ComputingNode randomNode(List<ComputingNode> nodes){
143                return nodes.get(rand.nextInt(nodes.size()));
144        }
145       
146       
147        private void adjustFrequency(List<Processor> processors){
148
149                for(Processor cpu: processors){
150                        RecsProcessorPowerInterface rppi = (RecsProcessorPowerInterface) cpu.getPowerInterface();
151                        int freeCores = 0;
152
153                        for(Core core: cpu.getCores()){
154                                if(core.getStatus() == ResourceStatus.FREE)
155                                        freeCores++;
156                        }
157                        if(freeCores == cpu.getCores().size())
158                                rppi.setPState(rppi.getHighestPState().getName());
159                        else
160                                rppi.setPState(rppi.getLowestPState().getName());
161
162                }
163        }
164
165}
Note: See TracBrowser for help on using the repository browser.