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

Revision 826, 6.0 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 RecsIn2OutSP 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
86                return null;
87        }
88
89        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
90                        ComputingNode node, TaskInterface<?> task) {
91
92                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
93
94                int cpuRequest;
95                try {
96                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
97                } catch (NoSuchFieldException e) {
98                        cpuRequest = 0;
99                }
100
101                if (cpuRequest != 0) {
102
103                        List<Core> cores = node.getProcessors().get(0).getCores();
104
105                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
106                        for (int i = 0; i < cores.size(); i++) {
107                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
108                                        //choosenResources.add(cores.get(i));
109                                        cpuRequest--;
110                                }
111                        }
112                        if (cpuRequest > 0) {
113                                //return null;
114                        }
115                        choosenResources.add(node.getProcessors().get(0));
116                        ProcessingElements pe = new ProcessingElements();
117                        pe.addAll(choosenResources);
118                        map.put(StandardResourceUnitName.PE, pe);
119                        return map;
120                }
121                return null;
122        }
123       
124        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
125                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
126                for (ComputingNode node : nodes) {
127                        int cpuRequest;
128                        try {
129                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
130                        } catch (NoSuchFieldException e) {
131                                cpuRequest = 0;
132                        }
133
134                        if (cpuRequest != 0) {
135
136                                List<Core> cores = node.getProcessors().get(0).getCores();
137                                if (cores.size() < cpuRequest) {
138                                        continue;
139                                }
140
141                                int freeCores = 0;
142                                for(Core core: cores){
143                                        if(core.getStatus() == ResourceStatus.FREE)
144                                                freeCores++;
145                                }
146                               
147                                if(freeCores != cores.size())
148                                        continue;
149                               
150                                try {
151                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
152                                                continue;
153                                } catch (FileNotFoundException e) {
154                                        continue;
155                                } catch (IOException e) {
156                                        continue;
157                                } catch (MissingResourceException e){
158                                        continue;
159                                }
160                                filteredNodes.add(node);
161                        }
162                }
163               
164                return filteredNodes;
165        }
166       
167        private void adjustOtherFans(List<ComputingNode> nodes){
168                for(ComputingNode node : nodes){
169                        if(node.getFreeProcessorsNumber() == node.getProcessorsNumber()){
170                                //node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_OFF);
171                        } else {
172                                node.getAirThroughputInterface().setAirThroughputState(StandardAirThroughputStateName.FAN_ON);
173                        }
174                }
175        }
176       
177        class ResourceIdComparator implements Comparator<ComputingNode> {
178
179                public int compare(ComputingNode node1, ComputingNode node2) {
180                        Integer id1 = Integer.parseInt(node1.getName().split("_")[1]);
181                        Integer id2 = Integer.parseInt(node2.getName().split("_")[1]);
182                        if (id1 > id2)
183                                return 1;
184                        else if (id1 < id2)
185                                return -1;
186                        else
187                                return 0;
188
189                }
190        }
191       
192}
193
194
Note: See TracBrowser for help on using the repository browser.