-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloudSimProject981.java
More file actions
404 lines (336 loc) · 13.7 KB
/
CloudSimProject981.java
File metadata and controls
404 lines (336 loc) · 13.7 KB
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
396
397
398
399
400
401
402
403
404
/*
* Title: CloudSim Project for Cloud Computing Course 98-1
* Author: Soroush Hashemi Far
*/
package org.cloudsub.cloudsim.power_examples;
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.CloudSimTags;
import org.cloudbus.cloudsim.core.SimEntity;
import org.cloudbus.cloudsim.core.predicates.PredicateType;
import org.cloudbus.cloudsim.power.*;
import org.cloudbus.cloudsim.power.models.PowerModelLinear;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
import org.cloudbus.cloudsim.util.WorkloadFileReader;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.*;
public class CloudSimProject981 {
private static PowerDatacenter datacenter0, datacenter1;
private static DatacenterBroker broker;
private static List<Vm> vmlist;
private static int mips = 1000; // Host MIPS
private static int ram = 4096; // Host Memory (MB)
private static long storage = 10000000; // Host Storage
private static int bw = 1000000; // Host BW
private static int maxpower = 117; // Host Max Power
private static int staticPowerPercentage = 50; // Host Static Power Percentage
private static String arch = "x86"; // Datacenter Architecture
private static String os = "Linux"; // Datacenter OS Types
private static String vmm = "Xen"; // Datacenter VMM Types
private static double time_zone = 10.0; // Datacenter Machines Timezone
private static double cost = 3.0; // The cost of using processing in this resource (in Datacenter)
private static double costPerMem = 0.05; // The cost of using memory in this resource (in Datacenter)
private static double costPerStorage = 0.1; // The cost of using storage in this resource (in Datacenter)
private static double costPerBw = 0.1; // The cost of using bw in this resource (in Datacenter)
private static int VMmips = 1000; // VM Mips Needed
private static long VMsize = 10000; // VM Image Size (MB)
private static int VMram = 512; // VM Memory Needed (MB)
private static long VMbw = 1000; // VM BW needed
private static int VMpesNumber = 1; // VM Number of CPUs
private static String VMvmm = "Xen"; // VM VMM name
private static int numberOfVMs = 6;
private static int numberOfPEs = 5;
private static int numberOfHosts = 2;
private static int pauseTime = -1;
public static void main(String[] args) {
Log.printLine("Starting CloudSim Project (Cloud Computing 98-1)...");
try {
CloudSim.init(1, Calendar.getInstance(), false);
datacenter0 = createDatacenter("DataCenter_0");
datacenter1 = createDatacenter("DataCenter_1");
broker = createBroker("MainBroker");
assert broker != null;
int brokerId = broker.getId();
vmlist = createVM(brokerId, numberOfVMs);
List<Cloudlet> cloudletList = loadCloudlet(brokerId, "/media/soroush/E8263FAE263F7D1E2/soroush/cloud computing/cloudsim/LCG-2005-1.swf", 1000, 2000);
broker.submitVmList(vmlist);
broker.submitCloudletList(cloudletList);
// Part 2
Runnable pauseANDresumeThread = () -> {
if(pauseTime != -1){
CloudSim.pauseSimulation(pauseTime);
// Wait until simulation stops
while(!CloudSim.isPaused()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Determine cloudlets deployed on each VM
for(Vm vm : vmlist) {
List<Integer> list = new LinkedList<>();
for (Cloudlet cloudlet : cloudletList) {
if(cloudlet.getVmId() == vm.getId())
list.add(cloudlet.getCloudletId());
}
Log.printLine("Cloudlets exist in VM #" + vm.getId() + ": " + list);
}
Scanner scanner = new Scanner(System.in);
while(true) {
Log.printLine();
// Determine available amount of processing capacity in each host for DC0 & DC1
List<Map<String, Double>> availMipsMap;
for(PowerDatacenter datacenter : new PowerDatacenter[]{datacenter0, datacenter1}) {
availMipsMap = new LinkedList<>();
for (Host host : datacenter.getHostList()) {
Map<String, Double> hostAvailMipsMap = new HashMap<>();
hostAvailMipsMap.put("host #" + host.getId(), host.getAvailableMips());
availMipsMap.add(hostAvailMipsMap);
}
Log.printLine("Hosts available processing (Mips) in " + datacenter.getName() + ": " + availMipsMap);
}
// Determine utilization of each host in DC0 & DC1
List<Map<String, Double>> utilMap;
for(PowerDatacenter datacenter : new PowerDatacenter[]{datacenter0, datacenter1}) {
utilMap = new LinkedList<>();
for (PowerHost host : datacenter.getVmAllocationPolicy().<PowerHostUtilizationHistory>getHostList()) {
Map<String, Double> hostUtilizationMap = new HashMap<>();
hostUtilizationMap.put("host #" + host.getId(), host.getUtilizationOfCpu() * 100);
utilMap.add(hostUtilizationMap);
}
Log.printLine("Utilization of hosts in " + datacenter.getName() + ": " + utilMap);
}
Log.printLine();
// Part 3
Log.printLine("Enter twice to continue. Or enter datacenter number for migration");
String input = scanner.nextLine();
if (input.equals("")) {
CloudSim.resumeSimulation();
break;
}
else {
Log.printLine("Enter vm number to migrate in this datacenter");
String vmNum = scanner.nextLine();
Log.printLine("Enter host number to migrate on, in this datacenter");
String hostNum = scanner.nextLine();
try {
boolean result = manuallyMigrateVM(Integer.valueOf(input), Integer.valueOf(vmNum), Integer.valueOf(hostNum));
if (result)
Log.printLine("Migration of VM #" + vmNum + " successfully done");
else
Log.printLine("Migration of VM #" + vmNum + " has not successfully done");
} catch (Exception e){
e.printStackTrace();
Log.printLine("The migration has been cancelled due to an unexpected error");
}
}
}
}
};
new Thread(pauseANDresumeThread).start();
CloudSim.startSimulation();
CloudSim.stopSimulation();
List<Cloudlet> newCloudletsList = broker.getCloudletReceivedList();
Log.printLine("Received " + newCloudletsList.size() + " cloudlets");
printCloudletList(newCloudletsList);
Log.printLine("Simulation has finished!");
}
catch (Exception e)
{
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected error");
}
}
/**
* Create multiple power hosts, each with multiple CPUs
*/
public static List<PowerHost> createHostList(){
List<Pe> peList1 = new ArrayList<>();
// Generate cpu cores
for(int i=0; i<numberOfPEs; i++)
peList1.add(new Pe(i, new PeProvisionerSimple(mips)));
// Generate hosts and assign cpu cores to them
List<PowerHost> hostList = new ArrayList<>();
for(int i=0; i<numberOfHosts; i++)
hostList.add(
new PowerHostUtilizationHistory(
i,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList1,
new PowerTestVmSchedulerTimeShared(peList1),
new PowerModelLinear(maxpower, staticPowerPercentage)
)
);
return hostList;
}
/**
* Create power datacenter with a given name
* @param name Name of the datacenter
*/
private static PowerDatacenter createDatacenter(String name) {
List<PowerHost> hostList = createHostList();
LinkedList<Storage> storageList = new LinkedList<>();
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);
PowerDatacenter datacenter = null;
try {
datacenter = new PowerDatacenter(name, characteristics, new PowerTestVmAllocationPolicy(hostList), storageList, 15);
datacenter.setDisableMigrations(false);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
/**
* Create power broker with a given name
* @param name Name of the broker
*/
private static DatacenterBroker createBroker(String name){
DatacenterBroker broker;
try {
// Assign cloudlets to datacenters sequentially
// broker = new DatacenterBroker(name);
// Assign 3 cloudlets to each datacenter
broker = new PowerDatacenterBroker3VMs(name);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
/**
* Create multiple VMs
* @param brokerId Id of broker
* @param vms number of vms to create
*/
private static List<Vm> createVM(int brokerId, int vms) {
LinkedList<Vm> list = new LinkedList<>();
for(int i=0; i<vms; i++) {
list.add(new Vm(i, brokerId, VMmips, VMpesNumber, VMram, VMbw, VMsize, VMvmm, new CloudletSchedulerSpaceShared()));
}
return list;
}
/**
* Load workload data set from swf formatted file
* @param brokerId Id of broker
* @param filedir workload data set file path
* @param startIndex Start index to load
* @param finishIndex Last index to load
*/
private static List<Cloudlet> loadCloudlet(int brokerId, String filedir, int startIndex, int finishIndex) throws FileNotFoundException {
List<Cloudlet> cloudletList;
WorkloadFileReader workloadFileReader = new WorkloadFileReader(filedir, 1);
cloudletList = workloadFileReader.generateWorkload().subList(startIndex, finishIndex);
for (Cloudlet cloudlet : cloudletList)
cloudlet.setUserId(brokerId);
return cloudletList;
}
/**
* Execute VM migration manually
* Based on classes PowerTestVmAllocationPolicy & PowerDatacenter
* @param targetDatacenterNum Destination datacenter id (0, 1, ...)
* @param VMnum VM id to migrate (0, 1, ...)
* @param targetHostNum Destination host id (0, 1, ...)
*/
private static boolean manuallyMigrateVM(int targetDatacenterNum, int VMnum, int targetHostNum){
Vm vm = vmlist.get(VMnum);
PowerHost oldHost = (PowerHost) vm.getHost();
PowerDatacenter datacenter;
if(targetDatacenterNum == 0)
datacenter = datacenter0;
else if(targetDatacenterNum == 1)
datacenter = datacenter1;
else
return false;
if(targetHostNum < 0 || targetHostNum > numberOfHosts-1)
return false;
if(VMnum < 0 || VMnum > numberOfVMs-1)
return false;
// Check target host to pass the utilization condition
PowerHost targetHost = datacenter.getVmAllocationPolicy().<PowerHostUtilizationHistory>getHostList().get(targetHostNum);
if(targetHost.getUtilizationOfCpu() * 100 >= 90){
Log.printLine("Host #" + targetHost.getId() + " utilization is >90. Searching for new target host...");
targetHost = null;
double sumMips = 0;
for(int i=0; i<vm.getNumberOfPes(); i++)
sumMips += vm.getCurrentRequestedMips().get(i);
for (PowerHostUtilizationHistory host1 : datacenter.getVmAllocationPolicy().<PowerHostUtilizationHistory>getHostList()) {
if ((1-host1.getUtilizationOfCpu()) * host1.getTotalMips() <= sumMips) {
targetHost = host1;
break;
}
}
}
// Pack vm and target host to run migration
Map<String, Object> migrate = new HashMap<>();
if (targetHost != null){
migrate.put("vm", vm);
migrate.put("host", targetHost);
}
else{
return false;
}
if (oldHost == null) {
Log.formatLine(
"Migration of VM #%d to Host #%d is started",
vm.getId(),
targetHost.getId());
} else {
Log.formatLine(
"Migration of VM #%d from Host #%d to Host #%d is started",
vm.getId(),
oldHost.getId(),
targetHost.getId());
}
targetHost.addMigratingInVm(vm);
datacenter.incrementMigrationCount();
// Datacenter sends an event to itself with migration tag to run migration and delay the simulation time
datacenter.send(
datacenter.getId(),
vm.getRam() / ((double) targetHost.getBw() / (2 * 8000)),
CloudSimTags.VM_MIGRATE,
migrate
);
CloudSim.cancelAll(datacenter.getId(), new PredicateType(CloudSimTags.VM_DATACENTER_EVENT));
datacenter.send(datacenter.getId(), datacenter.getSchedulingInterval(), CloudSimTags.VM_DATACENTER_EVENT);
return true;
}
/**
* Prints the Cloudlet objects
* @param list list of Cloudlets
*/
private static void printCloudletList(List<Cloudlet> list) {
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + "Data center ID" + indent + "VM ID" + indent + indent + "Time" + indent + "Start Time" + indent + "Finish Time");
double execTime = 0;
int numOfSuccessCloudlets = 0;
DecimalFormat dft = new DecimalFormat("###.##");
for (Cloudlet value : list) {
Log.print(indent + value.getCloudletId() + indent + indent);
if (value.getStatus() == Cloudlet.SUCCESS) {
Log.print("SUCCESS");
Log.printLine(
indent + indent + value.getResourceId() +
indent + indent + indent + value.getVmId() +
indent + indent + indent + dft.format(value.getActualCPUTime()) +
indent + indent + dft.format(value.getExecStartTime()) +
indent + indent + indent + dft.format(value.getFinishTime())
);
execTime += value.getFinishTime() - value.getExecStartTime();
numOfSuccessCloudlets += 1;
}
}
Log.printLine();
Log.printLine(String.format("Energy consumption: %.2f kWh", (datacenter0.getPower() + datacenter1.getPower()) / (3600 * 1000)));
Log.printLine("Number of VM migrations: DC0 = " + datacenter0.getMigrationCount() + ", DC1 = " + datacenter1.getMigrationCount());
Log.printLine("Average response time: " + execTime * 1. / numOfSuccessCloudlets);
Log.printLine();
}
}