1 package at.ac.tuwien.lsdc.management;
3 import java.util.Collection;
4 import java.util.HashMap;
7 import at.ac.tuwien.lsdc.exception.VMsRunningException;
8 import at.ac.tuwien.lsdc.types.PhysicalMachine;
11 * This class is responsible to start and stop PMs & VMs
12 * also it will be used to put an application on a VM
13 * move an application and get utilization data
16 public class MachineManager {
18 private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
22 public MachineManager(int numPMs) {
28 * Start a physical machine
29 * @return the PM that was started, null if all machines already running
31 public PhysicalMachine startPhysicalMachine() {
32 if(PMs.size() < numPMs) {
33 PhysicalMachine pm = new PhysicalMachine();
34 PMs.put(pm.getId(), pm);
42 * Stops a physical machine with the given id
43 * @param id the id of the PM to stop
44 * @throws VMsRunningException is thrown when there are still VMs running on this PM
46 public void stopPhysicalMachine(int id) throws VMsRunningException {
47 if(PMs.containsKey(id)) {
54 * Returns all running physical machines
55 * @return the currently active PMs
57 public Collection<PhysicalMachine> getPMs () {
62 * Returns the maximum number of available physical machines
63 * @return the maximum number of PMs
65 public int getMaxPMs() {
70 * Get the total size on all PMs
71 * @return the total size on all PMs
73 public int getTotalSize() {
75 for (PhysicalMachine pm : PMs.values()) {
77 totalSize += pm.getCurrentSize();
83 * Get the total Ram usage of all PMs
84 * @return the total usage of Ram
86 public int getTotalRam() {
88 for (PhysicalMachine pm : PMs.values()) {
90 totalRam += pm.getCurrentRam();
96 * Get the total Cpu usage of all PMs
97 * @return the total usage of Cpu power
99 public int getTotalCpu() {
101 for (PhysicalMachine pm : PMs.values()) {
103 totalCpu += pm.getCurrentCpu();
109 * Gets the total power consumption summed up from each PM
110 * @return the total power consumption
112 public double getCurrentConsumption() {
113 double consumption = 0;
114 for (PhysicalMachine pm : PMs.values()) {
116 consumption = consumption + pm.getConsumption();
122 * Gets the number of currently running PMs
123 * @return the number of currently running PMs
125 public int countCurrentlyRunningPMs() {
127 for (PhysicalMachine pm : PMs.values()) {
135 * Gets the number of currently running VMs
136 * @return the number of currently running VMs
138 public int countCurrentlyRunningVMs() {
140 for (PhysicalMachine pm : PMs.values()) {
141 if (pm.isRunning()) {
142 running += pm.countCurrentlyRunningVMs();