package example.localplugin; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import schedframe.events.scheduling.SchedulingEvent; import schedframe.resources.ResourceStatus; import schedframe.resources.computing.ComputingResource; import schedframe.resources.computing.Processor; import schedframe.resources.units.ProcessingElements; import schedframe.resources.units.ResourceUnit; import schedframe.resources.units.ResourceUnitName; import schedframe.resources.units.StandardResourceUnitName; import schedframe.scheduling.manager.resources.ClusterResourceManager; import schedframe.scheduling.manager.resources.ResourceManager; import schedframe.scheduling.manager.tasks.JobRegistry; import schedframe.scheduling.plan.SchedulingPlanInterface; import schedframe.scheduling.plan.impl.SchedulingPlan; import schedframe.scheduling.plugin.ModuleList; import schedframe.scheduling.queue.TaskQueue; import schedframe.scheduling.queue.TaskQueueList; import schedframe.scheduling.tasks.TaskInterface; import dcworms.schedframe.scheduling.ExecTask; import example.localplugin.BaseLocalSchedulingPlugin; import gridsim.dcworms.DCWormsTags; public class FCFSBF_RandomPluginWithPausingResuming extends BaseLocalSchedulingPlugin { public SchedulingPlanInterface schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, ResourceManager resManager, ModuleList modules) { ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; List processors = resourceManager.getProcessors(); SchedulingPlan plan = new SchedulingPlan(); // choose the events types to serve. // Different actions for different events are possible. switch (event.getType()) { case START_TASK_EXECUTION: case TASK_RESUMED: //case TIMER: // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) TaskQueue q = queues.get(0); // check all tasks in queue for (int i = 0; i < q.size(); i++) { TaskInterface task = q.get(i); // if status of the tasks in READY if (task.getStatus() == DCWormsTags.READY) { Map choosenResources = chooseResourcesForExecution(processors, task); if (choosenResources != null) { addToSchedulingPlan(plan, task, choosenResources); } else { ExecTask taskToPause = getTaskWithLowestPriority(jobRegistry.getRunningTasks()); if(getTaskPriority(task) > getTaskPriority(taskToPause)){ jobRegistry.pauseTask(taskToPause.getJobId(), taskToPause.getId()); } } } } break; case TASK_FINISHED: for(ExecTask exec: jobRegistry.getTasks(DCWormsTags.PAUSED)){ jobRegistry.resumeTask(exec.getJobId(), exec.getId()); } q = queues.get(0); // check all tasks in queue for (int i = 0; i < q.size(); i++) { TaskInterface task = q.get(i); // if status of the tasks in READY if (task.getStatus() == DCWormsTags.READY) { Map choosenResources = chooseResourcesForExecution(processors, task); if (choosenResources != null) { addToSchedulingPlan(plan, task, choosenResources); } } } break; case TASK_PAUSED: q = queues.get(0); // check all tasks in queue for (int i = 0; i < q.size(); i++) { TaskInterface task = q.get(i); // if status of the tasks in READY if (task.getStatus() == DCWormsTags.READY) { Map choosenResources = chooseResourcesForExecution(processors, task); if (choosenResources != null) { addToSchedulingPlan(plan, task, choosenResources); } } } } return plan; } private Map chooseResourcesForExecution( List processors, TaskInterface task) { Map map = new HashMap(1); int cpuRequest; try { cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); } catch (NoSuchFieldException e) { cpuRequest = 0; } if (cpuRequest != 0) { if (processors.size() < cpuRequest) { return null; } List choosenResources = new ArrayList(cpuRequest); for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { if (processors.get(i).getStatus() == ResourceStatus.FREE) { choosenResources.add(processors.get(i)); cpuRequest--; } } if (cpuRequest > 0) { return null; } ProcessingElements pe = new ProcessingElements(); pe.addAll(choosenResources); map.put(StandardResourceUnitName.PE, pe); return map; } return null; } private int getTaskPriority(TaskInterface task){ int priority; try { double cpuRequest = task.getCpuCntRequest(); priority = (-1) * Double.valueOf(cpuRequest).intValue(); } catch (NoSuchFieldException e) { priority = Integer.MIN_VALUE; } return priority; } private ExecTask getTaskWithLowestPriority(List tasks){ ExecTask lowestPriorityTask = null; int lowestPriority = Integer.MAX_VALUE; for(ExecTask execTask: tasks){ int taskPriority = getTaskPriority(execTask); if(taskPriority < lowestPriority){ lowestPriority = getTaskPriority(execTask); lowestPriorityTask = execTask; } } return lowestPriorityTask; } }