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