1 package at.ac.tuwien.lsdc.management;
3 import java.util.Collection;
4 import java.util.HashMap;
6 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
7 import at.ac.tuwien.lsdc.exception.VMsRunningException;
8 import at.ac.tuwien.lsdc.types.PhysicalMachine;
9 import at.ac.tuwien.lsdc.util.NumberUtils;
12 * This class is responsible to start and stop PMs & VMs also it will be used to
13 * put an application on a VM move an application and get utilization data
16 public class MachineManager {
18 private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
22 private int totalPMs = 0;
23 private int totalVMs = 0;
25 public MachineManager(int numPMs) {
30 * Start a physical machine
32 * @return the PM that was started, null if all machines already running
34 public PhysicalMachine startPhysicalMachine() {
35 if (PMs.size() >= numPMs)
36 throw new OutOfPMsException("We cannot start another PM. All out PMs are already running!: " + numPMs);
38 PhysicalMachine pm = new PhysicalMachine();
39 PMs.put(pm.getId(), pm);
46 * Stops a physical machine with the given id
49 * the id of the PM to stop
50 * @throws VMsRunningException
51 * is thrown when there are still VMs running on this PM
53 public void stopPhysicalMachine(int id) throws VMsRunningException {
54 if (PMs.containsKey(id)) {
55 PhysicalMachine pm = PMs.get(id);
56 totalVMs += pm.getTotalVMs();
63 * Returns all running physical machines
65 * @return the currently active PMs
67 public Collection<PhysicalMachine> getPMs() {
72 * Returns the maximum number of available physical machines
74 * @return the maximum number of PMs
76 public int getMaxPMs() {
81 * Get the total size on all PMs
83 * @return the total size on all PMs
85 public int getTotalSize() {
87 for (PhysicalMachine pm : PMs.values()) {
89 totalSize += pm.getCurrentSize();
95 * Get the total Ram usage of all PMs
97 * @return the total usage of Ram
99 public int getTotalRam() {
101 for (PhysicalMachine pm : PMs.values()) {
103 totalRam += pm.getCurrentRam();
109 * Get the total Cpu usage of all PMs
111 * @return the total usage of Cpu power
113 public int getTotalCpu() {
115 for (PhysicalMachine pm : PMs.values()) {
117 totalCpu += pm.getCurrentCPU();
123 * Gets the total power consumption summed up from each PM
125 * @return the total power consumption
127 public double getCurrentConsumption() {
128 double consumption = 0;
129 for (PhysicalMachine pm : PMs.values()) {
131 consumption += pm.getConsumption();
133 consumption = NumberUtils.roundDouble(consumption);
138 * Gets the number of currently running PMs
140 * @return the number of currently running PMs
142 public int countCurrentlyRunningPMs() {
144 for (PhysicalMachine pm : PMs.values()) {
152 * Gets the number of currently running VMs
154 * @return the number of currently running VMs
156 public int countCurrentlyRunningVMs() {
158 for (PhysicalMachine pm : PMs.values()) {
159 if (pm.isRunning()) {
160 running += pm.countCurrentlyRunningVMs();
166 public int getTotalPMs() {
170 public int getTotalVMs() {