1 package at.ac.tuwien.lsdc.types;
3 import java.util.HashMap;
4 import java.util.Iterator;
6 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
7 import at.ac.tuwien.lsdc.exception.VMResizeException;
8 import at.ac.tuwien.lsdc.exception.VMsRunningException;
9 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
11 public class PhysicalMachine implements Comparable<PhysicalMachine> {
13 final int BEFORE = -1;
17 private static int count = 0;
19 private HashMap<Integer, VirtualMachine> VMs = new HashMap<Integer, VirtualMachine>();
23 private final int maxSize = 50000;
24 private final int maxRAM = 4700;
25 private final int maxCPU = 2400;
27 private int reservedSize;
28 private int reservedRAM;
29 private int reservedCPU;
31 private final int initialSize = 850;
32 private final int initialRAM = 300;
33 private final int initialCPU = 500;
35 private int totalVMs = 0;
37 private boolean running = false;
39 public PhysicalMachine() {
45 reservedSize = initialSize;
46 reservedRAM = initialRAM;
47 reservedCPU = initialCPU;
51 public void stop() throws VMsRunningException {
53 throw new VMsRunningException("PM cannot be stopped. Some VMs still running");
55 VMs = new HashMap<Integer, VirtualMachine>();
62 public double getConsumption() {
64 return 200.0 + 0.3 * (getCurrentCPU() - initialCPU);
69 // Creates a VirtualMachine of maximum possible Size
70 public VirtualMachine startVirtualMachine(VMType type) {
71 VirtualMachine vm = new VirtualMachine(maxSize - initialSize - VirtualMachine.initialSize,
72 maxRAM - initialRAM - VirtualMachine.initialRAM, maxCPU - initialCPU
73 - VirtualMachine.initialCPU, this, type);
77 public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
78 if (checkVM(sz, ram, cpu)) {
79 VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
80 VMs.put(vm.getId(), vm);
81 reservedSize = reservedSize + vm.getReservedSize();
82 reservedRAM = reservedRAM + vm.getReservedRAM();
83 reservedCPU = reservedCPU + vm.getReservedCPU();
90 public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
91 if (VMs.containsKey(vm.getId())) {
92 if (vm.getApplications().size() != 0) {
93 throw new ActiveApplicationsException(
94 "Applications must be migrated before stopping a VM, VM id " + vm.getId());
96 VMs.remove(vm.getId());
97 reservedSize = reservedSize - vm.getReservedSize();
98 reservedRAM = reservedRAM - vm.getReservedRAM();
99 reservedCPU = reservedCPU - vm.getReservedCPU();
105 * Checks if there is enough space for the VM initial resources plus its netto resources
106 * @param size the _netto_ size reserved on the VM
107 * @param RAM the _netto_ RAM reserved on the VM
108 * @param CPU the _netto_ CPU reserved on the VM
109 * @return true if there is enough space to run the VM
111 public boolean checkVM(int size, int RAM, int CPU) {
112 return ((VirtualMachine.initialSize + size) <= availableSize())
113 && ((VirtualMachine.initialRAM + RAM) <= availableRAM())
114 && ((VirtualMachine.initialCPU + CPU) <= availableCPU());
117 public boolean resizeUp(int diffSize, int diffRAM, int diffCPU) throws VMResizeException {
118 reservedSize = reservedSize + diffSize;
119 reservedRAM = reservedRAM + diffRAM;
120 reservedCPU = reservedCPU + diffCPU;
124 public boolean resizeDown(Application app) {
125 reservedSize = reservedSize - app.getSize();
126 reservedRAM = reservedRAM - app.getRam();
127 reservedCPU = reservedCPU - app.getCpu();
131 public void resizeVM(VirtualMachine vm, Application app) {
135 public boolean checkExtendVM(int sizeDiff, int ramDiff, int CPUDiff) {
136 return ((sizeDiff <= availableSize()) && (ramDiff <= availableRAM()) && (CPUDiff <= availableCPU()));
139 private int availableSize() {
140 return maxSize - reservedSize;
143 private int availableRAM() {
144 return maxRAM - reservedRAM;
147 private int availableCPU() {
148 return maxCPU - reservedCPU;
151 public int getCurrentSize() {
152 int currSize = initialSize;
153 for (VirtualMachine vm : VMs.values())
154 currSize += vm.getSize();
158 public int getCurrentRam() {
159 int currRAM = initialRAM;
160 for (VirtualMachine vm : VMs.values())
161 currRAM += vm.getRAM();
165 public int getCurrentCPU() {
166 int currCPU = initialCPU;
167 for (VirtualMachine vm : VMs.values())
168 currCPU += vm.getCPU();
172 public double getSizeUtilization() {
173 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
176 public double getRamUtilization() {
177 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
180 public double getCpuUtilization() {
181 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
184 public double getAverageUtilization() {
185 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
192 public boolean isRunning() {
196 public VirtualMachine getVirtualMachine(int id) {
200 public VirtualMachine getLatestVM() {
201 VirtualMachine vm = null;
202 for (Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext();) {
208 public Integer getLatestVMID() {
209 return getLatestVM().getId();
213 * return a list of all VMs running on this PM.
215 * @return a HashMap with all VMs running on this PM.
217 public HashMap<Integer, VirtualMachine> getVirtualMachines() {
221 public int countCurrentlyRunningVMs() {
222 return VMs.values().size();
225 public int getReservedSize() {
229 public int getReservedRAM() {
233 public int getReservedCPU() {
237 public int getTotalVMs() {
242 public int compareTo(PhysicalMachine other) {
246 if (getAverageUtilization() < other.getAverageUtilization())
248 else if (getAverageUtilization() > other.getAverageUtilization())