From a7db7ceee2ec71800dd64efe232de6b78d821795 Mon Sep 17 00:00:00 2001
From: Andreas Egger <egger.andreas.1@gmail.com>
Date: Tue, 21 May 2013 21:02:34 +0200
Subject: [PATCH] Getting executable

---
 .../lsdc/management/MachineManager.java       | 55 ++++++++++++++++---
 .../tuwien/lsdc/sched/AbstractScheduler.java  | 46 +++++++++++-----
 src/at/ac/tuwien/lsdc/sched/SchedulerA.java   |  9 +--
 .../ac/tuwien/lsdc/types/PhysicalMachine.java | 14 ++++-
 4 files changed, 97 insertions(+), 27 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/management/MachineManager.java b/src/at/ac/tuwien/lsdc/management/MachineManager.java
index 804634b..cbdea40 100644
--- a/src/at/ac/tuwien/lsdc/management/MachineManager.java
+++ b/src/at/ac/tuwien/lsdc/management/MachineManager.java
@@ -24,14 +24,6 @@ public class MachineManager {
 	}
 	
 	
-	private void init(int numPMs) {
-		for (int i = 0; i < numPMs; i++) {
-			PhysicalMachine pm = new PhysicalMachine();
-			PMs.put(pm.getId(), pm);
-		}
-	}
-	
-	
 	/**
 	 * Start a physical machine
 	 * @return the PM that was started, null if all machines already running
@@ -73,6 +65,45 @@ public class MachineManager {
 	public int getMaxPMs() {
 		return numPMs;
 	}
+	
+	/**
+	 * Get the total size on all PMs
+	 * @return the total size on all PMs
+	 */
+	public int getTotalSize() {
+		int totalSize = 0;
+		for (PhysicalMachine pm : PMs.values()) {
+			if (pm.isRunning())
+				totalSize += pm.getCurrentSize();
+		}
+		return totalSize;
+	}
+	
+	/**
+	 * Get the total Ram usage of all PMs
+	 * @return the total usage of Ram
+	 */
+	public int getTotalRam() {
+		int totalRam = 0;
+		for (PhysicalMachine pm : PMs.values()) {
+			if (pm.isRunning())
+				totalRam += pm.getCurrentRam();
+		}
+		return totalRam;
+	}
+	
+	/**
+	 * Get the total Cpu usage of all PMs
+	 * @return the total usage of Cpu power
+	 */
+	public int getTotalCpu() {
+		int totalCpu = 0;
+		for (PhysicalMachine pm : PMs.values()) {
+			if (pm.isRunning())
+				totalCpu += pm.getCurrentCpu();
+		}
+		return totalCpu;
+	}
 
 	/**
 	 * Gets the total power consumption summed up from each PM
@@ -87,6 +118,10 @@ public class MachineManager {
 		return consumption;
 	}
 
+	/**
+	 * Gets the number of currently running PMs
+	 * @return the number of currently running PMs
+	 */
 	public int countCurrentlyRunningPMs() {
 		int running = 0;
 		for (PhysicalMachine pm : PMs.values()) {
@@ -96,6 +131,10 @@ public class MachineManager {
 		return running;
 	}
 
+	/**
+	 * Gets the number of currently running VMs
+	 * @return the number of currently running VMs
+	 */
 	public int countCurrentlyRunningVMs() {
 		int running = 0;
 		for (PhysicalMachine pm : PMs.values()) {
diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
index 6f5547b..da6df0b 100644
--- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
+++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java
@@ -38,15 +38,12 @@ public abstract class AbstractScheduler {
 	protected MachineManager manager;
 	protected File schedulerLog;
 	protected CSVLogger logger;
-	protected VMType VMType;
+	protected VMType vmType;
 
 
 	// 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;
-	
+	// (start outSourced, end outSourced, start inSourced, end inSourced)	
 	protected SortedMap<Long, LinkedList<SchedulerEvent>> eventMap;
 
 	// Scheduler has an internal Time "Abstraction"
@@ -138,6 +135,10 @@ public abstract class AbstractScheduler {
 		}
 	}
 
+	/**
+	 * Start the actual scheduling algorithm. 
+	 * Each scheduler will implement the method handleEvents differently
+	 */
 	protected void startScheduling() {
 		while (true) {
 			LinkedList<SchedulerEvent> events = eventMap.get(currTime);
@@ -148,21 +149,36 @@ public abstract class AbstractScheduler {
 
 			if (currTime == endTime) {
 				// reached last Event, Scheduler will shut down
+				log.debug("Last event reached at time "+currTime);
 				break;
 			}
 		}
 	}
 
-	// this method is where the Scheduling Algorithm resides
-	// it reads the Events (start & stop of applications)
+	/**
+	 * this method is where the Scheduling Algorithm resides
+	 * it reads the Events (start & stop of applications)
+	 * @param events the events to be read and used by the scheduler
+	 */
 	protected abstract void handleEvents(LinkedList<SchedulerEvent> events);
 
+	/**
+	 * Does the logging for each timestamp.
+	 * It uses the CSVLogger to write directly to the specific
+	 * scheduler/scenario csv file
+	 */
 	protected void doStateLogging() {
-		SchedulerData data = new SchedulerData(currTime, 1, 1, 1,
-				manager.countCurrentlyRunningPMs(),
-				manager.countCurrentlyRunningVMs(),
-				manager.getCurrentConsumption(), numCurrInSourced,
-				numCurrOutSourced);
+		SchedulerData data = new SchedulerData(
+								currTime, 
+								manager.getTotalRam(), 
+								manager.getTotalCpu(), 
+								manager.getTotalSize(),
+								manager.countCurrentlyRunningPMs(),
+								manager.countCurrentlyRunningVMs(),
+								manager.getCurrentConsumption(), 
+								numCurrInSourced,
+								numCurrOutSourced
+							);
 		totalConsumption += manager.getCurrentConsumption();
 		try {
 			logger.logSchedulerData(data);
@@ -171,8 +187,10 @@ public abstract class AbstractScheduler {
 		}
 	}
 
-	// this creates the total summary which should be written to a CSV at the
-	// end
+	/**
+	 * this creates the total summary which should be written to a CSV at the end
+	 * @return a ScenarioData Object that holds the values to be logged
+	 */
 	protected ScenarioData doEndLogging() {
 		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 2fa1bdd..266a988 100644
--- a/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
+++ b/src/at/ac/tuwien/lsdc/sched/SchedulerA.java
@@ -21,6 +21,7 @@ import at.ac.tuwien.lsdc.types.SchedulerType;
 import at.ac.tuwien.lsdc.types.SortedApplication;
 import at.ac.tuwien.lsdc.types.SortedPhysicalMachine;
 import at.ac.tuwien.lsdc.types.VirtualMachine;
+import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
 
 
 public class SchedulerA extends AbstractScheduler {
@@ -32,7 +33,7 @@ public class SchedulerA extends AbstractScheduler {
 			throws IOException {
 		super(numPMs, numCloudPartners, schedulerLog, schedulerType, scenario);
 		
-		this.VMType = VMType.NonResizable;
+		this.vmType = VMType.NonResizable;
 	}
 
 	@Override
@@ -63,7 +64,7 @@ public class SchedulerA extends AbstractScheduler {
 				boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 				
 				if(enoughResources) {
-					pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
+					pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), vmType);
 					appDeployed = true;
 					log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
 				}
@@ -80,7 +81,7 @@ public class SchedulerA extends AbstractScheduler {
 					boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 					
 					if(enoughResources) {
-						VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
+						VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), vmType);
 						vm.startApplication(app);
 						appDeployed = true;
 						log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
@@ -93,7 +94,7 @@ public class SchedulerA extends AbstractScheduler {
 					boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
 					
 					if(enoughResources) {
-						VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
+						VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), vmType);
 						vm.startApplication(app);
 						appDeployed = true;
 						log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
index 05df3d6..be3f25f 100644
--- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
@@ -70,7 +70,7 @@ public class PhysicalMachine {
 	public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
 		if (VMs.containsKey(vm.getId())) {
 			if (vm.getApplications().size() != 0) {
-				throw new ActiveApplicationsException("Applications must be migrated before stopping a vm, vm id "+vm.getId());
+				throw new ActiveApplicationsException("Applications must be migrated before stopping a VM, VM id "+vm.getId());
 			} else {
 				VMs.remove(vm.getId());
 				size = size - vm.getSize();
@@ -97,6 +97,18 @@ public class PhysicalMachine {
 		return maxCPU - CPU;
 	}
 	
+	public int getCurrentSize() {
+		return size;
+	}
+	
+	public int getCurrentRam() {
+		return RAM;
+	}
+	
+	public int getCurrentCpu() {
+		return CPU;
+	}
+	
 	public double getSizeUtilization() {
 		return ((double)(size - initialSize) / (maxSize - initialSize)) * 100;
 	}
-- 
2.43.0