source: xssim/trunk/src/example/localplugin/FCFSClusterLocalPlugin.java @ 129

Revision 129, 5.8 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                                       
65                                        /****************3 ways to schedule task****************/
66                                       
67                                        //1. Choosing particular resources to perform execution
68                                        Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, subTask);
69                                        if (choosenResources != null) {
70                                                addToSchedulingPlan(plan, task, choosenResources);
71                                        }
72                                       
73                                        //2. Choosing resource scheduler/provider to submit task.  If the given resource doesn't contains/isn't
74                                        //a scheduler, random resources from the given resource will be chosen in order to perform execution
75                                        /*String provName = chooseProviderForExecution(resourceManager);
76                                        if (provName != null) {
77                                                addToSchedulingPlan(plan, task, provName);
78                                        }*/
79
80                                        //3. Scheduler will choose random resources to perform execution
81                                        //addToSchedulingPlan(plan, task);
82
83                                }
84                        }
85
86                        break;
87                }
88                return plan;
89        }
90
91        private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(
92                        ClusterResourceManager resourceManager, ExecTaskInterface task) {
93
94                HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>();
95
96                int cpuRequest;
97                try {
98                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
99                } catch (NoSuchFieldException e) {
100                        cpuRequest = 1;
101                }
102
103                if (cpuRequest != 0) {
104                        List<ComputingResource> choosenResources = null;
105                        List<CPU> processors = null;
106                        processors = resourceManager.getProcessors();
107                        if (processors.size() < cpuRequest) {
108                                // log.warn("Task requires more cpus than is availiable in this resource.");
109                                return null;
110                        }
111
112                        choosenResources = new ArrayList<ComputingResource>();
113
114                        for (int i = 0; i < processors.size() && cpuRequest > 0; i++) {
115                                if (processors.get(i).getStatus() == ResourceStatus.FREE) {
116                                        choosenResources.add(processors.get(i));
117                                        cpuRequest--;
118                                }
119                        }
120                        if (cpuRequest > 0) {
121                                // log.info("Task " + task.getJobId() + "_" + task.getId() +
122                                // " requires more cpus than is availiable in this moment.");
123                                return null;
124                        }
125
126                        ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName());
127                        result.addAll(choosenResources);
128                        map.put(ResourceParameterName.PROCESSINGELEMENTS, result);
129
130                }
131                int memoryRequest;
132                try {
133                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
134                } catch (NoSuchFieldException e) {
135                        memoryRequest = 0;
136                }
137                if (memoryRequest != 0) {
138                        List<ComputingNode> nodes = resourceManager.getComputingNodes();
139
140                        Memory memory = null;
141                        for (ComputingNode node : nodes) {
142
143                                if (node.getFreeMemory() >= memoryRequest) {
144                                        memory = new Memory(node.getMemory(), memoryRequest, memoryRequest);
145                                } else
146                                        return null;
147                        }
148                        map.put(ResourceParameterName.MEMORY, memory);
149                }
150
151                return map;
152        }
153
154        private String chooseProviderForExecution(ResourceManagerInterface unitsManager) {
155                List<ComputingResource> processingElements;
156                Properties properties = new Properties();
157                properties.setProperty("type", ResourceType.COMPUTING_NODE.toString());
158                // properties.setProperty("status", ResourceStatus.FREE.toString());
159                processingElements = (List<ComputingResource>) unitsManager.filterResources(properties);
160                return processingElements.get(0).getName();
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}
Note: See TracBrowser for help on using the repository browser.