]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/PhysicalMachine.java
Refactorings and bugfixes
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / types / PhysicalMachine.java
1 package at.ac.tuwien.lsdc.types;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5
6 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
7 import at.ac.tuwien.lsdc.exception.VMsRunningException;
8 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
9
10 public class PhysicalMachine implements Comparable<PhysicalMachine> {
11
12         final int BEFORE = -1;
13         final int EQUAL = 0;
14         final int AFTER = 1;
15
16         private static int count = 0;
17
18         private HashMap<Integer, VirtualMachine> VMs = new HashMap<Integer, VirtualMachine>();
19
20         private int id;
21
22         private final int maxSize = 50000;
23         private final int maxRAM = 4700;
24         private final int maxCPU = 2400;
25
26         private int reservedSize;
27         private int reservedRAM;
28         private int reservedCPU;
29
30         private final int initialSize = 850;
31         private final int initialRAM = 300;
32         private final int initialCPU = 500;
33
34         private int totalVMs = 0;
35
36         private boolean running = false;
37
38         public PhysicalMachine() {
39                 id = count;
40                 count++;
41         }
42
43         public void start() {
44                 reservedSize = initialSize;
45                 reservedRAM = initialRAM;
46                 reservedCPU = initialCPU;
47                 running = true;
48         }
49
50         public void stop() throws VMsRunningException {
51                 if (VMs.size() > 0)
52                         throw new VMsRunningException("PM cannot be stopped. Some VMs still running");
53
54                 VMs = new HashMap<Integer, VirtualMachine>();
55                 reservedSize = 0;
56                 reservedRAM = 0;
57                 reservedCPU = 0;
58                 running = false;
59         }
60
61         public double getConsumption() {
62                 if (running)
63                         return 200.0 + 0.3 * (getCurrentCPU() - initialCPU);
64                 else
65                         return 0.0;
66         }
67
68         public VirtualMachine startVirtualMachine(int sz, int ram, int cpu, VMType type) {
69                 if (checkVM(sz, ram, cpu)) {
70                         VirtualMachine vm = new VirtualMachine(sz, ram, cpu, this, type);
71                         VMs.put(vm.getId(), vm);
72                         reservedSize = reservedSize + vm.getReservedSize();
73                         reservedRAM = reservedRAM + vm.getReservedRAM();
74                         reservedCPU = reservedCPU + vm.getReservedCPU();
75                         totalVMs++;
76                         return vm;
77                 } else
78                         return null;
79         }
80
81         public void stopVirtualMachine(VirtualMachine vm) throws ActiveApplicationsException {
82                 if (VMs.containsKey(vm.getId())) {
83                         if (vm.getApplications().size() != 0) {
84                                 throw new ActiveApplicationsException(
85                                                 "Applications must be migrated before stopping a VM, VM id " + vm.getId());
86                         } else {
87                                 VMs.remove(vm.getId());
88                                 reservedSize = reservedSize - vm.getReservedSize();
89                                 reservedRAM = reservedRAM - vm.getReservedRAM();
90                                 reservedCPU = reservedCPU - vm.getReservedCPU();
91                         }
92                 }
93         }
94
95         public boolean checkVM(int size, int RAM, int CPU) {
96                 return ((VirtualMachine.initialSize + size) <= availableSize())
97                                 && ((VirtualMachine.initialRAM + RAM) <= availableRAM())
98                                 && ((VirtualMachine.initialCPU + CPU) <= availableCPU());
99         }
100
101         private int availableSize() {
102                 return maxSize - reservedSize;
103         }
104
105         private int availableRAM() {
106                 return maxRAM - reservedRAM;
107         }
108
109         private int availableCPU() {
110                 return maxCPU - reservedCPU;
111         }
112
113         public int getCurrentSize() {
114                 int currSize = initialSize;
115                 for (VirtualMachine vm : VMs.values())
116                         currSize += vm.getSize();
117                 return currSize;
118         }
119
120         public int getCurrentRam() {
121                 int currRAM = initialRAM;
122                 for (VirtualMachine vm : VMs.values())
123                         currRAM += vm.getRAM();
124                 return currRAM;
125         }
126
127         public int getCurrentCPU() {
128                 int currCPU = initialCPU;
129                 for (VirtualMachine vm : VMs.values())
130                         currCPU += vm.getCPU();
131                 return currCPU;
132         }
133
134         public double getSizeUtilization() {
135                 return ((double) (getCurrentSize() - initialSize) / (maxSize - initialSize)) * 100;
136         }
137
138         public double getRamUtilization() {
139                 return ((double) (getCurrentRam() - initialRAM) / (maxRAM - initialRAM)) * 100;
140         }
141
142         public double getCpuUtilization() {
143                 return ((double) (getCurrentCPU() - initialCPU) / (maxCPU - initialCPU)) * 100;
144         }
145
146         public double getAverageUtilization() {
147                 return (getSizeUtilization() + getRamUtilization() + getCpuUtilization()) / 3.0;
148         }
149
150         public int getId() {
151                 return id;
152         }
153
154         public boolean isRunning() {
155                 return running;
156         }
157
158         public VirtualMachine getVirtualMachine(int id) {
159                 return VMs.get(id);
160         }
161         
162         public VirtualMachine getLatestVM() {
163                 VirtualMachine vm = null;
164                 for(Iterator<VirtualMachine> it = VMs.values().iterator(); it.hasNext(); ) {
165                         vm = it.next();
166                 }
167                 return vm;
168         }
169         
170         public Integer getLatestVMID() {
171                 return getLatestVM().getId();
172         }
173
174         /**
175          * return a list of all VMs running on this PM.
176          *
177          * @return a HashMap with all VMs running on this PM.
178          */
179         public HashMap<Integer, VirtualMachine> getVirtualMachines() {
180                 return VMs;
181         }
182
183         public int countCurrentlyRunningVMs() {
184                 return VMs.values().size();
185         }
186
187         public int getReservedSize() {
188                 return reservedSize;
189         }
190
191         public int getReservedRAM() {
192                 return reservedRAM;
193         }
194
195         public int getReservedCPU() {
196                 return reservedCPU;
197         }
198
199         public int getTotalVMs() {
200                 return totalVMs;
201         }
202
203         @Override
204         public int compareTo(PhysicalMachine other) {
205                 if (this == other)
206                         return EQUAL;
207
208                 if (getAverageUtilization() < other.getAverageUtilization())
209                         return BEFORE;
210                 else if (getAverageUtilization() > other.getAverageUtilization())
211                         return AFTER;
212                 else
213                         return EQUAL;
214         }
215 }