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

Revision 708, 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() && cpuRequest > 0; 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                        ProcessingElements pe = new ProcessingElements();
103                        pe.addAll(choosenResources);
104                        map.put(StandardResourceUnitName.PE, pe);
105                        return map;
106                }
107                return null;
108        }
109       
110        private List<ComputingNode> filterNodes(List<ComputingNode> nodes, TaskInterface<?> task){
111                List<ComputingNode> filteredNodes = new ArrayList<ComputingNode>();
112                for (ComputingNode node : nodes) {
113                        int cpuRequest;
114                        try {
115                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
116                        } catch (NoSuchFieldException e) {
117                                cpuRequest = 0;
118                        }
119
120                        if (cpuRequest != 0) {
121
122                                List<Core> cores = node.getProcessors().get(0).getCores();
123                                if (cores.size() < cpuRequest) {
124                                        continue;
125                                }
126
127                                int freeCores = 0;
128                                for(Core core: cores){
129                                        if(core.getStatus() == ResourceStatus.FREE)
130                                                freeCores++;
131                                }
132                               
133                                if(freeCores != cores.size())
134                                        continue;
135                               
136                                try {
137                                        if(!getExecutiveness(createExecutivenessQuery(task, node)))
138                                                continue;
139                                } catch (FileNotFoundException e) {
140                                        continue;
141                                } catch (IOException e) {
142                                        continue;
143                                } catch (MissingResourceException e){
144                                        continue;
145                                }
146                                filteredNodes.add(node);
147                        }
148                }
149               
150                return filteredNodes;
151        }
152       
153        private ComputingNode randomNode(List<ComputingNode> nodes){
154                return nodes.get(rand.nextInt(nodes.size()));
155        }
156}
Note: See TracBrowser for help on using the repository browser.