]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
Small adjustments
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / sched / AbstractScheduler.java
1 package at.ac.tuwien.lsdc.sched;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.Writer;
6 import java.util.HashMap;
7 import java.util.LinkedList;
8 import java.util.Map;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import at.ac.tuwien.lsdc.types.Application;
14 import at.ac.tuwien.lsdc.types.ScenarioData;
15 import at.ac.tuwien.lsdc.types.ScenarioType;
16 import at.ac.tuwien.lsdc.types.SchedulerData;
17 import at.ac.tuwien.lsdc.types.SchedulerEvent;
18 import at.ac.tuwien.lsdc.types.SchedulerType;
19 import at.ac.tuwien.lsdc.util.CSVLogger;
20
21 public abstract class AbstractScheduler {
22         
23         private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
24         
25         // the following types are only needed for correct
26         // log output
27         protected SchedulerType schedulerType;
28         protected ScenarioType scenario;
29         protected int numInSourced;
30         protected int numOutSourced;
31         protected int numCurrInSourced;
32         protected int numCurrOutSourced;
33         
34         File schedulerLog;
35         CSVLogger logger;
36
37         // this map saves the following Type of Events:
38         // start of an Application, end of an Application
39         // (start outSourced, end outSourced, start inSourced, end inSourced)
40         protected HashMap<Long, LinkedList<SchedulerEvent>> eventMap;
41
42         // Scheduler has an internal Time "Abstraction"
43         // at every point in time it checks for Events in his "EventList"
44         // and handles them (via the individual scheduling algorithm)
45         protected long currTime;
46
47         // the timestamp at which the Scheduler is finished
48         // it is updated with every added "EndEvent"
49         protected long endTime;
50
51         public AbstractScheduler(File schedulerLog, SchedulerType schedulerType, ScenarioType scenario) throws IOException {
52                 this.schedulerLog = schedulerLog;
53                 this.schedulerType = schedulerType;
54                 this.scenario = scenario;
55                 this.eventMap = new HashMap<Long,LinkedList<SchedulerEvent>>();
56                 this.logger = new CSVLogger(schedulerLog);
57         }
58         
59         // Initialize Scheduler with Data from CSV
60         // CSV will be parsed and sent as List<Application> to Scheduler
61         public ScenarioData initAndStart(LinkedList<Application> apps) {
62                 for (Application a : apps) {
63                         //System.out.println(a);
64                         // read start timestamp
65                         // save event in map
66                 }
67                 startScheduling();
68                 try {
69                         logger.close();
70                 } catch (IOException e) {
71                         log.error("couldn't close logger");
72                 }
73                 return doEndLogging();
74         }
75
76         protected void startScheduling() {
77                 while (true) {
78                         LinkedList<SchedulerEvent> events = eventMap.get(currTime);
79                         handleEvents(events);
80                         doStateLogging();
81                         //advance Time to next Event
82                         if (currTime == endTime) {
83                                 // reached last Event, Scheduler will shut down
84                                 break;
85                         }
86                 }
87         }
88
89         // this method is where the Scheduling Algorithm resides
90         // it reads the Events (start & stop of applications)
91         protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
92
93         protected void doStateLogging() {
94                 SchedulerData data = new SchedulerData(currTime,1,1,1,1,1,1,numCurrInSourced,numCurrOutSourced);
95                 try {
96                         logger.logSchedulerData(data);
97                 } catch (IOException e) {
98                         log.error("error logging data");
99                 }
100         }
101
102         // this creates the total summary which should be written to a CSV at the
103         // end
104         protected ScenarioData doEndLogging() {
105                 return new ScenarioData(schedulerType.toString(),scenario.toString(),1,1,1,1,1,1);
106         }
107 }