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