source: DCWoRMS/trunk/src/example/localplugin/FCFSCPUFreqScalingClusterLocalPlugin.java @ 512

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