source: DCWoRMS/trunk/src/test/article2/recs/plugins/scheduling/exp2/RecsOut2InSP.java @ 826

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