source: DCWoRMS/branches/coolemall/src/test/cpusharing/FCFSBF_RandomPlugin.java @ 1393

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