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

Revision 497, 5.5 KB checked in by wojtekp, 13 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;
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++) {
51                                WorkloadUnit job = q.get(i);
52                                TaskInterface<?> task = (TaskInterface<?>) job;
53                                // if status of the tasks in READY
54                                if (task.getStatus() == DCWormsTags.READY) {
55                                       
56                                        /****************3 ways to schedule task****************/
57                                       
58                                        /****************1. Choosing particular resources to perform execution****************/
59                                        Map<ResourceUnitName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
60                                        if (choosenResources != null) {
61                                                addToSchedulingPlan(plan, task, choosenResources);
62                                        }
63                                       
64                                        /****************2. Choosing resource scheduler/provider to submit task.  If the given resource doesn't contains/isn't
65                                        a scheduler, random resources from the given resource will be chosen in order to perform execution****************/
66                                        /*String provName = chooseProviderForExecution(resourceManager);
67                                        if (provName != null) {
68                                                addToSchedulingPlan(plan, task, provName);
69                                        }*/
70
71                                        /****************3. Scheduler will choose random resources to perform execution****************/
72                                        //addToSchedulingPlan(plan, task);
73                                }
74                        }
75
76                        break;
77                }
78                return plan;
79        }
80
81        private Map<ResourceUnitName, ResourceUnit> chooseResourcesForExecution(
82                        ClusterResourceManager resourceManager, TaskInterface<?> task) {
83
84                Map<ResourceUnitName, ResourceUnit> map = new HashMap<ResourceUnitName, ResourceUnit>();
85                List<ComputingNode> nodes = resourceManager.getComputingNodes();
86                for (ComputingNode node : nodes) {
87                        int cpuRequest;
88                        try {
89                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
90                        } catch (NoSuchFieldException e) {
91                                cpuRequest = 1;
92                        }
93
94                        if (cpuRequest != 0) {
95
96                                List<Processor> processors = node.getProcessors();
97                                if (processors.size() < cpuRequest) {
98                                        continue;
99                                }
100
101                                List<ComputingResource> choosenResources = new ArrayList<ComputingResource>();                         
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                                        continue;
110                                }
111
112                                ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName());
113                                result.addAll(choosenResources);
114                                map.put(StandardResourceUnitName.PE, result);
115                               
116                                int memoryRequest;
117                                try {
118                                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
119                                } catch (NoSuchFieldException e) {
120                                        memoryRequest = 0;
121                                }
122                                if (memoryRequest != 0) {
123
124                                        Memory memory = null;
125                                        try{
126                                                if (node.getFreeMemory() >= memoryRequest) {           
127                                                        memory = new Memory(node.getMemory(), memoryRequest, memoryRequest);
128                                                }               
129                                        } catch(NoSuchFieldException e){
130                                                continue;
131                                        }
132                                        if(memory == null)
133                                                continue;
134                                        else {
135                                                map.put(StandardResourceUnitName.MEMORY, memory);
136                                                return map;
137                                        }
138                                } else return map;
139                        }
140                }
141       
142                return null;
143        }
144
145        @SuppressWarnings("unchecked")
146        private String chooseProviderForExecution(ResourceManager unitsManager) {
147                List<ComputingResource> processingElements;
148                Properties properties = new Properties();
149                properties.setProperty("type", StandardResourceType.ComputingNode.toString());
150                // properties.setProperty("status", ResourceStatus.FREE.toString());
151                processingElements = (List<ComputingResource>) unitsManager.filterResources(properties);
152                return processingElements.get(0).getName();
153        }
154
155        public String getName() {
156                return getClass().getName();
157        }
158
159}
Note: See TracBrowser for help on using the repository browser.