1 package at.ac.tuwien.lsdc.management;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
9 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
10 import at.ac.tuwien.lsdc.exception.VMsRunningException;
11 import at.ac.tuwien.lsdc.types.PhysicalMachine;
12 import at.ac.tuwien.lsdc.util.NumberUtils;
15 * This class is responsible to start and stop PMs & VMs also it will be used to
16 * put an application on a VM move an application and get utilization data
19 public class MachineManager {
21 private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
25 private int totalPMs = 0;
26 private int totalVMs = 0;
27 private int totalResizeCalls = 0;
29 public MachineManager(int numPMs) {
34 * Start a physical machine
36 * @return the PM that was started, null if all machines already running
38 public PhysicalMachine startPhysicalMachine() {
39 if (PMs.size() >= numPMs)
40 throw new OutOfPMsException("We cannot start another PM. All out PMs are already running!: " + numPMs);
42 PhysicalMachine pm = new PhysicalMachine();
43 PMs.put(pm.getId(), pm);
50 * Stops a physical machine with the given id
53 * the id of the PM to stop
54 * @throws VMsRunningException
55 * is thrown when there are still VMs running on this PM
57 public void stopPhysicalMachine(int id) throws VMsRunningException {
58 if (PMs.containsKey(id)) {
59 PhysicalMachine pm = PMs.get(id);
60 totalVMs += pm.getTotalVMs();
62 totalResizeCalls += pm.getTotalResizeCalls();
68 * Returns all running physical machines
70 * @return the currently active PMs
72 public Collection<PhysicalMachine> getPMs() {
77 * Returns the maximum number of available physical machines
79 * @return the maximum number of PMs
81 public int getMaxPMs() {
86 * Get the total size on all PMs
88 * @return the total size on all PMs
90 public int getTotalSize() {
92 for (PhysicalMachine pm : PMs.values()) {
94 totalSize += pm.getCurrentSize();
100 * Get the total Ram usage of all PMs
102 * @return the total usage of Ram
104 public int getTotalRam() {
106 for (PhysicalMachine pm : PMs.values()) {
108 totalRam += pm.getCurrentRam();
114 * Get the total Cpu usage of all PMs
116 * @return the total usage of Cpu power
118 public int getTotalCpu() {
120 for (PhysicalMachine pm : PMs.values()) {
122 totalCpu += pm.getCurrentCPU();
128 * Gets the current power consumption summed up from each PM
130 * @return the current power consumption
132 public double getCurrentConsumption() {
133 double consumption = 0;
134 for (PhysicalMachine pm : PMs.values()) {
136 consumption += pm.getConsumption();
138 consumption = NumberUtils.roundDouble(consumption);
143 * Gets the number of currently running PMs
145 * @return the number of currently running PMs
147 public int countCurrentlyRunningPMs() {
149 for (PhysicalMachine pm : PMs.values()) {
157 * Gets the number of currently running VMs
159 * @return the number of currently running VMs
161 public int countCurrentlyRunningVMs() {
163 for (PhysicalMachine pm : PMs.values()) {
164 if (pm.isRunning()) {
165 running += pm.countCurrentlyRunningVMs();
172 * sorting physical machines by resource utilization
174 * @return List of PMs sorted by resource utilization
176 public List<PhysicalMachine> getSortedPMs() {
177 List<PhysicalMachine> sortedPMs = new ArrayList<PhysicalMachine>(PMs.values());
178 Collections.sort(sortedPMs);
182 public int getTotalPMs() {
186 public int getTotalVMs() {
190 public int getTotalResizeCalls() {
191 return totalResizeCalls;