source: DCWoRMS/trunk/src/test/article/recs/plugins/scheduling/RecsExclusivenessRandomSP.java @ 734

Revision 734, 4.7 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.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;
32
33public class RecsExclusivenessRandomSP extends RecsSP {
34
35        private Random rand = new Random(5);
36       
37        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
38                        ResourceManager resManager, ModuleList modules) {
39
40                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
41                SchedulingPlan plan = new SchedulingPlan();
42                // choose the events types to serve.
43                // Different actions for different events are possible.
44                switch (event.getType()) {
45                case START_TASK_EXECUTION:
46                case TASK_FINISHED:
47                //case TIMER:
48                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
49                        TaskQueue q = queues.get(0);
50                        // check all tasks in queue
51
52                        for (int i = 0; i < q.size(); i++) {
53                                TaskInterface<?> task = q.get(i);
54                                initApplicationType(task);
55                               
56                                // if status of the tasks in READY
57                                if (task.getStatus() == DCWormsTags.READY) {
58                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
59                                        if (choosenResources != null) {
60                                                addToSchedulingPlan(plan, task, choosenResources);
61                                        }
62                                }
63                        }
64
65                        break;
66                }
67                return plan;
68        }
69
70        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
71                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
72
73                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
74               
75                List<ComputingNode> nodes = resourceManager.getComputingNodes();
76                List<ComputingNode> avNodes = filterNodes(nodes, task);
77                if(avNodes.size() == 0)
78                        return null;
79                ComputingNode node = randomNode(avNodes);
80
81                int cpuRequest;
82                try {
83                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
84                } catch (NoSuchFieldException e) {
85                        cpuRequest = 0;
86                }
87
88                if (cpuRequest != 0) {
89
90                        List<Core> cores = node.getProcessors().get(0).getCores();
91
92                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
93                        for (int i = 0; i < cores.size(); i++) {
94                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
95                                        //choosenResources.add(cores.get(i));
96                                        cpuRequest--;
97                                }
98                        }
99                        if (cpuRequest > 0) {
100                                //return null;
101                        }
102                        choosenResources.add(node);
103                        ProcessingElements pe = new ProcessingElements();
104                        pe.addAll(choosenResources);
105                        map.put(StandardResourceUnitName.PE, pe);
106                        return map;
107                }
108                return null;
109        }
110       
111        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
112                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
113                for (ComputingNode node : nodes) {
114                        int cpuRequest;
115                        try {
116                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
117                        } catch (NoSuchFieldException e) {
118                                cpuRequest = 0;
119                        }
120
121                        if (cpuRequest != 0) {
122
123                                List<Core> cores = node.getProcessors().get(0).getCores();
124                                if (cores.size() < cpuRequest) {
125                                        continue;
126                                }
127
128                                int freeCores = 0;
129                                for(Core core: cores){
130                                        if(core.getStatus() == ResourceStatus.FREE)
131                                                freeCores++;
132                                }
133                               
134                                if(freeCores != cores.size())
135                                        continue;
136                               
137                                try {
138                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
139                                                continue;
140                                } catch (FileNotFoundException e) {
141                                        continue;
142                                } catch (IOException e) {
143                                        continue;
144                                } catch (MissingResourceException e){
145                                        continue;
146                                }
147                                filteredNodes.add(node);
148                        }
149                }
150               
151                return filteredNodes;
152        }
153       
154        private ComputingNode randomNode(List<ComputingNode> nodes){
155                return nodes.get(rand.nextInt(nodes.size()));
156        }
157}
Note: See TracBrowser for help on using the repository browser.