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 {
11 private static int count = 0;
13 private HashMap<Integer, VirtualMachine> VMs = new HashMap<Integer, VirtualMachine>();
17 private final int maxSize = 50000;
18 private final int maxRAM = 4700;
19 private final int maxCPU = 2400;
21 private int reservedSize;
22 private int reservedRAM;
23 private int reservedCPU;
25 private final int initialSize = 850;
26 private final int initialRAM = 300;
27 private final int initialCPU = 500;
29 private int totalVMs = 0;
31 private boolean running = false;
33 public PhysicalMachine() {
39 reservedSize = initialSize;
40 reservedRAM = initialRAM;
41 reservedCPU = initialCPU;
45 public void stop() throws VMsRunningException {
47 throw new VMsRunningException(
48 "PM cannot be stopped. Some VMs still running");
50 VMs = new HashMap<Integer, VirtualMachine>();
57 public double getConsumption() {
59 return 200.0 + 0.3 * (getCurrentCPU() - initialCPU);
64 public VirtualMachine startVirtualMachine(int sz, int ram, int cpu,
66 if (checkVM(sz, ram, cpu)) {
67 VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
68 VMs.put(vm.getId(), vm);
69 reservedSize = reservedSize + vm.getReservedSize();
70 reservedRAM = reservedRAM + vm.getReservedRAM();
71 reservedCPU = reservedCPU + vm.getReservedCPU();
78 public void stopVirtualMachine(VirtualMachine vm)
79 throws ActiveApplicationsException {
80 if (VMs.containsKey(vm.getId())) {
81 if (vm.getApplications().size() != 0) {
82 throw new ActiveApplicationsException(
83 "Applications must be migrated before stopping a VM, VM id "
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()) && ((VirtualMachine.initialRAM + RAM) <= availableRAM())
96 && ((VirtualMachine.initialCPU + CPU) <= availableCPU());
99 private int availableSize() {
100 return maxSize - reservedSize;
103 private int availableRAM() {
104 return maxRAM - reservedRAM;
107 private int availableCPU() {
108 return maxCPU - reservedCPU;
111 public int getCurrentSize() {
112 int currSize = initialSize;
113 for (VirtualMachine vm : VMs.values())
114 currSize += vm.getSize();
118 public int getCurrentRam() {
119 int currRAM = initialRAM;
120 for (VirtualMachine vm : VMs.values())
121 currRAM += vm.getRAM();
125 public int getCurrentCPU() {
126 int currCPU = initialCPU;
127 for (VirtualMachine vm : VMs.values())
128 currCPU += vm.getCPU();
132 public double getSizeUtilization() {
133 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
136 public double getRamUtilization() {
137 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
140 public double getCpuUtilization() {
141 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
144 public double getAverageUtilization() {
145 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
152 public boolean isRunning() {
156 public VirtualMachine getVirtualMachine(int id) {
161 * return a list of all VMs running on this PM.
163 * @return a HashMap with all VMs running on this PM.
165 public HashMap<Integer, VirtualMachine> getVirtualMachines() {
169 public int countCurrentlyRunningVMs() {
170 return VMs.values().size();
173 public int getReservedSize() {
177 public int getReservedRAM() {
181 public int getReservedCPU() {
185 public int getTotalVMs() {