From ce573e0a4462c4b8aa375508f9c933a952b22244 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Thu, 23 May 2013 10:46:15 +0200 Subject: [PATCH] changed names of resoure variables to reserved*, change calculation of checkVM, make resize into own function. fix resource in/decrease for VMs on PMs --- .../ac/tuwien/lsdc/types/PhysicalMachine.java | 61 +++++++++++-------- .../ac/tuwien/lsdc/types/VirtualMachine.java | 49 +++++++++------ 2 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java index 9e434e9..caf9901 100644 --- a/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java +++ b/src/at/ac/tuwien/lsdc/types/PhysicalMachine.java @@ -18,9 +18,9 @@ public class PhysicalMachine { private final int maxRAM = 4700; private final int maxCPU = 2400; - private int size; - private int RAM; - private int CPU; + private int reservedSize; + private int reservedRAM; + private int reservedCPU; private final int initialSize = 850; private final int initialRAM = 300; @@ -34,9 +34,9 @@ public class PhysicalMachine { } public void start() { - size = initialSize; - RAM = initialRAM; - CPU = initialCPU; + reservedSize = initialSize; + reservedRAM = initialRAM; + reservedCPU = initialCPU; running = true; } @@ -46,15 +46,15 @@ public class PhysicalMachine { "PM cannot be stopped. Some VMs still running"); VMs = new HashMap(); - size = 0; - RAM = 0; - CPU = 0; + reservedSize = 0; + reservedRAM = 0; + reservedCPU = 0; running = false; } public double getConsumption() { if (running) - return 200.0 + 0.3 * (CPU - initialCPU); + return 200.0 + 0.3 * (getCurrentCPU() - initialCPU); else return 0.0; } @@ -64,9 +64,9 @@ public class PhysicalMachine { if (checkVM(sz, ram, cpu)) { VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type); VMs.put(vm.getId(), vm); - size = size + vm.getSize(); - RAM = RAM + vm.getRAM(); - CPU = CPU + vm.getCPU(); + reservedSize = reservedSize + vm.getReservedSize(); + reservedRAM = reservedRAM + vm.getReservedRAM(); + reservedCPU = reservedCPU + vm.getReservedCPU(); return vm; } else return null; @@ -81,28 +81,28 @@ public class PhysicalMachine { + vm.getId()); } else { VMs.remove(vm.getId()); - size = size - vm.getSize(); - RAM = RAM - vm.getRAM(); - CPU = CPU - vm.getCPU(); + reservedSize = reservedSize - vm.getReservedSize(); + reservedRAM = reservedRAM - vm.getReservedRAM(); + reservedCPU = reservedCPU - vm.getReservedCPU(); } } } public boolean checkVM(int size, int RAM, int CPU) { - return (size <= availableSize()) && (RAM <= availableRAM()) - && (CPU <= availableCPU()); + return ((VirtualMachine.initialSize + size) <= availableSize()) && ((VirtualMachine.initialRAM + RAM) <= availableRAM()) + && ((VirtualMachine.initialCPU + CPU) <= availableCPU()); } private int availableSize() { - return maxSize - getCurrentSize(); + return maxSize - reservedSize; } private int availableRAM() { - return maxRAM - getCurrentRam(); + return maxRAM - reservedRAM; } private int availableCPU() { - return maxCPU - getCurrentCPU(); + return maxCPU - reservedCPU; } public int getCurrentSize() { @@ -127,15 +127,15 @@ public class PhysicalMachine { } public double getSizeUtilization() { - return ((double) (size - initialSize) / (maxSize - initialSize)) * 100; + return ((double) (reservedSize - initialSize) / (maxSize - initialSize)) * 100; } public double getRamUtilization() { - return ((double) (RAM - initialRAM) / (maxRAM - initialRAM)) * 100; + return ((double) (reservedRAM - initialRAM) / (maxRAM - initialRAM)) * 100; } public double getCpuUtilization() { - return ((double) (CPU - initialCPU) / (maxCPU - initialCPU)) * 100; + return ((double) (reservedCPU - initialCPU) / (maxCPU - initialCPU)) * 100; } public double getAverageUtilization() { @@ -157,4 +157,17 @@ public class PhysicalMachine { public int countCurrentlyRunningVMs() { return VMs.values().size(); } + + public int getReservedSize() { + return reservedSize; + } + + public int getReservedRAM() { + return reservedRAM; + } + + public int getReservedCPU() { + return reservedCPU; + } + } diff --git a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java index 5b2de93..a6476bd 100644 --- a/src/at/ac/tuwien/lsdc/types/VirtualMachine.java +++ b/src/at/ac/tuwien/lsdc/types/VirtualMachine.java @@ -23,18 +23,18 @@ public class VirtualMachine { public static final int initialRAM = 50; public static final int initialCPU = 150; - private int size; - private int RAM; - private int CPU; + private int reservedSize; + private int reservedRAM; + private int reservedCPU; private VMType type; public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) { this.id = count; count++; - this.size = size + initialSize; - this.RAM = RAM + initialRAM; - this.CPU = CPU + initialCPU; + this.reservedSize = size + initialSize; + this.reservedRAM = RAM + initialRAM; + this.reservedCPU = CPU + initialCPU; this.runningOn = pm; this.posOnPM = pm.countCurrentlyRunningVMs(); this.type = type; @@ -45,12 +45,16 @@ public class VirtualMachine { applications.put(app.getID(), app); app.setRunningOn(this); return true; - } else if (type == VMType.Resizable && runningOn.checkVM(app.getSize(), app.getRam(), app.getCpu())) { - applications.put(app.getID(), app); - size = size + app.getSize(); - RAM = RAM + app.getRam(); - CPU = CPU + app.getCpu(); - app.setRunningOn(this); + } else + return false; + } + + public boolean resizeVM(int newSize, int newRAM, int newCPU) { + if (type == VMType.Resizable && runningOn.checkVM(newSize-reservedSize, newRAM-reservedRAM, newCPU-reservedCPU )) { + //Resize VM + reservedSize = reservedSize + newSize; + reservedRAM = reservedRAM + newRAM; + reservedCPU = reservedCPU + newCPU; return true; } else return false; @@ -58,9 +62,6 @@ public class VirtualMachine { public boolean stopApplication(Application app) { if (applications.containsKey(app.getID())) { - size = size - app.getSize(); - RAM = RAM - app.getRam(); - CPU = CPU - app.getCpu(); applications.remove(app.getID()); app.setRunningOn(null); return true; @@ -75,15 +76,15 @@ public class VirtualMachine { } private int availableSize() { - return size - getSize(); + return reservedSize - getSize(); } private int availableRAM() { - return RAM - getRAM(); + return reservedRAM - getRAM(); } private int availableCPU() { - return CPU - getCPU(); + return reservedCPU - getCPU(); } public int getId() { @@ -123,4 +124,16 @@ public class VirtualMachine { return usedCPU; } + public int getReservedSize() { + return reservedSize; + } + + public int getReservedRAM() { + return reservedRAM; + } + + public int getReservedCPU() { + return reservedCPU; + } + } -- 2.43.0