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

Revision 256, 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        public FCFSPreferedRandomClusterLocalPlugin() {
34                rand = new Random(5);
35        }
36
37        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
38                         ResourceManagerInterface resManager, ModuleList modules) {
39               
40                int scenario = 1;
41               
42                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
43               
44                if( init)
45                {
46                        List<Processor> cpus = resourceManager.getProcessors();
47                       
48                        for( Processor cpu : cpus)
49                                cpu.getPowerInterface().setPState( PStateType.P0);
50                       
51                        init = false;
52                }
53               
54
55               
56                SchedulingPlanNew plan = new SchedulingPlanNew();
57                // chose the events types to serve.
58                // Different actions for different events are possible.
59                switch (event.getType()) {
60                case START_TASK_EXECUTION:
61                case TASK_FINISHED:
62                        // our tasks are placed only in first queue (see
63                        // BaseLocalPlugin.placeJobsInQueues() method)
64                        Queue q = queues.get(0);
65                        // check all tasks in queue
66
67                        for (int i = 0; i < q.size(); i++) {
68                                GSSIMJobInterface<?> job = q.get(i);
69                                TaskInterface<?> task = (TaskInterface<?>) job;
70                                // if status of the tasks in READY
71                                if (task.getStatus() == Gridlet.READY) {
72                                       
73                                        ComputingNode node = chooseRandomProvider(resourceManager, task);
74                                       
75                                        if (node != null) {
76                                                List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task);
77                                                addToSchedulingPlan(plan, task, cpus);
78                                        }
79                                        else
80                                        {
81                                                switch( scenario)
82                                                {
83                                                case 0: break;
84                                                case 1: break;
85                                                case 2:
86                                                        findSuitableNodes( "", getCpuRequest(task), ResourceStatus.UNAVAILABLE, resourceManager.getComputingNodes());
87                                                        break;
88                                                }
89                                        }
90                                       
91                                       
92                                }
93                        }
94                       
95                        switch( scenario)
96                        {
97                                case 0: break;
98                                case 1: 
99                               
100                                        for( Processor cpu : resourceManager.getProcessors())
101                                        {
102                                                switch( cpu.getStatus())
103                                                {
104                                                        case FREE: cpu.getPowerInterface().setPState( PStateType.P3); break;
105                                                        case PENDING: cpu.getPowerInterface().setPState( PStateType.P0); break;
106                                                }
107                                               
108                                        }
109                                        break;
110                               
111                                case 2:
112                                        for( Processor cpu : resourceManager.getProcessors())
113                                        {
114                                                switch( cpu.getStatus())
115                                                {
116                                                        case FREE: cpu.getPowerInterface().setPowerState( PowerState.OFF); break;
117                                                }       
118                                        }
119                                        break;
120                               
121                                default: break;
122                        }
123                       
124                        break;
125                }
126                return plan;
127        }
128
129        private List<ComputingNode> findSuitableNodes(String model, int cpuRequest, ResourceStatus status, List<ComputingNode> nodes){
130                List<ComputingNode> avNodes = new ArrayList<ComputingNode>();
131                for(ComputingNode node: nodes)
132                {
133                        if( (model == null || node.getCategory().getName().equals(model)))
134                        {       
135                                @SuppressWarnings("unchecked")
136                                List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status);
137                               
138                                if( cpus.size() >= cpuRequest)
139                                        avNodes.add(node);
140                        }
141                }
142                return avNodes;
143        }
144       
145        private int getCpuRequest( TaskInterface<?> task)
146        {
147                int cpuRequest;
148               
149                try {
150                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
151                } catch (NoSuchFieldException e) {
152                        cpuRequest = 1;
153                }
154               
155                return cpuRequest;
156        }
157       
158        private ComputingNode chooseRandomProvider(ClusterResourceManager resourceManager, TaskInterface<?> task) {
159               
160                int cpuRequest = getCpuRequest(task);
161               
162                List<ComputingNode> nodes = null;
163               
164                String prefered = null;
165               
166                nodes = findSuitableNodes(prefered, cpuRequest, ResourceStatus.FREE, resourceManager.getComputingNodes());
167                if( nodes.size() > 0)
168                {       
169                        int nodeIdx = rand.nextInt(nodes.size());
170                        ComputingNode node = nodes.get(nodeIdx);
171                                               
172                        return node;
173                }
174                else
175                {
176                        System.out.println("NO resources: " + prefered);
177                }
178               
179                if( prefered != null)
180                {       
181                        if( prefered.equals("A"))
182                                prefered = "B";
183                        else
184                                prefered = "A";
185                       
186                        nodes = findSuitableNodes(prefered, cpuRequest, ResourceStatus.FREE, 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}
218
Note: See TracBrowser for help on using the repository browser.