source: xssim/branches/tpiontek/src/example/localplugin/FCFSRandomClusterLocalPlugin.java @ 250

Revision 250, 3.3 KB checked in by piontek, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.localplugin;
2
3import gridsim.Gridlet;
4
5import java.util.ArrayList;
6import java.util.List;
7import java.util.Properties;
8import java.util.Random;
9
10import schedframe.scheduling.TaskInterface;
11import schedframe.scheduling.events.SchedulingEvent;
12import schedframe.scheduling.plugin.grid.ModuleList;
13import test.rewolucja.GSSIMJobInterface;
14import test.rewolucja.energy.profile.PStateType;
15import test.rewolucja.resources.manager.implementation.ClusterResourceManager;
16import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
17import test.rewolucja.resources.physical.implementation.ComputingNode;
18import test.rewolucja.resources.physical.implementation.Processor;
19import test.rewolucja.scheduling.JobRegistryInterface;
20import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
21import test.rewolucja.scheduling.plan.SchedulingPlanNew;
22import test.rewolucja.scheduling.queue.Queue;
23import test.rewolucja.scheduling.queue.QueueList;
24
25public 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                if( nodes.size() > 0)
102                {       
103                        int nodeIdx = rand.nextInt(nodes.size());
104                        return nodes.get(nodeIdx).getName();
105                }
106                else
107                        return null;
108        }
109       
110        public String getName() {
111                return getClass().getName();
112        }
113
114        public void init(Properties properties) {
115                // no extra initialization is expected.
116        }
117
118}
119
Note: See TracBrowser for help on using the repository browser.