[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | import gridsim.gssim.ResourceHistoryItem; |
---|
| 5 | import gridsim.gssim.SubmittedTask; |
---|
| 6 | |
---|
| 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.HashMap; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Map; |
---|
| 11 | import java.util.Properties; |
---|
| 12 | |
---|
| 13 | import schedframe.resources.units.ResourceUnit; |
---|
| 14 | import schedframe.scheduling.TaskInterface; |
---|
| 15 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 16 | import schedframe.scheduling.events.TaskFinishedEvent; |
---|
| 17 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 18 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 19 | import test.rewolucja.GSSIMJobInterface; |
---|
| 20 | import test.rewolucja.energy.profile.PStateType; |
---|
| 21 | import test.rewolucja.resources.ProcessingElements; |
---|
| 22 | import test.rewolucja.resources.ResourceStatus; |
---|
| 23 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
| 24 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 25 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 26 | import test.rewolucja.resources.physical.implementation.CPU; |
---|
| 27 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 28 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 29 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
| 30 | import test.rewolucja.scheduling.queue.GSSIMQueue; |
---|
| 31 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 32 | |
---|
| 33 | public class FCFSCPUFreqScalingClusterLocalPlugin extends BaseLocalPlugin { |
---|
| 34 | |
---|
| 35 | public FCFSCPUFreqScalingClusterLocalPlugin () { |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 39 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
| 40 | |
---|
| 41 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
| 42 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 43 | // our tasks are placed only in first queue (see |
---|
| 44 | // BaseLocalPlugin.placeTasksInQueues() method) |
---|
| 45 | GSSIMQueue q = queues.get(0); |
---|
| 46 | // chose the events types to serve. |
---|
| 47 | // Different actions for different events are possible. |
---|
| 48 | switch (event.getType()) { |
---|
| 49 | |
---|
| 50 | case START_TASK_EXECUTION: |
---|
| 51 | |
---|
| 52 | // check all tasks in queue |
---|
| 53 | for (int i = 0; i < q.size(); i++) { |
---|
| 54 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 55 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 56 | // if status of the tasks in READY |
---|
| 57 | if (task.getStatus() == Gridlet.READY) { |
---|
| 58 | |
---|
| 59 | Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 60 | if (choosenResources != null) { |
---|
| 61 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 62 | ProcessingElements pes = (ProcessingElements)choosenResources.get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
| 63 | List<CPU> processors = new ArrayList<CPU>(); |
---|
| 64 | for(ComputingResource res : pes){ |
---|
| 65 | processors.add((CPU) res); |
---|
| 66 | } |
---|
| 67 | adjustFrequency(ResourceStatus.BUSY,processors); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | break; |
---|
| 72 | |
---|
| 73 | case TASK_FINISHED: |
---|
| 74 | TaskFinishedEvent finEvent = (TaskFinishedEvent) event; |
---|
| 75 | SubmittedTask subTask = jobRegistry.getSubmittedTask(finEvent.getJobId(), finEvent.getTaskId()); |
---|
| 76 | List<ResourceHistoryItem> usedResourcesList = subTask.getUsedResources(); |
---|
| 77 | ProcessingElements pes = (ProcessingElements)usedResourcesList.get(usedResourcesList.size() - 1).getResourceUnits().get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
| 78 | List<CPU> processors = new ArrayList<CPU>(); |
---|
| 79 | for(ComputingResource res : pes){ |
---|
| 80 | processors.add((CPU) res); |
---|
| 81 | } |
---|
| 82 | adjustFrequency(ResourceStatus.FREE, processors); |
---|
| 83 | break; |
---|
| 84 | |
---|
| 85 | case TASK_REQUESTED_TIME_EXPIRED: |
---|
| 86 | // check all tasks in queue |
---|
| 87 | for (int i = 0; i < q.size(); i++) { |
---|
| 88 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 89 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 90 | // if status of the tasks in READY |
---|
| 91 | if (task.getStatus() == Gridlet.READY) { |
---|
| 92 | |
---|
| 93 | Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task); |
---|
| 94 | if (choosenResources != null) { |
---|
| 95 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 96 | pes = (ProcessingElements)choosenResources.get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
| 97 | processors = new ArrayList<CPU>(); |
---|
| 98 | for(ComputingResource res : pes){ |
---|
| 99 | processors.add((CPU) res); |
---|
| 100 | } |
---|
| 101 | adjustFrequency(ResourceStatus.BUSY,processors); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | break; |
---|
| 106 | } |
---|
| 107 | return plan; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution( |
---|
| 111 | ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
| 112 | |
---|
| 113 | HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>(); |
---|
| 114 | |
---|
| 115 | int cpuRequest; |
---|
| 116 | try { |
---|
| 117 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
| 118 | } catch (NoSuchFieldException e) { |
---|
| 119 | cpuRequest = 1; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | if (cpuRequest != 0) { |
---|
| 123 | List<ComputingResource> choosenResources = null; |
---|
| 124 | List<CPU> processors = resourceManager.getProcessors(); |
---|
| 125 | if (processors.size() < cpuRequest) { |
---|
| 126 | // log.warn("Task requires more cpus than is availiable in this resource."); |
---|
| 127 | return null; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | choosenResources = new ArrayList<ComputingResource>(); |
---|
| 131 | |
---|
| 132 | for (int i = 0; i < processors.size() && cpuRequest > 0; i++) { |
---|
| 133 | if (processors.get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 134 | choosenResources.add(processors.get(i)); |
---|
| 135 | cpuRequest--; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | if (cpuRequest > 0) { |
---|
| 139 | // log.info("Task " + task.getJobId() + "_" + task.getId() + |
---|
| 140 | // " requires more cpus than is availiable in this moment."); |
---|
| 141 | return null; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getParent().getName()); |
---|
| 145 | result.addAll(choosenResources); |
---|
| 146 | map.put(ResourceParameterName.PROCESSINGELEMENTS, result); |
---|
| 147 | } |
---|
| 148 | return map; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | private void adjustFrequency(ResourceStatus status, List<CPU> processors){ |
---|
| 152 | switch(status){ |
---|
| 153 | case BUSY: |
---|
| 154 | for(CPU cpu: processors){ |
---|
| 155 | cpu.getPowerInterface().setPState(PStateType.P0); |
---|
| 156 | } |
---|
| 157 | break; |
---|
| 158 | case FREE: |
---|
| 159 | for(CPU cpu: processors){ |
---|
| 160 | cpu.getPowerInterface().setPState(PStateType.P3); |
---|
| 161 | } |
---|
| 162 | break; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | public String getName() { |
---|
| 167 | return getClass().getName(); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | public void init(Properties properties) { |
---|
| 171 | // no extra initialization is expected. |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | } |
---|