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