[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.Node; |
---|
| 12 | import schedframe.resources.computing.Processor; |
---|
| 13 | import schedframe.resources.units.ProcessingElements; |
---|
| 14 | import schedframe.resources.units.ResourceUnit; |
---|
| 15 | import schedframe.resources.units.ResourceUnitName; |
---|
| 16 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 17 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
| 18 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 19 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
| 20 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
| 21 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 22 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
| 23 | import schedframe.scheduling.plugin.ModuleList; |
---|
| 24 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 25 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 26 | import schedframe.scheduling.tasks.TaskInterface; |
---|
| 27 | import dcworms.schedframe.scheduling.ExecTask; |
---|
| 28 | import example.localplugin.BaseLocalSchedulingPlugin; |
---|
| 29 | import gridsim.dcworms.DCWormsTags; |
---|
| 30 | |
---|
| 31 | public class FCFSBF_RandomPluginWithMigration extends BaseLocalSchedulingPlugin { |
---|
| 32 | |
---|
| 33 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
| 34 | ResourceManager resManager, ModuleList modules) { |
---|
| 35 | |
---|
| 36 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 37 | List<Processor> processors = resourceManager.getProcessors(); |
---|
| 38 | SchedulingPlan plan = new SchedulingPlan(); |
---|
| 39 | // choose the events types to serve. |
---|
| 40 | // Different actions for different events are possible. |
---|
| 41 | switch (event.getType()) { |
---|
| 42 | case START_TASK_EXECUTION: |
---|
| 43 | case TASK_FINISHED: |
---|
| 44 | //case TIMER: |
---|
| 45 | // our tasks are placed only in first queue (see BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
| 46 | TaskQueue q = queues.get(0); |
---|
| 47 | // check all tasks in queue |
---|
| 48 | |
---|
| 49 | for (int i = 0; i < q.size(); i++) { |
---|
| 50 | TaskInterface<?> task = q.get(i); |
---|
| 51 | // if status of the tasks in READY |
---|
| 52 | if (task.getStatus() == DCWormsTags.READY) { |
---|
| 53 | Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(processors, task); |
---|
| 54 | if (choosenResources != null) { |
---|
| 55 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | break; |
---|
[1440] | 60 | case RESOURCE_POWER_LIMIT_EXCEEDED: |
---|
[1415] | 61 | String src = event.getSource(); |
---|
| 62 | optimizeEnergyUsage(jobRegistry, resourceManager , src); |
---|
| 63 | break; |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | return plan; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | private void optimizeEnergyUsage(JobRegistry jobRegistry, ClusterResourceManager resourceManager, String resName) { |
---|
| 71 | Node overLoadedNode = (Node)resourceManager .getResourceByName(resName); |
---|
| 72 | Node leastLoadedNode = findLeastLoadedResource(resourceManager.getNodes()); |
---|
| 73 | |
---|
| 74 | if(leastLoadedNode.getLoadInterface().getRecentUtilization().getValue() >= overLoadedNode.getLoadInterface().getRecentUtilization().getValue()){ |
---|
| 75 | return; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | JobRegistry srcHostJr = new JobRegistryImpl(overLoadedNode.getFullName()); |
---|
| 79 | int runningTasksNumber = srcHostJr.getRunningTasks().size(); |
---|
| 80 | for(int i = srcHostJr.getRunningTasks().size() - 1; i >= 0 && runningTasksNumber > 2; i--) { |
---|
| 81 | ExecTask execTask = srcHostJr.getRunningTasks().get(i); |
---|
| 82 | Map<ResourceUnitName, ResourceUnit> destinationResources = chooseResourcesForExecution(findLeastLoadedResource(resourceManager.getNodes()).getProcessors(), execTask); |
---|
| 83 | jobRegistry.migrateTask(execTask.getJobId(), execTask.getId(), destinationResources); |
---|
| 84 | runningTasksNumber--; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution( |
---|
| 89 | List<Processor> processors, TaskInterface<?> task) { |
---|
| 90 | |
---|
| 91 | Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>(1); |
---|
| 92 | |
---|
| 93 | int cpuRequest; |
---|
| 94 | try { |
---|
| 95 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 96 | } catch (NoSuchFieldException e) { |
---|
| 97 | cpuRequest = 0; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | if (cpuRequest != 0) { |
---|
| 101 | |
---|
| 102 | if (processors.size() < cpuRequest) { |
---|
| 103 | return null; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | List<ComputingResource> choosenResources = new ArrayList<ComputingResource>(cpuRequest); |
---|
| 107 | for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { |
---|
| 108 | if (processors.get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 109 | choosenResources.add(processors.get(i)); |
---|
| 110 | cpuRequest--; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | if (cpuRequest > 0) { |
---|
| 114 | return null; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | ProcessingElements pe = new ProcessingElements(); |
---|
| 118 | pe.addAll(choosenResources); |
---|
| 119 | map.put(StandardResourceUnitName.PE, pe); |
---|
| 120 | return map; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return null; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | private Node findLeastLoadedResource(List<Node> nodes ){ |
---|
| 127 | Node leastLoadedNode = null; |
---|
| 128 | double minLoad = Double.MAX_VALUE; |
---|
| 129 | |
---|
| 130 | for(int i = 0; i < nodes.size(); i++){ |
---|
| 131 | Node node = nodes.get(i); |
---|
| 132 | double totalLoad = node.getLoadInterface().getRecentUtilization().getValue(); |
---|
| 133 | if(totalLoad < minLoad){ |
---|
| 134 | leastLoadedNode= node; |
---|
| 135 | minLoad = totalLoad; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | return leastLoadedNode; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | } |
---|