]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/management/MachineManager.java
Merge branch 'master' of ssh://dev.somenet.org:666/tu/lsdc
[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         private void init(int numPMs) {
28                 for (int i = 0; i < numPMs; i++) {
29                         PhysicalMachine pm = new PhysicalMachine();
30                         PMs.put(pm.getId(), pm);
31                 }
32         }
33         
34         
35         /**
36          * Start a physical machine
37          * @return the PM that was started, null if all machines already running
38          */
39         public PhysicalMachine startPhysicalMachine() {
40                 if(PMs.size() < numPMs) {
41                         PhysicalMachine pm = new PhysicalMachine();
42                         PMs.put(pm.getId(), pm);
43                         pm.start();
44                         return pm;
45                 }
46                 return null;
47         }
48         
49         /**
50          * Stops a physical machine with the given id
51          * @param id the id of the PM to stop
52          * @throws VMsRunningException is thrown when there are still VMs running on this PM
53          */
54         public void stopPhysicalMachine(int id) throws VMsRunningException {
55                 if(PMs.containsKey(id)) {
56                         PMs.get(id).stop();
57                         PMs.remove(id);
58                 }
59         }
60         
61         /**
62          * Returns all running physical machines
63          * @return the currently active PMs
64          */
65         public Collection<PhysicalMachine> getPMs () {
66                 return PMs.values();
67         }
68         
69         /**
70          * Returns the maximum number of available physical machines
71          * @return the maximum number of PMs
72          */
73         public int getMaxPMs() {
74                 return numPMs;
75         }
76
77         /**
78          * Gets the total power consumption summed up from each PM
79          * @return the total power consumption
80          */
81         public double getCurrentConsumption() {
82                 double consumption = 0;
83                 for (PhysicalMachine pm : PMs.values()) {
84                         if (pm.isRunning())
85                                 consumption = consumption + pm.getConsumption();
86                 }
87                 return consumption;
88         }
89
90         public int countCurrentlyRunningPMs() {
91                 int running = 0;
92                 for (PhysicalMachine pm : PMs.values()) {
93                         if (pm.isRunning())
94                                 running++;
95                 }
96                 return running;
97         }
98
99         public int countCurrentlyRunningVMs() {
100                 int running = 0;
101                 for (PhysicalMachine pm : PMs.values()) {
102                         if (pm.isRunning()) {
103                                 running += pm.countCurrentlyRunningVMs();
104                         }
105                 }
106                 return running;
107         }
108 }