1 package org.cloudbus.cloudsim;
2
3 import java.util.ArrayList;//This class provides methods to manipulate the size of the array that is used internally to store the list.
4 import java.util.List;
5
6 /**【虚拟机】
7 * Vm represents a VM: 【it runs inside a Host, sharing hostList
8 * with other VMs. It processes cloudlets. 】This processing happens according
9 * to a policy, defined by the CloudletScheduler. Each VM has a owner(每个虚拟机有一个所有者), which can
10 * submit cloudlets to the VM to be executed
11 *
12 * @author Rodrigo N. Calheiros
13 * @author Anton Beloglazov
14 * @since CloudSim Toolkit 1.0
15 */
16 public class Vm {
17
18 /** The id. ID号*/
19 private int id;
20
21 /** The user id. 用户编号*/
22 private int userId;
23
24 private String uid;
25
26 /** The size. 大小 */
27 private long size;
28
29 /** The MIPS.处理速度MIPS */
30 private double mips;
31
32 /** The PEs number. PE数*/
33 private int pesNumber;
34
35 /** The ram. 内存*/
36 private int ram;
37
38 /** The bw.带宽 */
39 private long bw;
40
41 /** The vmm. 虚拟机管理程序*/
42 private String vmm;
43
44 /** The Cloudlet scheduler. 云任务调度程序*/
45 private CloudletScheduler cloudletScheduler;
46
47 /** The host.主机 */
48 private Host host;
49
50 /** In migration flag. 迁移标志*/
51 private boolean inMigration;
52
53 /** The current allocated size.当前分配大小 */
54 private long currentAllocatedSize;
55
56 /** The current allocated ram. 当前分配内存*/
57 private int currentAllocatedRam;
58
59 /** The current allocated bw. 当前分配带宽*/
60 private long currentAllocatedBw;
61
62 /** The current allocated mips. 当前分配mips*/
63 private List<Double> currentAllocatedMips;
64
65 /** The recently created. 最近创建*/
66 private boolean recentlyCreated;
67
68 /**【虚拟机特征对象】
69 * Creates a new VMCharacteristics object.
70 *
71 * @param id unique ID of the VM
72 * @param userId ID of the VM's owner
73 * @param size amount of storage
74 * @param ram amount of ram
75 * @param bw amount of bandwidth
76 * @param pesNumber amount of CPUs CPU总和
77 * @param vmm virtual machine monitor 虚拟机监控器
78 * @param cloudletScheduler cloudletScheduler policy for cloudlets云任务调度协议
79 * @param priority the priority 优先权
80 * @param mips the mips
81 *
82 * @pre id >= 0
83 * @pre userId >= 0
84 * @pre size > 0
85 * @pre ram > 0
86 * @pre bw > 0
87 * @pre cpus > 0
88 * @pre priority >= 0
89 * @pre cloudletScheduler != null
90 * @post $none
91 */
92 public Vm(int id, int userId, double mips, int pesNumber, int ram, long bw, long size, String vmm, CloudletScheduler cloudletScheduler) {
93 setId(id);
94 setUserId(userId);
95 setUid(getUid(userId, id));
96 setMips(mips);
97 setPesNumber(pesNumber);
98 setRam(ram);
99 setBw(bw);
100 setSize(size);
101 setVmm(vmm);
102 setCloudletScheduler(cloudletScheduler);
103
104 setInMigration(false);
105 setRecentlyCreated(true);
106
107 setCurrentAllocatedBw(0);
108 setCurrentAllocatedMips(null);//怎么是null
109 setCurrentAllocatedRam(0);
110 setCurrentAllocatedSize(0);
111 }
112
113 /**【更新运行在虚拟机上云任务】
114 * Updates the processing of cloudlets running on this VM.
115 *
116 * @param currentTime current simulation time
117 * @param mipsShare array with MIPS share of each Pe available to the scheduler
118 *
119 * @return time predicted completion time【预测完成时间】 of the earliest finishing cloudlet, or 0
120 * if there is no next events
121 *
122 * @pre currentTime >= 0
123 * @post $none
124 */
125 public double updateVmProcessing(double currentTime, List<Double> mipsShare) {
126 if (mipsShare != null) {
127 return getCloudletScheduler().updateVmProcessing(currentTime, mipsShare);
128 }
129 return 0.0;
130 }
131
132 /**当前请求的MIPS
133 * Gets the current requested mips.
134 *
135 * @return the current requested mips
136 */
137 public List<Double> getCurrentRequestedMips() {
138 List<Double> currentRequestedMips = getCloudletScheduler().getCurrentRequestedMips();
139
140 if (isRecentlyCreated()) {//新创建虚拟机
141 boolean mipsIsNull = true;
142 for (double mips : currentRequestedMips) {
143 if (mips > 0.0) {
144 mipsIsNull = false;
145 setRecentlyCreated(false);
146 break;
147 }
148 }
149
150 //if (mipsIsNull && isRecentlyCreated()) {
151 if (mipsIsNull) {
152 currentRequestedMips = new ArrayList<Double>();
153 for (int i = 0; i < getPesNumber(); i++) {
154 currentRequestedMips.add(getMips());
155 }
156 }
157 }
158
159 return currentRequestedMips;
160 }
161
162 /**当前请求的MIPS总数
163 * Gets the current requested total mips.
164 *
165 * @return the current requested total mips
166 */
167 public double getCurrentRequestedTotalMips() {
168 double totalRequestedMips = 0;
169 for (double mips : getCurrentRequestedMips()) {
170 totalRequestedMips += mips;
171 }
172 return totalRequestedMips;
173 }
174
175 /**请求的带宽
176 * Gets the current requested bw.
177 *
178 * @return the current requested bw
179 */
180 public long getCurrentRequestedBw() {
181 return getBw();
182 }
183
184 /**请求的内存
185 * Gets the current requested ram.
186 *
187 * @return the current requested ram
188 */
189 public int getCurrentRequestedRam() {
190 return getRam();
191 }
192
193 /**CPU利用率
194 * Get utilization created by all clouddlets running on this VM.
195 *
196 * @param time the time
197 *
198 * @return total utilization
199 */
200 public double getTotalUtilizationOfCpu(double time) {
201 return getCloudletScheduler().getTotalUtilizationOfCpu(time);
202 }
203
204 /**MIPS利用率
205 * Get utilization created by all cloudlets running on this VM in MIPS.
206 *
207 * @param time the time
208 *
209 * @return total utilization
210 */
211 public double getTotalUtilizationOfCpuMips(double time) {
212 return getTotalUtilizationOfCpu(time) * getMips();
213 }
214
215 public void setUid(String uid) {
216 this.uid = uid;
217 }
218
219 /**虚拟机的唯一标示
220 * Get unique string identificator of the VM.
221 *
222 * @return string uid
223 */
224 public String getUid() {
225 return uid;
226 }
227
228 /**生成虚拟机标示
229 * Generate unique string identificator of the VM.
230 *
231 * @param userId the user id
232 * @param vmId the vm id
233 *
234 * @return string uid
235 */
236 public static String getUid(int userId, int vmId) {
237 return userId + "-" + vmId;
238 }
239
240 /**取ID
241 * Gets the id.
242 *
243 * @return the id
244 */
245 public int getId() {
246 return id;
247 }
248
249 /**设置ID
250 * Sets the id.
251 *
252 * @param id the new id
253 */
254 protected void setId(int id) {
255 this.id = id;
256 }
257
258 /**设置用户ID
259 * Sets the user id.
260 *
261 * @param userId the new user id
262 */
263 protected void setUserId(int userId) {
264 this.userId = userId;
265 }
266
267 /**虚拟机所有者ID
268 * Gets the ID of the owner of the VM.
269 *
270 * @return VM's owner ID
271 *
272 * @pre $none
273 * @post $none
274 */
275 public int getUserId() {
276 return userId;
277 }
278
279 /**取MIPS
280 * Gets the mips.
281 *
282 * @return the mips
283 */
284 public double getMips() {
285 return mips;
286 }
287
288 /**设置MIPS
289 * Sets the mips.
290 *
291 * @param mips the new mips
292 */
293 protected void setMips(double mips) {
294 this.mips = mips;
295 }
296
297 /**取pe数
298 * Gets the pes number.
299 *
300 * @return the pes number
301 */
302 public int getPesNumber() {
303 return pesNumber;
304 }
305
306 /**设置
307 * Sets the pes number.
308 *
309 * @param pesNumber the new pes number
310 */
311 protected void setPesNumber(int pesNumber) {
312 this.pesNumber = pesNumber;
313 }
314
315 /**总内存
316 * Gets the amount of ram.
317 *
318 * @return amount of ram
319 *
320 * @pre $none
321 * @post $none
322 */
323 public int getRam() {
324 return ram;
325 }
326
327 /**设置内存
328 * Sets the amount of ram.
329 *
330 * @param ram new amount of ram
331 *
332 * @pre ram > 0
333 * @post $none
334 */
335 public void setRam(int ram) {
336 this.ram = ram;
337 }
338
339 /**总带宽
340 * Gets the amount of bandwidth.
341 *
342 * @return amount of bandwidth
343 *
344 * @pre $none
345 * @post $none
346 */
347 public long getBw() {
348 return bw;
349 }
350
351 /**设置带宽
352 * Sets the amount of bandwidth.
353 *
354 * @param bw new amount of bandwidth
355 *
356 * @pre bw > 0
357 * @post $none
358 */
359 public void setBw(long bw) {
360 this.bw = bw;
361 }
362
363 /**取存储
364 * Gets the amount of storage.
365 *
366 * @return amount of storage
367 *
368 * @pre $none
369 * @post $none
370 */
371 public long getSize() {
372 return size;
373 }
374
375 /**设置存储
376 * Sets the amount of storage.
377 *
378 * @param size new amount of storage
379 *
380 * @pre size > 0
381 * @post $none
382 */
383 public void setSize(long size) {
384 this.size = size;
385 }
386
387 /**取VMM
388 * Gets the VMM.
389 *
390 * @return VMM
391 *
392 * @pre $none
393 * @post $none
394 */
395 public String getVmm(){
396 return vmm;
397 }
398
399 /**设置VMM
400 * Sets the VMM.
401 *
402 * @param vmm the new VMM
403 */
404 protected void setVmm(String vmm) {
405 this.vmm = vmm;
406 }
407
408 /**设置主机
409 * Sets the host that runs this VM.
410 *
411 * @param host Host running the VM
412 *
413 * @pre host != $null
414 * @post $none
415 */
416 public void setHost(Host host){
417 this.host = host;
418 }
419
420 /**获取主机
421 * Gets the host.
422 *
423 * @return the host
424 */
425 public Host getHost() {
426 return host;
427 }
428
429 /**虚拟机调度程序
430 * Gets the vm scheduler.
431 *
432 * @return the vm scheduler
433 */
434 public CloudletScheduler getCloudletScheduler() {
435 return cloudletScheduler;
436 }
437
438 /**设置虚拟机调度程序
439 * Sets the vm scheduler.
440 *
441 * @param cloudletScheduler the new vm scheduler
442 */
443 protected void setCloudletScheduler(CloudletScheduler cloudletScheduler) {
444 this.cloudletScheduler = cloudletScheduler;
445 }
446
447 /**是否迁移
448 * Checks if is in migration.
449 *
450 * @return true, if is in migration
451 */
452 public boolean isInMigration() {
453 return inMigration;
454 }
455
456 /**
457 * Sets the in migration.
458 *
459 * @param inMigration the new in migration
460 */
461 public void setInMigration(boolean inMigration) {
462 this.inMigration = inMigration;
463 }
464
465 /**当前分配大小
466 * Gets the current allocated size.
467 *
468 * @return the current allocated size
469 */
470 public long getCurrentAllocatedSize() {
471 return currentAllocatedSize;
472 }
473
474 /**
475 * Sets the current allocated size.
476 *
477 * @param currentAllocatedSize the new current allocated size
478 */
479 protected void setCurrentAllocatedSize(long currentAllocatedSize) {
480 this.currentAllocatedSize = currentAllocatedSize;
481 }
482
483 /**取当前分配的内存
484 * Gets the current allocated ram.
485 *
486 * @return the current allocated ram
487 */
488 public int getCurrentAllocatedRam() {
489 return currentAllocatedRam;
490 }
491
492 /**设置当前分配的内存
493 * Sets the current allocated ram.
494 *
495 * @param currentAllocatedRam the new current allocated ram
496 */
497 public void setCurrentAllocatedRam(int currentAllocatedRam) {
498 this.currentAllocatedRam = currentAllocatedRam;
499 }
500
501 /**取当前分配的带宽
502 * Gets the current allocated bw.
503 *
504 * @return the current allocated bw
505 */
506 public long getCurrentAllocatedBw() {
507 return currentAllocatedBw;
508 }
509
510 /**设置当前分配的带宽
511 * Sets the current allocated bw.
512 *
513 * @param currentAllocatedBw the new current allocated bw
514 */
515 public void setCurrentAllocatedBw(long currentAllocatedBw) {
516 this.currentAllocatedBw = currentAllocatedBw;
517 }
518
519 /**取当前分配的MIPS
520 * Gets the current allocated mips.
521 *
522 * @return the current allocated mips
523 */
524 public List<Double> getCurrentAllocatedMips() {
525 return currentAllocatedMips;
526 }
527
528 /**设置当前分配的MIPS
529 * Sets the current allocated mips.
530 *
531 * @param currentAllocatedMips the new current allocated mips
532 */
533 public void setCurrentAllocatedMips(List<Double> currentAllocatedMips) {
534 this.currentAllocatedMips = currentAllocatedMips;
535 }
536
537 /**是否最近创建
538 * Checks if is recently created.
539 *
540 * @return true, if is recently created
541 */
542 public boolean isRecentlyCreated() {
543 return recentlyCreated;
544 }
545
546 /**
547 * Sets the recently created.
548 *
549 * @param recentlyCreated the new recently created
550 */
551 public void setRecentlyCreated(boolean recentlyCreated) {
552 this.recentlyCreated = recentlyCreated;
553 }
554
555 }