[1415] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.HashMap; |
---|
| 5 | import java.util.List; |
---|
| 6 | import java.util.Map; |
---|
| 7 | |
---|
| 8 | import schedframe.events.scheduling.SchedulingEvent; |
---|
| 9 | import schedframe.resources.ResourceStatus; |
---|
| 10 | import schedframe.resources.computing.ComputingResource; |
---|
| 11 | import schedframe.resources.computing.Processor; |
---|
| 12 | import schedframe.resources.units.ProcessingElements; |
---|
| 13 | import schedframe.resources.units.ResourceUnit; |
---|
| 14 | import schedframe.resources.units.ResourceUnitName; |
---|
| 15 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 16 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
| 17 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 18 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 19 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 20 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
| 21 | import schedframe.scheduling.plugin.ModuleList; |
---|
| 22 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 23 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 24 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 25 | import dcworms.schedframe.scheduling.ExecTask; |
---|
| 26 | import example.localplugin.BaseLocalSchedulingPlugin; |
---|
| 27 | import gridsim.dcworms.DCWormsTags; |
---|
| 28 | |
---|
| 29 | public class FCFSBF_RandomPluginWithPausingResuming 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_RESUMED: |
---|
| 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 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task); |
---|
| 53 | if (choosenResources != null) { |
---|
| 54 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 55 | } else { |
---|
| 56 | ExecTask taskToPause = getTaskWithLowestPriority(jobRegistry.getRunningTasks()); |
---|
| 57 | if(getTaskPriority(task) > getTaskPriority(taskToPause)){ |
---|
| 58 | jobRegistry.pauseTask(taskToPause.getJobId(), taskToPause.getId()); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | break; |
---|
| 65 | case TASK_FINISHED: |
---|
| 66 | for(ExecTask exec: jobRegistry.getTasks(DCWormsTags.PAUSED)){ |
---|
| 67 | jobRegistry.resumeTask(exec.getJobId(), exec.getId()); |
---|
| 68 | } |
---|
| 69 | q = queues.get(0); |
---|
| 70 | // check all tasks in queue |
---|
| 71 | |
---|
| 72 | for (int i = 0; i < q.size(); i++) { |
---|
| 73 | TaskInterface<?> task = q.get(i); |
---|
| 74 | // if status of the tasks in READY |
---|
| 75 | if (task.getStatus() == DCWormsTags.READY) { |
---|
| 76 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task); |
---|
| 77 | if (choosenResources != null) { |
---|
| 78 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | break; |
---|
| 84 | case TASK_PAUSED: |
---|
| 85 | q = queues.get(0); |
---|
| 86 | // check all tasks in queue |
---|
| 87 | |
---|
| 88 | for (int i = 0; i < q.size(); i++) { |
---|
| 89 | TaskInterface<?> task = q.get(i); |
---|
| 90 | // if status of the tasks in READY |
---|
| 91 | if (task.getStatus() == DCWormsTags.READY) { |
---|
| 92 | |
---|
| 93 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task); |
---|
| 94 | if (choosenResources != null) { |
---|
| 95 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | return plan; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
| 106 | List<Processor> processors, TaskInterface<?> task) { |
---|
| 107 | |
---|
| 108 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(1); |
---|
| 109 | |
---|
| 110 | int cpuRequest; |
---|
| 111 | try { |
---|
| 112 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 113 | } catch (NoSuchFieldException e) { |
---|
| 114 | cpuRequest = 0; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if (cpuRequest != 0) { |
---|
| 118 | |
---|
| 119 | if (processors.size() < cpuRequest) { |
---|
| 120 | return null; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(cpuRequest); |
---|
| 124 | for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { |
---|
| 125 | if (processors.get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 126 | choosenResources.add(processors.get(i)); |
---|
| 127 | cpuRequest--; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | if (cpuRequest > 0) { |
---|
| 131 | return null; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | ProcessingElements pe = new ProcessingElements(); |
---|
| 135 | pe.addAll(choosenResources); |
---|
| 136 | map.put(StandardResourceUnitName.PE, pe); |
---|
| 137 | return map; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | return null; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | private int getTaskPriority(TaskInterface<?> task){ |
---|
| 145 | int priority; |
---|
| 146 | |
---|
| 147 | try { |
---|
| 148 | double cpuRequest = task.getCpuCntRequest(); |
---|
| 149 | priority = (-1) * Double.valueOf(cpuRequest).intValue(); |
---|
| 150 | } catch (NoSuchFieldException e) { |
---|
| 151 | priority = Integer.MIN_VALUE; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | return priority; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | private ExecTask getTaskWithLowestPriority(List<ExecTask> tasks){ |
---|
| 158 | ExecTask lowestPriorityTask = null; |
---|
| 159 | int lowestPriority = Integer.MAX_VALUE; |
---|
| 160 | for(ExecTask execTask: tasks){ |
---|
| 161 | int taskPriority = getTaskPriority(execTask); |
---|
| 162 | if(taskPriority < lowestPriority){ |
---|
| 163 | lowestPriority = getTaskPriority(execTask); |
---|
| 164 | lowestPriorityTask = execTask; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | return lowestPriorityTask; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | } |
---|