source: xssim/src/example/localplugin/FCFS_EBFLocalPlugin.java @ 104

Revision 104, 5.7 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.units.Memory;
14import schedframe.resources.units.ResourceUnit;
15import schedframe.scheduling.TaskInterface;
16import schedframe.scheduling.events.SchedulingEvent;
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.interfaces.ResourceManagerInterface;
24import test.rewolucja.resources.physical.base.ComputingResource;
25import test.rewolucja.resources.physical.implementation.CPU;
26import test.rewolucja.resources.physical.implementation.ComputingNode;
27import test.rewolucja.scheduling.JobRegistryInterface;
28import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
29import test.rewolucja.scheduling.plan.SchedulingPlanNew;
30import test.rewolucja.scheduling.queue.GSSIMQueue;
31import test.rewolucja.scheduling.queue.QueueList;
32
33public class FCFS_EBFLocalPlugin extends BaseLocalPlugin {
34
35        public FCFS_EBFLocalPlugin() {
36        }
37
38        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
39                        ResourceManagerInterface resourceManager, ModuleList modules) {
40
41                SchedulingPlanNew plan = new SchedulingPlanNew();
42                // chose the events types to serve.
43                // Different actions for different events are possible.
44                switch (event.getType()) {
45                case START_TASK_EXECUTION:
46                case TASK_FINISHED:
47                        // our tasks are placed only in first queue (see
48                        // BaseLocalPlugin.placeTasksInQueues() method)
49                        GSSIMQueue q = queues.get(0);
50                        // check all tasks in queue
51
52                        for (int i = 0; i < q.size(); i++) {
53                                GSSIMJobInterface<?> job = q.get(i);
54                                TaskInterface<?> task = (TaskInterface<?>) job;
55                                // if status of the tasks in READY
56                                if (task.getStatus() == Gridlet.READY) {
57
58                                        SubmittedTask subTask = (SubmittedTask) task;
59                                        // DataCenterResourceManager dcrm =
60                                        // (DataCenterResourceManager)unitsManager;
61                                        // List<CPU> cpus = dcrm.getProcessors();
62                                        // cpus.get(0).getPowerInterface().setFrequency(100);
63                                        Map<ResourceParameterName, ResourceUnit> choosenResources = null;
64                                        choosenResources = chooseResourcesForExecution(resourceManager, subTask);
65                                        // String name =
66                                        // chooseProviderForExecution(resourceManager);
67                                        if (choosenResources != null) {
68                                                // q.remove(i);
69                                                // i--;
70                                                addToSchedulingPlan(plan, task, choosenResources);
71                                        }
72                                }
73                        }
74
75                }
76                return plan;
77        }
78
79        public HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(
80                        ResourceManagerInterface unitsManager, ExecTaskInterface task) {
81
82                HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>();
83
84                int cpuRequest;
85                try {
86                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
87                } catch (NoSuchFieldException e) {
88                        cpuRequest = 1;
89                }
90
91                if (cpuRequest != 0) {
92                        List<ComputingResource> choosenResources = null;
93                        List<ComputingResource> processingElements = null;
94                        try {
95                                Properties properties = new Properties();
96                                properties.setProperty("type", ResourceType.CPU.toString());
97                                // properties.setProperty("status",
98                                // ResourceStatus.FREE.toString());
99                                processingElements = (List<ComputingResource>) unitsManager.filterResources(properties);
100                        } catch (Exception e) {
101                                // TODO Auto-generated catch block
102                                e.printStackTrace();
103                        }
104                        if (processingElements.size() < cpuRequest) {
105                                // log.warn("Task requires more cpus than is availiable in this resource.");
106                                return null;
107                        }
108
109                        choosenResources = new ArrayList<ComputingResource>();
110
111                        for (int i = 0; i < processingElements.size() && cpuRequest > 0; i++) {
112                                if (processingElements.get(i).getStatus() == ResourceStatus.FREE) {
113                                        choosenResources.add(processingElements.get(i));
114                                        cpuRequest--;
115                                        CPU cpu = (CPU)processingElements.get(i);
116                                        //cpu.getParent().getPowerProfile().setPowerState(PowerState.OFF);
117                                }
118                        }
119                        if (cpuRequest > 0) {
120                                // log.info("Task " + task.getJobId() + "_" + task.getId() +
121                                // " requires more cpus than is availiable in this moment.");
122                                return null;
123                        }
124
125                        ProcessingElements result = new ProcessingElements(processingElements.get(0).getParent().getName());
126                        result.addAll(choosenResources);
127                        map.put(ResourceParameterName.PROCESSINGELEMENTS, result);
128
129                }
130                int memoryRequest;
131                try {
132                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
133                } catch (NoSuchFieldException e) {
134                        memoryRequest = 0;
135                }
136                if (memoryRequest != 0) {
137                        List<ComputingNode> nodes = null;
138                        try {
139                                Properties properties = new Properties();
140                                properties.setProperty("type", ResourceType.COMPUTING_NODE.toString());
141                                nodes = (List<ComputingNode>) unitsManager.filterResources(properties);
142                        } catch (Exception e) {
143                                // TODO Auto-generated catch block
144                                e.printStackTrace();
145                        }
146                        Memory memory = null;
147                        for (ComputingNode node : nodes) {
148
149                                if (node.getFreeMemory() >= memoryRequest) {
150                                        memory = new Memory(node.getMemory(), memoryRequest, memoryRequest);
151                                } else
152                                        return null;
153                        }
154                        map.put(ResourceParameterName.MEMORY, memory);
155
156                }
157
158                return map;
159        }
160
161        public String getName() {
162                return getClass().getName();
163        }
164
165        public void init(Properties properties) {
166                // no extra initialization is expected.
167        }
168
169}
Note: See TracBrowser for help on using the repository browser.