source: DCWoRMS/branches/coolemall/src/test/pausing_resuming/FCFSBF_RandomPluginPausing.java @ 1378

Revision 1378, 5.3 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.pausing_resuming;
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.Processor;
12import schedframe.resources.units.ProcessingElements;
13import schedframe.resources.units.ResourceUnit;
14import schedframe.resources.units.ResourceUnitName;
15import schedframe.resources.units.StandardResourceUnitName;
16import schedframe.scheduling.manager.resources.ClusterResourceManager;
17import schedframe.scheduling.manager.resources.ResourceManager;
18import schedframe.scheduling.manager.tasks.JobRegistry;
19import schedframe.scheduling.plan.SchedulingPlanInterface;
20import schedframe.scheduling.plan.impl.SchedulingPlan;
21import schedframe.scheduling.plugin.grid.ModuleList;
22import schedframe.scheduling.queue.TaskQueue;
23import schedframe.scheduling.queue.TaskQueueList;
24import schedframe.scheduling.tasks.TaskInterface;
25import dcworms.schedframe.scheduling.ExecTask;
26import example.localplugin.BaseLocalSchedulingPlugin;
27import gridsim.dcworms.DCWormsTags;
28
29public class FCFSBF_RandomPluginPausing 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 TIMER:
42                        // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method)
43                        TaskQueue q = queues.get(0);
44                        // check all tasks in queue
45
46                        for (int i = 0; i < q.size(); i++) {
47                                TaskInterface<?> task = q.get(i);
48                                // if status of the tasks in READY
49                                if (task.getStatus() == DCWormsTags.READY) {
50
51                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task);
52                                        if (choosenResources != null) {
53                                                addToSchedulingPlan(plan, task, choosenResources);
54                                        } else {
55                                                ExecTask taskToPause = getTaskWithLowestPriority(jobRegistry.getRunningTasks());
56                                                if(getTaskPriority(task) > getTaskPriority(taskToPause)){
57                                                        jobRegistry.pauseTask(taskToPause.getJobId(), taskToPause.getId());
58                                                }
59                                        }
60                                }
61                        }
62
63                        break;
64                case TASK_FINISHED:
65                        for(ExecTask exec: jobRegistry.getTasks(DCWormsTags.PAUSED)){
66                                jobRegistry.resumeTask(exec.getJobId(), exec.getId());
67                        }
68                        q = queues.get(0);
69                        // check all tasks in queue
70
71                        for (int i = 0; i < q.size(); i++) {
72                                TaskInterface<?> task = q.get(i);
73                                // if status of the tasks in READY
74                                if (task.getStatus() == DCWormsTags.READY) {
75                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task);
76                                        if (choosenResources != null) {
77                                                addToSchedulingPlan(plan, task, choosenResources);
78                                        }
79                                }
80                        }
81
82                        break;
83                case TASK_PAUSED:
84                         q = queues.get(0);
85                        // check all tasks in queue
86
87                        for (int i = 0; i < q.size(); i++) {
88                                TaskInterface<?> task = q.get(i);
89                                // if status of the tasks in READY
90                                if (task.getStatus() == DCWormsTags.READY) {
91
92                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task);
93                                        if (choosenResources != null) {
94                                                addToSchedulingPlan(plan, task, choosenResources);
95                                        }
96                                }
97                        }
98
99                }
100               
101                return plan;
102        }
103
104        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
105                        List<Processor> processors, TaskInterface<?> task) {
106
107                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(1);
108
109                int cpuRequest;
110                try {
111                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
112                } catch (NoSuchFieldException e) {
113                        cpuRequest = 0;
114                }
115
116                if (cpuRequest != 0) {
117
118                        if (processors.size() < cpuRequest) {
119                                return null;
120                        }
121
122                        List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(cpuRequest);                               
123                        for (int i = 0; i < processors.size() && cpuRequest > 0; i++) {
124                                if (processors.get(i).getStatus() == ResourceStatus.FREE) {
125                                        choosenResources.add(processors.get(i));
126                                        cpuRequest--;
127                                }
128                        }
129                        if (cpuRequest > 0) {
130                                return null;
131                        }
132
133                        ProcessingElements pe = new ProcessingElements();
134                        pe.addAll(choosenResources);
135                        map.put(StandardResourceUnitName.PE, pe);
136                        return map;
137                }
138
139                return null;
140        }
141       
142       
143        private int getTaskPriority(TaskInterface<?> task){
144                int priority;
145               
146                try {
147                        double cpuRequest = task.getCpuCntRequest();
148                        priority = (-1) * Double.valueOf(cpuRequest).intValue();
149                } catch (NoSuchFieldException e) {
150                        priority = Integer.MIN_VALUE;
151                }
152               
153                return priority;
154        }
155       
156        private ExecTask getTaskWithLowestPriority(List<ExecTask> tasks){
157                ExecTask lowestPriorityTask = null;
158                int lowestPriority = Integer.MAX_VALUE;
159                for(ExecTask execTask: tasks){
160                        int taskPriority = getTaskPriority(execTask);
161                        if(taskPriority < lowestPriority){
162                                lowestPriority = getTaskPriority(execTask);
163                                lowestPriorityTask = execTask;
164                        }
165                }
166               
167                return lowestPriorityTask;
168        }
169
170
171}
Note: See TracBrowser for help on using the repository browser.