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;
8 public class VirtualMachine {
11 Resizable, NonResizable
14 private static int count = 0;
17 private PhysicalMachine runningOn;
19 private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
23 public static final int initialSize = 100;
24 public static final int initialRAM = 50;
25 public static final int initialCPU = 150;
27 private int reservedSize;
28 private int reservedRAM;
29 private int reservedCPU;
33 private int totalResizeCalls = 0;
35 public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
38 reservedSize = size + initialSize;
39 reservedRAM = RAM + initialRAM;
40 reservedCPU = CPU + initialCPU;
42 posOnPM = pm.countCurrentlyRunningVMs();
46 public boolean startApplication(Application app) {
47 if (enoughResources(app)) {
48 applications.put(app.getID(), app);
49 app.setRunningOn(this);
56 * Set the VM size to the given new values.
59 * the new size of the VM, excluding the initial consumption.
61 * the new RAM of the VM, excluding the initial consumption.
63 * the new CPU of the VM, excluding the initial consumption.
66 // public boolean resizeVM(int newSize, int newRAM, int newCPU) {
67 // if (type == VMType.Resizable
68 // && runningOn.checkExtendVM(newSize - reservedSize, newRAM - reservedRAM, newCPU
71 // reservedSize = initialSize + newSize;
72 // reservedRAM = initialRAM + newRAM;
73 // reservedCPU = initialCPU + newCPU;
77 //// throw new VMResizeException("Could not resize VM!", this);
80 public boolean resizeUp(Application app) throws VMResizeException {
82 int diffSize = (app.getSize() - availableSize());
83 int diffRAM = (app.getRam() - availableRAM());
84 int diffCPU = (app.getCpu() - availableCPU());
86 if(!(type == VMType.Resizable) || !runningOn.checkExtendVM(diffSize, diffRAM, diffCPU)) {
87 throw new VMResizeException("Could not resize VM!", this);
90 runningOn.resizeUp(diffSize, diffRAM, diffCPU);
92 reservedSize = reservedSize + diffSize;
93 reservedRAM = reservedRAM + diffRAM;
94 reservedCPU = reservedCPU + diffCPU;
99 public boolean resizeDown(Application app) {
100 runningOn.resizeDown(app);
102 reservedSize = reservedSize - app.getSize();
103 reservedRAM = reservedRAM - app.getRam();
104 reservedCPU = reservedCPU - app.getCpu();
109 public boolean stopApplication(Application app) {
110 if (applications.containsKey(app.getID())) {
111 applications.remove(app.getID());
117 public boolean enoughResources(Application app) {
118 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
121 public int availableSize() {
122 return reservedSize - getSize();
125 public int availableRAM() {
126 return reservedRAM - getRAM();
129 public int availableCPU() {
130 return reservedCPU - getCPU();
137 public PhysicalMachine getRunningOn() {
141 public int getPositionOnPM() {
145 public ArrayList<Application> getApplications() {
146 return new ArrayList<Application>(applications.values());
149 public int getSize() {
150 int usedSize = initialSize;
151 for (Application a : applications.values())
152 usedSize += a.getSize();
156 public int getRAM() {
157 int usedRAM = initialRAM;
158 for (Application a : applications.values())
159 usedRAM += a.getRam();
163 public int getCPU() {
164 int usedCPU = initialCPU;
165 for (Application a : applications.values())
166 usedCPU += a.getCpu();
170 public int getReservedSize() {
174 public int getReservedRAM() {
178 public int getReservedCPU() {
182 public int getTotalResizeCalls() {
183 return totalResizeCalls;
187 public String toString() {
188 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
189 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";