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

Revision 104, 5.2 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.manager.implementation.ClusterResourceManager;
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
35public class FCFSClusterLocalPlugin extends BaseLocalPlugin {
36
37        public FCFSClusterLocalPlugin() {
38        }
39
40        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
41                        ResourceManagerInterface resManager, ModuleList modules) {
42
43                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
44                SchedulingPlanNew plan = new SchedulingPlanNew();
45                // chose the events types to serve.
46                // Different actions for different events are possible.
47                switch (event.getType()) {
48                case START_TASK_EXECUTION:
49                case TASK_FINISHED:
50                case TIMER:
51                        // our tasks are placed only in first queue (see
52                        // BaseLocalPlugin.placeTasksInQueues() method)
53                        GSSIMQueue q = queues.get(0);
54                        // check all tasks in queue
55
56
57                        for (int i = 0; i < q.size(); i++) {
58                                GSSIMJobInterface<?> job = q.get(i);
59                                TaskInterface<?> task = (TaskInterface<?>) job;
60                                // if status of the tasks in READY
61                                if (task.getStatus() == Gridlet.READY) {
62
63                                        SubmittedTask subTask = (SubmittedTask) task;
64                                        Map<ResourceParameterName, ResourceUnit> choosenResources = null;
65                                        choosenResources = chooseResourcesForExecution(resourceManager, subTask);
66                                        //String provname = chooseProviderForExecution(resourceManager);
67                                        if (choosenResources != null) {
68                                                addToSchedulingPlan(plan, task, choosenResources);
69                                        }
70
71                                }
72                        }
73
74                        break;
75                }
76                return plan;
77        }
78
79        public HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(
80                        ClusterResourceManager resourceManager, 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<CPU> processors = null;
94                        processors = resourceManager.getProcessors();
95                        if (processors.size() < cpuRequest) {
96                                // log.warn("Task requires more cpus than is availiable in this resource.");
97                                return null;
98                        }
99
100                        choosenResources = new ArrayList<ComputingResource>();
101
102                        for (int i = 0; i < processors.size() && cpuRequest > 0; i++) {
103                                if (processors.get(i).getStatus() == ResourceStatus.FREE) {
104                                        choosenResources.add(processors.get(i));
105                                        cpuRequest--;
106                                }
107                        }
108                        if (cpuRequest > 0) {
109                                // log.info("Task " + task.getJobId() + "_" + task.getId() +
110                                // " requires more cpus than is availiable in this moment.");
111                                return null;
112                        }
113
114                        ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName());
115                        result.addAll(choosenResources);
116                        map.put(ResourceParameterName.PROCESSINGELEMENTS, result);
117
118                }
119                int memoryRequest;
120                try {
121                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
122                } catch (NoSuchFieldException e) {
123                        memoryRequest = 0;
124                }
125                if (memoryRequest != 0) {
126                        List<ComputingNode> nodes = resourceManager.getComputingNodes();
127
128                        Memory memory = null;
129                        for (ComputingNode node : nodes) {
130
131                                if (node.getFreeMemory() >= memoryRequest) {
132                                        memory = new Memory(node.getMemory(), memoryRequest, memoryRequest);
133                                } else
134                                        return null;
135                        }
136                        map.put(ResourceParameterName.MEMORY, memory);
137                }
138
139                return map;
140        }
141
142        public String getName() {
143                return getClass().getName();
144        }
145
146        public void init(Properties properties) {
147                // no extra initialization is expected.
148        }
149
150        public String chooseProviderForExecution(ResourceManagerInterface unitsManager) {
151                List<ComputingResource> processingElements;
152                Properties properties = new Properties();
153                properties.setProperty("type", ResourceType.COMPUTING_NODE.toString());
154                // properties.setProperty("status", ResourceStatus.FREE.toString());
155                processingElements = (List<ComputingResource>) unitsManager.filterResources(properties);
156                return processingElements.get(0).getName();
157        }
158
159}
Note: See TracBrowser for help on using the repository browser.