changed names of resoure variables to reserved*, change calculation of checkVM, make...
authorStefan Derkits <stefan@derkits.at>
Thu, 23 May 2013 08:46:15 +0000 (10:46 +0200)
committerStefan Derkits <stefan@derkits.at>
Thu, 23 May 2013 08:46:15 +0000 (10:46 +0200)
src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
src/at/ac/tuwien/lsdc/types/VirtualMachine.java

index 9e434e95bdc96e2a8b424e963f541e5e8143935f..caf9901550d0cf12f24961d1033a102c9cf28fd3 100644 (file)
@@ -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<Integer, VirtualMachine>();
-               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;
+       }
+
 }
index 5b2de939b5d0abdc8e01083a9ac161d12abdaac4..a6476bd89056680c8e3654194ee5c91c8581e8b2 100644 (file)
@@ -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;
+       }
+
 }