source: xssim/branches/tpiontek/src/example/localplugin/FCFSConsolidationClusterLocalPlugin.java @ 270

Revision 270, 6.1 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;
4import gridsim.gssim.ResourceHistoryItem;
5import gridsim.gssim.SubmittedTask;
6
7import java.util.ArrayList;
8import java.util.Collections;
9import java.util.Comparator;
10import java.util.List;
11import java.util.Properties;
12
13import schedframe.resources.PowerState;
14import schedframe.scheduling.TaskInterface;
15import schedframe.scheduling.events.SchedulingEvent;
16import schedframe.scheduling.events.TaskFinishedEvent;
17import schedframe.scheduling.plugin.grid.ModuleList;
18import schedframe.scheduling.utils.ResourceParameterName;
19import test.rewolucja.GSSIMJobInterface;
20import test.rewolucja.resources.ProcessingElements;
21import test.rewolucja.resources.ResourceStatus;
22import test.rewolucja.resources.ResourceType;
23import test.rewolucja.resources.manager.implementation.ClusterResourceManager;
24import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
25import test.rewolucja.resources.physical.base.ComputingResource;
26import test.rewolucja.resources.physical.implementation.ComputingNode;
27import test.rewolucja.resources.physical.implementation.Processor;
28import test.rewolucja.scheduling.JobRegistryInterface;
29import test.rewolucja.scheduling.UsedResourceList;
30import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
31import test.rewolucja.scheduling.plan.SchedulingPlanNew;
32import test.rewolucja.scheduling.queue.Queue;
33import test.rewolucja.scheduling.queue.QueueList;
34
35public class FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin {
36       
37        private int scenario = 1;
38
39        public FCFSConsolidationClusterLocalPlugin () {
40        }
41
42        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
43                        ResourceManagerInterface resManager, ModuleList modules) {
44
45                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
46                SchedulingPlanNew plan = new SchedulingPlanNew();
47                // chose the events types to serve.
48                // Different actions for different events are possible.
49                switch (event.getType()) {
50                case TASK_FINISHED:
51                        if( scenario == 1)
52                        {       
53                                TaskFinishedEvent finEvent = (TaskFinishedEvent) event;
54                                SubmittedTask subTask = jobRegistry.getSubmittedTask(finEvent.getJobId(), finEvent.getTaskId());
55                                UsedResourceList<ResourceHistoryItem> usedResourcesList = subTask.getUsedResources();
56                                ProcessingElements pes = (ProcessingElements)usedResourcesList.getLast().getResourceUnits().get(ResourceParameterName.PROCESSINGELEMENTS);
57                               
58                                ComputingNode cn = ((Processor)pes.get(0)).getComputingNode();
59                                if( cn.getFreeProcessors().size() == cn.getProcessorsNumber())
60                                        cn.getPowerInterface().setPowerState( PowerState.OFF);
61                        }
62                case START_TASK_EXECUTION:
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 = chooseResourcesForExecution(resourceManager, PowerState.ON, task);
75                                       
76                                        if (node  != null) {
77                                                List<Processor> cpus = chooseProcessorsForExecution(node, ResourceStatus.FREE, task);
78                                                //System.out.println("Uruchamiam na zasobie: " + node.getName());
79                                                addToSchedulingPlan(plan, task, cpus);
80                                        }
81                                        else
82                                        {
83                                                node = chooseResourcesForExecution(resourceManager, PowerState.OFF, task);
84                                               
85                                                if( node != null)
86                                                {
87                                                        //System.out.println("Wlaczam wezel: " + node.getName());
88                                                        node.getPowerInterface().setPowerState( PowerState.ON);
89                                                        i--;
90                                                }       
91                                        }
92                                }
93                        }
94                       
95                        switch( scenario)
96                        {
97                                case 0: break;
98                                case 1:
99                                        List<ComputingNode> nodes = resourceManager.getComputingNodes();
100                                        for( ComputingNode node : nodes)
101                                                if( node.getFreeProcessors().size() == node.getProcessorsNumber())
102                                                {
103                                                        //System.out.println("Wylaczam zasob" + node.getName());
104                                                        node.getPowerInterface().setPowerState( PowerState.OFF);
105                                                        //System.out.println("Zasob  w stanie: " + node.getPowerInterface().getPowerState());
106                                                }
107                                        break;
108                        }
109                       
110
111                        break;
112                }
113               
114                return plan;
115        }
116       
117        private ComputingNode chooseResourcesForExecution(ClusterResourceManager resourceManager, PowerState status, TaskInterface<?> task) {
118
119                //System.out.println("Szukam zasobow w stanie: "  +status);
120               
121                List<ComputingNode> nodes = resourceManager.getComputingNodes();
122                nodes = findSuitableNodes(task, status, nodes);
123               
124                Collections.sort(nodes, new Comparator<ComputingNode>(){
125                    public int compare(ComputingNode node1, ComputingNode node2){   
126                        return node1.getCategory().getName().compareTo(node2.getCategory().getName());
127                    }
128                });
129               
130                if(nodes.size() > 0)
131                        return nodes.get(0);
132                                 
133                return null;
134        }
135       
136        private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, PowerState status, List<ComputingNode> nodes){
137               
138                int cpuRequest = getCpuRequest(task);
139               
140                List<ComputingNode> suitableNodes = new ArrayList<ComputingNode>();
141                       
142                for(ComputingNode node: nodes)
143                {
144                        if( node.getPowerInterface().getPowerState() == status)
145                                switch( status)
146                                {
147                                        case ON:
148                                                if(node.getFreeProcessorsNumber() >= cpuRequest)
149                                                {
150                                                        suitableNodes.add(node);
151                                                }       
152                                                break;
153                                        case OFF:
154                                                if( node.getProcessorsNumber() >= cpuRequest)
155                                                        suitableNodes.add(node);
156                                                break;
157                                }
158                }
159       
160                return suitableNodes;
161        }
162       
163        public String getName() {
164                return getClass().getName();
165        }
166
167        public void init(Properties properties) {
168                // no extra initialization is expected.
169        }
170       
171        private List<Processor> chooseProcessorsForExecution(
172                        ComputingNode node, ResourceStatus status, TaskInterface<?> task) {
173               
174                int cpuRequest = getCpuRequest(task);
175
176                List<Processor> cpus = (List<Processor>)node.getDescendantsByTypeAndStatus( ResourceType.CPU, status);
177               
178                return cpus.subList(0, cpuRequest);
179        }
180       
181        private int getCpuRequest( TaskInterface<?> task)
182        {
183                int cpuRequest;
184               
185                try {
186                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
187                } catch (NoSuchFieldException e) {
188                        cpuRequest = 1;
189                }
190               
191                return cpuRequest;
192        }
193}
Note: See TracBrowser for help on using the repository browser.