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 private int totalPMs = 0;
22 private int totalVMs = 0;
24 public MachineManager(int numPMs) {
29 * Start a physical machine
31 * @return the PM that was started, null if all machines already running
33 public PhysicalMachine startPhysicalMachine() {
34 if (PMs.size() < numPMs) {
35 PhysicalMachine pm = new PhysicalMachine();
36 PMs.put(pm.getId(), pm);
45 * Stops a physical machine with the given id
48 * the id of the PM to stop
49 * @throws VMsRunningException
50 * is thrown when there are still VMs running on this PM
52 public void stopPhysicalMachine(int id) throws VMsRunningException {
53 if (PMs.containsKey(id)) {
54 PhysicalMachine pm = PMs.get(id);
55 totalVMs += pm.getTotalVMs();
62 * Returns all running physical machines
64 * @return the currently active PMs
66 public Collection<PhysicalMachine> getPMs() {
71 * Returns the maximum number of available physical machines
73 * @return the maximum number of PMs
75 public int getMaxPMs() {
80 * Get the total size on all PMs
82 * @return the total size on all PMs
84 public int getTotalSize() {
86 for (PhysicalMachine pm : PMs.values()) {
88 totalSize += pm.getCurrentSize();
94 * Get the total Ram usage of all PMs
96 * @return the total usage of Ram
98 public int getTotalRam() {
100 for (PhysicalMachine pm : PMs.values()) {
102 totalRam += pm.getCurrentRam();
108 * Get the total Cpu usage of all PMs
110 * @return the total usage of Cpu power
112 public int getTotalCpu() {
114 for (PhysicalMachine pm : PMs.values()) {
116 totalCpu += pm.getCurrentCPU();
122 * Gets the total power consumption summed up from each PM
124 * @return the total power consumption
126 public double getCurrentConsumption() {
127 double consumption = 0;
128 for (PhysicalMachine pm : PMs.values()) {
130 consumption += pm.getConsumption();
132 consumption = NumberUtils.roundDouble(consumption);
137 * Gets the number of currently running PMs
139 * @return the number of currently running PMs
141 public int countCurrentlyRunningPMs() {
143 for (PhysicalMachine pm : PMs.values()) {
151 * Gets the number of currently running VMs
153 * @return the number of currently running VMs
155 public int countCurrentlyRunningVMs() {
157 for (PhysicalMachine pm : PMs.values()) {
158 if (pm.isRunning()) {
159 running += pm.countCurrentlyRunningVMs();
165 public int getTotalPMs() {
169 public int getTotalVMs() {