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