1 package at.ac.tuwien.lsdc.management;
3 import java.math.RoundingMode;
4 import java.text.DecimalFormat;
5 import java.util.Collection;
6 import java.util.HashMap;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import at.ac.tuwien.lsdc.exception.VMsRunningException;
13 import at.ac.tuwien.lsdc.types.PhysicalMachine;
14 import at.ac.tuwien.lsdc.util.NumberUtils;
17 * This class is responsible to start and stop PMs & VMs also it will be used to
18 * put an application on a VM move an application and get utilization data
21 public class MachineManager {
23 private static final Logger log = LoggerFactory.getLogger(MachineManager.class);
25 private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
29 public MachineManager(int numPMs) {
34 * Start a physical machine
36 * @return the PM that was started, null if all machines already running
38 public PhysicalMachine startPhysicalMachine() {
39 if (PMs.size() < numPMs) {
40 PhysicalMachine pm = new PhysicalMachine();
41 PMs.put(pm.getId(), pm);
49 * Stops a physical machine with the given id
52 * the id of the PM to stop
53 * @throws VMsRunningException
54 * is thrown when there are still VMs running on this PM
56 public void stopPhysicalMachine(int id) throws VMsRunningException {
57 if (PMs.containsKey(id)) {
64 * Returns all running physical machines
66 * @return the currently active PMs
68 public Collection<PhysicalMachine> getPMs() {
73 * Returns the maximum number of available physical machines
75 * @return the maximum number of PMs
77 public int getMaxPMs() {
82 * Get the total size on all PMs
84 * @return the total size on all PMs
86 public int getTotalSize() {
88 for (PhysicalMachine pm : PMs.values()) {
90 totalSize += pm.getCurrentSize();
96 * Get the total Ram usage of all PMs
98 * @return the total usage of Ram
100 public int getTotalRam() {
102 for (PhysicalMachine pm : PMs.values()) {
104 totalRam += pm.getCurrentRam();
110 * Get the total Cpu usage of all PMs
112 * @return the total usage of Cpu power
114 public int getTotalCpu() {
116 for (PhysicalMachine pm : PMs.values()) {
118 totalCpu += pm.getCurrentCpu();
124 * Gets the total power consumption summed up from each PM
126 * @return the total power consumption
128 public double getCurrentConsumption() {
129 double consumption = 0;
130 for (PhysicalMachine pm : PMs.values()) {
131 if (pm.isRunning()) {
132 consumption += pm.getConsumption();
137 consumption = NumberUtils.roundDouble(consumption);
139 if(consumption < 0 || consumption > 9999)
140 log.info("consumptino is BIG " +consumption);
142 log.info("consumption normal "+consumption);
149 * Gets the number of currently running PMs
151 * @return the number of currently running PMs
153 public int countCurrentlyRunningPMs() {
155 for (PhysicalMachine pm : PMs.values()) {
163 * Gets the number of currently running VMs
165 * @return the number of currently running VMs
167 public int countCurrentlyRunningVMs() {
169 for (PhysicalMachine pm : PMs.values()) {
170 if (pm.isRunning()) {
171 running += pm.countCurrentlyRunningVMs();