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.VMsRunningException;
8 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
10 public class PhysicalMachine implements Comparable<PhysicalMachine> {
12 final int BEFORE = -1;
16 private static int count = 0;
18 private HashMap<Integer, VirtualMachine> VMs = new HashMap<Integer, VirtualMachine>();
22 private final int maxSize = 50000;
23 private final int maxRAM = 4700;
24 private final int maxCPU = 2400;
26 private int reservedSize;
27 private int reservedRAM;
28 private int reservedCPU;
30 private final int initialSize = 850;
31 private final int initialRAM = 300;
32 private final int initialCPU = 500;
34 private int totalVMs = 0;
36 private boolean running = false;
38 public PhysicalMachine() {
44 reservedSize = initialSize;
45 reservedRAM = initialRAM;
46 reservedCPU = initialCPU;
50 public void stop() throws VMsRunningException {
52 throw new VMsRunningException("PM cannot be stopped. Some VMs still running");
54 VMs = new HashMap<Integer, VirtualMachine>();
61 public double getConsumption() {
63 return 200.0 + 0.3 * (getCurrentCPU() - initialCPU);
68 // Creates a VirtualMachine of maximum possible Size
69 public VirtualMachine startVirtualMachine(VMType type) {
70 VirtualMachine vm = new VirtualMachine(maxSize - initialSize - VirtualMachine.initialSize,
71 maxRAM - initialRAM - VirtualMachine.initialRAM, maxCPU - initialCPU
72 - VirtualMachine.initialCPU, this, type);
76 public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
77 if (checkVM(sz, ram, cpu)) {
78 VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
79 VMs.put(vm.getId(), vm);
80 reservedSize = reservedSize + vm.getReservedSize();
81 reservedRAM = reservedRAM + vm.getReservedRAM();
82 reservedCPU = reservedCPU + vm.getReservedCPU();
89 public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
90 if (VMs.containsKey(vm.getId())) {
91 if (vm.getApplications().size() != 0) {
92 throw new ActiveApplicationsException(
93 "Applications must be migrated before stopping a VM, VM id " + vm.getId());
95 VMs.remove(vm.getId());
96 reservedSize = reservedSize - vm.getReservedSize();
97 reservedRAM = reservedRAM - vm.getReservedRAM();
98 reservedCPU = reservedCPU - vm.getReservedCPU();
103 public boolean checkVM(int size, int RAM, int CPU) {
104 return ((VirtualMachine.initialSize + size) <= availableSize())
105 && ((VirtualMachine.initialRAM + RAM) <= availableRAM())
106 && ((VirtualMachine.initialCPU + CPU) <= availableCPU());
109 private int availableSize() {
110 return maxSize - reservedSize;
113 private int availableRAM() {
114 return maxRAM - reservedRAM;
117 private int availableCPU() {
118 return maxCPU - reservedCPU;
121 public int getCurrentSize() {
122 int currSize = initialSize;
123 for (VirtualMachine vm : VMs.values())
124 currSize += vm.getSize();
128 public int getCurrentRam() {
129 int currRAM = initialRAM;
130 for (VirtualMachine vm : VMs.values())
131 currRAM += vm.getRAM();
135 public int getCurrentCPU() {
136 int currCPU = initialCPU;
137 for (VirtualMachine vm : VMs.values())
138 currCPU += vm.getCPU();
142 public double getSizeUtilization() {
143 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
146 public double getRamUtilization() {
147 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
150 public double getCpuUtilization() {
151 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
154 public double getAverageUtilization() {
155 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
162 public boolean isRunning() {
166 public VirtualMachine getVirtualMachine(int id) {
170 public VirtualMachine getLatestVM() {
171 VirtualMachine vm = null;
172 for(Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext(); ) {
178 public Integer getLatestVMID() {
179 return getLatestVM().getId();
183 * return a list of all VMs running on this PM.
185 * @return a HashMap with all VMs running on this PM.
187 public HashMap<Integer, VirtualMachine> getVirtualMachines() {
191 public int countCurrentlyRunningVMs() {
192 return VMs.values().size();
195 public int getReservedSize() {
199 public int getReservedRAM() {
203 public int getReservedCPU() {
207 public int getTotalVMs() {
212 public int compareTo(PhysicalMachine other) {
216 if (getAverageUtilization() < other.getAverageUtilization())
218 else if (getAverageUtilization() > other.getAverageUtilization())