]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/management/MachineManager.java
log resize events
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / management / MachineManager.java
1 package at.ac.tuwien.lsdc.management;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.List;
8
9 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
10 import at.ac.tuwien.lsdc.exception.VMsRunningException;
11 import at.ac.tuwien.lsdc.types.PhysicalMachine;
12 import at.ac.tuwien.lsdc.util.NumberUtils;
13
14 /**
15  * This class is responsible to start and stop PMs & VMs also it will be used to
16  * put an application on a VM move an application and get utilization data
17  *
18  */
19 public class MachineManager {
20
21         private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
22
23         private int numPMs;
24
25         private int totalPMs = 0;
26         private int totalVMs = 0;
27         private int totalResizeCalls = 0;
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                         throw new OutOfPMsException("We cannot start another PM. All out PMs are already running!: " + numPMs);
41
42                 PhysicalMachine pm = new PhysicalMachine();
43                 PMs.put(pm.getId(), pm);
44                 pm.start();
45                 totalPMs++;
46                 return pm;
47         }
48
49         /**
50          * Stops a physical machine with the given id
51          *
52          * @param id
53          *          the id of the PM to stop
54          * @throws VMsRunningException
55          *           is thrown when there are still VMs running on this PM
56          */
57         public void stopPhysicalMachine(int id) throws VMsRunningException {
58                 if (PMs.containsKey(id)) {
59                         PhysicalMachine pm = PMs.get(id);
60                         totalVMs += pm.getTotalVMs();
61                         pm.stop();
62                         totalResizeCalls += pm.getTotalResizeCalls();
63                         PMs.remove(id);
64                 }
65         }
66
67         /**
68          * Returns all running physical machines
69          *
70          * @return the currently active PMs
71          */
72         public Collection<PhysicalMachine> getPMs() {
73                 return PMs.values();
74         }
75
76         /**
77          * Returns the maximum number of available physical machines
78          *
79          * @return the maximum number of PMs
80          */
81         public int getMaxPMs() {
82                 return numPMs;
83         }
84
85         /**
86          * Get the total size on all PMs
87          *
88          * @return the total size on all PMs
89          */
90         public int getTotalSize() {
91                 int totalSize = 0;
92                 for (PhysicalMachine pm : PMs.values()) {
93                         if (pm.isRunning())
94                                 totalSize += pm.getCurrentSize();
95                 }
96                 return totalSize;
97         }
98
99         /**
100          * Get the total Ram usage of all PMs
101          *
102          * @return the total usage of Ram
103          */
104         public int getTotalRam() {
105                 int totalRam = 0;
106                 for (PhysicalMachine pm : PMs.values()) {
107                         if (pm.isRunning())
108                                 totalRam += pm.getCurrentRam();
109                 }
110                 return totalRam;
111         }
112
113         /**
114          * Get the total Cpu usage of all PMs
115          *
116          * @return the total usage of Cpu power
117          */
118         public int getTotalCpu() {
119                 int totalCpu = 0;
120                 for (PhysicalMachine pm : PMs.values()) {
121                         if (pm.isRunning())
122                                 totalCpu += pm.getCurrentCPU();
123                 }
124                 return totalCpu;
125         }
126
127         /**
128          * Gets the current power consumption summed up from each PM
129          *
130          * @return the current power consumption
131          */
132         public double getCurrentConsumption() {
133                 double consumption = 0;
134                 for (PhysicalMachine pm : PMs.values()) {
135                         if (pm.isRunning())
136                                 consumption += pm.getConsumption();
137                 }
138                 consumption = NumberUtils.roundDouble(consumption);
139                 return consumption;
140         }
141
142         /**
143          * Gets the number of currently running PMs
144          *
145          * @return the number of currently running PMs
146          */
147         public int countCurrentlyRunningPMs() {
148                 int running = 0;
149                 for (PhysicalMachine pm : PMs.values()) {
150                         if (pm.isRunning())
151                                 running++;
152                 }
153                 return running;
154         }
155
156         /**
157          * Gets the number of currently running VMs
158          *
159          * @return the number of currently running VMs
160          */
161         public int countCurrentlyRunningVMs() {
162                 int running = 0;
163                 for (PhysicalMachine pm : PMs.values()) {
164                         if (pm.isRunning()) {
165                                 running += pm.countCurrentlyRunningVMs();
166                         }
167                 }
168                 return running;
169         }
170
171         /**
172          * sorting physical machines by resource utilization
173          *
174          * @return List of PMs sorted by resource utilization
175          */
176         public List<PhysicalMachine> getSortedPMs() {
177                 List<PhysicalMachine> sortedPMs = new ArrayList<PhysicalMachine>(PMs.values());
178                 Collections.sort(sortedPMs);
179                 return sortedPMs;
180         }
181
182         public int getTotalPMs() {
183                 return totalPMs;
184         }
185
186         public int getTotalVMs() {
187                 return totalVMs;
188         }
189
190         public int getTotalResizeCalls() {
191                 return totalResizeCalls;
192         }
193
194 }