source: DCWoRMS/trunk/src/test/article/recs/plugins/scheduling/RecsExclusivenessDFSSP.java @ 663

Revision 663, 5.0 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 java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Random;
8
9import schedframe.events.scheduling.SchedulingEvent;
10import schedframe.resources.ResourceStatus;
11import schedframe.resources.computing.ComputingNode;
12import schedframe.resources.computing.ComputingResource;
13import schedframe.resources.computing.Core;
14import schedframe.resources.computing.Processor;
15import schedframe.resources.units.ProcessingElements;
16import schedframe.resources.units.ResourceUnit;
17import schedframe.resources.units.ResourceUnitName;
18import schedframe.resources.units.StandardResourceUnitName;
19import schedframe.scheduling.manager.resources.ClusterResourceManager;
20import schedframe.scheduling.manager.resources.ResourceManager;
21import schedframe.scheduling.manager.tasks.JobRegistry;
22import schedframe.scheduling.plan.SchedulingPlanInterface;
23import schedframe.scheduling.plan.impl.SchedulingPlan;
24import schedframe.scheduling.plugin.grid.ModuleList;
25import schedframe.scheduling.queue.TaskQueue;
26import schedframe.scheduling.queue.TaskQueueList;
27import schedframe.scheduling.tasks.TaskInterface;
28import test.article.recs.utils.RecsProcessorPowerInterface;
29import example.localplugin.BaseLocalSchedulingPlugin;
30import gridsim.dcworms.DCWormsTags;
31
32public class RecsExclusivenessDFSSP extends BaseLocalSchedulingPlugin {
33
34        private Random rand = new Random(5);
35       
36        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
37                        ResourceManager resManager, ModuleList modules) {
38
39                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
40                SchedulingPlan plan = new SchedulingPlan();
41                // our tasks are placed only in first queue (see
42                // BaseLocalSchedulingPlugin.placeJobsInQueues() method)
43                TaskQueue q = queues.get(0);
44                // choose the events types to serve.
45                // Different actions for different events are possible.
46                switch (event.getType()) {
47               
48                case START_TASK_EXECUTION:
49                case TASK_FINISHED:
50                        // check all tasks in queue
51                        for (int i = 0; i < q.size(); i++) {
52                                TaskInterface<?> task = q.get(i);
53                                // if status of the tasks in READY
54                                if (task.getStatus() == DCWormsTags.READY) {
55
56                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
57                                        if (choosenResources  != null) {
58                                                addToSchedulingPlan(plan, task, choosenResources);
59                                        }
60                                }
61                        }
62                        adjustFrequency(resourceManager.getProcessors());
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       
144       
145        private void adjustFrequency(List<Processor> processors){
146
147                for(Processor cpu: processors){
148                        RecsProcessorPowerInterface rppi = (RecsProcessorPowerInterface) cpu.getPowerInterface();
149                        int freeCores = 0;
150
151                        for(Core core: cpu.getCores()){
152                                if(core.getStatus() == ResourceStatus.FREE)
153                                        freeCores++;
154                        }
155                        if(freeCores == cpu.getCores().size())
156                                rppi.setPState(rppi.getHighestPState().getName());
157                        else
158                                rppi.setPState(rppi.getLowestPState().getName());
159
160                }
161        }
162
163}
Note: See TracBrowser for help on using the repository browser.