]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/SchedSimulator.java
fixed scenario params in build.xml
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / SchedSimulator.java
1 package at.ac.tuwien.lsdc;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Collections;
6 import java.util.LinkedList;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import at.ac.tuwien.lsdc.sched.AbstractScheduler;
12 import at.ac.tuwien.lsdc.sched.SchedulerA;
13 import at.ac.tuwien.lsdc.sched.SchedulerB;
14 import at.ac.tuwien.lsdc.sched.SchedulerC;
15 import at.ac.tuwien.lsdc.types.Application;
16 import at.ac.tuwien.lsdc.types.ScenarioData;
17 import at.ac.tuwien.lsdc.types.ScenarioType;
18 import at.ac.tuwien.lsdc.types.SchedulerType;
19 import at.ac.tuwien.lsdc.util.CSVLogger;
20 import at.ac.tuwien.lsdc.util.CSVParser;
21
22 /**
23  * Read config (command line properties) and start Scheduler-Simulation.
24  */
25 public class SchedSimulator {
26
27         private static final String USAGE = "SchedSimulator needs exactly 7 parameters: scheduler, scenario, numPMS, numCloudPartners, inputFile, generalLogPath, schedulerLogPath";
28
29         private static final Logger log = LoggerFactory
30                         .getLogger(SchedSimulator.class);
31
32         private static ScenarioType scenario;
33         private static SchedulerType schedulerType;
34         private static int numPMs;
35         private static int numCloudPartners;
36         private static File inputFile;
37         private static File generalLog;
38         private static File schedulerLog;
39
40         public static void main(String[] args) throws Exception {
41                 if (args.length == 7) {
42                         parseCommandLineArgs(args);
43                 } else if (args.length == 1) {
44                         String[] splitArgs = args[0].split(" ");
45                         if (splitArgs.length == 7) {
46                                 parseCommandLineArgs(splitArgs);
47                         } else {
48                                 log.info(USAGE);
49                                 System.exit(1);
50                         }
51                 } else {
52                         log.info(USAGE);
53                         System.exit(1);
54                 }
55                 LinkedList<Application> apps = CSVParser.parseFile(inputFile);
56                 Collections.sort(apps);
57                 AbstractScheduler scheduler;
58                 switch (schedulerType) {
59                 case A:
60                         scheduler = new SchedulerA(numPMs, numCloudPartners, schedulerLog, scenario);
61                         break;
62                 case B:
63                         scheduler = new SchedulerB(numPMs, numCloudPartners, schedulerLog, scenario);
64                         break;
65                 default:
66                         scheduler = new SchedulerC(numPMs, numCloudPartners, schedulerLog, scenario);
67                         break;
68                 }
69                 ScenarioData data = scheduler.initAndStart(apps);
70                 CSVLogger logger = new CSVLogger(generalLog);
71                 logger.logScenarioData(data);
72         }
73
74         /**
75          * Commandline arguments parsed (mandatory):
76          * - type of scheduler
77          * - type of scenario
78          * - number of available PMs
79          * - number of cloud partners
80          * - scenario input file with applications
81          * - general output file for all schedulers
82          * - specific output file for this scheduler/scenario combination
83          * @param args the command line arguments to parse
84          */
85         private static void parseCommandLineArgs(String[] args) {
86                 String schedulerTypeStr = args[0];
87                 String scenarioStr = args[1];
88                 String numPMsStr = args[2];
89                 String numCloudPartnersStr = args[3];
90                 String inputFileStr = args[4];
91                 String generalLogStr = args[5];
92                 String schedulerLogStr = args[6];
93
94                 try {
95                         schedulerType = SchedulerType.valueOf(schedulerTypeStr);
96                         scenario = ScenarioType.valueOf(scenarioStr);
97                         numPMs = Integer.parseInt(numPMsStr);
98                         numCloudPartners = Integer.parseInt(numCloudPartnersStr);
99                         inputFile = new File(inputFileStr);
100                         
101                         if (!inputFile.canRead())
102                                 throw new IOException("Can not read input file");
103                         generalLog = new File(generalLogStr);
104                         boolean  generalLogOK = false;
105                         log.info(String.valueOf(generalLog.length()));
106                         if ( generalLog.canWrite() ) {
107                                 generalLogOK = true;
108                         }
109                         else if (!generalLog.exists()) {
110                                 log.info("General Log doesn't exist, create new File");
111                                 generalLog.createNewFile();
112                                 generalLogOK = true;
113                         }
114                         if (!generalLogOK)
115                                 throw new IOException("Can not write to general log file");
116                         
117                         boolean schedulerLogOK = false;
118                         schedulerLog = new File(schedulerLogStr);
119                         log.info(schedulerLogStr.toString());
120                         if (schedulerLog.exists()) {
121                                 schedulerLogOK = schedulerLog.canWrite();
122                                 schedulerLog.delete();
123                                 schedulerLog.createNewFile();
124                         } else {
125                                 schedulerLog.createNewFile();
126                                 schedulerLogOK = true;
127                         }
128                         if (!schedulerLogOK)
129                                 throw new IOException("Can not write to scheduler log file "
130                                                 + schedulerLogStr);
131                 } catch (Exception e) {
132                         e.printStackTrace();
133                         log.info(USAGE);
134                         System.exit(1);
135                 }
136         }
137
138 }