source: DCWoRMS/trunk/src/example/localplugin/FCFSBF_ClusterPlugin.java @ 531

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