source: xssim/branches/tpiontek/src/example/localplugin/FCFSPreferedRandomClusterLocalPlugin.java @ 258

Revision 258, 6.0 KB checked in by piontek, 13 years ago (diff)
RevLine 
[252]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
[256]10import schedframe.resources.PowerState;
[252]11import schedframe.scheduling.TaskInterface;
12import schedframe.scheduling.events.SchedulingEvent;
13import schedframe.scheduling.plugin.grid.ModuleList;
14import test.rewolucja.GSSIMJobInterface;
15import test.rewolucja.energy.profile.PStateType;
16import test.rewolucja.resources.ResourceStatus;
[256]17import test.rewolucja.resources.ResourceType;
[252]18import test.rewolucja.resources.manager.implementation.ClusterResourceManager;
19import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
20import test.rewolucja.resources.physical.implementation.ComputingNode;
21import test.rewolucja.resources.physical.implementation.Processor;
22import test.rewolucja.scheduling.JobRegistryInterface;
23import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
24import test.rewolucja.scheduling.plan.SchedulingPlanNew;
25import test.rewolucja.scheduling.queue.Queue;
26import test.rewolucja.scheduling.queue.QueueList;
27
28public class FCFSPreferedRandomClusterLocalPlugin extends BaseLocalPlugin {
29
30        private Random rand;
31        private boolean init = true;
32       
[258]33        int scenario = 2;
[257]34        String prefered = null;
35       
[252]36        public FCFSPreferedRandomClusterLocalPlugin() {
37                rand = new Random(5);
38        }
39
40        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
41                         ResourceManagerInterface resManager, ModuleList modules) {
42               
43                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
44               
45                if( init)
46                {
47                        List<Processor> cpus = resourceManager.getProcessors();
48                       
49                        for( Processor cpu : cpus)
50                                cpu.getPowerInterface().setPState( PStateType.P0);
51                       
52                        init = false;
53                }
54               
55
56               
57                SchedulingPlanNew plan = new SchedulingPlanNew();
58                // chose the events types to serve.
59                // Different actions for different events are possible.
60                switch (event.getType()) {
61                case START_TASK_EXECUTION:
62                case TASK_FINISHED:
63                        // our tasks are placed only in first queue (see
64                        // BaseLocalPlugin.placeJobsInQueues() method)
65                        Queue q = queues.get(0);
66                        // check all tasks in queue
67
68                        for (int i = 0; i < q.size(); i++) {
69                                GSSIMJobInterface<?> job = q.get(i);
70                                TaskInterface<?> task = (TaskInterface<?>) job;
71                                // if status of the tasks in READY
72                                if (task.getStatus() == Gridlet.READY) {
[256]73                                       
[257]74                                        ComputingNode node = chooseRandomProvider(resourceManager, ResourceStatus.FREE, task);
[256]75                                       
76                                        if (node != null) {
77                                                List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task);
78                                                addToSchedulingPlan(plan, task, cpus);
[252]79                                        }
[256]80                                        else
81                                        {
82                                                switch( scenario)
83                                                {
84                                                case 0: break;
85                                                case 1: break;
86                                                case 2:
[258]87                                                        node = chooseRandomProvider(resourceManager, ResourceStatus.UNAVAILABLE, task);
88                                                        if( node != null)
89                                                        {       
90                                                                List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.UNAVAILABLE, task);
91                                                                for( Processor cpu: cpus)
92                                                                        cpu.getPowerInterface().setPowerState( PowerState.ON);
93                                                                }
[256]94                                                        break;
95                                                }
96                                        }
[252]97                                }
98                        }
99                       
100                        switch( scenario)
101                        {
102                                case 0: break;
[256]103                                case 1: 
[252]104                               
[256]105                                        for( Processor cpu : resourceManager.getProcessors())
[252]106                                        {
[256]107                                                switch( cpu.getStatus())
108                                                {
109                                                        case FREE: cpu.getPowerInterface().setPState( PStateType.P3); break;
110                                                        case PENDING: cpu.getPowerInterface().setPState( PStateType.P0); break;
111                                                }
112                                               
[252]113                                        }
[256]114                                        break;
115                               
116                                case 2:
117                                        for( Processor cpu : resourceManager.getProcessors())
118                                        {
119                                                switch( cpu.getStatus())
120                                                {
121                                                        case FREE: cpu.getPowerInterface().setPowerState( PowerState.OFF); break;
122                                                }       
123                                        }
124                                        break;
125                               
[252]126                                default: break;
127                        }
128                       
129                        break;
130                }
131                return plan;
132        }
133
[256]134        private List<ComputingNode> findSuitableNodes(String model, int cpuRequest, ResourceStatus status, List<ComputingNode> nodes){
[252]135                List<ComputingNode> avNodes = new ArrayList<ComputingNode>();
[256]136                for(ComputingNode node: nodes)
137                {
138                        if( (model == null || node.getCategory().getName().equals(model)))
139                        {       
140                                @SuppressWarnings("unchecked")
141                                List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status);
142                               
143                                if( cpus.size() >= cpuRequest)
144                                        avNodes.add(node);
[252]145                        }
146                }
147                return avNodes;
148        }
149       
[256]150        private int getCpuRequest( TaskInterface<?> task)
151        {
152                int cpuRequest;
[252]153               
154                try {
155                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
156                } catch (NoSuchFieldException e) {
157                        cpuRequest = 1;
158                }
159               
[256]160                return cpuRequest;
161        }
162       
[257]163        private ComputingNode chooseRandomProvider(ClusterResourceManager resourceManager, ResourceStatus status, TaskInterface<?> task) {
[256]164               
165                int cpuRequest = getCpuRequest(task);
166               
[252]167                List<ComputingNode> nodes = null;
168               
169                String prefered = null;
170               
[257]171                nodes = findSuitableNodes(prefered, cpuRequest, status, resourceManager.getComputingNodes());
[252]172                if( nodes.size() > 0)
173                {       
174                        int nodeIdx = rand.nextInt(nodes.size());
175                        ComputingNode node = nodes.get(nodeIdx);
[256]176                                               
177                        return node;
[252]178                }
[256]179                else
180                {
181                        System.out.println("NO resources: " + prefered);
182                }
[252]183               
184                if( prefered != null)
185                {       
[257]186                        nodes = findSuitableNodes( getUnprefered(), cpuRequest, status, resourceManager.getComputingNodes());
[252]187                        if( nodes.size() > 0)
188                        {       
189                                int nodeIdx = rand.nextInt(nodes.size());
190                                ComputingNode node = nodes.get(nodeIdx);
[256]191                                                       
192                                return node;
[252]193                        }
194                }
195                       
196                return null;
197        }
198       
199        public String getName() {
200                return getClass().getName();
201        }
202
203        public void init(Properties properties) {
204                // no extra initialization is expected.
205        }
[256]206       
207        private List<Processor> chooseProcessorsForExecution( 
208                        ComputingNode node, ResourceStatus status, TaskInterface<?> task) {
209               
210                int cpuRequest = getCpuRequest(task);
[252]211
[256]212                List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status);
213               
214                return cpus.subList(0, cpuRequest);
215        }
[257]216       
217        private String getUnprefered()
218        {
219                if( prefered.equals("A"))
220                        return "B";
221               
222                if( prefered.equals("B"))
223                        return "A";
224               
225                return null;
226        }
[256]227
[252]228}
229
Note: See TracBrowser for help on using the repository browser.