source: xssim/trunk/src/example/localplugin/FCFSClusterLocalPlugin.java @ 169

Revision 169, 5.7 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.SubmittedTask;
5import gssim.schedframe.scheduling.ExecTaskInterface;
6
7import java.util.ArrayList;
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.ResourceType;
23import test.rewolucja.resources.manager.implementation.ClusterResourceManager;
24import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface;
25import test.rewolucja.resources.physical.base.ComputingResource;
26import test.rewolucja.resources.physical.implementation.CPU;
27import test.rewolucja.resources.physical.implementation.ComputingNode;
28import test.rewolucja.scheduling.JobRegistryInterface;
29import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew;
30import test.rewolucja.scheduling.plan.SchedulingPlanNew;
31import test.rewolucja.scheduling.queue.Queue;
32import test.rewolucja.scheduling.queue.QueueList;
33
34public class FCFSClusterLocalPlugin extends BaseLocalPlugin {
35
36        public FCFSClusterLocalPlugin() {
37        }
38
39        public SchedulingPlanInterfaceNew schedule(SchedulingEvent event, QueueList queues, JobRegistryInterface jobRegistry,
40                        ResourceManagerInterface resManager, ModuleList modules) {
41
42                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
43                SchedulingPlanNew plan = new SchedulingPlanNew();
44                // chose the events types to serve.
45                // Different actions for different events are possible.
46                switch (event.getType()) {
47                case START_TASK_EXECUTION:
48                case TASK_FINISHED:
49                case TIMER:
50                        // our tasks are placed only in first queue (see
51                        // BaseLocalPlugin.placeTasksInQueues() method)
52                        Queue q = queues.get(0);
53                        // check all tasks in queue
54
55
56                        for (int i = 0; i < q.size(); i++) {
57                                GSSIMJobInterface<?> job = q.get(i);
58                                TaskInterface<?> task = (TaskInterface<?>) job;
59                                // if status of the tasks in READY
60                                if (task.getStatus() == Gridlet.READY) {
61
62                                        SubmittedTask subTask = (SubmittedTask) task;
63                                       
64                                        /****************3 ways to schedule task****************/
65                                       
66                                        //1. Choosing particular resources to perform execution
67                                        Map<ResourceParameterName, ResourceUnit> choosenResources = chooseResourcesForExecution(resourceManager, subTask);
68                                        if (choosenResources != null) {
69                                                addToSchedulingPlan(plan, task, choosenResources);
70                                        }
71                                       
72                                        //2. Choosing resource scheduler/provider to submit task.  If the given resource doesn't contains/isn't
73                                        //a scheduler, random resources from the given resource will be chosen in order to perform execution
74                                        /*String provName = chooseProviderForExecution(resourceManager);
75                                        if (provName != null) {
76                                                addToSchedulingPlan(plan, task, provName);
77                                        }*/
78
79                                        //3. Scheduler will choose random resources to perform execution
80                                        //addToSchedulingPlan(plan, task);
81
82                                }
83                        }
84
85                        break;
86                }
87                return plan;
88        }
89
90        private HashMap<ResourceParameterName, ResourceUnit> chooseResourcesForExecution(
91                        ClusterResourceManager resourceManager, ExecTaskInterface task) {
92
93                HashMap<ResourceParameterName, ResourceUnit> map = new HashMap<ResourceParameterName, ResourceUnit>();
94
95                int cpuRequest;
96                try {
97                        cpuRequest = Double.valueOf(task.getCpuCntRequest()).intValue();
98                } catch (NoSuchFieldException e) {
99                        cpuRequest = 1;
100                }
101
102                if (cpuRequest != 0) {
103                        List<ComputingResource> choosenResources = null;
104                        List<CPU> processors = null;
105                        processors = resourceManager.getProcessors();
106                        if (processors.size() < cpuRequest) {
107                                // log.warn("Task requires more cpus than is availiable in this resource.");
108                                return null;
109                        }
110
111                        choosenResources = new ArrayList<ComputingResource>();
112
113                        for (int i = 0; i < processors.size() && cpuRequest > 0; i++) {
114                                if (processors.get(i).getStatus() == ResourceStatus.FREE) {
115                                        choosenResources.add(processors.get(i));
116                                        cpuRequest--;
117                                }
118                        }
119                        if (cpuRequest > 0) {
120                                // log.info("Task " + task.getJobId() + "_" + task.getId() +
121                                // " requires more cpus than is availiable in this moment.");
122                                return null;
123                        }
124
125                        ProcessingElements result = new ProcessingElements(processors.get(0).getParent().getName());
126                        result.addAll(choosenResources);
127                        map.put(ResourceParameterName.PROCESSINGELEMENTS, result);
128
129                }
130                int memoryRequest;
131                try {
132                        memoryRequest = Double.valueOf(task.getMemoryRequest()).intValue();
133                } catch (NoSuchFieldException e) {
134                        memoryRequest = 0;
135                }
136                if (memoryRequest != 0) {
137                        List<ComputingNode> nodes = resourceManager.getComputingNodes();
138
139                        Memory memory = null;
140                        for (ComputingNode node : nodes) {
141
142                                if (node.getFreeMemory() >= memoryRequest) {
143                                        memory = new Memory(node.getMemory(), memoryRequest, memoryRequest);
144                                } else
145                                        return null;
146                        }
147                        map.put(ResourceParameterName.MEMORY, memory);
148                }
149
150                return map;
151        }
152
153        private String chooseProviderForExecution(ResourceManagerInterface unitsManager) {
154                List<ComputingResource> processingElements;
155                Properties properties = new Properties();
156                properties.setProperty("type", ResourceType.COMPUTING_NODE.toString());
157                // properties.setProperty("status", ResourceStatus.FREE.toString());
158                processingElements = (List<ComputingResource>) unitsManager.filterResources(properties);
159                return processingElements.get(0).getName();
160        }
161
162        public String getName() {
163                return getClass().getName();
164        }
165
166        public void init(Properties properties) {
167                // no extra initialization is expected.
168        }
169
170}
Note: See TracBrowser for help on using the repository browser.