From 0ed0c076b7bf21464e55b35af76f8ce94bca3df8 Mon Sep 17 00:00:00 2001
From: Stefan Derkits <stefan@derkits.at>
Date: Wed, 22 May 2013 22:15:13 +0200
Subject: [PATCH] changed calculation of RAM,CPU & Size

---
 .../ac/tuwien/lsdc/types/PhysicalMachine.java | 58 +++++++++++--------
 .../ac/tuwien/lsdc/types/VirtualMachine.java  | 15 ++++-
 2 files changed, 47 insertions(+), 26 deletions(-)

diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
index 8190d72..b158de9 100644
--- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
@@ -13,7 +13,7 @@ public class PhysicalMachine {
 	private HashMap<Integer, VirtualMachine> VMs = new HashMap<Integer, VirtualMachine>();
 
 	private int id;
-	
+
 	private final int maxSize = 50000;
 	private final int maxRAM = 4700;
 	private final int maxCPU = 2400;
@@ -41,9 +41,10 @@ public class PhysicalMachine {
 	}
 
 	public void stop() throws VMsRunningException {
-		if(VMs.size() > 0)
-			throw new VMsRunningException("PM cannot be stopped. Some VMs still running");
-		
+		if (VMs.size() > 0)
+			throw new VMsRunningException(
+					"PM cannot be stopped. Some VMs still running");
+
 		VMs = new HashMap<Integer, VirtualMachine>();
 		size = 0;
 		RAM = 0;
@@ -52,15 +53,14 @@ public class PhysicalMachine {
 	}
 
 	public double getConsumption() {
-		if(CPU > initialCPU)
+		if (running)
 			return 200.0 + 0.3 * (CPU - initialCPU);
-		else if(running)
-			return 200.0;
 		else
 			return 0.0;
 	}
 
-	public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
+	public VirtualMachine startVirtualMachine(int sz, int ram, int cpu,
+			VMType type) {
 		if (checkVM(sz, ram, cpu)) {
 			VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
 			VMs.put(vm.getId(), vm);
@@ -72,10 +72,13 @@ public class PhysicalMachine {
 			return null;
 	}
 
-	public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
+	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();
@@ -101,31 +104,40 @@ public class PhysicalMachine {
 	private int availableCPU() {
 		return maxCPU - CPU;
 	}
-	
+
 	public int getCurrentSize() {
-		return size;
+		int currSize = initialSize;
+		for (VirtualMachine vm : VMs.values())
+			currSize += vm.getSize();
+		return currSize;
 	}
-	
+
 	public int getCurrentRam() {
-		return RAM;
+		int currRAM = initialRAM;
+		for (VirtualMachine vm : VMs.values())
+			currRAM += vm.getRAM();
+		return currRAM;
 	}
-	
+
 	public int getCurrentCpu() {
-		return CPU;
+		int currCPU = initialCPU;
+		for (VirtualMachine vm : VMs.values())
+			currCPU += vm.getCPU();
+		return currCPU;
 	}
-	
+
 	public double getSizeUtilization() {
-		return ((double)(size - initialSize) / (maxSize - initialSize)) * 100;
+		return ((double) (size - initialSize) / (maxSize - initialSize)) * 100;
 	}
-	
+
 	public double getRamUtilization() {
-		return ((double)(RAM - initialRAM) / (maxRAM - initialRAM)) * 100;
+		return ((double) (RAM - initialRAM) / (maxRAM - initialRAM)) * 100;
 	}
-	
+
 	public double getCpuUtilization() {
-		return ((double)(CPU - initialCPU) / (maxCPU - initialCPU)) * 100;
+		return ((double) (CPU - initialCPU) / (maxCPU - initialCPU)) * 100;
 	}
-	
+
 	public double getAverageUtilization() {
 		return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
 	}
diff --git a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
index 25933eb..7c29471 100644
--- a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
+++ b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java
@@ -103,15 +103,24 @@ public class VirtualMachine {
 	}
 
 	public int getSize() {
-		return size;
+		int usedSize = initialSize;
+		for (Application a : applications.values())
+			usedSize += a.getSize();
+		return usedSize;
 	}
 
 	public int getRAM() {
-		return RAM;
+		int usedRAM = initialRAM;
+		for (Application a : applications.values())
+			usedRAM += a.getRam();
+		return usedRAM;
 	}
 
 	public int getCPU() {
-		return CPU;
+		int usedCPU = initialCPU;
+		for (Application a : applications.values())
+			usedCPU += a.getCpu();
+		return usedCPU;
 	}
 
 }
-- 
2.43.0