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

Revision 258, 6.0 KB checked in by piontek, 13 years ago (diff)
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.resources.PowerState;
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;
17import test.rewolucja.resources.ResourceType;
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       
33        int scenario = 2;
34        String prefered = null;
35       
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) {
73                                       
74                                        ComputingNode node = chooseRandomProvider(resourceManager, ResourceStatus.FREE, task);
75                                       
76                                        if (node != null) {
77                                                List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task);
78                                                addToSchedulingPlan(plan, task, cpus);
79                                        }
80                                        else
81                                        {
82                                                switch( scenario)
83                                                {
84                                                case 0: break;
85                                                case 1: break;
86                                                case 2:
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                                                                }
94                                                        break;
95                                                }
96                                        }
97                                }
98                        }
99                       
100                        switch( scenario)
101                        {
102                                case 0: break;
103                                case 1: 
104                               
105                                        for( Processor cpu : resourceManager.getProcessors())
106                                        {
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                                               
113                                        }
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                               
126                                default: break;
127                        }
128                       
129                        break;
130                }
131                return plan;
132        }
133
134        private List<ComputingNode> findSuitableNodes(String model, int cpuRequest, ResourceStatus status, List<ComputingNode> nodes){
135                List<ComputingNode> avNodes = new ArrayList<ComputingNode>();
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);
145                        }
146                }
147                return avNodes;
148        }
149       
150        private int getCpuRequest( TaskInterface<?> task)
151        {
152                int cpuRequest;
153               
154                try {
155                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
156                } catch (NoSuchFieldException e) {
157                        cpuRequest = 1;
158                }
159               
160                return cpuRequest;
161        }
162       
163        private ComputingNode chooseRandomProvider(ClusterResourceManager resourceManager, ResourceStatus status, TaskInterface<?> task) {
164               
165                int cpuRequest = getCpuRequest(task);
166               
167                List<ComputingNode> nodes = null;
168               
169                String prefered = null;
170               
171                nodes = findSuitableNodes(prefered, cpuRequest, status, resourceManager.getComputingNodes());
172                if( nodes.size() > 0)
173                {       
174                        int nodeIdx = rand.nextInt(nodes.size());
175                        ComputingNode node = nodes.get(nodeIdx);
176                                               
177                        return node;
178                }
179                else
180                {
181                        System.out.println("NO resources: " + prefered);
182                }
183               
184                if( prefered != null)
185                {       
186                        nodes = findSuitableNodes( getUnprefered(), cpuRequest, status, resourceManager.getComputingNodes());
187                        if( nodes.size() > 0)
188                        {       
189                                int nodeIdx = rand.nextInt(nodes.size());
190                                ComputingNode node = nodes.get(nodeIdx);
191                                                       
192                                return node;
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        }
206       
207        private List<Processor> chooseProcessorsForExecution( 
208                        ComputingNode node, ResourceStatus status, TaskInterface<?> task) {
209               
210                int cpuRequest = getCpuRequest(task);
211
212                List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status);
213               
214                return cpus.subList(0, cpuRequest);
215        }
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        }
227
228}
229
Note: See TracBrowser for help on using the repository browser.