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 public boolean checkExtendVM(int sizeDiff, int ramDiff, int CPUDiff) {
110 return ((sizeDiff <= availableSize()) && (ramDiff <= availableRAM()) && (CPUDiff <= availableCPU()));
113 private int availableSize() {
114 return maxSize - reservedSize;
117 private int availableRAM() {
118 return maxRAM - reservedRAM;
121 private int availableCPU() {
122 return maxCPU - reservedCPU;
125 public int getCurrentSize() {
126 int currSize = initialSize;
127 for (VirtualMachine vm : VMs.values())
128 currSize += vm.getSize();
132 public int getCurrentRam() {
133 int currRAM = initialRAM;
134 for (VirtualMachine vm : VMs.values())
135 currRAM += vm.getRAM();
139 public int getCurrentCPU() {
140 int currCPU = initialCPU;
141 for (VirtualMachine vm : VMs.values())
142 currCPU += vm.getCPU();
146 public double getSizeUtilization() {
147 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
150 public double getRamUtilization() {
151 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
154 public double getCpuUtilization() {
155 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
158 public double getAverageUtilization() {
159 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
166 public boolean isRunning() {
170 public VirtualMachine getVirtualMachine(int id) {
174 public VirtualMachine getLatestVM() {
175 VirtualMachine vm = null;
176 for (Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext();) {
182 public Integer getLatestVMID() {
183 return getLatestVM().getId();
187 * return a list of all VMs running on this PM.
189 * @return a HashMap with all VMs running on this PM.
191 public HashMap<Integer, VirtualMachine> getVirtualMachines() {
195 public int countCurrentlyRunningVMs() {
196 return VMs.values().size();
199 public int getReservedSize() {
203 public int getReservedRAM() {
207 public int getReservedCPU() {
211 public int getTotalVMs() {
216 public int compareTo(PhysicalMachine other) {
220 if (getAverageUtilization() < other.getAverageUtilization())
222 else if (getAverageUtilization() > other.getAverageUtilization())