From d9de561758736706e6abfc4d8fb8439b1b4f3b86 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Thu, 16 May 2013 17:20:55 +0200 Subject: [PATCH] added CSV logging (still little bit buggy) --- build.xml | 2 + src/at/ac/tuwien/lsdc/SchedSimulator.java | 38 ++++++------- .../tuwien/lsdc/sched/AbstractScheduler.java | 19 +++++-- src/at/ac/tuwien/lsdc/sched/SchedulerA.java | 7 +++ src/at/ac/tuwien/lsdc/sched/SchedulerB.java | 7 +++ src/at/ac/tuwien/lsdc/sched/SchedulerC.java | 7 +++ src/at/ac/tuwien/lsdc/types/ScenarioData.java | 16 +++--- .../ac/tuwien/lsdc/types/SchedulerData.java | 16 +++--- src/at/ac/tuwien/lsdc/util/CSVLogger.java | 57 +++++++++++++++++++ .../tuwien/lsdc/util/CSVScenariosLogger.java | 35 ------------ .../tuwien/lsdc/util/CSVSchedulerLogger.java | 35 ------------ 11 files changed, 128 insertions(+), 111 deletions(-) create mode 100644 src/at/ac/tuwien/lsdc/util/CSVLogger.java delete mode 100644 src/at/ac/tuwien/lsdc/util/CSVScenariosLogger.java delete mode 100644 src/at/ac/tuwien/lsdc/util/CSVSchedulerLogger.java diff --git a/build.xml b/build.xml index 04771da..5c4e095 100644 --- a/build.xml +++ b/build.xml @@ -42,6 +42,7 @@ + @@ -51,6 +52,7 @@ + diff --git a/src/at/ac/tuwien/lsdc/SchedSimulator.java b/src/at/ac/tuwien/lsdc/SchedSimulator.java index 83af4ee..5571bff 100644 --- a/src/at/ac/tuwien/lsdc/SchedSimulator.java +++ b/src/at/ac/tuwien/lsdc/SchedSimulator.java @@ -14,10 +14,10 @@ import at.ac.tuwien.lsdc.sched.SchedulerB; import at.ac.tuwien.lsdc.sched.SchedulerC; import at.ac.tuwien.lsdc.types.Application; import at.ac.tuwien.lsdc.types.ScenarioData; -import at.ac.tuwien.lsdc.types.SchedulerEvent; +import at.ac.tuwien.lsdc.types.ScenarioType; import at.ac.tuwien.lsdc.types.SchedulerType; +import at.ac.tuwien.lsdc.util.CSVLogger; import at.ac.tuwien.lsdc.util.CSVParser; -import at.ac.tuwien.lsdc.util.CSVScenariosLogger; /** * Read config (command line properties) and start Scheduler-Simulation. @@ -30,16 +30,17 @@ public class SchedSimulator { .getLogger(SchedSimulator.class); private static SchedulerType schedulerType; + private static ScenarioType scenario; private static File inputFile; private static File logFile; private static File scenarioLogFile; public static void main(String[] args) throws Exception { - if (args.length == 4) { + if (args.length == 5) { parseCommandLineArgs(args); } else if (args.length == 1) { String[] newArgs = args[0].split(" "); - if (newArgs.length == 4) { + if (newArgs.length == 5) { parseCommandLineArgs(newArgs); } else { log.info(USAGE); @@ -51,25 +52,24 @@ public class SchedSimulator { AbstractScheduler scheduler; switch (schedulerType) { case A: - scheduler = new SchedulerA(); + scheduler = new SchedulerA(logFile,schedulerType,scenario); case B: - scheduler = new SchedulerB(); + scheduler = new SchedulerB(logFile,schedulerType,scenario); default: - scheduler = new SchedulerC(); + scheduler = new SchedulerC(logFile,schedulerType,scenario); } - ScenarioData data = scheduler.initAndStart(apps); - // CSVScenariosLogger logger = new - // CSVScenariosLogger("out_scen"+scenario+"sched"+schedulerType+".csv"); - // logger.appendScenarioData(data); + CSVLogger.logScenarioData(scenarioLogFile, data); } private static void parseCommandLineArgs(String[] args) { - String schedulerTypeStr = args[0]; - String inputFileStr = args[1]; - String logFileStr = args[2]; - String scenarioLogFileStr = args[3]; + String scenarioStr = args[0]; + String schedulerTypeStr = args[1]; + String inputFileStr = args[2]; + String logFileStr = args[3]; + String scenarioLogFileStr = args[4]; try { + scenario = ScenarioType.valueOf(scenarioStr); schedulerType = SchedulerType.valueOf(schedulerTypeStr); inputFile = new File(inputFileStr); if (!inputFile.canRead()) @@ -80,7 +80,7 @@ public class SchedSimulator { logFileOK = true; } else if (!logFile.exists()) { - createLogFile(); + logFile.createNewFile(); logFileOK = true; } if (!logFileOK) @@ -91,6 +91,7 @@ public class SchedSimulator { fileWriteOK = scenarioLogFile.canWrite(); scenarioLogFile.delete(); scenarioLogFile.createNewFile(); + System.out.println("Size of Scenariologfile: " + scenarioLogFile.length()); } else { scenarioLogFile.createNewFile(); fileWriteOK = true; @@ -104,8 +105,5 @@ public class SchedSimulator { System.exit(1); } } - - private static void createLogFile() { - - } + } diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java index 7200946..53261cf 100644 --- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java +++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java @@ -1,20 +1,25 @@ package at.ac.tuwien.lsdc.sched; +import java.io.File; import java.util.LinkedList; import java.util.Map; import at.ac.tuwien.lsdc.types.Application; import at.ac.tuwien.lsdc.types.ScenarioData; +import at.ac.tuwien.lsdc.types.ScenarioType; import at.ac.tuwien.lsdc.types.SchedulerEvent; +import at.ac.tuwien.lsdc.types.SchedulerType; public abstract class AbstractScheduler { // the following types are only needed for correct // log output - protected String schedulerName; - protected String scenario; + protected SchedulerType schedulerType; + protected ScenarioType scenario; protected int numInSourced; protected int numOutSourced; + + File logFile; // this map saves the following Type of Events: // start of an Application, end of an Application @@ -30,11 +35,17 @@ public abstract class AbstractScheduler { // it is updated with every added "EndEvent" protected long endTime; + public AbstractScheduler(File logFile, SchedulerType schedulerType, ScenarioType scenario) { + this.logFile = logFile; + this.schedulerType = schedulerType; + this.scenario = scenario; + } + // Initialize Scheduler with Data from CSV // CSV will be parsed and sent as List to Scheduler public ScenarioData initAndStart(LinkedList apps) { for (Application a : apps) { - System.out.println(a); + //System.out.println(a); // read start timestamp // save event in map } @@ -66,6 +77,6 @@ public abstract class AbstractScheduler { // this creates the total summary which should be written to a CSV at the // end protected ScenarioData doEndLogging() { - return new ScenarioData(schedulerName,scenario,1,1,1,1,1,1); + return new ScenarioData(schedulerType.toString(),scenario.toString(),1,1,1,1,1,1); } } diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java index d6fae68..17d991d 100644 --- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java +++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java @@ -1,12 +1,19 @@ package at.ac.tuwien.lsdc.sched; +import java.io.File; import java.util.LinkedList; +import at.ac.tuwien.lsdc.types.ScenarioType; import at.ac.tuwien.lsdc.types.SchedulerEvent; +import at.ac.tuwien.lsdc.types.SchedulerType; public class SchedulerA extends AbstractScheduler { + public SchedulerA(File logFile, SchedulerType schedulerType, ScenarioType scenario) { + super(logFile, schedulerType, scenario); + } + @Override protected void handleEvents(LinkedList events) { // TODO Auto-generated method stub diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java index 006f959..2784bad 100644 --- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java +++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java @@ -1,12 +1,19 @@ package at.ac.tuwien.lsdc.sched; +import java.io.File; import java.util.LinkedList; +import at.ac.tuwien.lsdc.types.ScenarioType; import at.ac.tuwien.lsdc.types.SchedulerEvent; +import at.ac.tuwien.lsdc.types.SchedulerType; public class SchedulerB extends AbstractScheduler { + public SchedulerB(File logFile, SchedulerType schedulerType, ScenarioType scenario) { + super(logFile, schedulerType, scenario); + } + @Override protected void handleEvents(LinkedList events) { // TODO Auto-generated method stub diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java index c106a38..bafd084 100644 --- a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java +++ b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java @@ -1,12 +1,19 @@ package at.ac.tuwien.lsdc.sched; +import java.io.File; import java.util.LinkedList; +import at.ac.tuwien.lsdc.types.ScenarioType; import at.ac.tuwien.lsdc.types.SchedulerEvent; +import at.ac.tuwien.lsdc.types.SchedulerType; public class SchedulerC extends AbstractScheduler { + public SchedulerC(File logFile, SchedulerType schedulerType, ScenarioType scenario) { + super(logFile,schedulerType,scenario); + } + @Override protected void handleEvents(LinkedList events) { // TODO Auto-generated method stub diff --git a/src/at/ac/tuwien/lsdc/types/ScenarioData.java b/src/at/ac/tuwien/lsdc/types/ScenarioData.java index fbb3cd5..a43c37c 100644 --- a/src/at/ac/tuwien/lsdc/types/ScenarioData.java +++ b/src/at/ac/tuwien/lsdc/types/ScenarioData.java @@ -1,7 +1,7 @@ package at.ac.tuwien.lsdc.types; public class ScenarioData { - + private String scheduler; private String scenario; private long totalPMs; @@ -10,7 +10,7 @@ public class ScenarioData { private long totalConsumption; private int totalInSourced; private int totalOutSourced; - + public ScenarioData(String scheduler, String scenario, long totalPMs, long totalVMs, long totalDuration, long totalConsumption, int totalInSourced, int totalOutSourced) { @@ -24,12 +24,10 @@ public class ScenarioData { this.totalOutSourced = totalOutSourced; } - @Override - public String toString() { - return scheduler + ";" + scenario + ";" + totalPMs + ";" + totalVMs + ";" + totalDuration - + ";" + totalConsumption - + ";" + totalInSourced + ";" - + totalOutSourced; + public String[] toStringArray() { + return new String[] { scheduler, scenario, String.valueOf(totalPMs), String.valueOf(totalVMs), + String.valueOf(totalDuration), String.valueOf(totalConsumption), String.valueOf(totalInSourced), + String.valueOf(totalOutSourced) }; } - + } diff --git a/src/at/ac/tuwien/lsdc/types/SchedulerData.java b/src/at/ac/tuwien/lsdc/types/SchedulerData.java index ca4d14c..ed90f56 100644 --- a/src/at/ac/tuwien/lsdc/types/SchedulerData.java +++ b/src/at/ac/tuwien/lsdc/types/SchedulerData.java @@ -1,7 +1,7 @@ package at.ac.tuwien.lsdc.types; public class SchedulerData { - + private long timestamp; private long totalRAM; private long totalCPU; @@ -11,7 +11,7 @@ public class SchedulerData { private long totalConsumption; private int inSourced; private int outSourced; - + public SchedulerData(long timestamp, long totalRAM, long totalCPU, long totalSize, long runningPMs, long runningVMs, long totalConsumption, int inSourced, int outSourced) { @@ -26,12 +26,12 @@ public class SchedulerData { this.outSourced = outSourced; } - @Override - public String toString() { - return timestamp + ";" + totalRAM + ";" + totalCPU + ";" - + totalSize + ";" + runningPMs + ";" - + runningVMs + ";" + totalConsumption - + ";" + inSourced + ";" + outSourced; + public String[] toStringArray() { + return new String[] { String.valueOf(timestamp), + String.valueOf(totalRAM), String.valueOf(totalCPU), + String.valueOf(totalSize), String.valueOf(runningPMs), + String.valueOf(runningVMs), String.valueOf(totalConsumption), + String.valueOf(inSourced), String.valueOf(outSourced) }; } } diff --git a/src/at/ac/tuwien/lsdc/util/CSVLogger.java b/src/at/ac/tuwien/lsdc/util/CSVLogger.java new file mode 100644 index 0000000..1dd8045 --- /dev/null +++ b/src/at/ac/tuwien/lsdc/util/CSVLogger.java @@ -0,0 +1,57 @@ +package at.ac.tuwien.lsdc.util; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import at.ac.tuwien.lsdc.types.ScenarioData; +import at.ac.tuwien.lsdc.types.SchedulerData; +import au.com.bytecode.opencsv.CSVWriter; + +public class CSVLogger { + + public static void logScenarioData(File file, ScenarioData data) + throws IOException { + if (file.length() == 0) { + writeScenarioDataHeader(file); + } + appendCSVLine(file,data.toStringArray()); + } + + public static void logSchedulerData(File file, SchedulerData data) + throws IOException { + if (file.length() == 0) { + writeSchedulerDataHeader(file); + } + appendCSVLine(file,data.toStringArray()); + } + + private static void writeScenarioDataHeader(File file) throws IOException { + String[] header = new String[] { "Scheduler", "Scenario", "TotalPMs", + "TotalVMs", "TotalDuration", "TotalPowerConsumption", + "TotalInSourced", "TotalOutSourced" }; + appendCSVLine(file, header); + } + + private static void writeSchedulerDataHeader(File file) throws IOException { + String[] header = new String[] { "Timestamp", "TotalRAM", "TotalCPU", + "TotalSize", "RunningPMs", "RunningVMs", + "TotalPowerConsumption", "InSourced", "OutSourced" }; + appendCSVLine(file, header); + } + + private static void appendCSVLine(File file, String[] strings) + throws IOException { + System.out.println("Write to " + file.toString()); + System.out.print("LogCSV: "); + for (String s : strings) { + System.out.println(s + " "); + } + System.out.println(); + CSVWriter writer = new CSVWriter(new FileWriter(file), ';', + CSVWriter.NO_QUOTE_CHARACTER); + writer.writeNext(strings); + writer.close(); + } + +} diff --git a/src/at/ac/tuwien/lsdc/util/CSVScenariosLogger.java b/src/at/ac/tuwien/lsdc/util/CSVScenariosLogger.java deleted file mode 100644 index ca28f85..0000000 --- a/src/at/ac/tuwien/lsdc/util/CSVScenariosLogger.java +++ /dev/null @@ -1,35 +0,0 @@ -package at.ac.tuwien.lsdc.util; - -import java.io.FileWriter; -import java.io.IOException; -import java.util.List; - -import at.ac.tuwien.lsdc.types.ScenarioData; -import au.com.bytecode.opencsv.CSVWriter; - -public class CSVScenariosLogger { - - private final String filename; - - public CSVScenariosLogger(String filename) { - this.filename = filename; - } - - public void appendScenarioData( ScenarioData data ) { - //check if filename exists - //if not create file with header - //append line data.toString() - } - - public void writeFile(List data) throws IOException { - CSVWriter writer = new CSVWriter(new FileWriter(filename), '\t'); - writer.writeAll(data); - writer.close(); - } - - public void writeLine(String[] data) throws IOException { - CSVWriter writer = new CSVWriter(new FileWriter(filename), '\t'); - writer.writeNext(data); - writer.close(); - } -} diff --git a/src/at/ac/tuwien/lsdc/util/CSVSchedulerLogger.java b/src/at/ac/tuwien/lsdc/util/CSVSchedulerLogger.java deleted file mode 100644 index b87da9b..0000000 --- a/src/at/ac/tuwien/lsdc/util/CSVSchedulerLogger.java +++ /dev/null @@ -1,35 +0,0 @@ -package at.ac.tuwien.lsdc.util; - -import java.io.FileWriter; -import java.io.IOException; -import java.util.List; - -import at.ac.tuwien.lsdc.types.SchedulerData; -import au.com.bytecode.opencsv.CSVWriter; - -public class CSVSchedulerLogger { - - private final String filename; - - public CSVSchedulerLogger(String filename) { - this.filename = filename; - } - - public void appendSchedulerData(SchedulerData data) { - // check if filename exists - // if not create file with header - // append line data.toString() - } - - public void writeFile(List data) throws IOException { - CSVWriter writer = new CSVWriter(new FileWriter(filename), '\t'); - writer.writeAll(data); - writer.close(); - } - - public void writeLine(String[] data) throws IOException { - CSVWriter writer = new CSVWriter(new FileWriter(filename), '\t'); - writer.writeNext(data); - writer.close(); - } -} -- 2.43.0