]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/management/MachineManager.java
moved sorting of PMs into MachineManager
[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
28         public MachineManager(int numPMs) {
29                 this.numPMs = numPMs;
30         }
31
32         /**
33          * Start a physical machine
34          *
35          * @return the PM that was started, null if all machines already running
36          */
37         public PhysicalMachine startPhysicalMachine() {
38                 if (PMs.size() >= numPMs)
39                         throw new OutOfPMsException("We cannot start another PM. All out PMs are already running!: " + numPMs);
40
41                 PhysicalMachine pm = new PhysicalMachine();
42                 PMs.put(pm.getId(), pm);
43                 pm.start();
44                 totalPMs++;
45                 return pm;
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                         PhysicalMachine pm = PMs.get(id);
59                         totalVMs += pm.getTotalVMs();
60                         pm.stop();
61                         PMs.remove(id);
62                 }
63         }
64
65         /**
66          * Returns all running physical machines
67          *
68          * @return the currently active PMs
69          */
70         public Collection<PhysicalMachine> getPMs() {
71                 return PMs.values();
72         }
73
74         /**
75          * Returns the maximum number of available physical machines
76          *
77          * @return the maximum number of PMs
78          */
79         public int getMaxPMs() {
80                 return numPMs;
81         }
82
83         /**
84          * Get the total size on all PMs
85          *
86          * @return the total size on all PMs
87          */
88         public int getTotalSize() {
89                 int totalSize = 0;
90                 for (PhysicalMachine pm : PMs.values()) {
91                         if (pm.isRunning())
92                                 totalSize += pm.getCurrentSize();
93                 }
94                 return totalSize;
95         }
96
97         /**
98          * Get the total Ram usage of all PMs
99          *
100          * @return the total usage of Ram
101          */
102         public int getTotalRam() {
103                 int totalRam = 0;
104                 for (PhysicalMachine pm : PMs.values()) {
105                         if (pm.isRunning())
106                                 totalRam += pm.getCurrentRam();
107                 }
108                 return totalRam;
109         }
110
111         /**
112          * Get the total Cpu usage of all PMs
113          *
114          * @return the total usage of Cpu power
115          */
116         public int getTotalCpu() {
117                 int totalCpu = 0;
118                 for (PhysicalMachine pm : PMs.values()) {
119                         if (pm.isRunning())
120                                 totalCpu += pm.getCurrentCPU();
121                 }
122                 return totalCpu;
123         }
124
125         /**
126          * Gets the total power consumption summed up from each PM
127          *
128          * @return the total power consumption
129          */
130         public double getCurrentConsumption() {
131                 double consumption = 0;
132                 for (PhysicalMachine pm : PMs.values()) {
133                         if (pm.isRunning())
134                                 consumption += pm.getConsumption();
135                 }
136                 consumption = NumberUtils.roundDouble(consumption);
137                 return consumption;
138         }
139
140         /**
141          * Gets the number of currently running PMs
142          *
143          * @return the number of currently running PMs
144          */
145         public int countCurrentlyRunningPMs() {
146                 int running = 0;
147                 for (PhysicalMachine pm : PMs.values()) {
148                         if (pm.isRunning())
149                                 running++;
150                 }
151                 return running;
152         }
153
154         /**
155          * Gets the number of currently running VMs
156          *
157          * @return the number of currently running VMs
158          */
159         public int countCurrentlyRunningVMs() {
160                 int running = 0;
161                 for (PhysicalMachine pm : PMs.values()) {
162                         if (pm.isRunning()) {
163                                 running += pm.countCurrentlyRunningVMs();
164                         }
165                 }
166                 return running;
167         }
168
169         /**
170          * sorting physical machines by resource utilization
171          *
172          * @return List of PMs sorted by resource utilization
173          */
174         public List<PhysicalMachine> getSortedPMs() {
175                 List<PhysicalMachine> sortedPMs = new ArrayList<PhysicalMachine>(PMs.values());
176                 Collections.sort(sortedPMs);
177                 return sortedPMs;
178         }
179
180         public int getTotalPMs() {
181                 return totalPMs;
182         }
183
184         public int getTotalVMs() {
185                 return totalVMs;
186         }
187 }