1 | package example.localplugin; |
---|
2 | |
---|
3 | import gridsim.Gridlet; |
---|
4 | |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.List; |
---|
7 | import java.util.Properties; |
---|
8 | import java.util.Random; |
---|
9 | |
---|
10 | import schedframe.scheduling.TaskInterface; |
---|
11 | import schedframe.scheduling.events.SchedulingEvent; |
---|
12 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
13 | import test.rewolucja.GSSIMJobInterface; |
---|
14 | import test.rewolucja.energy.profile.PStateType; |
---|
15 | import test.rewolucja.resources.manager.implementation.ClusterResourceManager; |
---|
16 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
17 | import test.rewolucja.resources.physical.implementation.ComputingNode; |
---|
18 | import test.rewolucja.resources.physical.implementation.Processor; |
---|
19 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
20 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
21 | import test.rewolucja.scheduling.plan.SchedulingPlanNew; |
---|
22 | import test.rewolucja.scheduling.queue.Queue; |
---|
23 | import test.rewolucja.scheduling.queue.QueueList; |
---|
24 | |
---|
25 | public class FCFSRandomClusterLocalPlugin extends BaseLocalPlugin { |
---|
26 | |
---|
27 | private Random rand; |
---|
28 | private boolean init = true; |
---|
29 | |
---|
30 | public FCFSRandomClusterLocalPlugin() { |
---|
31 | rand = new Random(5); |
---|
32 | } |
---|
33 | |
---|
34 | public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry, |
---|
35 | ResourceManagerInterface resManager, ModuleList modules) { |
---|
36 | |
---|
37 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
38 | |
---|
39 | if( init) |
---|
40 | { |
---|
41 | List<Processor> cpus = resourceManager.getProcessors(); |
---|
42 | |
---|
43 | for( Processor cpu : cpus) |
---|
44 | cpu.getPowerInterface().setPState( PStateType.P3); |
---|
45 | |
---|
46 | init = false; |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | SchedulingPlanNew plan = new SchedulingPlanNew(); |
---|
52 | // chose the events types to serve. |
---|
53 | // Different actions for different events are possible. |
---|
54 | switch (event.getType()) { |
---|
55 | case START_TASK_EXECUTION: |
---|
56 | case TASK_FINISHED: |
---|
57 | // our tasks are placed only in first queue (see |
---|
58 | // BaseLocalPlugin.placeJobsInQueues() method) |
---|
59 | Queue q = queues.get(0); |
---|
60 | // check all tasks in queue |
---|
61 | |
---|
62 | for (int i = 0; i < q.size(); i++) { |
---|
63 | GSSIMJobInterface<?> job = q.get(i); |
---|
64 | TaskInterface<?> task = (TaskInterface<?>) job; |
---|
65 | // if status of the tasks in READY |
---|
66 | if (task.getStatus() == Gridlet.READY) { |
---|
67 | String nodeName = chooseRandomProvider(resourceManager, task); |
---|
68 | if (nodeName != null) { |
---|
69 | addToSchedulingPlan(plan, task, nodeName); |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | break; |
---|
74 | } |
---|
75 | return plan; |
---|
76 | } |
---|
77 | |
---|
78 | private List<ComputingNode> findSuitableNodes(int cpuRequest, List<ComputingNode> nodes){ |
---|
79 | List<ComputingNode> avNodes = new ArrayList<ComputingNode>(); |
---|
80 | for(ComputingNode node: nodes){ |
---|
81 | if(node.getFreeProcessorsNumber() >= cpuRequest){ |
---|
82 | avNodes.add(node); |
---|
83 | } |
---|
84 | } |
---|
85 | return avNodes; |
---|
86 | } |
---|
87 | |
---|
88 | private String chooseRandomProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) { |
---|
89 | |
---|
90 | int cpuRequest; |
---|
91 | try { |
---|
92 | cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue(); |
---|
93 | } catch (NoSuchFieldException e) { |
---|
94 | cpuRequest = 1; |
---|
95 | } |
---|
96 | |
---|
97 | List<ComputingNode> nodes = resourceManager.getComputingNodes(); |
---|
98 | |
---|
99 | nodes = findSuitableNodes(cpuRequest, nodes); |
---|
100 | |
---|
101 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
102 | return nodes.get(nodeIdx).getName(); |
---|
103 | } |
---|
104 | |
---|
105 | public String getName() { |
---|
106 | return getClass().getName(); |
---|
107 | } |
---|
108 | |
---|
109 | public void init(Properties properties) { |
---|
110 | // no extra initialization is expected. |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|
114 | |
---|