[104] | 1 | package example.localplugin; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | import gridsim.gssim.SubmittedTask; |
---|
| 5 | import gssim.schedframe.scheduling.ExecTaskInterface; |
---|
| 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.PowerState; |
---|
| 14 | import schedframe.resources.units.Memory; |
---|
| 15 | import schedframe.resources.units.ResourceUnit; |
---|
| 16 | import schedframe.scheduling.TaskInterface; |
---|
| 17 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 18 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 19 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 20 | import test.rewolucja.GSSIMJobInterface; |
---|
| 21 | import test.rewolucja.resources.ProcessingElements; |
---|
| 22 | import test.rewolucja.resources.ResourceStatus; |
---|
| 23 | import test.rewolucja.resources.ResourceType; |
---|
| 24 | import test.rewolucja.resources.exception.ResourceException; |
---|
| 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.resources.physical.implementation.ComputingNode; |
---|
| 29 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 30 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 31 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
| 32 | import test.rewolucja.scheduling.queue.GSSIMQueue; |
---|
| 33 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * |
---|
| 37 | * @author Marcin Krystek |
---|
| 38 | * |
---|
| 39 | */ |
---|
| 40 | public class FCFSLocalPlugin extends BaseLocalPlugin { |
---|
| 41 | |
---|
| 42 | public FCFSLocalPlugin() { |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
| 46 | ResourceManagerInterface resourceManager, ModuleList modules) { |
---|
| 47 | |
---|
| 48 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
| 49 | // chose the events types to serve. |
---|
| 50 | // Different actions for different events are possible. |
---|
| 51 | switch (event.getType()) { |
---|
| 52 | case START_TASK_EXECUTION: |
---|
| 53 | case TASK_FINISHED: |
---|
| 54 | // our tasks are placed only in first queue (see |
---|
| 55 | // BaseLocalPlugin.placeTasksInQueues() method) |
---|
| 56 | GSSIMQueue q = queues.get(0); |
---|
| 57 | // check all tasks in queue |
---|
| 58 | |
---|
| 59 | /*if(q.size()>0){ |
---|
| 60 | ComputingNode cn = null; |
---|
| 61 | try { |
---|
| 62 | cn = (ComputingNode)resourceManager.getResourcesOfType(ResourceType.COMPUTING_NODE).get(0); |
---|
| 63 | } catch (ResourceException e) { |
---|
| 64 | // TODO Auto-generated catch block |
---|
| 65 | e.printStackTrace(); |
---|
| 66 | } |
---|
| 67 | if( cn.getPowerInterface().getPowerState()==PowerState.OFF){ |
---|
| 68 | cn.getPowerInterface().setPowerState(PowerState.ON); |
---|
| 69 | } |
---|
| 70 | }*/ |
---|
| 71 | for (int i = 0; i < q.size(); i++) { |
---|
| 72 | GSSIMJobInterface<?> job = q.get(i); |
---|
| 73 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
| 74 | // if status of the tasks in READY |
---|
| 75 | if (task.getStatus() == Gridlet.READY) { |
---|
| 76 | |
---|
| 77 | SubmittedTask subTask = (SubmittedTask) task; |
---|
| 78 | // DataCenterResourceManager dcrm = |
---|
| 79 | // (DataCenterResourceManager)unitsManager; |
---|
| 80 | // List<CPU> cpus = dcrm.getProcessors(); |
---|
| 81 | // cpus.get(0).getPowerInterface().setFrequency(100); |
---|
| 82 | Map<ResourceParameterName, ResourceUnit> choosenResources = null; |
---|
| 83 | choosenResources = chooseResourcesForExecution(resourceManager, subTask); |
---|
| 84 | // String name = |
---|
| 85 | // chooseProviderForExecution(resourceManager); |
---|
| 86 | if (choosenResources != null) { |
---|
| 87 | // q.remove(i); |
---|
| 88 | // i--; |
---|
| 89 | addToSchedulingPlan(plan, task, choosenResources); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /*ComputingNode cn = null; |
---|
| 96 | try { |
---|
| 97 | cn = (ComputingNode)resourceManager.getResourcesOfType(ResourceType.COMPUTING_NODE).get(0); |
---|
| 98 | } catch (ResourceException e) { |
---|
| 99 | // TODO Auto-generated catch block |
---|
| 100 | e.printStackTrace(); |
---|
| 101 | } |
---|
| 102 | if(cn.getFreeProcessorsNumber()==3){ |
---|
| 103 | cn.getPowerInterface().setPowerState(PowerState.OFF); |
---|
| 104 | }*/ |
---|
| 105 | break; |
---|
| 106 | } |
---|
| 107 | return plan; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | public HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution( |
---|
| 111 | ResourceManagerInterface resourceManager, ExecTaskInterface 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<ComputingResource> processingElements = null; |
---|
| 125 | try { |
---|
| 126 | Properties properties = new Properties(); |
---|
| 127 | properties.setProperty("type", ResourceType.CPU.toString()); |
---|
| 128 | // properties.setProperty("status", |
---|
| 129 | // ResourceStatus.FREE.toString()); |
---|
| 130 | processingElements = (List<ComputingResource>) resourceManager.filterResources(properties); |
---|
| 131 | } catch (Exception e) { |
---|
| 132 | // TODO Auto-generated catch block |
---|
| 133 | e.printStackTrace(); |
---|
| 134 | } |
---|
| 135 | if (processingElements.size() < cpuRequest) { |
---|
| 136 | // log.warn("Task requires more cpus than is availiable in this resource."); |
---|
| 137 | return null; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | choosenResources = new ArrayList<ComputingResource>(); |
---|
| 141 | |
---|
| 142 | for (int i = 0; i < processingElements.size() && cpuRequest > 0; i++) { |
---|
| 143 | if (processingElements.get(i).getStatus() == ResourceStatus.FREE) { |
---|
| 144 | choosenResources.add(processingElements.get(i)); |
---|
| 145 | cpuRequest--; |
---|
| 146 | CPU cpu = (CPU)processingElements.get(i); |
---|
| 147 | //cpu.getParent().getPowerProfile().setPowerState(PowerState.OFF); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | if (cpuRequest > 0) { |
---|
| 151 | // log.info("Task " + task.getJobId() + "_" + task.getId() + |
---|
| 152 | // " requires more cpus than is availiable in this moment."); |
---|
| 153 | return null; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | ProcessingElements result = new ProcessingElements(processingElements.get(0).getParent().getName()); |
---|
| 157 | result.addAll(choosenResources); |
---|
| 158 | map.put(ResourceParameterName.PROCESSINGELEMENTS, result); |
---|
| 159 | |
---|
| 160 | } |
---|
| 161 | int memoryRequest; |
---|
| 162 | try { |
---|
| 163 | memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue(); |
---|
| 164 | } catch (NoSuchFieldException e) { |
---|
| 165 | memoryRequest = 0; |
---|
| 166 | } |
---|
| 167 | if (memoryRequest != 0) { |
---|
| 168 | List<ComputingNode> nodes = null; |
---|
| 169 | try { |
---|
| 170 | Properties properties = new Properties(); |
---|
| 171 | properties.setProperty("type", ResourceType.COMPUTING_NODE.toString()); |
---|
| 172 | nodes = (List<ComputingNode>) resourceManager.filterResources(properties); |
---|
| 173 | } catch (Exception e) { |
---|
| 174 | // TODO Auto-generated catch block |
---|
| 175 | e.printStackTrace(); |
---|
| 176 | } |
---|
| 177 | Memory memory = null; |
---|
| 178 | for (ComputingNode node : nodes) { |
---|
| 179 | |
---|
| 180 | if (node.getFreeMemory() >= memoryRequest) { |
---|
| 181 | memory = new Memory(node.getMemory(), memoryRequest, memoryRequest); |
---|
| 182 | } else |
---|
| 183 | return null; |
---|
| 184 | } |
---|
| 185 | map.put(ResourceParameterName.MEMORY, memory); |
---|
| 186 | |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | return map; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | public String getName() { |
---|
| 193 | return getClass().getName(); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | public void init(Properties properties) { |
---|
| 197 | // no extra initialization is expected. |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | public String chooseProviderForExecution(ResourceManagerInterface unitsManager) { |
---|
| 201 | List<ComputingResource> processingElements; |
---|
| 202 | Properties properties = new Properties(); |
---|
| 203 | properties.setProperty("type", ResourceType.COMPUTING_NODE.toString()); |
---|
| 204 | // properties.setProperty("status", ResourceStatus.FREE.toString()); |
---|
| 205 | processingElements = (List<ComputingResource>) unitsManager.filterResources(properties); |
---|
| 206 | return processingElements.get(0).getName(); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | } |
---|