[1393] | 1 | package test.cpusharing; |
---|
| 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.computing.ComputingResource; |
---|
| 10 | import schedframe.resources.computing.Processor; |
---|
| 11 | import schedframe.resources.units.ProcessingElements; |
---|
| 12 | import schedframe.resources.units.ResourceUnit; |
---|
| 13 | import schedframe.resources.units.ResourceUnitName; |
---|
| 14 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 15 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
| 16 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 17 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 18 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 19 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
[1399] | 20 | import schedframe.scheduling.plugin.ModuleList; |
---|
[1393] | 21 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 22 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 23 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 24 | import example.localplugin.BaseLocalSchedulingPlugin; |
---|
| 25 | import gridsim.dcworms.DCWormsTags; |
---|
| 26 | |
---|
| 27 | public 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 | } |
---|