source: DCWoRMS/branches/coolemall/src/test/bull/Bull_FCFSBF_RandomPlugin.java @ 1463

Revision 1463, 3.4 KB checked in by wojtekp, 10 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.bull;
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.StandardResourceType;
11import schedframe.resources.computing.ComputingResource;
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.plan.SchedulingPlanInterface;
21import schedframe.scheduling.plan.impl.SchedulingPlan;
22import schedframe.scheduling.plugin.ModuleList;
23import schedframe.scheduling.queue.TaskQueue;
24import schedframe.scheduling.queue.TaskQueueList;
25import schedframe.scheduling.tasks.TaskInterface;
26import example.localplugin.BaseLocalSchedulingPlugin;
27import gridsim.dcworms.DCWormsTags;
28
29public class Bull_FCFSBF_RandomPlugin extends BaseLocalSchedulingPlugin {
30
31        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
32                        ResourceManager resManager, ModuleList modules) {
33
34                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
35                List<Processor> processors = resourceManager.getProcessors();
36                SchedulingPlan plan = new SchedulingPlan();
37                // choose the events types to serve.
38                // Different actions for different events are possible.
39                switch (event.getType()) {
40                case START_TASK_EXECUTION:
41                case TASK_FINISHED:
42                //case TIMER:
43                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
44                        TaskQueue q = queues.get(0);
45                        // check all tasks in queue
46
47                        for (int i = 0; i < q.size(); i++) {
48                                TaskInterface<?> task = q.get(i);
49                                // if status of the tasks in READY
50                                if (task.getStatus() == DCWormsTags.READY) {
51
52                                        if(resourceManager.getResourcesByTypeWithStatus(StandardResourceType.Processor, ResourceStatus.FREE).size()==0){
53                                                break;
54                                        }
55
56                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task);
57                                        if (choosenResources != null) {
58                                                addToSchedulingPlan(plan, task, choosenResources);
59                                        }
60                                }
61                        }
62
63                        break;
64                }
65                return plan;
66        }
67
68        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
69                        List<Processor> processors, TaskInterface<?> task) {
70
71                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(2);
72
73                int cpuRequest;
74                try {
75                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
76                } catch (NoSuchFieldException e) {
77                        cpuRequest = 0;
78                }
79
80                if (cpuRequest != 0) {
81
82                        if (processors.size() < cpuRequest) {
83                                return null;
84                        }
85
86                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(cpuRequest);                               
87                        for (int i = 0; i < processors.size() && cpuRequest > 0; i++) {
88                                if (processors.get(i).getStatus() == ResourceStatus.FREE) {
89                                        choosenResources.add(processors.get(i));
90                                        cpuRequest--;
91                                }
92                        }
93                        if (cpuRequest > 0) {
94                                return null;
95                        }
96
97                        ProcessingElements pe = new ProcessingElements();
98                        pe.addAll(choosenResources);
99                        map.put(StandardResourceUnitName.PE, pe);
100                        return map;
101                }
102
103                return null;
104        }
105
106}
Note: See TracBrowser for help on using the repository browser.