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