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