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;
36 private int totalResizeCalls = 0;
38 private boolean running = false;
40 public PhysicalMachine() {
46 reservedSize = initialSize;
47 reservedRAM = initialRAM;
48 reservedCPU = initialCPU;
52 public void stop() throws VMsRunningException {
54 throw new VMsRunningException("PM cannot be stopped. Some VMs still running");
56 VMs = new HashMap<Integer, VirtualMachine>();
63 public double getConsumption() {
65 return 200.0 + 0.3 * (getCurrentCPU() - initialCPU);
70 // Creates a VirtualMachine of maximum possible Size
71 public VirtualMachine startVirtualMachine(VMType type) {
72 VirtualMachine vm = startVirtualMachine(maxSize - initialSize - VirtualMachine.initialSize,
73 maxRAM - initialRAM - VirtualMachine.initialRAM, maxCPU - initialCPU
74 - VirtualMachine.initialCPU, type);
78 public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
79 if (checkVM(sz, ram, cpu)) {
80 VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
81 VMs.put(vm.getId(), vm);
82 reservedSize = reservedSize + vm.getReservedSize();
83 reservedRAM = reservedRAM + vm.getReservedRAM();
84 reservedCPU = reservedCPU + vm.getReservedCPU();
91 public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
92 if (VMs.containsKey(vm.getId())) {
93 if (vm.getApplications().size() != 0) {
94 throw new ActiveApplicationsException(
95 "Applications must be migrated before stopping a VM, VM id " + vm.getId());
97 VMs.remove(vm.getId());
98 reservedSize = reservedSize - vm.getReservedSize();
99 reservedRAM = reservedRAM - vm.getReservedRAM();
100 reservedCPU = reservedCPU - vm.getReservedCPU();
101 totalResizeCalls += vm.getTotalResizeCalls();
107 * Checks if there is enough space for the VM initial resources plus its netto resources
108 * @param size the _netto_ size reserved on the VM
109 * @param RAM the _netto_ RAM reserved on the VM
110 * @param CPU the _netto_ CPU reserved on the VM
111 * @return true if there is enough space to run the VM
113 public boolean checkVM(int size, int RAM, int CPU) {
114 return ((VirtualMachine.initialSize + size) <= availableSize())
115 && ((VirtualMachine.initialRAM + RAM) <= availableRAM())
116 && ((VirtualMachine.initialCPU + CPU) <= availableCPU());
119 public boolean resizeUp(int diffSize, int diffRAM, int diffCPU) throws VMResizeException {
120 reservedSize = reservedSize + diffSize;
121 reservedRAM = reservedRAM + diffRAM;
122 reservedCPU = reservedCPU + diffCPU;
126 public boolean resizeDown(Application app) {
127 reservedSize = reservedSize - app.getSize();
128 reservedRAM = reservedRAM - app.getRam();
129 reservedCPU = reservedCPU - app.getCpu();
133 public boolean checkExtendVM(int sizeDiff, int ramDiff, int CPUDiff) {
134 return ((sizeDiff <= availableSize()) && (ramDiff <= availableRAM()) && (CPUDiff <= availableCPU()));
137 private int availableSize() {
138 return maxSize - reservedSize;
141 private int availableRAM() {
142 return maxRAM - reservedRAM;
145 private int availableCPU() {
146 return maxCPU - reservedCPU;
149 public int getCurrentSize() {
150 int currSize = initialSize;
151 for (VirtualMachine vm : VMs.values())
152 currSize += vm.getSize();
156 public int getCurrentRam() {
157 int currRAM = initialRAM;
158 for (VirtualMachine vm : VMs.values())
159 currRAM += vm.getRAM();
163 public int getCurrentCPU() {
164 int currCPU = initialCPU;
165 for (VirtualMachine vm : VMs.values())
166 currCPU += vm.getCPU();
170 public double getSizeUtilization() {
171 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
174 public double getRamUtilization() {
175 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
178 public double getCpuUtilization() {
179 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
182 public double getAverageUtilization() {
183 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
190 public boolean isRunning() {
194 public VirtualMachine getVirtualMachine(int id) {
198 public VirtualMachine getLatestVM() {
199 VirtualMachine vm = null;
200 for (Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext();) {
206 public Integer getLatestVMID() {
207 return getLatestVM().getId();
211 * return a list of all VMs running on this PM.
213 * @return a HashMap with all VMs running on this PM.
215 public HashMap<Integer, VirtualMachine> getVirtualMachines() {
219 public int countCurrentlyRunningVMs() {
220 return VMs.values().size();
223 public int getReservedSize() {
227 public int getReservedRAM() {
231 public int getReservedCPU() {
235 public int getTotalVMs() {
239 public int getTotalResizeCalls() {
240 return totalResizeCalls;
244 public int compareTo(PhysicalMachine other) {
248 if (getAverageUtilization() < other.getAverageUtilization())
250 else if (getAverageUtilization() > other.getAverageUtilization())