source: xssim/trunk/src/example/localplugin/FCFSConsolidationClusterLocalPlugin.java @ 192

Revision 192, 4.9 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;
4
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.Comparator;
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.manager.implementation.ClusterResourceManager;
23import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
24import test.rewolucja.resources.physical.base.ComputingResource;
25import test.rewolucja.resources.physical.implementation.ComputingNode;
26import test.rewolucja.scheduling.JobRegistryInterface;
27import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
28import test.rewolucja.scheduling.plan.SchedulingPlanNew;
29import test.rewolucja.scheduling.queue.Queue;
30import test.rewolucja.scheduling.queue.QueueList;
31
32public class FCFSConsolidationClusterLocalPlugin extends BaseLocalPlugin {
33
34        public FCFSConsolidationClusterLocalPlugin () {
35        }
36
37        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
38                        ResourceManagerInterface resManager, ModuleList modules) {
39
40                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
41                SchedulingPlanNew plan = new SchedulingPlanNew();
42                // chose the events types to serve.
43                // Different actions for different events are possible.
44                switch (event.getType()) {
45                case START_TASK_EXECUTION:
46                case TASK_FINISHED:
47                        // our tasks are placed only in first queue (see
48                        // BaseLocalPlugin.placeJobsInQueues() method)
49                        Queue q = queues.get(0);
50                        // check all tasks in queue
51
52                        for (int i = 0; i < q.size(); i++) {
53                                GSSIMJobInterface<?> job = q.get(i);
54                                TaskInterface<?> task = (TaskInterface<?>) job;
55                                // if status of the tasks in READY
56                                if (task.getStatus() == Gridlet.READY) {
57
58                                        Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, task);
59                                        if (choosenResources  != null) {
60                                                addToSchedulingPlan(plan, task, choosenResources);
61                                        }
62                                }
63                        }
64
65                        break;
66                }
67                return plan;
68        }
69       
70        private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(ClusterResourceManager resourceManager, TaskInterface<?> task) {
71
72                List<ComputingNode> nodes = resourceManager.getComputingNodes();
73                nodes = findSuitableNodes(task, nodes);
74                Collections.sort(nodes, new Comparator<ComputingNode>(){
75                    public int compare(ComputingNode node1, ComputingNode node2){   
76                        return node1.getCategory().getName().compareTo(node2.getCategory().getName());
77                    }
78                });
79                if(nodes.size() > 0)
80                {
81                        ComputingNode node = nodes.get(0);
82                        HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>();
83                        List<ComputingResource> choosenResources =  new ArrayList<ComputingResource>();
84                        int cpuRequest;
85                        try {
86                                cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
87                        } catch (NoSuchFieldException e) {
88                                cpuRequest = 1;
89                        }
90                        for (int i = 0; i < node.getProcessors().size() && cpuRequest > 0; i++) {
91                                if (node.getProcessors().get(i).getStatus() == ResourceStatus.FREE) {
92                                        choosenResources.add(node.getProcessors().get(i));
93                                        cpuRequest--;
94                                }
95                        }
96                        ProcessingElements result = new ProcessingElements(node.getName());
97                        result.addAll(choosenResources);
98                        map.put(ResourceParameterName.PROCESSINGELEMENTS, result);
99                       
100                        int memoryRequest;
101                        try {
102                                memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
103                        } catch (NoSuchFieldException e) {
104                                memoryRequest = 0;
105                        }
106                        if (memoryRequest != 0) {
107                                Memory memory = new Memory(node.getMemoryUnit(), memoryRequest, memoryRequest);
108                                map.put(ResourceParameterName.MEMORY, memory);
109                        }
110                        return map;
111                } else
112                        return null;
113        }
114       
115        private List<ComputingNode> findSuitableNodes(TaskInterface<?> task, List<ComputingNode> nodes){
116                int cpuRequest;
117                try {
118                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
119                } catch (NoSuchFieldException e) {
120                        cpuRequest = 1;
121                }
122                int memoryRequest;
123                try {
124                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
125                } catch (NoSuchFieldException e) {
126                        memoryRequest = 0;
127                }
128                List<ComputingNode> suitableNodes = new ArrayList<ComputingNode>();
129                for(ComputingNode node: nodes){
130                        if(node.getFreeProcessorsNumber() >= cpuRequest && node.getFreeMemory() >= memoryRequest){
131                                suitableNodes.add(node);
132                        }
133                }
134                return suitableNodes;
135        }
136       
137        public String getName() {
138                return getClass().getName();
139        }
140
141        public void init(Properties properties) {
142                // no extra initialization is expected.
143        }
144
145}
Note: See TracBrowser for help on using the repository browser.