source: DCWoRMS/branches/coolemall/src/experiments/e2dc2013/recs/plugins/scheduling/exp2/RecsRandomSP.java @ 1499

Revision 1499, 5.2 KB checked in by wojtekp, 10 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package experiments.e2dc2013.recs.plugins.scheduling.exp2;
2
3import experiments.e2dc2013.recs.plugins.scheduling.RecsSP;
4import gridsim.dcworms.DCWormsTags;
5
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
10import java.util.Random;
11
12import schedframe.events.scheduling.SchedulingEvent;
13import schedframe.resources.ResourceStatus;
14import schedframe.resources.computing.Node;
15import schedframe.resources.computing.ComputingResource;
16import schedframe.resources.computing.Core;
17import schedframe.resources.computing.profiles.energy.airthroughput.StandardAirflowStateName;
18import schedframe.resources.computing.profiles.energy.airthroughput.CustomAirflowStateName;
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.manager.tasks.JobRegistryImpl;
27import schedframe.scheduling.plan.SchedulingPlanInterface;
28import schedframe.scheduling.plan.impl.SchedulingPlan;
29import schedframe.scheduling.plugin.ModuleList;
30import schedframe.scheduling.queue.TaskQueue;
31import schedframe.scheduling.queue.TaskQueueList;
32import schedframe.scheduling.tasks.TaskInterface;
33
34public class RecsRandomSP extends RecsSP {
35
36        private Random rand = new Random(5);
37       
38        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
39                        ResourceManager resManager, ModuleList modules) {
40
41                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
42                SchedulingPlan plan = new SchedulingPlan();
43                // choose the events types to serve.
44                // Different actions for different events are possible.
45                switch (event.getType()) {
46                case START_TASK_EXECUTION:
47                case TASK_FINISHED:
48                //case TIMER:
49                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
50                        TaskQueue q = queues.get(0);
51
52                        List<Node> notSelectedNodes = resourceManager.getNodes();
53                        // check all tasks in queue
54                        for (int i = 0; i < q.size(); i++) {
55                                TaskInterface<?> task = q.get(i);
56                                // if status of the tasks in READY
57                                if (task.getStatus() == DCWormsTags.READY) {
58                                        Node node = chooseRandomProvider(resourceManager, task);
59                                        if (node != null) {
60                                                node.getAirflowInterface().setAirflowState(StandardAirflowStateName.ON);
61                                                notSelectedNodes.remove(node);
62                                                addToSchedulingPlan(plan, task, chooseResourcesForExecution(node, task));
63                                        }
64                                }
65                        }
66                        adjustOtherFans(notSelectedNodes);
67                        break;
68                }
69                return plan;
70        }
71       
72        private Node chooseRandomProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) {
73                List<Node> nodes = filterNodes(resourceManager.getNodes(), task);
74                if(nodes.size() > 0)
75                        return randomNode(nodes);
76                return null;
77        }
78
79        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
80                        Node node, TaskInterface<?> task) {
81
82                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
83
84                int cpuRequest;
85                try {
86                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
87                } catch (NoSuchFieldException e) {
88                        cpuRequest = 0;
89                }
90
91                if (cpuRequest != 0) {
92
93                        List<Core> cores = node.getProcessors().get(0).getCores();
94
95                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
96                        for (int i = 0; i < cores.size(); i++) {
97                                if (cores.get(i).getStatus() == ResourceStatus.FREE) {
98                                        //choosenResources.add(cores.get(i));
99                                        cpuRequest--;
100                                }
101                        }
102                        if (cpuRequest > 0) {
103                                //return null;
104                        }
105                        choosenResources.add(node.getProcessors().get(0));
106                        ProcessingElements pe = new ProcessingElements();
107                        pe.addAll(choosenResources);
108                        map.put(StandardResourceUnitName.PE, pe);
109                        return map;
110                }
111                return null;
112        }
113       
114        private List<Node> filterNodes(List<Node> nodes, TaskInterface<?> task){
115                List<Node> filteredNodes = new ArrayList<Node>();
116                for (Node node : nodes) {
117                        int cpuRequest;
118                        try {
119                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
120                        } catch (NoSuchFieldException e) {
121                                cpuRequest = 0;
122                        }
123
124                        if (cpuRequest != 0) {
125
126                                List<Core> cores = node.getProcessors().get(0).getCores();
127                                if (cores.size() < cpuRequest) {
128                                        continue;
129                                }
130
131                                int freeCores = 0;
132                                for(Core core: cores){
133                                        if(core.getStatus() == ResourceStatus.FREE)
134                                                freeCores++;
135                                }
136                               
137                                if(freeCores != cores.size())
138                                        continue;
139
140                                filteredNodes.add(node);
141                        }
142                }
143               
144                return filteredNodes;
145        }
146       
147        private Node randomNode(List<Node> nodes){
148                return nodes.get(rand.nextInt(nodes.size()));
149        }
150       
151        private void adjustOtherFans(List<Node> nodes){
152                for(Node node : nodes){
153                        if(node.getFreeProcessorsNumber() == node.getProcessorsNumber()){
154                                node.getAirflowInterface().setAirflowState(StandardAirflowStateName.OFF);
155                        } else if(new JobRegistryImpl(node.getFullName()).getRunningTasks().size() > 1)
156                                node.getAirflowInterface().setAirflowState(new CustomAirflowStateName("FAN_ON_TURBO"));
157                        else
158                                node.getAirflowInterface().setAirflowState(StandardAirflowStateName.ON);
159                }
160        }
161}
Note: See TracBrowser for help on using the repository browser.