source: DCWoRMS/trunk/src/schedframe/scheduling/Scheduler.java @ 478

Revision 478, 9.0 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package schedframe.scheduling;
2
3import eduni.simjava.Sim_event;
4import eduni.simjava.Sim_port;
5import eduni.simjava.Sim_system;
6import gridsim.GridSim;
7import gridsim.GridSimCore;
8import gridsim.GridSimTags;
9import gridsim.IO_data;
10import gridsim.gssim.WormsTags;
11
12import java.util.ArrayList;
13import java.util.List;
14import java.util.Map;
15import java.util.Properties;
16
17import schedframe.Initializable;
18import schedframe.PluginConfiguration;
19import schedframe.events.scheduling.SchedulingEventType;
20import schedframe.resources.Resource;
21import schedframe.resources.ResourceStatus;
22import schedframe.resources.ResourceType;
23import schedframe.resources.computing.ComputingResource;
24import schedframe.resources.providers.LocalSystem;
25import schedframe.resources.units.ResourceUnit;
26import schedframe.resources.units.ResourceUnitName;
27import schedframe.scheduling.manager.resources.ManagedComputingResources;
28import schedframe.scheduling.manager.resources.ManagedResources;
29import schedframe.scheduling.policy.AbstractManagementSystem;
30import schedframe.scheduling.queue.TaskQueue;
31import schedframe.scheduling.queue.QueueDescription;
32import schedframe.scheduling.tasks.WorkloadUnit;
33
34public class Scheduler extends GridSimCore implements Resource, Initializable{
35       
36        protected ResourceStatus status;
37        protected ResourceType type;
38       
39        protected Scheduler parent;
40        protected List<Scheduler> children;
41       
42        protected AbstractManagementSystem managementSystem;
43       
44        protected ManagedComputingResources compResources;     
45
46        public Scheduler(AbstractManagementSystem manSys, ResourceType schedType, ManagedResources managedResources) throws Exception {
47                super(manSys.getName(), 1);
48                this.managementSystem = manSys;
49                this.compResources = new ManagedComputingResources();
50                this.status = ResourceStatus.AVAILABLE;
51                this.children = new ArrayList<Scheduler>(1);
52                this.type = schedType;
53                init(managedResources);
54        }
55
56        public void init(ManagedResources managedResources){
57                addResources(managedResources.getComputingResources());
58                managementSystem.init(this, managedResources);
59               
60                //required to terminate all scheduling entities after the end of simulation
61                Integer schedulerIdObj = new Integer(get_id());
62                List<Integer> schedRes = GridSim.getGridResourceList();
63                schedRes.add(schedulerIdObj);
64
65                /*if (supportsAR) {
66                        res = GridSim.getAdvancedReservationList();
67                        res.add(resIdObj);
68                } */
69
70        }
71       
72        public void addChild(Scheduler child) {
73                child.setParent(this);
74                if (children == null) {
75                        children = new ArrayList<Scheduler>(1);
76                }
77                children.add(child);
78        }
79
80        public List<Scheduler> getChildren() {
81                return children;
82        }
83
84        public void setParent(Scheduler newParent) {
85                this.parent = newParent;
86        }
87
88        public Scheduler getParent() {
89                return parent;
90        }
91
92        /*public void addResource(ComputingResource resource) {
93                compResources.add(resource);
94        }*/
95
96        public void addResources(List<ComputingResource> res) {
97                for(ComputingResource resource:res){
98                        compResources.add(resource);
99                        resource.setScheduler(this);
100                        for(ComputingResource child: resource.filterDescendants(new Properties())){
101                                child.setScheduler(this);
102                        }
103                }
104        }
105
106        public ManagedComputingResources getCompResources() {
107                return compResources;
108        }
109
110        public void body() {
111
112                PluginConfiguration pluginConfig = managementSystem.getSchedulingPluginConfiguration();
113                if (pluginConfig != null) {
114                        Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents();
115                        if (events != null) {
116                                Object obj = events.get(SchedulingEventType.TIMER);
117                                if (obj != null) {
118                                        int delay = (Integer) obj;
119                                        send(this.get_id(), delay, WormsTags.TIMER);
120                                }
121                        }
122                }
123
124                // Process events until END_OF_SIMULATION is received from the
125                // GridSimShutdown Entity
126                Sim_event ev = new Sim_event();
127                sim_get_next(ev);
128                boolean run = true;
129                while (Sim_system.running()  && run) {
130                        // sim_get_next(ev);
131                        // if the simulation finishes then exit the loop
132                        if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) {
133                                // managemetnSystem_.setEndSimulation();
134                                run = false;
135                                /*Sim_stat stats = get_stat();
136                                List<Object[]> measures = stats.get_measures();
137                                for (Object[] info : measures) {
138                                        String measure = (String) info[0];
139                                        if (measure
140                                                        .startsWith(GssimConstants.TASKS_QUEUE_LENGTH_MEASURE_NAME)) {
141                                                System.out.println("====="+this.get_name()+";"+stats.average(measure));
142                                        }
143                                }*/
144                                break;
145                        }
146                       
147                        // process the received event
148                        processRequest(ev);
149                        sim_get_next(ev);
150                }
151        }
152       
153        protected void processRequest(Sim_event ev) {
154                switch (ev.get_tag()) {
155
156                case GridSimTags.GRIDLET_SUBMIT:
157                        processGSSIMJobSubmit(ev, false);
158                        break;
159
160                case GridSimTags.GRIDLET_SUBMIT_ACK:
161                        processGSSIMJobSubmit(ev, true);
162                        break;
163
164                case GridSimTags.GRIDLET_RETURN:
165                        processGSSIMJobReturn(ev);
166                        break;
167
168                default:
169                        processOtherRequest(ev);
170                        break;
171                }
172        }
173
174        protected void processOtherRequest(Sim_event ev) {
175                switch (ev.get_tag()) {
176                case WormsTags.QUERY_RESOURCE_DESC:
177                        SchedulerDescription desc = new SchedulerDescription(new LocalSystem(get_name(), null, null));
178                        Map<ResourceUnitName, List<ResourceUnit>> units = managementSystem.getResourceManager().getSharedResourceUnits();
179                        desc.addResourceUnitList(units);
180                        desc.addQueuesDescription(getQueuesDescription());
181
182                        IO_data data = new IO_data(desc, 0, ev.get_src());
183                        send(ev.get_src(), GridSimTags.SCHEDULE_NOW, WormsTags.QUERY_RESOURCE_DESC_RESULT, data);
184                        break;
185
186                default:
187                        managementSystem.processEvent(ev);
188                        break;
189                }
190        }
191       
192        /*public boolean processOtherEvent(Sim_event ev) {
193                return false;
194        }*/
195       
196        protected void processGSSIMJobReturn(Sim_event ev) {
197                WorkloadUnit job = (WorkloadUnit) ev.get_data();
198                managementSystem.notifyReturnedWorkloadUnit(job);
199        }
200
201        protected void processGSSIMJobSubmit(Sim_event ev, boolean ack) {
202                WorkloadUnit job = (WorkloadUnit) ev.get_data();
203                managementSystem.notifySubmittedWorkloadUnit(job, ack);
204        }
205       
206        public void send(String entityName, double delay, int tag, Object data){
207                super.send(entityName, delay, tag, data);
208        }
209       
210        public void send(int destId, double delay, int tag){
211                super.send(destId, delay, tag);
212        }
213       
214        public void send(int destId, double delay, int tag, Object data){
215                super.send(destId, delay, tag, data);
216        }
217       
218        public void send(Sim_port destPort, double delay, int tag, Object data) {
219                super.send(destPort, delay, tag, data);
220        }
221
222        public void sendInternal(double delay, int tag, Object data) {
223                this.send(this.get_id(), delay, tag, data);
224        }
225       
226        /*protected void send(int dest, int tag, int transaction, Object obj){
227                IO_data data = new Transaction_IO_data(transaction, obj, 8, dest);
228                super.send(dest, GridSimTags.SCHEDULE_NOW, tag, data);
229        }*/
230       
231        public Sim_port getOutputPort() {
232                return output;
233        }
234
235        /*public Scheduler getScheduler(String resourceName){
236                ComputingResource resourceWithName = null;
237                for(int i = 0 ; i < compResources.size() && resourceWithName == null; i++){
238                        ComputingResource resource = compResources.get(i);
239                        if(resource.getName().equals(resourceName))
240                                resourceWithName = resource;
241                        else
242                                try {
243                                        resourceWithName = resource.getDescendantByName(resourceName);
244                                } catch (ResourceException e) {
245                                        return null;
246                                }
247                }
248                if(resourceWithName == null)
249                        return null;
250                List<ComputingResource> children = resourceWithName.getChildren();
251                Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>();
252                for(ComputingResource child:children) {
253                        childrenSchedulers.add(child.getScheduler());
254                }
255                Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers);
256                while(childrenSchedulers.size() != 1){
257                        childrenSchedulers = new HashSet<Scheduler>();
258                        for(Scheduler s: tempChildrenSchedulers){
259                                childrenSchedulers.add(s.getParent());
260                        }
261                        tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers);
262                }
263                Iterator<Scheduler> it = childrenSchedulers.iterator();
264                Scheduler potentialScheduler = it.next();
265                if(potentialScheduler.getResources().containsAll(children))
266                        return potentialScheduler;
267                return null;
268
269        }*/
270       
271        public List<QueueDescription> getQueuesDescription(){
272                List<QueueDescription> queues = new ArrayList<QueueDescription>();
273                for(TaskQueue queue: managementSystem.getAccessQueues()){
274                        QueueDescription qd;
275                        try {
276                                qd = new QueueDescription(queue.getName(), queue.getPriority(), queue.supportReservations(), getQueueLoad(queue.getName()));
277                                queues.add(qd);
278                        } catch (NoSuchFieldException e) {;
279                        }
280                }
281                return queues;
282
283        }
284        private long getQueueLoad(String queueName) throws NoSuchFieldException{
285                Map<String, Integer> queue_size = managementSystem.getQueuesSize();
286                long load = 0;
287                if(queueName == null){
288                        for(String queue: queue_size.keySet()){
289                                load += queue_size.get(queue);
290                        }
291                        return load;
292                }
293                else if(queue_size.containsKey(queueName))
294                        return queue_size.get(queueName);
295                else
296                        throw new NoSuchFieldException("Queue " + queueName +
297                                                        " is not available in resource " + get_name());
298        }
299
300        public ResourceType getType() {
301                return this.type;
302        }
303
304        public ResourceStatus getStatus() {
305                return status;
306        }
307       
308        public void setStatus(ResourceStatus newStatus) {
309                status = newStatus;
310        }
311       
312        public void getResourceDescription(){
313               
314        }
315
316        public void initiate() {
317
318        }
319}
Note: See TracBrowser for help on using the repository browser.