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

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