]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/management/MachineManager.java
final presentation. NEED BETTER INPUT FILE!
[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 static 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         /**
183          * sorting physical machines by resource utilization
184          *
185          * @return List of PMs sorted by resource utilization
186          */
187         public List<PhysicalMachine> getRevSortedPMs() {
188                 List<PhysicalMachine> revSortedPMs = getSortedPMs();
189                 Collections.reverse(revSortedPMs);
190                 return revSortedPMs;
191         }
192
193         public int getTotalPMs() {
194                 return totalPMs;
195         }
196
197         public int getTotalVMs() {
198                 return totalVMs;
199         }
200
201         public static int getTotalResizeCalls() {
202                 return totalResizeCalls;
203         }
204         
205         public static void addResizeCall() {
206                 totalResizeCalls ++;
207         }
208
209 }