]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/management/MachineManager.java
Fixed: Output File OK
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / management / MachineManager.java
1 package at.ac.tuwien.lsdc.management;
2
3 import java.math.RoundingMode;
4 import java.text.DecimalFormat;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.List;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import at.ac.tuwien.lsdc.exception.VMsRunningException;
13 import at.ac.tuwien.lsdc.types.PhysicalMachine;
14 import at.ac.tuwien.lsdc.util.NumberUtils;
15
16 /**
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
19  * 
20  */
21 public class MachineManager {
22
23         private static final Logger log = LoggerFactory.getLogger(MachineManager.class);
24         
25         private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
26
27         private int numPMs;
28
29         public MachineManager(int numPMs) {
30                 this.numPMs = numPMs;
31         }
32
33         /**
34          * Start a physical machine
35          * 
36          * @return the PM that was started, null if all machines already running
37          */
38         public PhysicalMachine startPhysicalMachine() {
39                 if (PMs.size() < numPMs) {
40                         PhysicalMachine pm = new PhysicalMachine();
41                         PMs.put(pm.getId(), pm);
42                         pm.start();
43                         return pm;
44                 }
45                 return null;
46         }
47
48         /**
49          * Stops a physical machine with the given id
50          * 
51          * @param id
52          *            the id of the PM to stop
53          * @throws VMsRunningException
54          *             is thrown when there are still VMs running on this PM
55          */
56         public void stopPhysicalMachine(int id) throws VMsRunningException {
57                 if (PMs.containsKey(id)) {
58                         PMs.get(id).stop();
59                         PMs.remove(id);
60                 }
61         }
62
63         /**
64          * Returns all running physical machines
65          * 
66          * @return the currently active PMs
67          */
68         public Collection<PhysicalMachine> getPMs() {
69                 return PMs.values();
70         }
71
72         /**
73          * Returns the maximum number of available physical machines
74          * 
75          * @return the maximum number of PMs
76          */
77         public int getMaxPMs() {
78                 return numPMs;
79         }
80
81         /**
82          * Get the total size on all PMs
83          * 
84          * @return the total size on all PMs
85          */
86         public int getTotalSize() {
87                 int totalSize = 0;
88                 for (PhysicalMachine pm : PMs.values()) {
89                         if (pm.isRunning())
90                                 totalSize += pm.getCurrentSize();
91                 }
92                 return totalSize;
93         }
94
95         /**
96          * Get the total Ram usage of all PMs
97          * 
98          * @return the total usage of Ram
99          */
100         public int getTotalRam() {
101                 int totalRam = 0;
102                 for (PhysicalMachine pm : PMs.values()) {
103                         if (pm.isRunning())
104                                 totalRam += pm.getCurrentRam();
105                 }
106                 return totalRam;
107         }
108
109         /**
110          * Get the total Cpu usage of all PMs
111          * 
112          * @return the total usage of Cpu power
113          */
114         public int getTotalCpu() {
115                 int totalCpu = 0;
116                 for (PhysicalMachine pm : PMs.values()) {
117                         if (pm.isRunning())
118                                 totalCpu += pm.getCurrentCpu();
119                 }
120                 return totalCpu;
121         }
122
123         /**
124          * Gets the total power consumption summed up from each PM
125          * 
126          * @return the total power consumption
127          */
128         public double getCurrentConsumption() {
129                 double consumption = 0;
130                 for (PhysicalMachine pm : PMs.values()) {
131                         if (pm.isRunning()) {
132                                 consumption += pm.getConsumption();
133                                 
134                         }
135                 }
136                 
137                 consumption = NumberUtils.roundDouble(consumption);
138                 
139                 if(consumption < 0 || consumption > 9999)
140                         log.info("consumptino is BIG " +consumption);
141                 else
142                         log.info("consumption normal "+consumption);
143                 
144                 
145                 return consumption;
146         }
147
148         /**
149          * Gets the number of currently running PMs
150          * 
151          * @return the number of currently running PMs
152          */
153         public int countCurrentlyRunningPMs() {
154                 int running = 0;
155                 for (PhysicalMachine pm : PMs.values()) {
156                         if (pm.isRunning())
157                                 running++;
158                 }
159                 return running;
160         }
161
162         /**
163          * Gets the number of currently running VMs
164          * 
165          * @return the number of currently running VMs
166          */
167         public int countCurrentlyRunningVMs() {
168                 int running = 0;
169                 for (PhysicalMachine pm : PMs.values()) {
170                         if (pm.isRunning()) {
171                                 running += pm.countCurrentlyRunningVMs();
172                         }
173                 }
174                 return running;
175         }
176 }