1 package at.ac.tuwien.lsdc.types;
3 import java.util.HashMap;
5 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
6 import at.ac.tuwien.lsdc.exception.VMsRunningException;
7 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
9 public class PhysicalMachine implements Comparable<PhysicalMachine> {
11 final int BEFORE = -1;
15 private static int count = 0;
17 private HashMap<Integer, VirtualMachine> VMs = new HashMap<Integer, VirtualMachine>();
21 private final int maxSize = 50000;
22 private final int maxRAM = 4700;
23 private final int maxCPU = 2400;
25 private int reservedSize;
26 private int reservedRAM;
27 private int reservedCPU;
29 private final int initialSize = 850;
30 private final int initialRAM = 300;
31 private final int initialCPU = 500;
33 private int totalVMs = 0;
35 private boolean running = false;
37 public PhysicalMachine() {
43 reservedSize = initialSize;
44 reservedRAM = initialRAM;
45 reservedCPU = initialCPU;
49 public void stop() throws VMsRunningException {
51 throw new VMsRunningException("PM cannot be stopped. Some VMs still running");
53 VMs = new HashMap<Integer, VirtualMachine>();
60 public double getConsumption() {
62 return 200.0 + 0.3 * (getCurrentCPU() - initialCPU);
67 public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
68 if (checkVM(sz, ram, cpu)) {
69 VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
70 VMs.put(vm.getId(), vm);
71 reservedSize = reservedSize + vm.getReservedSize();
72 reservedRAM = reservedRAM + vm.getReservedRAM();
73 reservedCPU = reservedCPU + vm.getReservedCPU();
80 public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
81 if (VMs.containsKey(vm.getId())) {
82 if (vm.getApplications().size() != 0) {
83 throw new ActiveApplicationsException(
84 "Applications must be migrated before stopping a VM, VM id " + vm.getId());
86 VMs.remove(vm.getId());
87 reservedSize = reservedSize - vm.getReservedSize();
88 reservedRAM = reservedRAM - vm.getReservedRAM();
89 reservedCPU = reservedCPU - vm.getReservedCPU();
94 public boolean checkVM(int size, int RAM, int CPU) {
95 return ((VirtualMachine.initialSize + size) <= availableSize())
96 && ((VirtualMachine.initialRAM + RAM) <= availableRAM())
97 && ((VirtualMachine.initialCPU + CPU) <= availableCPU());
100 private int availableSize() {
101 return maxSize - reservedSize;
104 private int availableRAM() {
105 return maxRAM - reservedRAM;
108 private int availableCPU() {
109 return maxCPU - reservedCPU;
112 public int getCurrentSize() {
113 int currSize = initialSize;
114 for (VirtualMachine vm : VMs.values())
115 currSize += vm.getSize();
119 public int getCurrentRam() {
120 int currRAM = initialRAM;
121 for (VirtualMachine vm : VMs.values())
122 currRAM += vm.getRAM();
126 public int getCurrentCPU() {
127 int currCPU = initialCPU;
128 for (VirtualMachine vm : VMs.values())
129 currCPU += vm.getCPU();
133 public double getSizeUtilization() {
134 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
137 public double getRamUtilization() {
138 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
141 public double getCpuUtilization() {
142 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
145 public double getAverageUtilization() {
146 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
153 public boolean isRunning() {
157 public VirtualMachine getVirtualMachine(int id) {
162 * return a list of all VMs running on this PM.
164 * @return a HashMap with all VMs running on this PM.
166 public HashMap<Integer, VirtualMachine> getVirtualMachines() {
170 public int countCurrentlyRunningVMs() {
171 return VMs.values().size();
174 public int getReservedSize() {
178 public int getReservedRAM() {
182 public int getReservedCPU() {
186 public int getTotalVMs() {
191 public int compareTo(PhysicalMachine other) {
195 if (getAverageUtilization() < other.getAverageUtilization())
197 else if (getAverageUtilization() > other.getAverageUtilization())