source: DCWoRMS/trunk/src/example/localplugin/FCFSClusterLocalPlugin.java @ 513

Revision 513, 5.3 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
RevLine 
[477]1package example.localplugin;
2
[493]3import gridsim.dcworms.DCWormsTags;
[477]4
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9import java.util.Properties;
10
11import schedframe.events.scheduling.SchedulingEvent;
12import schedframe.resources.ResourceStatus;
13import schedframe.resources.StandardResourceType;
14import schedframe.resources.computing.ComputingNode;
15import schedframe.resources.computing.ComputingResource;
16import schedframe.resources.computing.Processor;
17import schedframe.resources.units.Memory;
18import schedframe.resources.units.ProcessingElements;
19import schedframe.resources.units.ResourceUnit;
20import schedframe.resources.units.ResourceUnitName;
21import schedframe.resources.units.StandardResourceUnitName;
22import schedframe.scheduling.manager.resources.ClusterResourceManager;
23import schedframe.scheduling.manager.resources.ResourceManager;
24import schedframe.scheduling.manager.tasks.JobRegistry;
25import schedframe.scheduling.plan.SchedulingPlanInterface;
26import schedframe.scheduling.plan.impl.SchedulingPlan;
27import schedframe.scheduling.plugin.grid.ModuleList;
28import schedframe.scheduling.queue.TaskQueue;
29import schedframe.scheduling.queue.TaskQueueList;
30import schedframe.scheduling.tasks.TaskInterface;
31import schedframe.scheduling.tasks.WorkloadUnit;
32
33public class FCFSClusterLocalPlugin extends BaseLocalSchedulingPlugin {
34
35        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
36                        ResourceManager resManager, ModuleList modules) {
37
38                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
39                SchedulingPlan plan = new SchedulingPlan();
40                // chose the events types to serve.
41                // Different actions for different events are possible.
42                switch (event.getType()) {
43                case START_TASK_EXECUTION:
44                case TASK_FINISHED:
45                //case TIMER:
46                        // our tasks are placed only in first queue (see BaseLocalPlugin.placeJobsInQueues() method)
47                        TaskQueue q = queues.get(0);
48                        // check all tasks in queue
49
50                        for (int i = 0; i < q.size(); i++) {
[513]51                                TaskInterface<?> task = q.get(i);
[477]52                                // if status of the tasks in READY
[481]53                                if (task.getStatus() == DCWormsTags.READY) {
[477]54                                       
55                                        /****************3 ways to schedule task****************/
56                                       
57                                        /****************1. Choosing particular resources to perform execution****************/
[497]58                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
[477]59                                        if (choosenResources != null) {
60                                                addToSchedulingPlan(plan, task, choosenResources);
61                                        }
62                                       
63                                        /****************2. Choosing resource scheduler/provider to submit task.  If the given resource doesn't contains/isn't
64                                        a scheduler, random resources from the given resource will be chosen in order to perform execution****************/
65                                        /*String provName = chooseProviderForExecution(resourceManager);
66                                        if (provName != null) {
67                                                addToSchedulingPlan(plan, task, provName);
68                                        }*/
69
70                                        /****************3. Scheduler will choose random resources to perform execution****************/
71                                        //addToSchedulingPlan(plan, task);
72                                }
73                        }
74
75                        break;
76                }
77                return plan;
78        }
79
80        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
81                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
82
83                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
84                List<ComputingNode> nodes = resourceManager.getComputingNodes();
85                for (ComputingNode node : nodes) {
86                        int cpuRequest;
87                        try {
88                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
89                        } catch (NoSuchFieldException e) {
90                                cpuRequest = 1;
91                        }
92
93                        if (cpuRequest != 0) {
94
95                                List<Processor> processors = node.getProcessors();
96                                if (processors.size() < cpuRequest) {
97                                        continue;
98                                }
99
100                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
101                                for (int i = 0; i < processors.size() && cpuRequest > 0; i++) {
102                                        if (processors.get(i).getStatus() == ResourceStatus.FREE) {
103                                                choosenResources.add(processors.get(i));
104                                                cpuRequest--;
105                                        }
106                                }
107                                if (cpuRequest > 0) {
108                                        continue;
109                                }
110
[505]111                                ProcessingElements result = new ProcessingElements();
[477]112                                result.addAll(choosenResources);
113                                map.put(StandardResourceUnitName.PE, result);
114                               
115                                int memoryRequest;
116                                try {
117                                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
118                                } catch (NoSuchFieldException e) {
119                                        memoryRequest = 0;
120                                }
121                                if (memoryRequest != 0) {
122
123                                        Memory memory = null;
124                                        try{
125                                                if (node.getFreeMemory() >= memoryRequest) {           
126                                                        memory = new Memory(node.getMemory(), memoryRequest, memoryRequest);
127                                                }               
128                                        } catch(NoSuchFieldException e){
129                                                continue;
130                                        }
131                                        if(memory == null)
132                                                continue;
133                                        else {
134                                                map.put(StandardResourceUnitName.MEMORY, memory);
135                                                return map;
136                                        }
[497]137                                } else return map;
[477]138                        }
139                }
140       
141                return null;
142        }
143
144        @SuppressWarnings("unchecked")
145        private String chooseProviderForExecution(ResourceManager unitsManager) {
146                List<ComputingResource> processingElements;
147                Properties properties = new Properties();
148                properties.setProperty("type", StandardResourceType.ComputingNode.toString());
149                // properties.setProperty("status", ResourceStatus.FREE.toString());
150                processingElements = (List<ComputingResource>) unitsManager.filterResources(properties);
151                return processingElements.get(0).getName();
152        }
153
154}
Note: See TracBrowser for help on using the repository browser.