source: xssim/trunk/src/example/localplugin/FCFSLocalPlugin.java @ 104

Revision 104, 6.9 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.localplugin;
2
3import gridsim.Gridlet;
4import gridsim.gssim.SubmittedTask;
5import gssim.schedframe.scheduling.ExecTaskInterface;
6
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11import java.util.Properties;
12
13import schedframe.resources.PowerState;
14import schedframe.resources.units.Memory;
15import schedframe.resources.units.ResourceUnit;
16import schedframe.scheduling.TaskInterface;
17import schedframe.scheduling.events.SchedulingEvent;
18import schedframe.scheduling.plugin.grid.ModuleList;
19import schedframe.scheduling.utils.ResourceParameterName;
20import test.rewolucja.GSSIMJobInterface;
21import test.rewolucja.resources.ProcessingElements;
22import test.rewolucja.resources.ResourceStatus;
23import test.rewolucja.resources.ResourceType;
24import test.rewolucja.resources.exception.ResourceException;
25import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
26import test.rewolucja.resources.physical.base.ComputingResource;
27import test.rewolucja.resources.physical.implementation.CPU;
28import test.rewolucja.resources.physical.implementation.ComputingNode;
29import test.rewolucja.scheduling.JobRegistryInterface;
30import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
31import test.rewolucja.scheduling.plan.SchedulingPlanNew;
32import test.rewolucja.scheduling.queue.GSSIMQueue;
33import test.rewolucja.scheduling.queue.QueueList;
34
35/**
36 *
37 * @author Marcin Krystek
38 *
39 */
40public class FCFSLocalPlugin extends BaseLocalPlugin {
41
42        public FCFSLocalPlugin() {
43        }
44
45        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
46                        ResourceManagerInterface resourceManager, ModuleList modules) {
47
48                SchedulingPlanNew plan = new SchedulingPlanNew();
49                // chose the events types to serve.
50                // Different actions for different events are possible.
51                switch (event.getType()) {
52                case START_TASK_EXECUTION:
53                case TASK_FINISHED:
54                        // our tasks are placed only in first queue (see
55                        // BaseLocalPlugin.placeTasksInQueues() method)
56                        GSSIMQueue q = queues.get(0);
57                        // check all tasks in queue
58
59                        /*if(q.size()>0){
60                                ComputingNode cn = null;
61                                try {
62                                        cn = (ComputingNode)resourceManager.getResourcesOfType(ResourceType.COMPUTING_NODE).get(0);
63                                } catch (ResourceException e) {
64                                        // TODO Auto-generated catch block
65                                        e.printStackTrace();
66                                }
67                                if( cn.getPowerInterface().getPowerState()==PowerState.OFF){
68                                        cn.getPowerInterface().setPowerState(PowerState.ON);
69                                }
70                        }*/
71                        for (int i = 0; i < q.size(); i++) {
72                                GSSIMJobInterface<?> job = q.get(i);
73                                TaskInterface<?> task = (TaskInterface<?>) job;
74                                // if status of the tasks in READY
75                                if (task.getStatus() == Gridlet.READY) {
76
77                                        SubmittedTask subTask = (SubmittedTask) task;
78                                        // DataCenterResourceManager dcrm =
79                                        // (DataCenterResourceManager)unitsManager;
80                                        // List<CPU> cpus = dcrm.getProcessors();
81                                        // cpus.get(0).getPowerInterface().setFrequency(100);
82                                        Map<ResourceParameterName, ResourceUnit> choosenResources = null;
83                                        choosenResources = chooseResourcesForExecution(resourceManager, subTask);
84                                        // String name =
85                                        // chooseProviderForExecution(resourceManager);
86                                        if (choosenResources != null) {
87                                                // q.remove(i);
88                                                // i--;
89                                                addToSchedulingPlan(plan, task, choosenResources);
90                                        }
91
92                                }
93                        }
94
95                        /*ComputingNode cn = null;
96                        try {
97                                cn = (ComputingNode)resourceManager.getResourcesOfType(ResourceType.COMPUTING_NODE).get(0);
98                        } catch (ResourceException e) {
99                                // TODO Auto-generated catch block
100                                e.printStackTrace();
101                        }
102                        if(cn.getFreeProcessorsNumber()==3){
103                                cn.getPowerInterface().setPowerState(PowerState.OFF);
104                        }*/
105                        break;
106                }
107                return plan;
108        }
109
110        public HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(
111                        ResourceManagerInterface resourceManager, ExecTaskInterface task) {
112
113                HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>();
114
115                int cpuRequest;
116                try {
117                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
118                } catch (NoSuchFieldException e) {
119                        cpuRequest = 1;
120                }
121
122                if (cpuRequest != 0) {
123                        List<ComputingResource> choosenResources = null;
124                        List<ComputingResource> processingElements = null;
125                        try {
126                                Properties properties = new Properties();
127                                properties.setProperty("type", ResourceType.CPU.toString());
128                                // properties.setProperty("status",
129                                // ResourceStatus.FREE.toString());
130                                processingElements = (List<ComputingResource>) resourceManager.filterResources(properties);
131                        } catch (Exception e) {
132                                // TODO Auto-generated catch block
133                                e.printStackTrace();
134                        }
135                        if (processingElements.size() < cpuRequest) {
136                                // log.warn("Task requires more cpus than is availiable in this resource.");
137                                return null;
138                        }
139
140                        choosenResources = new ArrayList<ComputingResource>();
141
142                        for (int i = 0; i < processingElements.size() && cpuRequest > 0; i++) {
143                                if (processingElements.get(i).getStatus() == ResourceStatus.FREE) {
144                                        choosenResources.add(processingElements.get(i));
145                                        cpuRequest--;
146                                        CPU cpu = (CPU)processingElements.get(i);
147                                        //cpu.getParent().getPowerProfile().setPowerState(PowerState.OFF);
148                                }
149                        }
150                        if (cpuRequest > 0) {
151                                // log.info("Task " + task.getJobId() + "_" + task.getId() +
152                                // " requires more cpus than is availiable in this moment.");
153                                return null;
154                        }
155
156                        ProcessingElements result = new ProcessingElements(processingElements.get(0).getParent().getName());
157                        result.addAll(choosenResources);
158                        map.put(ResourceParameterName.PROCESSINGELEMENTS, result);
159
160                }
161                int memoryRequest;
162                try {
163                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
164                } catch (NoSuchFieldException e) {
165                        memoryRequest = 0;
166                }
167                if (memoryRequest != 0) {
168                        List<ComputingNode> nodes = null;
169                        try {
170                                Properties properties = new Properties();
171                                properties.setProperty("type", ResourceType.COMPUTING_NODE.toString());
172                                nodes = (List<ComputingNode>) resourceManager.filterResources(properties);
173                        } catch (Exception e) {
174                                // TODO Auto-generated catch block
175                                e.printStackTrace();
176                        }
177                        Memory memory = null;
178                        for (ComputingNode node : nodes) {
179
180                                if (node.getFreeMemory() >= memoryRequest) {
181                                        memory = new Memory(node.getMemory(), memoryRequest, memoryRequest);
182                                } else
183                                        return null;
184                        }
185                        map.put(ResourceParameterName.MEMORY, memory);
186
187                }
188
189                return map;
190        }
191
192        public String getName() {
193                return getClass().getName();
194        }
195
196        public void init(Properties properties) {
197                // no extra initialization is expected.
198        }
199
200        public String chooseProviderForExecution(ResourceManagerInterface unitsManager) {
201                List<ComputingResource> processingElements;
202                Properties properties = new Properties();
203                properties.setProperty("type", ResourceType.COMPUTING_NODE.toString());
204                // properties.setProperty("status", ResourceStatus.FREE.toString());
205                processingElements = (List<ComputingResource>) unitsManager.filterResources(properties);
206                return processingElements.get(0).getName();
207        }
208
209}
Note: See TracBrowser for help on using the repository browser.