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

Revision 257, 5.7 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 = 1;
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                                                        //ComputingNode node = chooseRandomProvider(resourceManager, ResourceStatus.UNAVAILABLE, task);
88                                                        break;
89                                                }
90                                        }
91                                }
92                        }
93                       
94                        switch( scenario)
95                        {
96                                case 0: break;
97                                case 1: 
98                               
99                                        for( Processor cpu : resourceManager.getProcessors())
100                                        {
101                                                switch( cpu.getStatus())
102                                                {
103                                                        case FREE: cpu.getPowerInterface().setPState( PStateType.P3); break;
104                                                        case PENDING: cpu.getPowerInterface().setPState( PStateType.P0); break;
105                                                }
106                                               
107                                        }
108                                        break;
109                               
110                                case 2:
111                                        for( Processor cpu : resourceManager.getProcessors())
112                                        {
113                                                switch( cpu.getStatus())
114                                                {
115                                                        case FREE: cpu.getPowerInterface().setPowerState( PowerState.OFF); break;
116                                                }       
117                                        }
118                                        break;
119                               
120                                default: break;
121                        }
122                       
123                        break;
124                }
125                return plan;
126        }
127
128        private List<ComputingNode> findSuitableNodes(String model, int cpuRequest, ResourceStatus status, List<ComputingNode> nodes){
129                List<ComputingNode> avNodes = new ArrayList<ComputingNode>();
130                for(ComputingNode node: nodes)
131                {
132                        if( (model == null || node.getCategory().getName().equals(model)))
133                        {       
134                                @SuppressWarnings("unchecked")
135                                List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status);
136                               
137                                if( cpus.size() >= cpuRequest)
138                                        avNodes.add(node);
139                        }
140                }
141                return avNodes;
142        }
143       
144        private int getCpuRequest( TaskInterface<?> task)
145        {
146                int cpuRequest;
147               
148                try {
149                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
150                } catch (NoSuchFieldException e) {
151                        cpuRequest = 1;
152                }
153               
154                return cpuRequest;
155        }
156       
157        private ComputingNode chooseRandomProvider(ClusterResourceManager resourceManager, ResourceStatus status, TaskInterface<?> task) {
158               
159                int cpuRequest = getCpuRequest(task);
160               
161                List<ComputingNode> nodes = null;
162               
163                String prefered = null;
164               
165                nodes = findSuitableNodes(prefered, cpuRequest, status, resourceManager.getComputingNodes());
166                if( nodes.size() > 0)
167                {       
168                        int nodeIdx = rand.nextInt(nodes.size());
169                        ComputingNode node = nodes.get(nodeIdx);
170                                               
171                        return node;
172                }
173                else
174                {
175                        System.out.println("NO resources: " + prefered);
176                }
177               
178                if( prefered != null)
179                {       
180                        nodes = findSuitableNodes( getUnprefered(), cpuRequest, status, resourceManager.getComputingNodes());
181                        if( nodes.size() > 0)
182                        {       
183                                int nodeIdx = rand.nextInt(nodes.size());
184                                ComputingNode node = nodes.get(nodeIdx);
185                                                       
186                                return node;
187                        }
188                }
189                       
190                return null;
191        }
192       
193        public String getName() {
194                return getClass().getName();
195        }
196
197        public void init(Properties properties) {
198                // no extra initialization is expected.
199        }
200       
201        private List<Processor> chooseProcessorsForExecution( 
202                        ComputingNode node, ResourceStatus status, TaskInterface<?> task) {
203               
204                int cpuRequest = getCpuRequest(task);
205
206                List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status);
207               
208                return cpus.subList(0, cpuRequest);
209        }
210       
211        private String getUnprefered()
212        {
213                if( prefered.equals("A"))
214                        return "B";
215               
216                if( prefered.equals("B"))
217                        return "A";
218               
219                return null;
220        }
221
222}
223
Note: See TracBrowser for help on using the repository browser.