1 package at.ac.tuwien.lsdc.management;
3 import java.util.Collection;
4 import java.util.HashMap;
6 import at.ac.tuwien.lsdc.exception.VMsRunningException;
7 import at.ac.tuwien.lsdc.types.PhysicalMachine;
8 import at.ac.tuwien.lsdc.util.NumberUtils;
11 * This class is responsible to start and stop PMs & VMs also it will be used to
12 * put an application on a VM move an application and get utilization data
15 public class MachineManager {
17 private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
21 public MachineManager(int numPMs) {
26 * Start a physical machine
28 * @return the PM that was started, null if all machines already running
30 public PhysicalMachine startPhysicalMachine() {
31 if (PMs.size() < numPMs) {
32 PhysicalMachine pm = new PhysicalMachine();
33 PMs.put(pm.getId(), pm);
41 * Stops a physical machine with the given id
44 * the id of the PM to stop
45 * @throws VMsRunningException
46 * is thrown when there are still VMs running on this PM
48 public void stopPhysicalMachine(int id) throws VMsRunningException {
49 if (PMs.containsKey(id)) {
56 * Returns all running physical machines
58 * @return the currently active PMs
60 public Collection<PhysicalMachine> getPMs() {
65 * Returns the maximum number of available physical machines
67 * @return the maximum number of PMs
69 public int getMaxPMs() {
74 * Get the total size on all PMs
76 * @return the total size on all PMs
78 public int getTotalSize() {
80 for (PhysicalMachine pm : PMs.values()) {
82 totalSize += pm.getCurrentSize();
88 * Get the total Ram usage of all PMs
90 * @return the total usage of Ram
92 public int getTotalRam() {
94 for (PhysicalMachine pm : PMs.values()) {
96 totalRam += pm.getCurrentRam();
102 * Get the total Cpu usage of all PMs
104 * @return the total usage of Cpu power
106 public int getTotalCpu() {
108 for (PhysicalMachine pm : PMs.values()) {
110 totalCpu += pm.getCurrentCpu();
116 * Gets the total power consumption summed up from each PM
118 * @return the total power consumption
120 public double getCurrentConsumption() {
121 double consumption = 0;
122 for (PhysicalMachine pm : PMs.values()) {
124 consumption += pm.getConsumption();
126 consumption = NumberUtils.roundDouble(consumption);
131 * Gets the number of currently running PMs
133 * @return the number of currently running PMs
135 public int countCurrentlyRunningPMs() {
137 for (PhysicalMachine pm : PMs.values()) {
145 * Gets the number of currently running VMs
147 * @return the number of currently running VMs
149 public int countCurrentlyRunningVMs() {
151 for (PhysicalMachine pm : PMs.values()) {
152 if (pm.isRunning()) {
153 running += pm.countCurrentlyRunningVMs();