-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFogDevice.java
2136 lines (1840 loc) · 68.4 KB
/
FogDevice.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.fog.entities;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import org.apache.commons.math3.util.Pair;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicy;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.CloudSimTags;
import org.cloudbus.cloudsim.core.SimEvent;
import org.cloudbus.cloudsim.power.PowerDatacenter;
import org.cloudbus.cloudsim.power.PowerHost;
import org.cloudbus.cloudsim.power.models.PowerModel;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
import org.cloudbus.cloudsim.sdn.overbooking.BwProvisionerOverbooking;
import org.cloudbus.cloudsim.sdn.overbooking.PeProvisionerOverbooking;
import org.fog.application.AppEdge;
import org.fog.application.AppLoop;
import org.fog.application.AppModule;
import org.fog.application.Application;
import org.fog.localization.Coordinate;// myiFogSim
import org.fog.localization.Distances;
import org.fog.placement.MobileController;
import org.fog.policy.AppModuleAllocationPolicy;
import org.fog.scheduler.StreamOperatorScheduler;
import org.fog.scheduler.TupleScheduler;
import org.fog.utils.Config;
import org.fog.utils.FogEvents;
import org.fog.utils.FogUtils;
import org.fog.utils.Logger;
import org.fog.utils.ModuleLaunchConfig;
import org.fog.utils.NetworkUsageMonitor;
import org.fog.utils.TimeKeeper;
import org.fog.vmmigration.BeforeMigration;
import org.fog.vmmigration.CompleteVM;
import org.fog.vmmigration.ContainerVM;
import org.fog.vmmigration.DecisionMigration;
import org.fog.vmmigration.LiveMigration;
import org.fog.vmmigration.MyStatistics;
import org.fog.vmmigration.Service;
import org.fog.vmmobile.LogMobile;
import org.fog.vmmobile.constants.MobileEvents;
import org.fog.vmmobile.constants.Policies;
//import org.fog.scheduler.TupleScheduler;
import org.fog.application.selectivity.SelectivityModel;
import org.cloudbus.cloudsim.CloudletScheduler;
public class FogDevice extends PowerDatacenter {
protected Queue<Tuple> northTupleQueue;
protected Queue<Pair<Tuple, Integer>> southTupleQueue;
protected List<String> activeApplications;
protected ArrayList<String[]> path;
protected Map<String, Application> applicationMap;
protected Map<String, List<String>> appToModulesMap;
protected Map<Integer, Double> childToLatencyMap;
protected Map<Integer, Integer> cloudTrafficMap;
protected double lockTime;
/**
* ID of the parent Fog Device
*/
protected int parentId;
protected int volatilParentId;
/**
* ID of the Controller
*/
protected int controllerId;
/**
* IDs of the children Fog devices
*/
protected List<Integer> childrenIds;
protected Map<Integer, List<String>> childToOperatorsMap;
/**
* Flag denoting whether the link southwards from this FogDevice is busy
*/
protected boolean isSouthLinkBusy;
boolean NextVM = false;
/**
* Flag denoting whether the link northwards from this FogDevice is busy
*/
protected boolean isNorthLinkBusy;
protected double uplinkBandwidth;
protected double downlinkBandwidth;
protected double uplinkLatency;
protected List<Pair<Integer, Double>> associatedActuatorIds;
protected double energyConsumption;
protected double lastUtilizationUpdateTime;
protected double lastUtilization;
private int level;
protected double ratePerMips;
protected double totalCost;
protected Map<String, Map<String, Integer>> moduleInstanceCount;
protected Coordinate coord;
protected Set<ApDevice> apDevices;
protected Set<MobileDevice> smartThings;
protected Set<MobileDevice> smartThingsWithVm;
protected Set<FogDevice> serverCloudlets;
protected boolean available;
protected Service service;
private HashMap<FogDevice, Double> netServerCloudlets;
protected DecisionMigration migrationStrategy;
protected int policyReplicaVM;
private FogDevice serverCloudletToVmMigrate;
protected BeforeMigration beforeMigration;
protected int startTravelTime;
protected int travelTimeId;
protected int travelPredicTime;
protected int mobilityPrecitionError;
protected int myId;
public int getMyId() {
return myId;
}
public void setMyId(int myId) {
this.myId = myId;
}
public HashMap<FogDevice, Double> getNetServerCloudlets() {
return netServerCloudlets;
}
public void setNetServerCloudlets(HashMap<FogDevice, Double> netServerCloudlets) {
this.netServerCloudlets = netServerCloudlets;
}
public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public Set<MobileDevice> getSmartThings() {
return smartThings;
}
public void setSmartThings(MobileDevice st, int action) {
if (action == Policies.ADD) {
this.smartThings.add(st);
}
else {
this.smartThings.remove(st);
}
}
public Set<ApDevice> getApDevices() {
return apDevices;
}
public void setApDevices(ApDevice ap, int action) {
if (action == Policies.ADD) {
this.apDevices.add(ap);
}
else {
this.apDevices.remove(ap);
}
}
public Coordinate getCoord() {
return coord;
}
public void setCoord(int coordX, int coordY) {
this.coord.setCoordX(coordX);
this.coord.setCoordY(coordY);
}
public int getStartTravelTime() {
return startTravelTime;
}
public void setStartTravelTime(int startTravelTime) {
this.startTravelTime = startTravelTime;
}
public int getTravelTimeId() {
return travelTimeId;
}
public void setTravelTimeId(int travelTimeId) {
this.travelTimeId = travelTimeId;
}
public int getTravelPredicTime() {
return travelPredicTime;
}
public void setTravelPredicTime(int travelPredicTime) {
this.travelPredicTime = travelPredicTime;
}
public int getMobilityPrecitionError() {
return mobilityPrecitionError;
}
public void setMobilityPredictionError(int mobilityPrecitionError) {
this.mobilityPrecitionError = mobilityPrecitionError;
}
public FogDevice() {
}
public FogDevice(String name, int coordX, int coordY, int id) {
super(name);
this.coord = new Coordinate();
this.setCoord(coordX, coordY);
this.setMyId(id);
smartThings = new HashSet<>();
apDevices = new HashSet<>();
netServerCloudlets = new HashMap<>();
serverCloudlets = new HashSet<>();
this.setAvailable(true);
}
public FogDevice(String name) {
super(name);
}
public FogDevice(String name, FogDeviceCharacteristics characteristics,
VmAllocationPolicy vmAllocationPolicy, List<Storage> storageList,
double schedulingInterval, double uplinkBandwidth, double downlinkBandwidth,
double uplinkLatency, double ratePerMips, int coordX, int coordY, int id, Service service,
DecisionMigration migrationStrategy, int policyReplicaVM, BeforeMigration beforeMigration)
throws Exception {
super(name, characteristics, vmAllocationPolicy, storageList, schedulingInterval);
this.coord = new Coordinate();
this.setCoord(coordX, coordY);
this.setMyId(id);
smartThings = new HashSet<>();
smartThingsWithVm = new HashSet<>();
apDevices = new HashSet<>();
netServerCloudlets = new HashMap<>();
setVolatilParentId(-1);
this.setAvailable(true);
this.setService(service);
setBeforeMigrate(beforeMigration);
setPolicyReplicaVM(policyReplicaVM);
setMigrationStrategy(migrationStrategy);
setCharacteristics(characteristics);
setVmAllocationPolicy(vmAllocationPolicy);
setLastProcessTime(0.0);
setStorageList(storageList);
setVmList(new ArrayList<Vm>());
setSchedulingInterval(schedulingInterval);
setUplinkBandwidth(uplinkBandwidth);
setDownlinkBandwidth(downlinkBandwidth);
setUplinkLatency(uplinkLatency);
setRatePerMips(ratePerMips);
setServerCloudletToVmMigrate(null);
setAssociatedActuatorIds(new ArrayList<Pair<Integer, Double>>());
for (Host host : getCharacteristics().getHostList()) {
host.setDatacenter(this);
}
setActiveApplications(new ArrayList<String>());
setPath(new ArrayList<String[]>());
setTravelTimeId(-1);
setTravelPredicTime(0);
setMobilityPredictionError(0);
// If this resource doesn't have any PEs then no useful at all
if (getCharacteristics().getNumberOfPes() == 0) {
throw new Exception(super.getName()
+ " : Error - this entity has no PEs. Therefore, can't process any Cloudlets.");
}
// stores id of this class
getCharacteristics().setId(super.getId());
applicationMap = new HashMap<String, Application>();
appToModulesMap = new HashMap<String, List<String>>();
northTupleQueue = new LinkedList<Tuple>();
southTupleQueue = new LinkedList<Pair<Tuple, Integer>>();
setNorthLinkBusy(false);
setSouthLinkBusy(false);
setChildrenIds(new ArrayList<Integer>());
setChildToOperatorsMap(new HashMap<Integer, List<String>>());
this.cloudTrafficMap = new HashMap<Integer, Integer>();
this.lockTime = 0;
this.energyConsumption = 0;
this.lastUtilization = 0;
setTotalCost(0);
setModuleInstanceCount(new HashMap<String, Map<String, Integer>>());
setChildToLatencyMap(new HashMap<Integer, Double>());
}
public FogDevice(
String name,
FogDeviceCharacteristics characteristics,
VmAllocationPolicy vmAllocationPolicy,
List<Storage> storageList,
double schedulingInterval,
double uplinkBandwidth, double downlinkBandwidth, double uplinkLatency, double ratePerMips
, int coordX, int coordY, int id
) throws Exception {
super(name, characteristics, vmAllocationPolicy, storageList, schedulingInterval);
this.coord = new Coordinate();
this.setCoord(coordX, coordY);
this.setMyId(id);
smartThings = new HashSet<>();
smartThingsWithVm = new HashSet<>();
apDevices = new HashSet<>();
netServerCloudlets = new HashMap<>();
setVolatilParentId(-1);
this.setAvailable(true);
setCharacteristics(characteristics);
setVmAllocationPolicy(vmAllocationPolicy);
setLastProcessTime(0.0);
setStorageList(storageList);
setVmList(new ArrayList<Vm>());
setSchedulingInterval(schedulingInterval);
setUplinkBandwidth(uplinkBandwidth);
setDownlinkBandwidth(downlinkBandwidth);
setUplinkLatency(uplinkLatency);
setRatePerMips(ratePerMips);
setServerCloudletToVmMigrate(null);
setAssociatedActuatorIds(new ArrayList<Pair<Integer, Double>>());
for (Host host : getCharacteristics().getHostList()) {
host.setDatacenter(this);
}
setActiveApplications(new ArrayList<String>());
setPath(new ArrayList<String[]>());
setTravelTimeId(-1);
setTravelPredicTime(0);
setMobilityPredictionError(0);
// If this resource doesn't have any PEs then no useful at all
if (getCharacteristics().getNumberOfPes() == 0) {
throw new Exception(super.getName()
+ " : Error - this entity has no PEs. Therefore, can't process any Cloudlets.");
}
// stores id of this class
getCharacteristics().setId(super.getId());
applicationMap = new HashMap<String, Application>();
appToModulesMap = new HashMap<String, List<String>>();
northTupleQueue = new LinkedList<Tuple>();
southTupleQueue = new LinkedList<Pair<Tuple, Integer>>();
setNorthLinkBusy(false);
setSouthLinkBusy(false);
setChildrenIds(new ArrayList<Integer>());
setChildToOperatorsMap(new HashMap<Integer, List<String>>());
this.cloudTrafficMap = new HashMap<Integer, Integer>();
this.lockTime = 0;
this.energyConsumption = 0;
this.lastUtilization = 0;
setTotalCost(0);
setModuleInstanceCount(new HashMap<String, Map<String, Integer>>());
setChildToLatencyMap(new HashMap<Integer, Double>());
}
public FogDevice(
String name,
FogDeviceCharacteristics characteristics,
VmAllocationPolicy vmAllocationPolicy,
List<Storage> storageList,
double schedulingInterval,
double uplinkBandwidth, double downlinkBandwidth, double uplinkLatency, double ratePerMips)
throws Exception {
super(name, characteristics, vmAllocationPolicy, storageList, schedulingInterval);
setCharacteristics(characteristics);
setVmAllocationPolicy(vmAllocationPolicy);
setLastProcessTime(0.0);
setStorageList(storageList);
setVmList(new ArrayList<Vm>());
setSchedulingInterval(schedulingInterval);
setUplinkBandwidth(uplinkBandwidth);
setDownlinkBandwidth(downlinkBandwidth);
setUplinkLatency(uplinkLatency);
setRatePerMips(ratePerMips);
setServerCloudletToVmMigrate(null);
setAssociatedActuatorIds(new ArrayList<Pair<Integer, Double>>());
for (Host host : getCharacteristics().getHostList()) {
host.setDatacenter(this);
}
setActiveApplications(new ArrayList<String>());
setPath(new ArrayList<String[]>());
setTravelTimeId(-1);
setTravelPredicTime(0);
setMobilityPredictionError(0);
// If this resource doesn't have any PEs then no useful at all
if (getCharacteristics().getNumberOfPes() == 0) {
throw new Exception(super.getName()
+ " : Error - this entity has no PEs. Therefore, can't process any Cloudlets.");
}
// stores id of this class
getCharacteristics().setId(super.getId());
applicationMap = new HashMap<String, Application>();
appToModulesMap = new HashMap<String, List<String>>();
northTupleQueue = new LinkedList<Tuple>();
southTupleQueue = new LinkedList<Pair<Tuple, Integer>>();
setNorthLinkBusy(false);
setSouthLinkBusy(false);
setChildrenIds(new ArrayList<Integer>());
setChildToOperatorsMap(new HashMap<Integer, List<String>>());
this.cloudTrafficMap = new HashMap<Integer, Integer>();
this.lockTime = 0;
this.energyConsumption = 0;
this.lastUtilization = 0;
setTotalCost(0);
setModuleInstanceCount(new HashMap<String, Map<String, Integer>>());
setChildToLatencyMap(new HashMap<Integer, Double>());
}
public FogDevice(
String name, long mips, int ram,
double uplinkBandwidth, double downlinkBandwidth, double ratePerMips, PowerModel powerModel)
throws Exception {
super(name, null, null, new LinkedList<Storage>(), 0);
List<Pe> peList = new ArrayList<Pe>();
// 3. Create PEs and add these into a list.
// need to store Pe id and MIPS Rating
peList.add(new Pe(0, new PeProvisionerOverbooking(mips)));
int hostId = FogUtils.generateEntityId();
long storage = 1000000; // host storage
int bw = 10000;
PowerHost host = new PowerHost(hostId, new RamProvisionerSimple(ram),
new BwProvisionerOverbooking(bw), storage, peList, new StreamOperatorScheduler(peList),
powerModel);
List<Host> hostList = new ArrayList<Host>();
hostList.add(host);
setVmAllocationPolicy(new AppModuleAllocationPolicy(hostList));
String arch = Config.FOG_DEVICE_ARCH;
String os = Config.FOG_DEVICE_OS;
String vmm = Config.FOG_DEVICE_VMM;
double time_zone = Config.FOG_DEVICE_TIMEZONE;
double cost = Config.FOG_DEVICE_COST;
double costPerMem = Config.FOG_DEVICE_COST_PER_MEMORY;
double costPerStorage = Config.FOG_DEVICE_COST_PER_STORAGE;
double costPerBw = Config.FOG_DEVICE_COST_PER_BW;
FogDeviceCharacteristics characteristics = new FogDeviceCharacteristics(
arch, os, vmm, host, time_zone, cost, costPerMem, costPerStorage, costPerBw);
setCharacteristics(characteristics);
setLastProcessTime(0.0);
setVmList(new ArrayList<Vm>());
setUplinkBandwidth(uplinkBandwidth);
setDownlinkBandwidth(downlinkBandwidth);
setUplinkLatency(uplinkLatency);
setAssociatedActuatorIds(new ArrayList<Pair<Integer, Double>>());
for (Host host1 : getCharacteristics().getHostList()) {
host1.setDatacenter(this);
}
setActiveApplications(new ArrayList<String>());
setPath(new ArrayList<String[]>());
setTravelTimeId(-1);
setTravelPredicTime(0);
setMobilityPredictionError(0);
if (getCharacteristics().getNumberOfPes() == 0) {
throw new Exception(super.getName()
+ " : Error - this entity has no PEs. Therefore, can't process any Cloudlets.");
}
getCharacteristics().setId(super.getId());
applicationMap = new HashMap<String, Application>();
appToModulesMap = new HashMap<String, List<String>>();
northTupleQueue = new LinkedList<Tuple>();
southTupleQueue = new LinkedList<Pair<Tuple, Integer>>();
setNorthLinkBusy(false);
setSouthLinkBusy(false);
setChildrenIds(new ArrayList<Integer>());
setChildToOperatorsMap(new HashMap<Integer, List<String>>());
this.cloudTrafficMap = new HashMap<Integer, Integer>();
this.lockTime = 0;
this.energyConsumption = 0;
this.lastUtilization = 0;
setTotalCost(0);
setChildToLatencyMap(new HashMap<Integer, Double>());
setModuleInstanceCount(new HashMap<String, Map<String, Integer>>());
}
/**
* Overrides this method when making a new and different type of resource. <br>
* <b>NOTE:</b> You do not need to override {@link #body()} method, if you
* use this method.
*
* @pre $none
* @post $none
*/
@Override
protected void registerOtherEntity() {
}
@Override
protected void processOtherEvent(SimEvent ev) {
switch (ev.getTag()) {
case FogEvents.TUPLE_ARRIVAL:
processTupleArrival(ev);
break;
case FogEvents.LAUNCH_MODULE:
processModuleArrival(ev);
break;
case FogEvents.RELEASE_OPERATOR:
processOperatorRelease(ev);
break;
case FogEvents.SENSOR_JOINED:
processSensorJoining(ev);
break;
case FogEvents.SEND_PERIODIC_TUPLE:
sendPeriodicTuple(ev);
break;
case FogEvents.APP_SUBMIT:
processAppSubmit(ev);
break;
case FogEvents.UPDATE_NORTH_TUPLE_QUEUE:
updateNorthTupleQueue();
break;
case FogEvents.UPDATE_SOUTH_TUPLE_QUEUE:
updateSouthTupleQueue();
break;
case FogEvents.ACTIVE_APP_UPDATE:
updateActiveApplications(ev);
break;
case FogEvents.ACTUATOR_JOINED:
processActuatorJoined(ev);
break;
case FogEvents.LAUNCH_MODULE_INSTANCE:
updateModuleInstanceCount(ev);
break;
case FogEvents.RESOURCE_MGMT:
manageResources(ev);
break;
case MobileEvents.MAKE_DECISION_MIGRATION:
invokeDecisionMigration(ev);
break;
case MobileEvents.TO_MIGRATION:
invokeBeforeMigration(ev);
break;
case MobileEvents.NO_MIGRATION:
invokeNoMigration(ev);
break;
case MobileEvents.START_MIGRATION:
invokeStartMigration(ev);
break;
case MobileEvents.ABORT_MIGRATION:
invokeAbortMigration(ev);
break;
case MobileEvents.REMOVE_VM_OLD_CLOUDLET:
removeVmOldServerCloudlet(ev);
break;
case MobileEvents.ADD_VM_NEW_CLOUDLET:
addVmNewServerCloudlet(ev);
break;
case MobileEvents.DELIVERY_VM:
deliveryVM(ev);
break;
case MobileEvents.CONNECT_ST_TO_SC:
connectServerCloudletSmartThing(ev);
break;
case MobileEvents.DESCONNECT_ST_TO_SC:
desconnectServerCloudletSmartThing(ev);
break;
case MobileEvents.UNLOCKED_MIGRATION:
unLockedMigration(ev);
break;
case MobileEvents.VM_MIGRATE:
myVmMigrate(ev);
break;
case MobileEvents.SET_MIG_STATUS_TRUE:
migStatusToLiveMigration(ev);
break;
case MobileEvents.MAKE_NEXT_VM:
makeNextVM(ev);
break;
case MobileEvents.MIGRROR:
Migrror(ev);
break;
case MobileEvents.DELIVERY:
Delivery(ev);
System.out.println("End of MIGRRORING....................................................");
break;
default:
break;
}
}
private void myVmMigrate(SimEvent ev) {
// TODO Auto-generated method stub
MobileDevice smartThing = (MobileDevice) ev.getData();
System.out.println("local " + smartThing.getVmLocalServerCloudlet().getName() + " "
+ smartThing.getVmLocalServerCloudlet().getActiveApplications() +
" apps "
+ smartThing.getVmLocalServerCloudlet().getApplicationMap().values().toString());
System.out.println("dest: " + smartThing.getDestinationServerCloudlet().getName() + " "
+ smartThing.getDestinationServerCloudlet().getActiveApplications() +
" apps "
+ smartThing.getDestinationServerCloudlet().getApplicationMap().values().toString());
System.out.println("smartthing id: " + smartThing.getMyId());
smartThing.getVmLocalServerCloudlet().applicationMap.values();
Application app = smartThing.getVmLocalServerCloudlet().applicationMap.get("MyApp_vr_game"
+ smartThing.getMyId());
if (app == null) {
System.out.println("Clock: " + CloudSim.clock() + " - FogDevice.java - App == Null");
System.exit(0);
}
getApplicationMap().put(app.getAppId(), app);
if (smartThing.getVmLocalServerCloudlet().getApplicationMap().remove(app.getAppId()) == null) {
System.out.println("FogDevice.java - applicationMap did not remove. return == null");
System.exit(0);
}
MobileController mobileController = (MobileController) CloudSim
.getEntity("MobileController");
mobileController.getModuleMapping().addModuleToDevice(
((AppModule) smartThing.getVmMobileDevice()).getName(), getName(), 1);
System.out.println("Antes de entrar no submitApplicationMigration - " + getName());
mobileController.getModuleMapping().getModuleMapping()
.remove(smartThing.getVmLocalServerCloudlet().getName());
if (!mobileController.getModuleMapping().getModuleMapping().containsKey(getName())) {
mobileController.getModuleMapping().getModuleMapping()
.put(getName(), new HashMap<String, Integer>());
mobileController.getModuleMapping().getModuleMapping().get(getName())
.put("AppModuleVm_" + smartThing.getName(), 1);
}
mobileController.submitApplicationMigration(smartThing, app, 1);
sendNow(mobileController.getId(), MobileEvents.APP_SUBMIT_MIGRATE, app);
}
private void unLockedMigration(SimEvent ev) {
MobileDevice smartThing = (MobileDevice) ev.getData();
smartThing.setLockedToMigration(false);
smartThing.setTimeFinishDeliveryVm(-1);
LogMobile.debug("FogDevice.java", smartThing.getName() + " had the migration unlocked");
}
private void saveConnectionCloudletSmartThing(MobileDevice st, String conType) {
try (FileWriter fw = new FileWriter(st.getMyId() + "ConClSmTh.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println(CloudSim.clock() + "\t" + st.getMyId() + "\t" + conType);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void desconnectServerCloudletSmartThing(SimEvent ev) {
MobileDevice smartThing = (MobileDevice) ev.getData();
desconnectServerCloudletSmartThing(smartThing);
MyStatistics.getInstance().startWithoutConnetion(smartThing.getMyId(), CloudSim.clock());
saveConnectionCloudletSmartThing(smartThing, "desconnectServerCloudletSmartThing");
}
private void connectServerCloudletSmartThing(SimEvent ev) {
MobileDevice smartThing = (MobileDevice) ev.getData();
connectServerCloudletSmartThing(smartThing);
MyStatistics.getInstance().finalWithoutConnection(smartThing.getMyId(), CloudSim.clock());
saveConnectionCloudletSmartThing(smartThing, "connectServerCloudletSmartThing");
if (smartThing.getTimeFinishDeliveryVm() == -1) {
MyStatistics.getInstance().startDelayAfterNewConnection(smartThing.getMyId(),
CloudSim.clock());
}
else {
smartThing.setMigStatus(false);
smartThing.setPostCopyStatus(false);
smartThing.setMigStatusLive(false);
if (MyStatistics.getInstance().getInitialWithoutVmTime().get(smartThing.getMyId()) != null) {
MyStatistics.getInstance().finalWithoutVmTime(smartThing.getMyId(), CloudSim.clock());
System.out.println("finalWithoutVmTime2: " + CloudSim.clock());
MyStatistics.getInstance().getInitialWithoutVmTime().remove(smartThing.getMyId());
}
LogMobile.debug("FogDevice.java", smartThing.getName()
+ " had migStatus to false - connectServerCloudlet");
MyStatistics.getInstance().startDelayAfterNewConnection(smartThing.getMyId(), 0.0);
MyStatistics.getInstance().finalDelayAfterNewConnection( smartThing.getMyId(),
getCharacteristics().getCpuTime( smartThing.getVmMobileDevice().getSize() * 1024 * 1024 * 8, 0.0));
}
if (!smartThing.getSourceServerCloudlet().equals(smartThing.getVmLocalServerCloudlet())) {
smartThing.getSourceServerCloudlet().desconnectServerCloudletSmartThing(smartThing);
smartThing.getVmLocalServerCloudlet().connectServerCloudletSmartThing(smartThing);
MyStatistics.getInstance().setMyCountLowestLatency(1);
}
}
private void addVmNewServerCloudlet(SimEvent ev) {
}
private void removeVmOldServerCloudlet(SimEvent ev) {
}
private void invokeAbortMigration(SimEvent ev) {
MobileDevice smartThing = (MobileDevice) ev.getData();
System.out.println("*_*_*_*_*_*_*_*_*_*_*_*_*_ABORT MIGRATION -> beforeMigration*_*_*_*_*_*_*_*_*_*_*_*: "
+ smartThing.getName());
MyStatistics.getInstance().getInitialWithoutVmTime().remove(smartThing.getMyId());
MyStatistics.getInstance().getInitialTimeDelayAfterNewConnection() .remove(smartThing.getMyId());
MyStatistics.getInstance().getInitialTimeWithoutConnection().remove(smartThing.getMyId());
smartThing.setMigStatus(false);
smartThing.setPostCopyStatus(false);
smartThing.setMigStatusLive(false);
smartThing.setLockedToMigration(false);
smartThing.setTimeFinishDeliveryVm(-1.0);
smartThing.setAbortMigration(true);
smartThing.setDestinationServerCloudlet(smartThing.getVmLocalServerCloudlet());
}
public boolean connectServerCloudletSmartThing(MobileDevice st) {
st.setSourceServerCloudlet(this);
setSmartThings(st, Policies.ADD);
st.setParentId(getId());
double latency = st.getUplinkLatency();
getChildToLatencyMap().put(st.getId(), latency);
addChild(st.getId());
setUplinkLatency(getUplinkLatency() + 0.123812950236);//
LogMobile.debug("FogDevice.java", st.getName() + " was connected to " + getName());
return true;
}
public boolean desconnectServerCloudletSmartThing(MobileDevice st) {
setSmartThings(st, Policies.REMOVE); // it'll remove the smartThing from serverCloudlets-smartThing's set
st.setSourceServerCloudlet(null);
// NetworkTopology.addLink(this.getId(), st.getId(), 0.0, 0.0);
setUplinkLatency(getUplinkLatency() - 0.123812950236);
removeChild(st.getId());
LogMobile.debug("FogDevice.java", st.getName() + " was desconnected to " + getName());
return true;
}
private void invokeStartMigration(SimEvent ev) {
MobileDevice smartThing = (MobileDevice) ev.getData();
// the smartThing is outside of the map
if (MobileController.getSmartThings().contains(smartThing)) {
if (!smartThing.isAbortMigration()) {
// the smartThing isn't connected in any ap right now
if (smartThing.getSourceAp() != null) {
int srcId = getId();
int entityId = smartThing.getDestinationServerCloudlet().getId();
Double delay = 1.0;
if (entityId != srcId) {// does not delay self messages
delay += getNetworkDelay(srcId, entityId);
}
send(smartThing.getVmLocalServerCloudlet().getId(), delay,
MobileEvents.DELIVERY_VM, smartThing);
LogMobile.debug("FogDevice.java", smartThing.getName()
+ " was scheduled the DELIVERY_VM from " +
smartThing.getVmLocalServerCloudlet().getName() + " to "
+ smartThing.getDestinationServerCloudlet().getName());
System.out.println("FogDevice.java" + smartThing.getName()
+ " was scheduled the DELIVERY_VM from " +
smartThing.getVmLocalServerCloudlet().getName() + " to "
+ smartThing.getDestinationServerCloudlet().getName() + " in "
+ CloudSim.clock() + " with delay " + delay);
sendNow(smartThing.getDestinationServerCloudlet().getId(),
MobileEvents.VM_MIGRATE, smartThing);
Map<String, Object> ma;
ma = new HashMap<String, Object>();
if (smartThing.getVmMobileDevice() == null) {
System.out.println(smartThing.getName() + " has a null VM");
}
ma.put("vm", smartThing.getVmMobileDevice());
ma.put("host", smartThing.getDestinationServerCloudlet().getHost());
if (ma.size() < 2) {
sendNow(getId(), MobileEvents.ABORT_MIGRATION, smartThing);
System.out.println("FogDevice.java ma.size()<2");
System.exit(0);
}
else {
sendNow(smartThing.getVmLocalServerCloudlet().getId(),
CloudSimTags.VM_MIGRATE, ma);
LogMobile.debug("FogDevice.java",
"CloudSim.VM_MIGRATE was scheduled to VM#: "
+ smartThing.getVmMobileDevice().getId() + " HOST#: " +
smartThing.getDestinationServerCloudlet().getHost().getId());
System.out.println("FogDevice.java"
+ " CloudSim.VM_MIGRATE was scheduled to VM#: "
+ smartThing.getVmMobileDevice().getId() + " HOST#: " +
smartThing.getDestinationServerCloudlet().getHost().getId());
}
}
else {
sendNow(smartThing.getVmLocalServerCloudlet().getId(),
MobileEvents.ABORT_MIGRATION, smartThing);
}
}
else {
smartThing.setAbortMigration(false);
}
}
else {
LogMobile.debug("FogDevice.java", smartThing.getName()
+ " was excluded from List of SmartThings!");
}
}
private void deliveryVM(SimEvent ev) {
MobileDevice smartThing = (MobileDevice) ev.getData();
if (MobileController.getSmartThings().contains(smartThing)) {
LogMobile.debug("FogDevice.java", "DELIVERY VM: " + smartThing.getName() + " (id: "
+ smartThing.getId() + ") from " + smartThing.getVmLocalServerCloudlet().getName()
+ " to " + smartThing.getDestinationServerCloudlet().getName());
smartThing.getVmLocalServerCloudlet().setSmartThingsWithVm(smartThing, Policies.REMOVE);
smartThing.setVmLocalServerCloudlet(smartThing.getDestinationServerCloudlet());
smartThing.setDestinationServerCloudlet(null);
smartThing.getVmLocalServerCloudlet().setSmartThingsWithVm(smartThing, Policies.ADD);
if (MyStatistics.getInstance().getInitialTimeDelayAfterNewConnection()
.containsKey(smartThing.getMyId())) {
smartThing.setMigStatus(false);
smartThing.setPostCopyStatus(false);
smartThing.setMigStatusLive(false);
if (MyStatistics.getInstance().getInitialWithoutVmTime().get(smartThing.getMyId()) != null) {
MyStatistics.getInstance().finalWithoutVmTime(smartThing.getMyId(), CloudSim.clock());
System.out.println("finalWithoutVmTime: " + CloudSim.clock());
MyStatistics.getInstance().getInitialWithoutVmTime() .remove(smartThing.getMyId());
}
LogMobile.debug("FogDevice.java", smartThing.getName()
+ " had migStatus to false - deliveryVM");
// handoff has been occurred first than delivery
MyStatistics.getInstance().finalDelayAfterNewConnection(smartThing.getMyId(), CloudSim.clock()
+ getCharacteristics().getCpuTime(smartThing.getVmMobileDevice().getSize() * 1024 * 1024 * 8, 0.0));
if (smartThing.getSourceServerCloudlet() == null) {
smartThing.setSourceServerCloudlet(smartThing.getVmLocalServerCloudlet());
System.out.println("CRASH " + smartThing.getMyId() + "\t source c "
+ smartThing.getSourceServerCloudlet()
+ "\t local server " + smartThing.getVmLocalServerCloudlet());
}
if (!smartThing.getSourceServerCloudlet().equals(
smartThing.getVmLocalServerCloudlet())) {
smartThing.getSourceServerCloudlet().desconnectServerCloudletSmartThing(
smartThing);
smartThing.getVmLocalServerCloudlet().connectServerCloudletSmartThing(
smartThing);
}
}
float migrationLocked = (smartThing.getVmMobileDevice().getSize() * (smartThing
.getSpeed() + 1)) + 20000;
if (migrationLocked < smartThing.getTravelPredicTime() * 1000) {
migrationLocked = smartThing.getTravelPredicTime() * 1000;
}
send(smartThing.getVmLocalServerCloudlet().getId(), migrationLocked,
MobileEvents.UNLOCKED_MIGRATION, smartThing);
MyStatistics.getInstance().countMigration();
MyStatistics.getInstance().historyMigrationTime(smartThing.getMyId(),
smartThing.getMigTime());
if (smartThing.getMigrationTechnique() instanceof CompleteVM) {
MyStatistics.getInstance().historyDowntime(smartThing.getMyId(),
smartThing.getMigTime());
}
else if (smartThing.getMigrationTechnique() instanceof ContainerVM) {
MyStatistics.getInstance().historyDowntime(smartThing.getMyId(),
smartThing.getMigTime());
}
else if (smartThing.getMigrationTechnique() instanceof LiveMigration) {
MyStatistics.getInstance().historyDowntime(smartThing.getMyId(),
smartThing.getMigTime() * 0.15);
}
smartThing.setTimeFinishDeliveryVm(CloudSim.clock());
}
else {
LogMobile.debug("FogDevice.java", smartThing.getName()
+ " was excluded by List of SmartThings! (inside Delivery Vm)");
}
}
private void invokeNoMigration(SimEvent ev) {
MobileDevice smartThing = (MobileDevice) ev.getData();
if (smartThing.isLockedToMigration()) {// isMigStatus()){
LogMobile.debug("FogDevice.java", "NO MIGRATE: " + smartThing.getName()
+ " already is in migration Process or the migration is locked");
}
else {
LogMobile.debug("FogDevice.java", "NO MIGRATE: " + smartThing.getName()
+ " is not in Migrate");
}
}
private void invokeBeforeMigration(SimEvent ev) {
MobileDevice smartThing = (MobileDevice) ev.getData();
if (MobileController.getSmartThings().contains(smartThing)) {
// the smartThing isn't connected in any ap right now
if (smartThing.getSourceAp() != null && !smartThing.isMigStatus()) {
double delayProcess = getBeforeMigrate().dataprepare(smartThing);
System.out.println("delayProcess" + delayProcess);
if (delayProcess >= 0) {
if (getPolicyReplicaVM() == Policies.LIVE_MIGRATION) {
smartThing.setPostCopyStatus(true);
smartThing.setTimeStartLiveMigration(CloudSim.clock());
}
else {
smartThing.setMigStatus(true);
MyStatistics.getInstance().startWithoutVmTime(smartThing.getMyId(),
CloudSim.clock());
smartThing.setTimeFinishDeliveryVm(-1.0);
// It'll happen according the Migration Time
send(smartThing.getVmLocalServerCloudlet().getId(), 0//smartThing.getMigTime() + delayProcess
, MobileEvents.START_MIGRATION, smartThing);
}
smartThing.setLockedToMigration(true);
}
}