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 public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
69 if (checkVM(sz, ram, cpu)) {
70 VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
71 VMs.put(vm.getId(), vm);
72 reservedSize = reservedSize + vm.getReservedSize();
73 reservedRAM = reservedRAM + vm.getReservedRAM();
74 reservedCPU = reservedCPU + vm.getReservedCPU();
81 public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
82 if (VMs.containsKey(vm.getId())) {
83 if (vm.getApplications().size() != 0) {
84 throw new ActiveApplicationsException(
85 "Applications must be migrated before stopping a VM, VM id " + vm.getId());
87 VMs.remove(vm.getId());
88 reservedSize = reservedSize - vm.getReservedSize();
89 reservedRAM = reservedRAM - vm.getReservedRAM();
90 reservedCPU = reservedCPU - vm.getReservedCPU();
95 public boolean checkVM(int size, int RAM, int CPU) {
96 return ((VirtualMachine.initialSize + size) <= availableSize())
97 && ((VirtualMachine.initialRAM + RAM) <= availableRAM())
98 && ((VirtualMachine.initialCPU + CPU) <= availableCPU());
101 private int availableSize() {
102 return maxSize - reservedSize;
105 private int availableRAM() {
106 return maxRAM - reservedRAM;
109 private int availableCPU() {
110 return maxCPU - reservedCPU;
113 public int getCurrentSize() {
114 int currSize = initialSize;
115 for (VirtualMachine vm : VMs.values())
116 currSize += vm.getSize();
120 public int getCurrentRam() {
121 int currRAM = initialRAM;
122 for (VirtualMachine vm : VMs.values())
123 currRAM += vm.getRAM();
127 public int getCurrentCPU() {
128 int currCPU = initialCPU;
129 for (VirtualMachine vm : VMs.values())
130 currCPU += vm.getCPU();
134 public double getSizeUtilization() {
135 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
138 public double getRamUtilization() {
139 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
142 public double getCpuUtilization() {
143 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
146 public double getAverageUtilization() {
147 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
154 public boolean isRunning() {
158 public VirtualMachine getVirtualMachine(int id) {
162 public VirtualMachine getLatestVM() {
163 VirtualMachine vm = null;
164 for(Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext(); ) {
170 public Integer getLatestVMID() {
171 return getLatestVM().getId();
175 * return a list of all VMs running on this PM.
177 * @return a HashMap with all VMs running on this PM.
179 public HashMap<Integer, VirtualMachine> getVirtualMachines() {
183 public int countCurrentlyRunningVMs() {
184 return VMs.values().size();
187 public int getReservedSize() {
191 public int getReservedRAM() {
195 public int getReservedCPU() {
199 public int getTotalVMs() {
204 public int compareTo(PhysicalMachine other) {
208 if (getAverageUtilization() < other.getAverageUtilization())
210 else if (getAverageUtilization() > other.getAverageUtilization())