source: DCWoRMS/trunk/src/example/localplugin/FCFSBF_DFSClusterPlugin.java @ 525

Revision 525, 4.0 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.localplugin;
2
3import gridsim.dcworms.DCWormsTags;
4
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9
10import schedframe.events.scheduling.SchedulingEvent;
11import schedframe.resources.ResourceStatus;
12import schedframe.resources.computing.ComputingResource;
13import schedframe.resources.computing.Processor;
14import schedframe.resources.units.ProcessingElements;
15import schedframe.resources.units.ResourceUnit;
16import schedframe.resources.units.ResourceUnitName;
17import schedframe.resources.units.StandardResourceUnitName;
18import schedframe.scheduling.manager.resources.ClusterResourceManager;
19import schedframe.scheduling.manager.resources.ResourceManager;
20import schedframe.scheduling.manager.tasks.JobRegistry;
21import schedframe.scheduling.plan.SchedulingPlanInterface;
22import schedframe.scheduling.plan.impl.SchedulingPlan;
23import schedframe.scheduling.plugin.grid.ModuleList;
24import schedframe.scheduling.queue.TaskQueue;
25import schedframe.scheduling.queue.TaskQueueList;
26import schedframe.scheduling.tasks.TaskInterface;
27
28public class FCFSBF_DFSClusterPlugin extends BaseLocalSchedulingPlugin {
29
30        List<Processor> allocatedCPUs;
31        public FCFSBF_DFSClusterPlugin () {
32                allocatedCPUs = new ArrayList<Processor>();
33        }
34
35        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
36                        ResourceManager resManager, ModuleList modules) {
37
38                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
39                SchedulingPlan plan = new SchedulingPlan();
40                // our tasks are placed only in first queue (see
41                // BaseLocalPlugin.placeJobsInQueues() method)
42                TaskQueue q = queues.get(0);
43                // chose the events types to serve.
44                // Different actions for different events are possible.
45                switch (event.getType()) {
46               
47                case START_TASK_EXECUTION:
48                case TASK_FINISHED:
49                        // check all tasks in queue
50                        for (int i = 0; i < q.size(); i++) {
51                                TaskInterface<?> task = q.get(i);
52                                // if status of the tasks in READY
53                                if (task.getStatus() == DCWormsTags.READY) {
54
55                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
56                                        if (choosenResources  != null) {
57                                                addToSchedulingPlan(plan, task, choosenResources);
58                                        }
59                                }
60                        }
61                        adjustFrequency(resourceManager.getProcessors());
62                }
63                return plan;
64        }
65       
66        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
67                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
68
69                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
70
71                int cpuRequest;
72                try {
73                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
74                } catch (NoSuchFieldException e) {
75                        cpuRequest = 1;
76                }
77
78                if (cpuRequest != 0) {
79                        List<ComputingResource> choosenResources = null;
80                        List<Processor> processors = resourceManager.getProcessors();
81                        processors.removeAll(allocatedCPUs);
82                        if (processors.size() < cpuRequest) {
83                                // log.warn("Task requires more cpus than is availiable in this resource.");
84                                return null;
85                        }
86
87                        choosenResources = new ArrayList<ComputingResource>();
88
89                        for (int i = 0; i < processors.size() && cpuRequest > 0; i++) {
90                                if (processors.get(i).getStatus() == ResourceStatus.FREE) {
91                                        choosenResources.add(processors.get(i));
92                                        cpuRequest--;
93                                }
94                        }
95                        if (cpuRequest > 0) {
96                                // log.info("Task " + task.getJobId() + "_" + task.getId() +
97                                // " requires more cpus than is availiable in this moment.");
98                                return null;
99                        }
100
101                        ProcessingElements result = new ProcessingElements();
102                        result.addAll(choosenResources);
103                        map.put(StandardResourceUnitName.PE, result);
104                }
105                return map;
106        }
107       
108        private void adjustFrequency(List<Processor> processors){
109
110                for(Processor cpu: processors){
111                        if(cpu.getStatus() == ResourceStatus.FREE) {
112                                if(cpu.getPowerInterface().getSupportedPStates().containsKey("P3"))
113                                        cpu.getPowerInterface().setPState("P3");
114                        }
115                        else{
116                                if(cpu.getPowerInterface().getSupportedPStates().containsKey("P0"))
117                                        cpu.getPowerInterface().setPState("P0");
118                        }
119
120                }
121
122        }
123
124
125}
Note: See TracBrowser for help on using the repository browser.