From 7bb776017d143a01abcdab5ae35072863378b10e Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Tue, 21 May 2013 15:52:09 +0200
Subject: [PATCH] start MachineManager from Scheduler, add more logging info

---
 src/at/ac/tuwien/lsdc/SchedSimulator.java     |  6 +-
 .../lsdc/management/MachineManager.java       | 32 ++++++++-
 .../tuwien/lsdc/sched/AbstractScheduler.java  | 71 ++++++++++++++-----
 src/at/ac/tuwien/lsdc/sched/SchedulerA.java   |  6 +-
 src/at/ac/tuwien/lsdc/sched/SchedulerB.java   |  6 +-
 src/at/ac/tuwien/lsdc/sched/SchedulerC.java   |  6 +-
 .../ac/tuwien/lsdc/types/PhysicalMachine.java |  4 ++
 src/at/ac/tuwien/lsdc/types/ScenarioData.java |  4 +-
 .../ac/tuwien/lsdc/types/SchedulerData.java   |  4 +-
 9 files changed, 106 insertions(+), 33 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/SchedSimulator.java b/src/at/ac/tuwien/lsdc/SchedSimulator.java
index 83256c0..1fd817f 100644
--- a/src/at/ac/tuwien/lsdc/SchedSimulator.java
+++ b/src/at/ac/tuwien/lsdc/SchedSimulator.java
@@ -57,11 +57,11 @@ public class SchedSimulator {
 		AbstractScheduler scheduler;
 		switch (schedulerType) {
 		case A:
-			scheduler = new SchedulerA(schedulerLog,schedulerType,scenario);
+			scheduler = new SchedulerA(numPMs,numCloudPartners,schedulerLog,schedulerType,scenario);
 		case B:
-			scheduler = new SchedulerB(schedulerLog,schedulerType,scenario);
+			scheduler = new SchedulerB(numPMs,numCloudPartners,schedulerLog,schedulerType,scenario);
 		default:
-			scheduler = new SchedulerC(schedulerLog,schedulerType,scenario);
+			scheduler = new SchedulerC(numPMs,numCloudPartners,schedulerLog,schedulerType,scenario);
 		}
 		ScenarioData data = scheduler.initAndStart(apps);
 		CSVLogger logger = new CSVLogger(generalLog);
diff --git a/src/at/ac/tuwien/lsdc/management/MachineManager.java b/src/at/ac/tuwien/lsdc/management/MachineManager.java
index 49bf340..5923a3c 100644
--- a/src/at/ac/tuwien/lsdc/management/MachineManager.java
+++ b/src/at/ac/tuwien/lsdc/management/MachineManager.java
@@ -11,7 +11,18 @@ public class MachineManager {
 
 	private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
 
-	public double getTotalConsumption() {
+	public MachineManager(int numPMs) {
+		init(numPMs);
+	}
+
+	private void init(int numPMs) {
+		for (int i = 0; i < numPMs; i++) {
+			PhysicalMachine pm = new PhysicalMachine();
+			PMs.put(pm.getId(), pm);
+		}
+	}
+
+	public double getCurrentConsumption() {
 		double consumption = 0;
 		for (PhysicalMachine pm : PMs.values()) {
 			if (pm.isRunning())
@@ -19,4 +30,23 @@ public class MachineManager {
 		}
 		return consumption;
 	}
+
+	public int countCurrentlyRunningPMs() {
+		int running = 0;
+		for (PhysicalMachine pm : PMs.values()) {
+			if (pm.isRunning())
+				running++;
+		}
+		return running;
+	}
+	
+	public int countCurrentlyRunningVMs() {
+		int running = 0;
+		for (PhysicalMachine pm : PMs.values()) {
+			if (pm.isRunning()) {
+				running += pm.countCurrentlyRunningVMs();
+			}
+		}
+		return running;
+	}
 }
diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
index 7c336de..d6aca08 100644
--- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
@@ -2,42 +2,47 @@ package at.ac.tuwien.lsdc.sched;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.Writer;
-import java.util.HashMap;
 import java.util.LinkedList;
-import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import at.ac.tuwien.lsdc.management.MachineManager;
 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.SchedulerData;
 import at.ac.tuwien.lsdc.types.SchedulerEvent;
+import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
 import at.ac.tuwien.lsdc.types.SchedulerType;
 import at.ac.tuwien.lsdc.util.CSVLogger;
 
 public abstract class AbstractScheduler {
-	
-	private static final Logger log = LoggerFactory.getLogger(AbstractScheduler.class);
-	
+
+	private static final Logger log = LoggerFactory
+			.getLogger(AbstractScheduler.class);
+
 	// the following types are only needed for correct
 	// log output
 	protected SchedulerType schedulerType;
 	protected ScenarioType scenario;
-	protected int numInSourced;
-	protected int numOutSourced;
+	protected int numTotalInSourced;
+	protected int numTotalOutSourced;
 	protected int numCurrInSourced;
 	protected int numCurrOutSourced;
-	
+	protected double totalConsumption;
+
+	protected MachineManager manager;
+
 	File schedulerLog;
 	CSVLogger logger;
 
 	// this map saves the following Type of Events:
 	// start of an Application, end of an Application
 	// (start outSourced, end outSourced, start inSourced, end inSourced)
-	protected HashMap<Long, LinkedList<SchedulerEvent>> eventMap;
+	protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
 
 	// Scheduler has an internal Time "Abstraction"
 	// at every point in time it checks for Events in his "EventList"
@@ -48,21 +53,43 @@ public abstract class AbstractScheduler {
 	// it is updated with every added "EndEvent"
 	protected long endTime;
 
-	public AbstractScheduler(File schedulerLog, SchedulerType schedulerType, ScenarioType scenario) throws IOException {
+	public AbstractScheduler(int numPMs, int numCloudPartners,
+			File schedulerLog, SchedulerType schedulerType,
+			ScenarioType scenario) throws IOException {
+		this.manager = new MachineManager(numPMs);
 		this.schedulerLog = schedulerLog;
 		this.schedulerType = schedulerType;
 		this.scenario = scenario;
-		this.eventMap = new HashMap<Long,LinkedList<SchedulerEvent>>();
+		this.eventMap = new TreeMap<Long, LinkedList<SchedulerEvent>>();
 		this.logger = new CSVLogger(schedulerLog);
+		initCloudPartners(numCloudPartners);
 	}
-	
+
+	private void initCloudPartners(int numCloudPartners) {
+		for (int i = 0; i < numCloudPartners; i++) {
+			// initialize a cloudpartner
+			// add to a member: List<CloudPartner>
+			// CloudPartner should have 2 functions:
+			// insource & outsource
+			// insource randomly gives an application to us
+			// outsource accepts randomly applications from us
+		}
+	}
+
 	// Initialize Scheduler with Data from CSV
 	// CSV will be parsed and sent as List<Application> to Scheduler
 	public ScenarioData initAndStart(LinkedList<Application> apps) {
 		for (Application a : apps) {
-			//System.out.println(a);
-			// read start timestamp
-			// save event in map
+			SchedulerEvent event = new SchedulerEvent(a.getTimestamp(),
+					EventType.startApplication, a);
+			LinkedList<SchedulerEvent> list = eventMap.get(a.getTimestamp());
+			if (list != null) {
+				list.add(event);
+			} else {
+				list = new LinkedList<SchedulerEvent>();
+				list.add(event);
+				eventMap.put(a.getTimestamp(), list);
+			}
 		}
 		startScheduling();
 		try {
@@ -78,7 +105,7 @@ public abstract class AbstractScheduler {
 			LinkedList<SchedulerEvent> events = eventMap.get(currTime);
 			handleEvents(events);
 			doStateLogging();
-			//advance Time to next Event
+			// advance Time to next Event
 			if (currTime == endTime) {
 				// reached last Event, Scheduler will shut down
 				break;
@@ -91,7 +118,12 @@ public abstract class AbstractScheduler {
 	protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
 
 	protected void doStateLogging() {
-		SchedulerData data = new SchedulerData(currTime,1,1,1,1,1,1,numCurrInSourced,numCurrOutSourced);
+		SchedulerData data = new SchedulerData(currTime, 1, 1, 1,
+				manager.countCurrentlyRunningPMs(),
+				manager.countCurrentlyRunningVMs(),
+				manager.getCurrentConsumption(), numCurrInSourced,
+				numCurrOutSourced);
+		totalConsumption += manager.getCurrentConsumption();
 		try {
 			logger.logSchedulerData(data);
 		} catch (IOException e) {
@@ -102,6 +134,7 @@ 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(schedulerType.toString(),scenario.toString(),1,1,1,1,1,1);
+		return new ScenarioData(schedulerType.toString(), scenario.toString(),
+				1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);
 	}
 }
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
index 91588d2..6ee56de 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
@@ -11,8 +11,10 @@ import at.ac.tuwien.lsdc.types.SchedulerType;
 
 public class SchedulerA extends AbstractScheduler {
 
-	public SchedulerA(File logFile, SchedulerType schedulerType, ScenarioType scenario) throws IOException {
-		super(logFile, schedulerType, scenario);
+	public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog,
+			SchedulerType schedulerType, ScenarioType scenario)
+			throws IOException {
+		super(numPMs, numCloudPartners, schedulerLog, schedulerType, scenario);
 	}
 
 	@Override
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
index 4e54be1..9f3bf9c 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerB.java
@@ -11,8 +11,10 @@ import at.ac.tuwien.lsdc.types.SchedulerType;
 
 public class SchedulerB extends AbstractScheduler {
 
-	public SchedulerB(File logFile, SchedulerType schedulerType, ScenarioType scenario) throws IOException {
-		super(logFile, schedulerType, scenario);
+	public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog,
+			SchedulerType schedulerType, ScenarioType scenario)
+			throws IOException {
+		super(numPMs, numCloudPartners, schedulerLog, schedulerType, scenario);
 	}
 
 	@Override
diff --git a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
index cc21496..73c8a41 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerC.java
@@ -11,8 +11,10 @@ import at.ac.tuwien.lsdc.types.SchedulerType;
 
 public class SchedulerC extends AbstractScheduler {
 
-	public SchedulerC(File logFile, SchedulerType schedulerType, ScenarioType scenario) throws IOException {
-		super(logFile,schedulerType,scenario);
+	public SchedulerC(int numPMs, int numCloudPartners, File schedulerLog,
+			SchedulerType schedulerType, ScenarioType scenario)
+			throws IOException {
+		super(numPMs, numCloudPartners, schedulerLog, schedulerType, scenario);
 	}
 
 	@Override
diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
index 9489797..5adc292 100644
--- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
@@ -106,4 +106,8 @@ public class PhysicalMachine {
 	public VirtualMachine getVirtualMachine(int id) {
 		return VMs.get(id);
 	}
+	
+	public int countCurrentlyRunningVMs() {
+		return VMs.values().size();
+	}
 }
diff --git a/src/at/ac/tuwien/lsdc/types/ScenarioData.java b/src/at/ac/tuwien/lsdc/types/ScenarioData.java
index a43c37c..e94734c 100644
--- a/src/at/ac/tuwien/lsdc/types/ScenarioData.java
+++ b/src/at/ac/tuwien/lsdc/types/ScenarioData.java
@@ -7,12 +7,12 @@ public class ScenarioData {
 	private long totalPMs;
 	private long totalVMs;
 	private long totalDuration;
-	private long totalConsumption;
+	private double totalConsumption;
 	private int totalInSourced;
 	private int totalOutSourced;
 
 	public ScenarioData(String scheduler, String scenario, long totalPMs,
-			long totalVMs, long totalDuration, long totalConsumption,
+			long totalVMs, long totalDuration, double totalConsumption,
 			int totalInSourced, int totalOutSourced) {
 		this.scheduler = scheduler;
 		this.scenario = scenario;
diff --git a/src/at/ac/tuwien/lsdc/types/SchedulerData.java b/src/at/ac/tuwien/lsdc/types/SchedulerData.java
index ed90f56..72cde36 100644
--- a/src/at/ac/tuwien/lsdc/types/SchedulerData.java
+++ b/src/at/ac/tuwien/lsdc/types/SchedulerData.java
@@ -8,13 +8,13 @@ public class SchedulerData {
 	private long totalSize;
 	private long runningPMs;
 	private long runningVMs;
-	private long totalConsumption;
+	private double 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) {
+			double totalConsumption, int inSourced, int outSourced) {
 		this.timestamp = timestamp;
 		this.totalRAM = totalRAM;
 		this.totalCPU = totalCPU;
-- 
2.43.0