source: DCWoRMS/branches/coolemall/src/example/localplugin/FCFSBF_RandomPluginWithMigration.java @ 1440

Revision 1440, 5.0 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.localplugin;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7
8import schedframe.events.scheduling.SchedulingEvent;
9import schedframe.resources.ResourceStatus;
10import schedframe.resources.computing.ComputingResource;
11import schedframe.resources.computing.Node;
12import schedframe.resources.computing.Processor;
13import schedframe.resources.units.ProcessingElements;
14import schedframe.resources.units.ResourceUnit;
15import schedframe.resources.units.ResourceUnitName;
16import schedframe.resources.units.StandardResourceUnitName;
17import schedframe.scheduling.manager.resources.ClusterResourceManager;
18import schedframe.scheduling.manager.resources.ResourceManager;
19import schedframe.scheduling.manager.tasks.JobRegistry;
20import schedframe.scheduling.manager.tasks.JobRegistryImpl;
21import schedframe.scheduling.plan.SchedulingPlanInterface;
22import schedframe.scheduling.plan.impl.SchedulingPlan;
23import schedframe.scheduling.plugin.ModuleList;
24import schedframe.scheduling.queue.TaskQueue;
25import schedframe.scheduling.queue.TaskQueueList;
26import schedframe.scheduling.tasks.TaskInterface;
27import dcworms.schedframe.scheduling.ExecTask;
28import example.localplugin.BaseLocalSchedulingPlugin;
29import gridsim.dcworms.DCWormsTags;
30
31public class FCFSBF_RandomPluginWithMigration extends BaseLocalSchedulingPlugin {
32
33        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
34                        ResourceManager resManager, ModuleList modules) {
35
36                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
37                List<Processor> processors = resourceManager.getProcessors();
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                                // if status of the tasks in READY
52                                if (task.getStatus() == DCWormsTags.READY) {
53                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task);
54                                        if (choosenResources != null) {
55                                                addToSchedulingPlan(plan, task, choosenResources);
56                                        }
57                                }
58                        }
59                        break;
60                case RESOURCE_POWER_LIMIT_EXCEEDED:
61                        String src = event.getSource();
62                        optimizeEnergyUsage(jobRegistry, resourceManager , src);
63                        break;
64
65                }
66               
67                return plan;
68        }
69
70        private void optimizeEnergyUsage(JobRegistry jobRegistry, ClusterResourceManager resourceManager, String resName) {
71                Node overLoadedNode = (Node)resourceManager .getResourceByName(resName);
72                Node leastLoadedNode = findLeastLoadedResource(resourceManager.getNodes());
73               
74                if(leastLoadedNode.getLoadInterface().getRecentUtilization().getValue() >= overLoadedNode.getLoadInterface().getRecentUtilization().getValue()){
75                        return;
76                }
77
78                JobRegistry srcHostJr = new JobRegistryImpl(overLoadedNode.getFullName());
79                int runningTasksNumber = srcHostJr.getRunningTasks().size();
80                for(int i = srcHostJr.getRunningTasks().size() - 1; i >= 0 && runningTasksNumber > 2; i--) {
81                        ExecTask execTask = srcHostJr.getRunningTasks().get(i);
82                        Map<ResourceUnitName, ResourceUnit> destinationResources = chooseResourcesForExecution(findLeastLoadedResource(resourceManager.getNodes()).getProcessors(), execTask);
83                        jobRegistry.migrateTask(execTask.getJobId(), execTask.getId(), destinationResources);
84                        runningTasksNumber--;
85                }
86        }
87
88        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
89                        List<Processor> processors, TaskInterface<?> task) {
90
91                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(1);
92
93                int cpuRequest;
94                try {
95                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
96                } catch (NoSuchFieldException e) {
97                        cpuRequest = 0;
98                }
99
100                if (cpuRequest != 0) {
101
102                        if (processors.size() < cpuRequest) {
103                                return null;
104                        }
105
106                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(cpuRequest);                               
107                        for (int i = 0; i < processors.size() && cpuRequest > 0; i++) {
108                                if (processors.get(i).getStatus() == ResourceStatus.FREE) {
109                                        choosenResources.add(processors.get(i));
110                                        cpuRequest--;
111                                }
112                        }
113                        if (cpuRequest > 0) {
114                                return null;
115                        }
116
117                        ProcessingElements pe = new ProcessingElements();
118                        pe.addAll(choosenResources);
119                        map.put(StandardResourceUnitName.PE, pe);
120                        return map;
121                }
122
123                return null;
124        }
125
126        private Node findLeastLoadedResource(List<Node> nodes ){
127                Node leastLoadedNode = null;
128                double minLoad = Double.MAX_VALUE;
129               
130                for(int i = 0; i < nodes.size(); i++){
131                        Node node = nodes.get(i);
132                        double totalLoad = node.getLoadInterface().getRecentUtilization().getValue();
133                        if(totalLoad < minLoad){
134                                leastLoadedNode= node;
135                                minLoad = totalLoad;
136                        }
137                }
138                return leastLoadedNode;
139        }
140
141}
Note: See TracBrowser for help on using the repository browser.