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