source: DCWoRMS/trunk/src/test/article2/recs/plugins/scheduling/exp2/RecsRandomSP.java @ 826

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