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