source: DCWoRMS/trunk/src/test/article2/recs/plugins/scheduling/RecsExclusivenessRandomSP.java @ 828

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