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

Revision 826, 5.5 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.profiles.energy.airthroughput.StandardAirThroughputStateName;
19import schedframe.resources.units.ProcessingElements;
20import schedframe.resources.units.ResourceUnit;
21import schedframe.resources.units.ResourceUnitName;
22import schedframe.resources.units.StandardResourceUnitName;
23import schedframe.scheduling.manager.resources.ClusterResourceManager;
24import schedframe.scheduling.manager.resources.ResourceManager;
25import schedframe.scheduling.manager.tasks.JobRegistry;
26import schedframe.scheduling.plan.SchedulingPlanInterface;
27import schedframe.scheduling.plan.impl.SchedulingPlan;
28import schedframe.scheduling.plugin.grid.ModuleList;
29import schedframe.scheduling.queue.TaskQueue;
30import schedframe.scheduling.queue.TaskQueueList;
31import schedframe.scheduling.tasks.TaskInterface;
32import test.article2.recs.plugins.scheduling.RecsSP;
33
34public class RecsInIdleSP extends RecsSP {
35
36        private static int START_ID = 9;
37        private static int END_ID = 17;
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
53                        List<ComputingNode> notSelectedNodes = resourceManager.getComputingNodes();
54                        // check all tasks in queue
55                        for (int i = 0; i < q.size(); i++) {
56                                TaskInterface<?> task = q.get(i);
57                                // if status of the tasks in READY
58                                if (task.getStatus() == DCWormsTags.READY) {
59                                        ComputingNode node = chooseProvider(resourceManager, task);
60                                        if (node != null) {
61                                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON);
62                                                notSelectedNodes.remove(node);
63                                                addToSchedulingPlan(plan, task, chooseResourcesForExecution(node, task));
64                                        }
65                                }
66                        }
67                        adjustOtherFans(notSelectedNodes);
68                        break;
69                }
70                return plan;
71        }
72       
73        private ComputingNode chooseProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) {
74                List<ComputingNode> nodes = filterNodes(resourceManager.getComputingNodes(), task);
75                for(ComputingNode node: nodes){
76                        Integer id = Integer.parseInt(node.getName().split("_")[1]);
77                        if((id >= START_ID  && id <= END_ID)){
78                                return node;
79                        }
80
81                }
82                return null;
83        }
84
85        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
86                        ComputingNode node, TaskInterface<?> task) {
87
88                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
89
90                int cpuRequest;
91                try {
92                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
93                } catch (NoSuchFieldException e) {
94                        cpuRequest = 0;
95                }
96
97                if (cpuRequest != 0) {
98
99                        List<Core> cores = node.getProcessors().get(0).getCores();
100
101                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
102                        for (int i = 0; i < cores.size(); i++) {
103                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
104                                        //choosenResources.add(cores.get(i));
105                                        cpuRequest--;
106                                }
107                        }
108                        if (cpuRequest > 0) {
109                                //return null;
110                        }
111                        choosenResources.add(node.getProcessors().get(0));
112                        ProcessingElements pe = new ProcessingElements();
113                        pe.addAll(choosenResources);
114                        map.put(StandardResourceUnitName.PE, pe);
115                        return map;
116                }
117                return null;
118        }
119       
120        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
121                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
122                for (ComputingNode node : nodes) {
123                        int cpuRequest;
124                        try {
125                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
126                        } catch (NoSuchFieldException e) {
127                                cpuRequest = 0;
128                        }
129
130                        if (cpuRequest != 0) {
131
132                                List<Core> cores = node.getProcessors().get(0).getCores();
133                                if (cores.size() < cpuRequest) {
134                                        continue;
135                                }
136
137                                int freeCores = 0;
138                                for(Core core: cores){
139                                        if(core.getStatus() == ResourceStatus.FREE)
140                                                freeCores++;
141                                }
142                               
143                                if(freeCores != cores.size())
144                                        continue;
145                               
146                                try {
147                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
148                                                continue;
149                                } catch (FileNotFoundException e) {
150                                        continue;
151                                } catch (IOException e) {
152                                        continue;
153                                } catch (MissingResourceException e){
154                                        continue;
155                                }
156                                filteredNodes.add(node);
157                        }
158                }
159               
160                return filteredNodes;
161        }
162       
163        private void adjustOtherFans(List<ComputingNode> nodes){
164                for(ComputingNode node : nodes){
165                        if(node.getFreeProcessorsNumber() == node.getProcessorsNumber()){
166                                //node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_OFF);
167                        } else {
168                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON);
169                        }
170                }
171        }
172}
Note: See TracBrowser for help on using the repository browser.