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