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

Revision 708, 5.3 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.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.ComputingNode;
17import schedframe.resources.computing.ComputingResource;
18import schedframe.resources.computing.Core;
19import schedframe.resources.computing.Processor;
20import schedframe.resources.units.ProcessingElements;
21import schedframe.resources.units.ResourceUnit;
22import schedframe.resources.units.ResourceUnitName;
23import schedframe.resources.units.StandardResourceUnitName;
24import schedframe.scheduling.manager.resources.ClusterResourceManager;
25import schedframe.scheduling.manager.resources.ResourceManager;
26import schedframe.scheduling.manager.tasks.JobRegistry;
27import schedframe.scheduling.plan.SchedulingPlanInterface;
28import schedframe.scheduling.plan.impl.SchedulingPlan;
29import schedframe.scheduling.plugin.grid.ModuleList;
30import schedframe.scheduling.queue.TaskQueue;
31import schedframe.scheduling.queue.TaskQueueList;
32import schedframe.scheduling.tasks.TaskInterface;
33import test.article.recs.utils.RecsProcessorPowerInterface;
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<ComputingNode> nodes = resourceManager.getComputingNodes();
78                List<ComputingNode> avNodes = filterNodes(nodes, task);
79                if(avNodes.size() == 0)
80                        return null;
81                ComputingNode 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() && cpuRequest > 0; i++) {
96                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
97                                        choosenResources.add(cores.get(i));
98                                        cpuRequest--;
99                                }
100                        }
101                        if (cpuRequest > 0) {
102                                return null;
103                        }
104                        ProcessingElements pe = new ProcessingElements();
105                        pe.addAll(choosenResources);
106                        map.put(StandardResourceUnitName.PE, pe);
107                        return map;
108                }
109                return null;
110        }
111       
112        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
113                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
114                for (ComputingNode node : nodes) {
115                        int cpuRequest;
116                        try {
117                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
118                        } catch (NoSuchFieldException e) {
119                                cpuRequest = 0;
120                        }
121
122                        if (cpuRequest != 0) {
123
124                                List<Core> cores = node.getProcessors().get(0).getCores();
125                                if (cores.size() < cpuRequest) {
126                                        continue;
127                                }
128
129                                int freeCores = 0;
130                                for(Core core: cores){
131                                        if(core.getStatus() == ResourceStatus.FREE)
132                                                freeCores++;
133                                }
134                               
135                                if(freeCores != cores.size())
136                                        continue;
137                                try {
138                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
139                                                continue;
140                                } catch (FileNotFoundException e) {
141                                        continue;
142                                } catch (IOException e) {
143                                        continue;
144                                } catch (MissingResourceException e){
145                                        continue;
146                                }
147                               
148                                filteredNodes.add(node);
149                        }
150                }
151               
152                return filteredNodes;
153        }
154       
155        private ComputingNode randomNode(List<ComputingNode> nodes){
156                return nodes.get(rand.nextInt(nodes.size()));
157        }
158       
159       
160        private void adjustFrequency(List<Processor> processors){
161
162                for(Processor cpu: processors){
163                        RecsProcessorPowerInterface rppi = (RecsProcessorPowerInterface) cpu.getPowerInterface();
164                        int freeCores = 0;
165
166                        for(Core core: cpu.getCores()){
167                                if(core.getStatus() == ResourceStatus.FREE)
168                                        freeCores++;
169                        }
170                        if(freeCores == cpu.getCores().size())
171                                rppi.setPState(rppi.getHighestPState().getName());
172                        else
173                                rppi.setPState(rppi.getHighestPState().getName());
174
175                }
176        }
177
178}
Note: See TracBrowser for help on using the repository browser.