forked from kaskavalci/CloudSim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCloudletSchedulerDynamicWorkload.java
395 lines (347 loc) · 9.46 KB
/
CloudletSchedulerDynamicWorkload.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Title: CloudSim Toolkit Description: CloudSim (Cloud Simulation) Toolkit for Modeling and
* Simulation of Clouds Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009-2012, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cloudbus.cloudsim.core.CloudSim;
/**
* CloudletSchedulerDynamicWorkload implements a policy of scheduling performed by a virtual machine
* assuming that there is just one cloudlet which is working as an online service.
*
* @author Anton Beloglazov
* @since CloudSim Toolkit 2.0
*/
public class CloudletSchedulerDynamicWorkload extends CloudletSchedulerTimeShared {
/** The mips. */
private double mips;
/** The number of PEs. */
private int numberOfPes;
/** The total mips. */
private double totalMips;
/** The under allocated mips. */
private Map<String, Double> underAllocatedMips;
/** The cache previous time. */
private double cachePreviousTime;
/** The cache current requested mips. */
private List<Double> cacheCurrentRequestedMips;
/**
* Instantiates a new vM scheduler time shared.
*
* @param mips the mips
* @param numberOfPes the pes number
*/
public CloudletSchedulerDynamicWorkload(double mips, int numberOfPes) {
super();
setMips(mips);
setNumberOfPes(numberOfPes);
setTotalMips(getNumberOfPes() * getMips());
setUnderAllocatedMips(new HashMap<String, Double>());
setCachePreviousTime(-1);
}
/**
* Updates the processing of cloudlets running under management of this scheduler.
*
* @param currentTime current simulation time
* @param mipsShare array with MIPS share of each Pe available to the scheduler
* @return time predicted completion time of the earliest finishing cloudlet, or 0 if there is
* no next events
* @pre currentTime >= 0
* @post $none
*/
@Override
public double updateVmProcessing(double currentTime, List<Double> mipsShare) {
setCurrentMipsShare(mipsShare);
double timeSpan = currentTime - getPreviousTime();
double nextEvent = Double.MAX_VALUE;
List<ResCloudlet> cloudletsToFinish = new ArrayList<ResCloudlet>();
for (ResCloudlet rcl : getCloudletExecList()) {
rcl.updateCloudletFinishedSoFar((long) (timeSpan
* getTotalCurrentAllocatedMipsForCloudlet(rcl, getPreviousTime()) * Consts.MILLION));
if (rcl.getRemainingCloudletLength() == 0) { // finished: remove from the list
cloudletsToFinish.add(rcl);
continue;
} else { // not finish: estimate the finish time
double estimatedFinishTime = getEstimatedFinishTime(rcl, currentTime);
if (estimatedFinishTime - currentTime < CloudSim.getMinTimeBetweenEvents()) {
estimatedFinishTime = currentTime + CloudSim.getMinTimeBetweenEvents();
}
if (estimatedFinishTime < nextEvent) {
nextEvent = estimatedFinishTime;
}
}
}
for (ResCloudlet rgl : cloudletsToFinish) {
getCloudletExecList().remove(rgl);
cloudletFinish(rgl);
}
setPreviousTime(currentTime);
if (getCloudletExecList().isEmpty()) {
return 0;
}
return nextEvent;
}
/**
* Receives an cloudlet to be executed in the VM managed by this scheduler.
*
* @param cl the cl
* @return predicted completion time
* @pre _gl != null
* @post $none
*/
@Override
public double cloudletSubmit(Cloudlet cl) {
return cloudletSubmit(cl, 0);
}
/**
* Receives an cloudlet to be executed in the VM managed by this scheduler.
*
* @param cl the cl
* @param fileTransferTime the file transfer time
* @return predicted completion time
* @pre _gl != null
* @post $none
*/
@Override
public double cloudletSubmit(Cloudlet cl, double fileTransferTime) {
ResCloudlet rcl = new ResCloudlet(cl);
rcl.setCloudletStatus(Cloudlet.INEXEC);
for (int i = 0; i < cl.getNumberOfPes(); i++) {
rcl.setMachineAndPeId(0, i);
}
getCloudletExecList().add(rcl);
return getEstimatedFinishTime(rcl, getPreviousTime());
}
/**
* Processes a finished cloudlet.
*
* @param rcl finished cloudlet
* @pre rgl != $null
* @post $none
*/
@Override
public void cloudletFinish(ResCloudlet rcl) {
rcl.setCloudletStatus(Cloudlet.SUCCESS);
rcl.finalizeCloudlet();
getCloudletFinishedList().add(rcl);
}
/**
* Get utilization created by all cloudlets.
*
* @param time the time
* @return total utilization
*/
@Override
public double getTotalUtilizationOfCpu(double time) {
double totalUtilization = 0;
for (ResCloudlet rcl : getCloudletExecList()) {
totalUtilization += rcl.getCloudlet().getUtilizationOfCpu(time);
}
return totalUtilization;
}
/**
* Gets the current mips.
*
* @return the current mips
*/
@Override
public List<Double> getCurrentRequestedMips() {
if (getCachePreviousTime() == getPreviousTime()) {
return getCacheCurrentRequestedMips();
}
List<Double> currentMips = new ArrayList<Double>();
double totalMips = getTotalUtilizationOfCpu(getPreviousTime()) * getTotalMips();
double mipsForPe = totalMips / getNumberOfPes();
for (int i = 0; i < getNumberOfPes(); i++) {
currentMips.add(mipsForPe);
}
setCachePreviousTime(getPreviousTime());
setCacheCurrentRequestedMips(currentMips);
return currentMips;
}
/**
* Gets the current mips.
*
* @param rcl the rcl
* @param time the time
* @return the current mips
*/
@Override
public double getTotalCurrentRequestedMipsForCloudlet(ResCloudlet rcl, double time) {
return rcl.getCloudlet().getUtilizationOfCpu(time) * getTotalMips();
}
/**
* Gets the total current mips for the clouddlet.
*
* @param rcl the rcl
* @param mipsShare the mips share
* @return the total current mips
*/
@Override
public double getTotalCurrentAvailableMipsForCloudlet(ResCloudlet rcl, List<Double> mipsShare) {
double totalCurrentMips = 0.0;
if (mipsShare != null) {
int neededPEs = rcl.getNumberOfPes();
for (double mips : mipsShare) {
totalCurrentMips += mips;
neededPEs--;
if (neededPEs <= 0) {
break;
}
}
}
return totalCurrentMips;
}
/**
* Gets the current mips.
*
* @param rcl the rcl
* @param time the time
* @return the current mips
*/
@Override
public double getTotalCurrentAllocatedMipsForCloudlet(ResCloudlet rcl, double time) {
double totalCurrentRequestedMips = getTotalCurrentRequestedMipsForCloudlet(rcl, time);
double totalCurrentAvailableMips = getTotalCurrentAvailableMipsForCloudlet(rcl, getCurrentMipsShare());
if (totalCurrentRequestedMips > totalCurrentAvailableMips) {
return totalCurrentAvailableMips;
}
return totalCurrentRequestedMips;
}
/**
* Update under allocated mips for cloudlet.
*
* @param rcl the rgl
* @param mips the mips
*/
public void updateUnderAllocatedMipsForCloudlet(ResCloudlet rcl, double mips) {
if (getUnderAllocatedMips().containsKey(rcl.getUid())) {
mips += getUnderAllocatedMips().get(rcl.getUid());
}
getUnderAllocatedMips().put(rcl.getUid(), mips);
}
/**
* Get estimated cloudlet completion time.
*
* @param rcl the rcl
* @param time the time
* @return the estimated finish time
*/
public double getEstimatedFinishTime(ResCloudlet rcl, double time) {
return time
+ ((rcl.getRemainingCloudletLength()) / getTotalCurrentAllocatedMipsForCloudlet(rcl, time));
}
/**
* Gets the total current mips.
*
* @return the total current mips
*/
public int getTotalCurrentMips() {
int totalCurrentMips = 0;
for (double mips : getCurrentMipsShare()) {
totalCurrentMips += mips;
}
return totalCurrentMips;
}
/**
* Sets the total mips.
*
* @param mips the new total mips
*/
public void setTotalMips(double mips) {
totalMips = mips;
}
/**
* Gets the total mips.
*
* @return the total mips
*/
public double getTotalMips() {
return totalMips;
}
/**
* Sets the pes number.
*
* @param pesNumber the new pes number
*/
public void setNumberOfPes(int pesNumber) {
numberOfPes = pesNumber;
}
/**
* Gets the pes number.
*
* @return the pes number
*/
public int getNumberOfPes() {
return numberOfPes;
}
/**
* Sets the mips.
*
* @param mips the new mips
*/
public void setMips(double mips) {
this.mips = mips;
}
/**
* Gets the mips.
*
* @return the mips
*/
public double getMips() {
return mips;
}
/**
* Sets the under allocated mips.
*
* @param underAllocatedMips the under allocated mips
*/
public void setUnderAllocatedMips(Map<String, Double> underAllocatedMips) {
this.underAllocatedMips = underAllocatedMips;
}
/**
* Gets the under allocated mips.
*
* @return the under allocated mips
*/
public Map<String, Double> getUnderAllocatedMips() {
return underAllocatedMips;
}
/**
* Gets the cache previous time.
*
* @return the cache previous time
*/
protected double getCachePreviousTime() {
return cachePreviousTime;
}
/**
* Sets the cache previous time.
*
* @param cachePreviousTime the new cache previous time
*/
protected void setCachePreviousTime(double cachePreviousTime) {
this.cachePreviousTime = cachePreviousTime;
}
/**
* Gets the cache current requested mips.
*
* @return the cache current requested mips
*/
protected List<Double> getCacheCurrentRequestedMips() {
return cacheCurrentRequestedMips;
}
/**
* Sets the cache current requested mips.
*
* @param cacheCurrentRequestedMips the new cache current requested mips
*/
protected void setCacheCurrentRequestedMips(List<Double> cacheCurrentRequestedMips) {
this.cacheCurrentRequestedMips = cacheCurrentRequestedMips;
}
}