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