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

Revision 204, 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.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.implementation.ClusterResourceManager;
24import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
25import test.rewolucja.resources.physical.base.ComputingResource;
26import test.rewolucja.resources.physical.implementation.Processor;
27import test.rewolucja.resources.physical.implementation.ComputingNode;
28import test.rewolucja.scheduling.JobRegistryInterface;
29import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
30import test.rewolucja.scheduling.plan.SchedulingPlanNew;
31import test.rewolucja.scheduling.queue.Queue;
32import test.rewolucja.scheduling.queue.QueueList;
33
34public class FCFSClusterLocalPlugin extends BaseLocalPlugin {
35
36        public FCFSClusterLocalPlugin() {
37        }
38
39        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
40                        ResourceManagerInterface resManager, ModuleList modules) {
41
42                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
43                SchedulingPlanNew plan = new SchedulingPlanNew();
44                // chose the events types to serve.
45                // Different actions for different events are possible.
46                switch (event.getType()) {
47                case START_TASK_EXECUTION:
48                case TASK_FINISHED:
49                case TIMER:
50                        // our tasks are placed only in first queue (see
51                        // BaseLocalPlugin.placeJobsInQueues() method)
52                        Queue q = queues.get(0);
53                        // check all tasks in queue
54
55                        for (int i = 0; i < q.size(); i++) {
56                                GSSIMJobInterface<?> job = q.get(i);
57                                TaskInterface<?> task = (TaskInterface<?>) job;
58                                // if status of the tasks in READY
59                                if (task.getStatus() == Gridlet.READY) {
60
61                                        SubmittedTask subTask = (SubmittedTask) task;
62                                       
63                                        /****************3 ways to schedule task****************/
64                                       
65                                        /****************1. Choosing particular resources to perform execution****************/
66                                        Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, subTask);
67                                        if (choosenResources != null) {
68                                                addToSchedulingPlan(plan, task, choosenResources);
69                                        }
70                                       
71                                        /****************2. Choosing resource scheduler/provider to submit task.  If the given resource doesn't contains/isn't
72                                        a scheduler, random resources from the given resource will be chosen in order to perform execution****************/
73                                        /*String provName = chooseProviderForExecution(resourceManager);
74                                        if (provName != null) {
75                                                addToSchedulingPlan(plan, task, provName);
76                                        }*/
77
78                                        /****************3. Scheduler will choose random resources to perform execution****************/
79                                        //addToSchedulingPlan(plan, task);
80                                }
81                        }
82
83                        break;
84                }
85                return plan;
86        }
87
88        private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(
89                        ClusterResourceManager resourceManager, ExecTaskInterface task) {
90
91                HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>();
92
93                int cpuRequest;
94                try {
95                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
96                } catch (NoSuchFieldException e) {
97                        cpuRequest = 1;
98                }
99
100                if (cpuRequest != 0) {
101                        List<ComputingResource> choosenResources = null;
102                        List<Processor> processors = null;
103                        processors = resourceManager.getProcessors();
104                        if (processors.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 < processors.size() && cpuRequest > 0; i++) {
112                                if (processors.get(i).getStatus() == ResourceStatus.FREE) {
113                                        choosenResources.add(processors.get(i));
114                                        cpuRequest--;
115                                }
116                        }
117                        if (cpuRequest > 0) {
118                                // log.info("Task " + task.getJobId() + "_" + task.getId() +
119                                // " requires more cpus than is availiable in this moment.");
120                                return null;
121                        }
122
123                        ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName());
124                        result.addAll(choosenResources);
125                        map.put(ResourceParameterName.PROCESSINGELEMENTS, result);
126
127                }
128                int memoryRequest;
129                try {
130                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
131                } catch (NoSuchFieldException e) {
132                        memoryRequest = 0;
133                }
134                if (memoryRequest != 0) {
135                        List<ComputingNode> nodes = resourceManager.getComputingNodes();
136
137                        Memory memory = null;
138                        for (ComputingNode node : nodes) {
139
140                                if (node.getFreeMemory() >= memoryRequest) {
141                                        memory = new Memory(node.getMemoryUnit(), memoryRequest, memoryRequest);
142                                } else
143                                        return null;
144                        }
145                        map.put(ResourceParameterName.MEMORY, memory);
146                }
147
148                return map;
149        }
150
151        @SuppressWarnings("unchecked")
152        private String chooseProviderForExecution(ResourceManagerInterface unitsManager) {
153                List<ComputingResource> processingElements;
154                Properties properties = new Properties();
155                properties.setProperty("type", ResourceType.COMPUTING_NODE.toString());
156                // properties.setProperty("status", ResourceStatus.FREE.toString());
157                processingElements = (List<ComputingResource>) unitsManager.filterResources(properties);
158                return processingElements.get(0).getName();
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.