1 package at.ac.tuwien.lsdc.types;
3 import java.util.ArrayList;
4 import java.util.HashMap;
6 import at.ac.tuwien.lsdc.exception.VMResizeException;
7 import at.ac.tuwien.lsdc.management.MachineManager;
9 public class VirtualMachine {
12 Resizable, NonResizable
15 private static int count = 0;
18 private PhysicalMachine runningOn;
20 private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
24 public static final int initialSize = 100;
25 public static final int initialRAM = 50;
26 public static final int initialCPU = 150;
28 private int reservedSize;
29 private int reservedRAM;
30 private int reservedCPU;
34 private int totalResizeCalls = 0;
36 public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
39 reservedSize = size + initialSize;
40 reservedRAM = RAM + initialRAM;
41 reservedCPU = CPU + initialCPU;
43 posOnPM = pm.countCurrentlyRunningVMs();
47 public boolean startApplication(Application app) {
48 if (enoughResources(app)) {
49 applications.put(app.getID(), app);
50 app.setRunningOn(this);
57 * Set the VM size to the given new values.
60 * the new size of the VM, excluding the initial consumption.
62 * the new RAM of the VM, excluding the initial consumption.
64 * the new CPU of the VM, excluding the initial consumption.
67 // public boolean resizeVM(int newSize, int newRAM, int newCPU) {
68 // if (type == VMType.Resizable
69 // && runningOn.checkExtendVM(newSize - reservedSize, newRAM - reservedRAM, newCPU
72 // reservedSize = initialSize + newSize;
73 // reservedRAM = initialRAM + newRAM;
74 // reservedCPU = initialCPU + newCPU;
78 //// throw new VMResizeException("Could not resize VM!", this);
81 public boolean resizeUp(Application app) throws VMResizeException {
83 int diffSize = (app.getSize() - availableSize());
84 int diffRAM = (app.getRam() - availableRAM());
85 int diffCPU = (app.getCpu() - availableCPU());
87 if(!(type == VMType.Resizable) || !runningOn.checkExtendVM(diffSize, diffRAM, diffCPU)) {
88 throw new VMResizeException("Could not resize VM!", this);
91 runningOn.resizeUp(diffSize, diffRAM, diffCPU);
93 reservedSize = reservedSize + diffSize;
94 reservedRAM = reservedRAM + diffRAM;
95 reservedCPU = reservedCPU + diffCPU;
96 // totalResizeCalls++;
97 MachineManager.addResizeCall();
101 public boolean resizeDown(Application app) {
102 runningOn.resizeDown(app);
104 reservedSize = reservedSize - app.getSize();
105 reservedRAM = reservedRAM - app.getRam();
106 reservedCPU = reservedCPU - app.getCpu();
107 // totalResizeCalls++;
108 MachineManager.addResizeCall();
112 public boolean stopApplication(Application app) {
113 if (applications.containsKey(app.getID())) {
114 applications.remove(app.getID());
120 public boolean enoughResources(Application app) {
121 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
124 public int availableSize() {
125 return reservedSize - getSize();
128 public int availableRAM() {
129 return reservedRAM - getRAM();
132 public int availableCPU() {
133 return reservedCPU - getCPU();
140 public PhysicalMachine getRunningOn() {
144 public int getPositionOnPM() {
148 public ArrayList<Application> getApplications() {
149 return new ArrayList<Application>(applications.values());
152 public int getSize() {
153 int usedSize = initialSize;
154 for (Application a : applications.values())
155 usedSize += a.getSize();
159 public int getRAM() {
160 int usedRAM = initialRAM;
161 for (Application a : applications.values())
162 usedRAM += a.getRam();
166 public int getCPU() {
167 int usedCPU = initialCPU;
168 for (Application a : applications.values())
169 usedCPU += a.getCpu();
173 public int getReservedSize() {
177 public int getReservedRAM() {
181 public int getReservedCPU() {
185 public int getTotalResizeCalls() {
186 return totalResizeCalls;
190 public String toString() {
191 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
192 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";