source: DCWoRMS/trunk/src/test/article/recs/plugins/scheduling/RecsExclusivenessRandomNodePowManSP.java @ 734

Revision 734, 5.5 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.computing.profiles.energy.power.StandardPowerStateName;
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.grid.ModuleList;
31import schedframe.scheduling.queue.TaskQueue;
32import schedframe.scheduling.queue.TaskQueueList;
33import schedframe.scheduling.tasks.TaskInterface;
34
35public class RecsExclusivenessRandomNodePowManSP 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                // choose the events types to serve.
45                // Different actions for different events are possible.
46                switch (event.getType()) {
47                case START_TASK_EXECUTION:
48                case TASK_FINISHED:
49                //case TIMER:
50                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
51                        TaskQueue q = queues.get(0);
52                        // check all tasks in queue
53
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                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
61                                        if (choosenResources != null) {
62                                                addToSchedulingPlan(plan, task, choosenResources);
63                                        }
64                                }
65                        }
66                        turnOffIdleNodes(resourceManager.getComputingNodes());
67                        break;
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                if(node.getStatus() == ResourceStatus.UNAVAILABLE) {
83                        node.getPowerInterface().setPowerState(StandardPowerStateName.ON);
84                }
85                int cpuRequest;
86                try {
87                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
88                } catch (NoSuchFieldException e) {
89                        cpuRequest = 0;
90                }
91
92                if (cpuRequest != 0) {
93
94                        List<Core> cores = node.getProcessors().get(0).getCores();
95
96                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
97                        for (int i = 0; i < cores.size(); i++) {
98                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
99                                        //choosenResources.add(cores.get(i));
100                                        cpuRequest--;
101                                }
102                        }
103                        choosenResources.add(node);
104                        if (cpuRequest > 0) {
105                                //return null;
106                        }
107                        ProcessingElements pe = new ProcessingElements();
108                        pe.addAll(choosenResources);
109                        map.put(StandardResourceUnitName.PE, pe);
110                        return map;
111                }
112                return null;
113        }
114       
115        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
116                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
117                for (ComputingNode node : nodes) {
118                        int cpuRequest;
119                        try {
120                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
121                        } catch (NoSuchFieldException e) {
122                                cpuRequest = 0;
123                        }
124
125                        if (cpuRequest != 0) {
126
127                                List<Core> cores = node.getProcessors().get(0).getCores();
128                                if (cores.size() < cpuRequest) {
129                                        continue;
130                                }
131
132                                int freeCores = 0;
133                                for(Core core: cores){
134                                        if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE)
135                                                freeCores++;
136                                }
137                               
138                                if(freeCores != cores.size())
139                                        continue;
140                               
141                                try {
142                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
143                                                continue;
144                                } catch (FileNotFoundException e) {
145                                        continue;
146                                } catch (IOException e) {
147                                        continue;
148                                } catch (MissingResourceException e){
149                                        continue;
150                                }
151                                filteredNodes.add(node);
152                        }
153                }
154               
155                return filteredNodes;
156        }
157       
158        private ComputingNode randomNode(List<ComputingNode> nodes){
159                return nodes.get(rand.nextInt(nodes.size()));
160        }
161       
162        private void turnOffIdleNodes(List<ComputingNode> nodes){
163                for(ComputingNode node : nodes){
164                        Processor proc = node.getProcessors().get(0);
165                        int freeCores = 0;
166                        for(Core core: proc.getCores()){
167                                if(core.getStatus() == ResourceStatus.FREE)
168                                        freeCores++;
169                        }
170                       
171                        if(freeCores == proc.getCores().size())
172                                node.getPowerInterface().setPowerState(StandardPowerStateName.OFF);
173                }
174        }
175
176}
Note: See TracBrowser for help on using the repository browser.