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

Revision 708, 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() && cpuRequest > 0; i++) {
98                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
99                                        choosenResources.add(cores.get(i));
100                                        cpuRequest--;
101                                }
102                        }
103                        if (cpuRequest > 0) {
104                                return null;
105                        }
106                        ProcessingElements pe = new ProcessingElements();
107                        pe.addAll(choosenResources);
108                        map.put(StandardResourceUnitName.PE, pe);
109                        return map;
110                }
111                return null;
112        }
113       
114        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
115                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
116                for (ComputingNode node : nodes) {
117                        int cpuRequest;
118                        try {
119                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
120                        } catch (NoSuchFieldException e) {
121                                cpuRequest = 0;
122                        }
123
124                        if (cpuRequest != 0) {
125
126                                List<Core> cores = node.getProcessors().get(0).getCores();
127                                if (cores.size() < cpuRequest) {
128                                        continue;
129                                }
130
131                                int freeCores = 0;
132                                for(Core core: cores){
133                                        if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE)
134                                                freeCores++;
135                                }
136                               
137                                if(freeCores != cores.size())
138                                        continue;
139                               
140                                try {
141                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
142                                                continue;
143                                } catch (FileNotFoundException e) {
144                                        continue;
145                                } catch (IOException e) {
146                                        continue;
147                                } catch (MissingResourceException e){
148                                        continue;
149                                }
150                                filteredNodes.add(node);
151                        }
152                }
153               
154                return filteredNodes;
155        }
156       
157        private ComputingNode randomNode(List<ComputingNode> nodes){
158                return nodes.get(rand.nextInt(nodes.size()));
159        }
160       
161        private void turnOffIdleNodes(List<ComputingNode> nodes){
162                for(ComputingNode node : nodes){
163                        Processor proc = node.getProcessors().get(0);
164                        int freeCores = 0;
165                        for(Core core: proc.getCores()){
166                                if(core.getStatus() == ResourceStatus.FREE)
167                                        freeCores++;
168                        }
169                       
170                        if(freeCores == proc.getCores().size())
171                                node.getPowerInterface().setPowerState(StandardPowerStateName.OFF);
172                }
173        }
174
175}
Note: See TracBrowser for help on using the repository browser.