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 = startVirtualMachine(maxSize - initialSize - VirtualMachine.initialSize,
72 maxRAM - initialRAM - VirtualMachine.initialRAM, maxCPU - initialCPU
73 - VirtualMachine.initialCPU, 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 boolean checkExtendVM(int sizeDiff, int ramDiff, int CPUDiff) {
132 return ((sizeDiff <= availableSize()) && (ramDiff <= availableRAM()) && (CPUDiff <= availableCPU()));
135 private int availableSize() {
136 return maxSize - reservedSize;
139 private int availableRAM() {
140 return maxRAM - reservedRAM;
143 private int availableCPU() {
144 return maxCPU - reservedCPU;
147 public int getCurrentSize() {
148 int currSize = initialSize;
149 for (VirtualMachine vm : VMs.values())
150 currSize += vm.getSize();
154 public int getCurrentRam() {
155 int currRAM = initialRAM;
156 for (VirtualMachine vm : VMs.values())
157 currRAM += vm.getRAM();
161 public int getCurrentCPU() {
162 int currCPU = initialCPU;
163 for (VirtualMachine vm : VMs.values())
164 currCPU += vm.getCPU();
168 public double getSizeUtilization() {
169 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
172 public double getRamUtilization() {
173 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
176 public double getCpuUtilization() {
177 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
180 public double getAverageUtilization() {
181 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
188 public boolean isRunning() {
192 public VirtualMachine getVirtualMachine(int id) {
196 public VirtualMachine getLatestVM() {
197 VirtualMachine vm = null;
198 for (Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext();) {
204 public Integer getLatestVMID() {
205 return getLatestVM().getId();
209 * return a list of all VMs running on this PM.
211 * @return a HashMap with all VMs running on this PM.
213 public HashMap<Integer, VirtualMachine> getVirtualMachines() {
217 public int countCurrentlyRunningVMs() {
218 return VMs.values().size();
221 public int getReservedSize() {
225 public int getReservedRAM() {
229 public int getReservedCPU() {
233 public int getTotalVMs() {
238 public int compareTo(PhysicalMachine other) {
242 if (getAverageUtilization() < other.getAverageUtilization())
244 else if (getAverageUtilization() > other.getAverageUtilization())