source: xssim/src/example/localplugin/FCFSConsolidationClusterLocalPlugin.java @ 104

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