source: DCWoRMS/branches/coolemall/src/test/drs_tst/recs/plugins/scheduling/RecsExclusivenessDFSSP.java @ 1399

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