]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/VirtualMachine.java
log resize events
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / types / VirtualMachine.java
1 package at.ac.tuwien.lsdc.types;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5
6 import at.ac.tuwien.lsdc.exception.VMResizeException;
7
8 public class VirtualMachine {
9
10         public enum VMType {
11                 Resizable, NonResizable
12         }
13
14         private static int count = 0;
15         private int posOnPM;
16
17         private PhysicalMachine runningOn;
18
19         private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
20
21         private int id;
22
23         public static final int initialSize = 100;
24         public static final int initialRAM = 50;
25         public static final int initialCPU = 150;
26
27         private int reservedSize;
28         private int reservedRAM;
29         private int reservedCPU;
30
31         private VMType type;
32
33         private int totalResizeCalls = 0;
34
35         public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
36                 id = count;
37                 count++;
38                 reservedSize = size + initialSize;
39                 reservedRAM = RAM + initialRAM;
40                 reservedCPU = CPU + initialCPU;
41                 runningOn = pm;
42                 posOnPM = pm.countCurrentlyRunningVMs();
43                 this.type = type;
44         }
45
46         public boolean startApplication(Application app) {
47                 if (enoughResources(app)) {
48                         applications.put(app.getID(), app);
49                         app.setRunningOn(this);
50                         return true;
51                 } else
52                         return false;
53         }
54
55         /**
56          * Set the VM size to the given new values.
57          *
58          * @param newSize
59          *          the new size of the VM, excluding the initial consumption.
60          * @param newRAM
61          *          the new RAM of the VM, excluding the initial consumption.
62          * @param newCPU
63          *          the new CPU of the VM, excluding the initial consumption.
64          * @return
65          */
66 //      public boolean resizeVM(int newSize, int newRAM, int newCPU) {
67 //              if (type == VMType.Resizable
68 //                              && runningOn.checkExtendVM(newSize - reservedSize, newRAM - reservedRAM, newCPU
69 //                                              - reservedCPU)) {
70 //                      // Resize VM
71 //                      reservedSize = initialSize + newSize;
72 //                      reservedRAM = initialRAM + newRAM;
73 //                      reservedCPU = initialCPU + newCPU;
74 //                      return true;
75 //              } else
76 //                      return false;
77 ////                    throw new VMResizeException("Could not resize VM!", this);
78 //      }
79
80         public boolean resizeUp(Application app) throws VMResizeException {
81
82                 int diffSize = (app.getSize() - availableSize());
83                 int diffRAM = (app.getRam() - availableRAM());
84                 int diffCPU = (app.getCpu() - availableCPU());
85
86                 if(!(type == VMType.Resizable) || !runningOn.checkExtendVM(diffSize, diffRAM, diffCPU)) {
87                         throw new VMResizeException("Could not resize VM!", this);
88                 }
89
90                 runningOn.resizeUp(diffSize, diffRAM, diffCPU);
91
92                 reservedSize = reservedSize + diffSize;
93                 reservedRAM = reservedRAM + diffRAM;
94                 reservedCPU = reservedCPU + diffCPU;
95                 totalResizeCalls++;
96                 return true;
97         }
98
99         public boolean resizeDown(Application app) {
100                 runningOn.resizeDown(app);
101
102                 reservedSize = reservedSize - app.getSize();
103                 reservedRAM = reservedRAM - app.getRam();
104                 reservedCPU = reservedCPU - app.getCpu();
105                 totalResizeCalls++;
106                 return true;
107         }
108
109         public boolean stopApplication(Application app) {
110                 if (applications.containsKey(app.getID())) {
111                         applications.remove(app.getID());
112                         return true;
113                 } else
114                         return false;
115         }
116
117         public boolean enoughResources(Application app) {
118                 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
119         }
120
121         public int availableSize() {
122                 return reservedSize - getSize();
123         }
124
125         public int availableRAM() {
126                 return reservedRAM - getRAM();
127         }
128
129         public int availableCPU() {
130                 return reservedCPU - getCPU();
131         }
132
133         public int getId() {
134                 return id;
135         }
136
137         public PhysicalMachine getRunningOn() {
138                 return runningOn;
139         }
140
141         public int getPositionOnPM() {
142                 return posOnPM;
143         }
144
145         public ArrayList<Application> getApplications() {
146                 return new ArrayList<Application>(applications.values());
147         }
148
149         public int getSize() {
150                 int usedSize = initialSize;
151                 for (Application a : applications.values())
152                         usedSize += a.getSize();
153                 return usedSize;
154         }
155
156         public int getRAM() {
157                 int usedRAM = initialRAM;
158                 for (Application a : applications.values())
159                         usedRAM += a.getRam();
160                 return usedRAM;
161         }
162
163         public int getCPU() {
164                 int usedCPU = initialCPU;
165                 for (Application a : applications.values())
166                         usedCPU += a.getCpu();
167                 return usedCPU;
168         }
169
170         public int getReservedSize() {
171                 return reservedSize;
172         }
173
174         public int getReservedRAM() {
175                 return reservedRAM;
176         }
177
178         public int getReservedCPU() {
179                 return reservedCPU;
180         }
181
182         public int getTotalResizeCalls() {
183                 return totalResizeCalls;
184         }
185
186         @Override
187         public String toString() {
188                 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
189                                 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";
190         }
191
192 }