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;
28 public MachineManager(int numPMs) {
33 * Start a physical machine
35 * @return the PM that was started, null if all machines already running
37 public PhysicalMachine startPhysicalMachine() {
38 if (PMs.size() >= numPMs)
39 throw new OutOfPMsException("We cannot start another PM. All out PMs are already running!: " + numPMs);
41 PhysicalMachine pm = new PhysicalMachine();
42 PMs.put(pm.getId(), pm);
49 * Stops a physical machine with the given id
52 * the id of the PM to stop
53 * @throws VMsRunningException
54 * is thrown when there are still VMs running on this PM
56 public void stopPhysicalMachine(int id) throws VMsRunningException {
57 if (PMs.containsKey(id)) {
58 PhysicalMachine pm = PMs.get(id);
59 totalVMs += pm.getTotalVMs();
66 * Returns all running physical machines
68 * @return the currently active PMs
70 public Collection<PhysicalMachine> getPMs() {
75 * Returns the maximum number of available physical machines
77 * @return the maximum number of PMs
79 public int getMaxPMs() {
84 * Get the total size on all PMs
86 * @return the total size on all PMs
88 public int getTotalSize() {
90 for (PhysicalMachine pm : PMs.values()) {
92 totalSize += pm.getCurrentSize();
98 * Get the total Ram usage of all PMs
100 * @return the total usage of Ram
102 public int getTotalRam() {
104 for (PhysicalMachine pm : PMs.values()) {
106 totalRam += pm.getCurrentRam();
112 * Get the total Cpu usage of all PMs
114 * @return the total usage of Cpu power
116 public int getTotalCpu() {
118 for (PhysicalMachine pm : PMs.values()) {
120 totalCpu += pm.getCurrentCPU();
126 * Gets the current power consumption summed up from each PM
128 * @return the current power consumption
130 public double getCurrentConsumption() {
131 double consumption = 0;
132 for (PhysicalMachine pm : PMs.values()) {
134 consumption += pm.getConsumption();
136 consumption = NumberUtils.roundDouble(consumption);
141 * Gets the number of currently running PMs
143 * @return the number of currently running PMs
145 public int countCurrentlyRunningPMs() {
147 for (PhysicalMachine pm : PMs.values()) {
155 * Gets the number of currently running VMs
157 * @return the number of currently running VMs
159 public int countCurrentlyRunningVMs() {
161 for (PhysicalMachine pm : PMs.values()) {
162 if (pm.isRunning()) {
163 running += pm.countCurrentlyRunningVMs();
170 * sorting physical machines by resource utilization
172 * @return List of PMs sorted by resource utilization
174 public List<PhysicalMachine> getSortedPMs() {
175 List<PhysicalMachine> sortedPMs = new ArrayList<PhysicalMachine>(PMs.values());
176 Collections.sort(sortedPMs);
180 public int getTotalPMs() {
184 public int getTotalVMs() {