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