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) {
27 private void init(int numPMs) {
28 for (int i = 0; i < numPMs; i++) {
29 PhysicalMachine pm = new PhysicalMachine();
30 PMs.put(pm.getId(), pm);
36 * Start a physical machine
37 * @return the PM that was started, null if all machines already running
39 public PhysicalMachine startPhysicalMachine() {
40 if(PMs.size() < numPMs) {
41 PhysicalMachine pm = new PhysicalMachine();
42 PMs.put(pm.getId(), pm);
50 * Stops a physical machine with the given id
51 * @param id the id of the PM to stop
52 * @throws VMsRunningException is thrown when there are still VMs running on this PM
54 public void stopPhysicalMachine(int id) throws VMsRunningException {
55 if(PMs.containsKey(id)) {
62 * Returns all running physical machines
63 * @return the currently active PMs
65 public Collection<PhysicalMachine> getPMs () {
70 * Returns the maximum number of available physical machines
71 * @return the maximum number of PMs
73 public int getMaxPMs() {
78 * Gets the total power consumption summed up from each PM
79 * @return the total power consumption
81 public double getCurrentConsumption() {
82 double consumption = 0;
83 for (PhysicalMachine pm : PMs.values()) {
85 consumption = consumption + pm.getConsumption();
90 public int countCurrentlyRunningPMs() {
92 for (PhysicalMachine pm : PMs.values()) {
99 public int countCurrentlyRunningVMs() {
101 for (PhysicalMachine pm : PMs.values()) {
102 if (pm.isRunning()) {
103 running += pm.countCurrentlyRunningVMs();