From 0cf09fabb9dbe2caf6c8e3e28fb89cd37ce39875 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Fri, 17 May 2013 17:00:07 +0200 Subject: [PATCH] fixed csv logging --- src/at/ac/tuwien/lsdc/SchedSimulator.java | 39 +++++++++---------- src/at/ac/tuwien/lsdc/util/CSVLogger.java | 46 +++++++++++------------ 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/at/ac/tuwien/lsdc/SchedSimulator.java b/src/at/ac/tuwien/lsdc/SchedSimulator.java index 5571bff..d334013 100644 --- a/src/at/ac/tuwien/lsdc/SchedSimulator.java +++ b/src/at/ac/tuwien/lsdc/SchedSimulator.java @@ -32,8 +32,8 @@ public class SchedSimulator { private static SchedulerType schedulerType; private static ScenarioType scenario; private static File inputFile; - private static File logFile; - private static File scenarioLogFile; + private static File logfile; + private static File schedulerLog; public static void main(String[] args) throws Exception { if (args.length == 5) { @@ -52,21 +52,23 @@ public class SchedSimulator { AbstractScheduler scheduler; switch (schedulerType) { case A: - scheduler = new SchedulerA(logFile,schedulerType,scenario); + scheduler = new SchedulerA(schedulerLog,schedulerType,scenario); case B: - scheduler = new SchedulerB(logFile,schedulerType,scenario); + scheduler = new SchedulerB(schedulerLog,schedulerType,scenario); default: - scheduler = new SchedulerC(logFile,schedulerType,scenario); + scheduler = new SchedulerC(schedulerLog,schedulerType,scenario); } ScenarioData data = scheduler.initAndStart(apps); - CSVLogger.logScenarioData(scenarioLogFile, data); + CSVLogger logger = new CSVLogger(logfile); + logger.logScenarioData(data); + logger.close(); } private static void parseCommandLineArgs(String[] args) { String scenarioStr = args[0]; String schedulerTypeStr = args[1]; String inputFileStr = args[2]; - String logFileStr = args[3]; + String schedulerLogStr = args[3]; String scenarioLogFileStr = args[4]; try { scenario = ScenarioType.valueOf(scenarioStr); @@ -74,26 +76,25 @@ public class SchedSimulator { inputFile = new File(inputFileStr); if (!inputFile.canRead()) throw new IOException("Can not read input file"); - logFile = new File(logFileStr); + logfile = new File(schedulerLogStr); boolean logFileOK = false; - if ( logFile.canWrite() ) { + if ( logfile.canWrite() ) { logFileOK = true; } - else if (!logFile.exists()) { - logFile.createNewFile(); + else if (!logfile.exists()) { + logfile.createNewFile(); logFileOK = true; } if (!logFileOK) - throw new IOException("Can not write to log file"); + throw new IOException("Can not write to scheduler log file"); boolean fileWriteOK = false; - scenarioLogFile = new File(scenarioLogFileStr); - if (scenarioLogFile.exists()) { - fileWriteOK = scenarioLogFile.canWrite(); - scenarioLogFile.delete(); - scenarioLogFile.createNewFile(); - System.out.println("Size of Scenariologfile: " + scenarioLogFile.length()); + schedulerLog = new File(scenarioLogFileStr); + if (schedulerLog.exists()) { + fileWriteOK = schedulerLog.canWrite(); + schedulerLog.delete(); + schedulerLog.createNewFile(); } else { - scenarioLogFile.createNewFile(); + schedulerLog.createNewFile(); fileWriteOK = true; } if (!fileWriteOK) diff --git a/src/at/ac/tuwien/lsdc/util/CSVLogger.java b/src/at/ac/tuwien/lsdc/util/CSVLogger.java index 1dd8045..291a91a 100644 --- a/src/at/ac/tuwien/lsdc/util/CSVLogger.java +++ b/src/at/ac/tuwien/lsdc/util/CSVLogger.java @@ -9,49 +9,47 @@ import at.ac.tuwien.lsdc.types.SchedulerData; import au.com.bytecode.opencsv.CSVWriter; public class CSVLogger { - - public static void logScenarioData(File file, ScenarioData data) + + private CSVWriter writer; + private File file; + + public CSVLogger(File file) throws IOException { + this.file = file; + this.writer = new CSVWriter(new FileWriter(file), ';', + CSVWriter.NO_QUOTE_CHARACTER); + } + + public void logScenarioData(ScenarioData data) throws IOException { if (file.length() == 0) { - writeScenarioDataHeader(file); + writeScenarioDataHeader(); } - appendCSVLine(file,data.toStringArray()); + writer.writeNext(data.toStringArray()); } - public static void logSchedulerData(File file, SchedulerData data) + public void logSchedulerData(SchedulerData data) throws IOException { if (file.length() == 0) { - writeSchedulerDataHeader(file); + writeSchedulerDataHeader(); } - appendCSVLine(file,data.toStringArray()); + writer.writeNext(data.toStringArray()); } - private static void writeScenarioDataHeader(File file) throws IOException { + private void writeScenarioDataHeader() throws IOException { String[] header = new String[] { "Scheduler", "Scenario", "TotalPMs", "TotalVMs", "TotalDuration", "TotalPowerConsumption", "TotalInSourced", "TotalOutSourced" }; - appendCSVLine(file, header); + writer.writeNext(header); } - private static void writeSchedulerDataHeader(File file) throws IOException { + private void writeSchedulerDataHeader() throws IOException { String[] header = new String[] { "Timestamp", "TotalRAM", "TotalCPU", "TotalSize", "RunningPMs", "RunningVMs", "TotalPowerConsumption", "InSourced", "OutSourced" }; - appendCSVLine(file, header); + writer.writeNext(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); + + public void close() throws IOException { writer.close(); } - } -- 2.43.0