source: DCWoRMS/trunk/src/test/article2/recs/plugins/scheduling/exp1/RecsInOffSP.java @ 826

Revision 826, 6.4 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.article2.recs.plugins.scheduling.exp1;
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;
12
13import schedframe.events.scheduling.SchedulingEvent;
14import schedframe.resources.ResourceStatus;
15import schedframe.resources.computing.ComputingNode;
16import schedframe.resources.computing.ComputingResource;
17import schedframe.resources.computing.Core;
18import schedframe.resources.computing.Processor;
19import schedframe.resources.computing.profiles.energy.airthroughput.StandardAirThroughputStateName;
20import schedframe.resources.computing.profiles.energy.airthroughput.UserAirThroughputStateName;
21import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName;
22import schedframe.resources.units.ProcessingElements;
23import schedframe.resources.units.ResourceUnit;
24import schedframe.resources.units.ResourceUnitName;
25import schedframe.resources.units.StandardResourceUnitName;
26import schedframe.scheduling.manager.resources.ClusterResourceManager;
27import schedframe.scheduling.manager.resources.ResourceManager;
28import schedframe.scheduling.manager.tasks.JobRegistry;
29import schedframe.scheduling.manager.tasks.JobRegistryImpl;
30import schedframe.scheduling.plan.SchedulingPlanInterface;
31import schedframe.scheduling.plan.impl.SchedulingPlan;
32import schedframe.scheduling.plugin.grid.ModuleList;
33import schedframe.scheduling.queue.TaskQueue;
34import schedframe.scheduling.queue.TaskQueueList;
35import schedframe.scheduling.tasks.TaskInterface;
36import test.article2.recs.plugins.scheduling.RecsSP;
37
38public class RecsInOffSP extends RecsSP {
39
40        private static int START_ID = 9;
41        private static int END_ID = 17;
42       
43        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
44                        ResourceManager resManager, ModuleList modules) {
45
46                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
47                SchedulingPlan plan = new SchedulingPlan();
48                // choose the events types to serve.
49                // Different actions for different events are possible.
50                switch (event.getType()) {
51                case START_TASK_EXECUTION:
52                case TASK_FINISHED:
53                //case TIMER:
54                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
55                        TaskQueue q = queues.get(0);
56
57                        List<ComputingNode> notSelectedNodes = resourceManager.getComputingNodes();
58                        // check all tasks in queue
59                        for (int i = 0; i < q.size(); i++) {
60                                TaskInterface<?> task = q.get(i);
61                                // if status of the tasks in READY
62                                if (task.getStatus() == DCWormsTags.READY) {
63                                        ComputingNode node = chooseProvider(resourceManager, task);
64                                        if (node != null) {
65                                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON);
66                                                notSelectedNodes.remove(node);
67                                                addToSchedulingPlan(plan, task, chooseResourcesForExecution(node, task));
68                                        }
69                                }
70                        }
71                        turnOffIdleNodes(resourceManager.getComputingNodes());
72                        adjustOtherFans(notSelectedNodes);
73                        break;
74                }
75                return plan;
76        }
77       
78        private ComputingNode chooseProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) {
79                List<ComputingNode> nodes = filterNodes(resourceManager.getComputingNodes(), task);
80                for(ComputingNode node: nodes){
81                        Integer id = Integer.parseInt(node.getName().split("_")[1]);
82                        if((id >= START_ID  && id <= END_ID)){
83                                if(node.getStatus() == ResourceStatus.UNAVAILABLE) {
84                                        node.getPowerInterface().setPowerState(StandardPowerStateName.ON);
85                                }
86                                return node;
87                        }
88
89                }
90                return null;
91        }
92
93        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
94                        ComputingNode node, TaskInterface<?> task) {
95
96                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
97
98                int cpuRequest;
99                try {
100                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
101                } catch (NoSuchFieldException e) {
102                        cpuRequest = 0;
103                }
104
105                if (cpuRequest != 0) {
106
107                        List<Core> cores = node.getProcessors().get(0).getCores();
108
109                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
110                        for (int i = 0; i < cores.size(); i++) {
111                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
112                                        //choosenResources.add(cores.get(i));
113                                        cpuRequest--;
114                                }
115                        }
116                        if (cpuRequest > 0) {
117                                //return null;
118                        }
119                        choosenResources.add(node.getProcessors().get(0));
120                        ProcessingElements pe = new ProcessingElements();
121                        pe.addAll(choosenResources);
122                        map.put(StandardResourceUnitName.PE, pe);
123                        return map;
124                }
125                return null;
126        }
127       
128        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
129                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
130                for (ComputingNode node : nodes) {
131                        int cpuRequest;
132                        try {
133                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
134                        } catch (NoSuchFieldException e) {
135                                cpuRequest = 0;
136                        }
137
138                        if (cpuRequest != 0) {
139
140                                List<Core> cores = node.getProcessors().get(0).getCores();
141                                if (cores.size() < cpuRequest) {
142                                        continue;
143                                }
144
145                                int freeCores = 0;
146                                for(Core core: cores){
147                                        if(core.getStatus() == ResourceStatus.FREE || core.getStatus() == ResourceStatus.UNAVAILABLE)
148                                                freeCores++;
149                                }
150                               
151                                if(freeCores != cores.size())
152                                        continue;
153                               
154                                try {
155                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
156                                                continue;
157                                } catch (FileNotFoundException e) {
158                                        continue;
159                                } catch (IOException e) {
160                                        continue;
161                                } catch (MissingResourceException e){
162                                        continue;
163                                }
164                                filteredNodes.add(node);
165                        }
166                }
167               
168                return filteredNodes;
169        }
170
171        private void turnOffIdleNodes(List<ComputingNode> nodes){
172                for(ComputingNode node : nodes){
173                        Processor proc = node.getProcessors().get(0);
174                        int freeCores = 0;
175                        for(Core core: proc.getCores()){
176                                if(core.getStatus() == ResourceStatus.FREE)
177                                        freeCores++;
178                        }
179                       
180                        if(freeCores == proc.getCores().size())
181                                node.getPowerInterface().setPowerState(StandardPowerStateName.OFF);
182                }
183        }
184       
185        private void adjustOtherFans(List<ComputingNode> nodes){
186                for(ComputingNode node : nodes){
187                        if(node.getFreeProcessorsNumber() == node.getProcessorsNumber()){
188                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_OFF);
189                        } else {
190                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON);
191                        }
192                }
193        }
194}
Note: See TracBrowser for help on using the repository browser.