forked from ik2sb/PICS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_entity_iaas.py
executable file
·1726 lines (1244 loc) · 73.6 KB
/
sim_entity_iaas.py
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
import sys
import time
import inspect
import logging
import random
import math
from threading import Condition
import sim_global_variables as simgval
import sim_lib_common as clib
import sim_object as simobj
import sim_entity_cloudbroker as broker
Global_Curr_Sim_Clock = -99
Global_Curr_Sim_Clock_CV = Condition()
g_flag_sim_entity_term = False
g_flag_sim_entity_term_CV = Condition()
g_sim_trace_log_handler = None # global var for sim trace log handler
g_my_block_id = None
g_log_handler = None
g_vm_log_handler = None
gQ_OUT = None
g_sim_conf = None
g_Creating_VMs = []
g_Creating_VMs_CV = Condition()
g_Running_VMs = []
g_Running_VMs_CV = Condition()
g_Stopped_VMs = []
g_Stopped_VMs_CV = Condition()
g_Activated_Storage_Containers = []
g_Activated_Storage_Containers_CV = Condition()
def set_log (path):
logger = logging.getLogger('iaas_cloud')
log_file = path + '/[' + str(g_my_block_id) + ']-iaas_cloud.log'
# for file logger...
hdlr = logging.FileHandler(log_file)
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
# for stdout...
ch = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.setLevel(logging.INFO)
return logger
def set_vm_log (path):
logger = logging.getLogger('vm_usage_log')
log_file = path + '/[' + str(g_my_block_id) + ']-vm_usage_list.log'
hdlr = logging.FileHandler(log_file)
#hdlr = logging.FileHandler(path + '/vm_usage_list.log')
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
return logger
def set_iaas_storage_usage_report_log (path):
logger = logging.getLogger('iaas_storage_usage_report_log')
log_file = path + '/5.report_storage_usage.csv'
hdlr = logging.FileHandler(log_file)
#hdlr = logging.FileHandler(path + '/job_complete_list.log')
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
return logger
def set_iaas_network_usage_report_log (path):
logger = logging.getLogger('iaas_network_usage_report_log')
log_file = path + '/6.report_network_usage.csv'
hdlr = logging.FileHandler(log_file)
#hdlr = logging.FileHandler(path + '/job_complete_list.log')
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
return logger
def set_iaas_sim_trace_log (path):
logger = logging.getLogger('storage_network_trace_log')
log_file = path + '/2.report_simulation_trace_iaas.csv'
hdlr = logging.FileHandler(log_file)
#hdlr = logging.FileHandler(path + '/job_complete_list.log')
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
return logger
def set_iaas_sim_entity_term_flag (val):
global g_flag_sim_entity_term
if val is not True and val is not False:
return
if g_flag_sim_entity_term == val:
return
g_flag_sim_entity_term_CV.acquire()
g_flag_sim_entity_term = val
g_flag_sim_entity_term_CV.notify()
g_flag_sim_entity_term_CV.release()
def iaas_check_sim_entity_termination ():
no_creating_VMs = len (g_Creating_VMs)
no_running_VMs = len (g_Running_VMs)
no_stopped_VMs = len (g_Stopped_VMs)
if no_creating_VMs < 1 and no_running_VMs < 1 and no_stopped_VMs > 0:
g_log_handler.info("[IaaS____] %s IaaS can be terminated - Len(creating_VMs):%d, Len(running_VMs):%d, Len(stopped_VMs):%d" \
%(str(Global_Curr_Sim_Clock), no_creating_VMs, no_running_VMs, no_stopped_VMs))
set_iaas_sim_entity_term_flag (True)
# sim entity termination procedure - send evt to ask sim event handler for termination
query_evt = clib.make_queue_event (Global_Curr_Sim_Clock, g_my_block_id, simgval.gBLOCK_SIM_EVENT_HANDLER, simgval.gEVT_ASK_SIMENTITY_TERMINATION, None, None)
gQ_OUT.put (query_evt)
else:
g_log_handler.info("[IaaS____] %s IaaS can NOT be terminated - Len(creating_VMs):%d, Len(running_VMs):%d, Len(stopped_VMs):%d" \
%(str(Global_Curr_Sim_Clock), no_creating_VMs, no_running_VMs, no_stopped_VMs))
def write_vm_usage_log (vm):
if vm.status != simgval.gVM_ST_TERMINATE:
clib.display_vm_obj(Global_Curr_Sim_Clock, vm)
clib.sim_exit()
return
vm_rt = simobj.get_vm_billing_clock(vm)
job_rt = clib.get_sum_job_actual_duration (vm.job_history)
vm_utilization = job_rt / float (vm_rt)
stlag_rate = vm.startup_lag / float (vm_rt)
idle_rate = 1 - vm_utilization - stlag_rate
# CL VMID RT CO IID TY ST TC TA TT SL NJ JR')
g_vm_log_handler.info("%d\t%d\t%d\t$%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%0.3f\t%0.3f\t%0.3f"
% (Global_Curr_Sim_Clock, vm.id, vm_rt, str(vm.cost), vm.instance_id, vm.type_name,
simgval.get_sim_code_str(vm.status), vm.time_create, vm.time_active, vm.time_terminate,
vm.startup_lag, len(vm.job_history), job_rt, vm_utilization, stlag_rate, idle_rate))
def iaas_send_evt_ack (evt_id, evt_org_code):
ack_evt = clib.make_queue_event (0, g_my_block_id, simgval.gBLOCK_SIM_EVENT_HANDLER, simgval.gEVT_ACK, evt_org_code, None)
ack_evt[0] = evt_id
gQ_OUT.put (ack_evt)
return
def calculate_Startup_Lag ():
return random.randint (g_sim_conf.min_startup_lag, g_sim_conf.max_startup_lag)
def get_vm_cost_and_billing_hour (vm_running_time, vm_unit_price):
billing_hour = math.ceil (vm_running_time/float(g_sim_conf.billing_unit_clock))
vm_cost = billing_hour * vm_unit_price
return vm_cost,billing_hour
def calculate_vm_cost (vm_obj):
vm_index = get_vm_index_from_List (3, vm_obj)
if vm_index < 0:
g_log_handler.error("[IaaS____] %s calculate_vm_cost Error! => Cannot Find VM (ID:%d, IID:%s, ST:%s) from Stopped VM List" \
% (str(Global_Curr_Sim_Clock), vm_obj.id, vm_obj.instance_id, simgval.get_sim_code_str(vm_obj.status)))
clib.logwrite_VM_Instance_Lists(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), "Current Stopped VM Lists", g_Stopped_VMs)
clib.sim_exit()
# calculate vm running time
vm_running_time = simobj.get_vm_billing_clock (vm_obj)
if vm_running_time is None:
g_log_handler.error("[IaaS____] %s calculate_vm_cost Error! => Invalid VM (ID:%d, IID:%s, ST:%s) for Cost Calculation" \
% (str(Global_Curr_Sim_Clock), vm_obj.id, vm_obj.instance_id, simgval.get_sim_code_str(vm_obj.status)))
clib.logwrite_vm_obj(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), vm_obj)
clib.sim_exit()
vm_cost, billing_hour = get_vm_cost_and_billing_hour (vm_running_time, vm_obj.unit_price)
simobj.set_VM_cost (vm_obj, vm_cost)
g_log_handler.info("[IaaS____] %s BILLING COST for VM (ID:%d, TY:%s, PR:$%s, ST:%s, TC:%d, TT:%d) is $%f" \
% (str(Global_Curr_Sim_Clock), vm_obj.id, vm_obj.type_name, vm_obj.unit_price, simgval.get_sim_code_str(vm_obj.status), vm_obj.time_create, vm_obj.time_terminate,vm_obj.cost))
g_log_handler.info("[IaaS____] %s BILLING COST ($%f) = BILL_HR(%d) * VM_PRICE(%f), BILL_HR = RNDUP(VM_RT(%d) / BTU(%d)" % (str(Global_Curr_Sim_Clock), vm_obj.cost, billing_hour, vm_obj.unit_price, vm_running_time, g_sim_conf.billing_unit_clock))
def get_vm_index_from_List (list_type, ref_vm):
return_val = -1 # cannot find index
if list_type == 1:
global g_Creating_VMs
global g_Creating_VMs_CV
g_Creating_VMs_CV.acquire()
for vm in g_Creating_VMs:
if vm.id == ref_vm.id and vm.time_create == ref_vm.time_create and vm.time_active is None:
return_val = g_Creating_VMs.index(vm)
break
g_Creating_VMs_CV.notify()
g_Creating_VMs_CV.release()
elif list_type == 2:
global g_Running_VMs
global g_Running_VMs_CV
g_Running_VMs_CV.acquire()
for vm in g_Running_VMs:
if vm.id == ref_vm.id and vm.time_create == ref_vm.time_create and vm.time_active == ref_vm.time_active and vm.startup_lag == ref_vm.startup_lag:
return_val = g_Running_VMs.index(vm)
break
g_Running_VMs_CV.notify()
g_Running_VMs_CV.release()
elif list_type == 3:
global g_Stopped_VMs
global g_Stopped_VMs_CV
g_Stopped_VMs_CV.acquire()
for vm in g_Stopped_VMs:
if vm.id == ref_vm.id and vm.time_create == ref_vm.time_create and vm.time_active == ref_vm.time_active and vm.startup_lag == ref_vm.startup_lag and vm.time_terminate == ref_vm.time_terminate:
return_val = g_Stopped_VMs.index(vm)
break
g_Stopped_VMs_CV.notify()
g_Stopped_VMs_CV.release()
else:
g_log_handler.error("[IaaS____] %s get_vm_index_from_List Error! => Invalid List Type (%s) for VM Instance (ID:%d, IID:%s, TY:%s, PR:$%s, TC:%d)" \
% (str(Global_Curr_Sim_Clock), str(list_type), ref_vm.id, ref_vm.instance_id, ref_vm.type_name, ref_vm.unit_price, ref_vm.time_create))
return return_val
def insert_VM_into_Creating_VMs (vm):
global g_Creating_VMs
global g_Creating_VMs_CV
vm_index = get_vm_index_from_List (1, vm)
if vm_index == -1: # same vm not found
g_Creating_VMs_CV.acquire()
g_Creating_VMs.append (vm)
vm_index = g_Creating_VMs.index (vm)
g_Creating_VMs_CV.notify()
g_Creating_VMs_CV.release()
else:
g_log_handler.error("[IaaS____] %s insert_VM_into_Creating_VMs Error! => Same VM Instance (ID:%d, IID:%s, TY:%s, PR:$%s, TC:%d) exists in g_Creating_VMs[%d]" \
% (str(Global_Curr_Sim_Clock), vm.id, vm.instance_id, vm.type_name, vm.unit_price, vm.time_create, vm_index))
clib.sim_exit()
#clib.logwrite_VM_Instance_Lists(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), "[INS] iaas_cloud.Creating_VMs", g_Creating_VMs)
return vm_index
def extract_VM_from_Creating_VM_List (vm):
global g_Creating_VMs
global g_Creating_VMs_CV
ret_val = None
vm_index = get_vm_index_from_List (1, vm)
if vm_index > -1:
g_Creating_VMs_CV.acquire()
vm_obj = g_Creating_VMs.pop(vm_index)
ret_val = vm_obj
g_Creating_VMs_CV.notify()
g_Creating_VMs_CV.release()
if vm_index == -1:
g_log_handler.error("[IaaS____] %s extract_VM_from_Creating_VM_List Error! => VM Instance (ID:%d, IID:%s, TY:%s, PR:$%s, TC:%d, TA:%d) is NOT existed in g_Creating_VMs" \
% (str(Global_Curr_Sim_Clock), vm.id, vm.instance_id, vm.type_name, vm.unit_price, vm.time_create, vm.time_active))
clib.sim_exit()
#clib.logwrite_VM_Instance_Lists(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), "[EXT] iaas_cloud.Creating_VMs", g_Creating_VMs)
return ret_val
def insert_VM_into_Running_VMs (vm):
global g_Running_VMs
global g_Running_VMs_CV
vm_index = get_vm_index_from_List (2, vm)
if vm_index == -1: # same vm not found -> ok insert
g_Running_VMs_CV.acquire()
g_Running_VMs.append (vm)
vm_index = g_Running_VMs.index (vm)
g_Running_VMs_CV.notify()
g_Running_VMs_CV.release()
else:
g_log_handler.error("[IaaS____] %s insert_VM_into_Running_VMs Error! => Same VM Instance (ID:%d, IID:%s, TY:%s, PR:$%s, TC:%s, TA:%s, SD:%s) exists in g_Creating_VMs[%d]" \
% (str(Global_Curr_Sim_Clock), vm.id, vm.instance_id, vm.type_name, vm.unit_price, str(vm.time_create), str(vm.time_active), str(vm.startup_lag), vm_index))
clib.sim_exit()
#clib.logwrite_VM_Instance_Lists(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), "[INS] iaas_cloud.g_Running_VMs", g_Running_VMs)
return vm_index
def extract_VM_from_Running_VM_List (vm):
global g_Running_VMs
global g_Running_VMs_CV
ret_val = None
vm_index = get_vm_index_from_List (2, vm)
if vm_index > -1:
g_Running_VMs_CV.acquire()
vm_obj = g_Running_VMs.pop(vm_index)
ret_val = vm_obj
g_Running_VMs_CV.notify()
g_Running_VMs_CV.release()
if vm_index == -1:
g_log_handler.error("[IaaS____] %s extract_VM_from_Running_VM_List Error! => VM Instance (ID:%d, IID:%s, TY:%s, PR:$%s, TC:%d, TE:%d) is NOT existed in g_Running_VMs" \
% (str(Global_Curr_Sim_Clock), vm.id, vm.instance_id, vm.type_name, vm.unit_price, vm.time_create, vm.time_terminate))
clib.sim_exit()
#clib.logwrite_VM_Instance_Lists(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), "[EXT] iaas_cloud.Running_VMs", g_Running_VMs)
return ret_val
def insert_VM_into_Stopped_VMs (vm):
global g_Stopped_VMs
global g_Stopped_VMs_CV
vm_index = get_vm_index_from_List (3, vm)
if vm_index == -1: # same vm not found -> ok insert
g_Stopped_VMs_CV.acquire()
g_Stopped_VMs.append (vm)
vm_index = g_Stopped_VMs.index (vm)
g_Stopped_VMs_CV.notify()
g_Stopped_VMs_CV.release()
else:
g_log_handler.error("[IaaS____] %s insert_VM_into_Stopped_VMs Error! => Same VM Instance (ID:%s, IID:%s, TY:%s, PR:$%s, TC:%s, TA:%s, TT:%s, SD:%s) exists in g_Creating_VMs[%s]" \
% (str(Global_Curr_Sim_Clock), vm.id, vm.instance_id, vm.type_name, vm.unit_price, str(vm.time_create), str(vm.time_active), str(vm.time_terminate), str(vm.startup_lag), vm_index))
clib.sim_exit()
#clib.logwrite_VM_Instance_Lists(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), "[INS] iaas_cloud.g_Stopped_VMs", g_Stopped_VMs)
return vm_index
# this is added for storage simulation especially for processing finer grained job model
def get_job_sub_runtime (job_obj, job_sub_exec_code):
job_sub_runtime = 0
if job_sub_exec_code == simgval.gEVT_SUB_JOB_IN_CPU_OP_COMPLETE:
if job_obj.use_input_file == True:
job_sub_runtime += job_obj.act_nettime_on_VM[0]
job_sub_runtime += job_obj.act_cputime_on_VM
elif job_sub_exec_code == simgval.gEVT_SUB_JOB_OUT_OP_COMPLETE:
if job_obj.use_output_file == True:
job_sub_runtime += job_obj.act_nettime_on_VM[1]
else:
clib.sim_exit()
elif job_sub_exec_code == simgval.gEVT_SUB_JOB_FAILED:
if job_obj.job_failure_recovered == False:
if job_obj.use_input_file == True:
job_sub_runtime += job_obj.act_nettime_on_VM[0]
job_sub_runtime += job_obj.vm_job_failure_time
else:
clib.sim_exit()
else:
job_str = simobj.get_job_info_str(job_obj)
g_log_handler.error("[IaaS____] %s get_job_sub_runtime Error! => Invalid Job Sub Exec Code (%s) for %s" \
% (str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(job_sub_exec_code), job_str))
clib.sim_exit()
return job_sub_runtime
def get_VM_type_obj_by_type_name (type_name):
for vm_type in g_sim_conf.vm_types:
if vm_type.vm_type_name == type_name:
return vm_type
curframe = inspect.currentframe()
callframe = inspect.getouterframes(curframe, 2)
callfile = callframe[1][1].split("/")[-1]
g_log_handler.error("[IaaS____] %s get_VM_type_obj_by_type_name Error! - Cannot find VM type obj for %s type VM - Caller:%s, %s:%d" %(str(Global_Curr_Sim_Clock), type_name, callframe[1][3], callfile, callframe[1][2]))
clib.sim_exit()
#########################################################################################
#
# related to cloud storage
#
#########################################################################################
def get_kb_storage_max_capacity ():
return g_sim_conf.max_capacity_of_storage * 1024
def get_max_num_of_storage_containers():
return g_sim_conf.max_num_of_storage_containers
def get_currently_completed_cloud_storage_capacity ():
total_storage_capacity = 0 # unit kb
for s in g_Activated_Storage_Containers:
storage_container_capacity = 0
for sc_file_object in s.sc_file_objects:
if sc_file_object.sfo_status == simgval.gSFO_ST_FILE_UPLOAD_COMPLETED:
storage_container_capacity += sc_file_object.sfo_size
total_storage_capacity += storage_container_capacity
return total_storage_capacity
def get_current_cloud_storage_capacity ():
total_storage_capacity = 0 # unit kb
for s in g_Activated_Storage_Containers:
storage_container_capacity = 0
for sc_file_object in s.sc_file_objects:
if sc_file_object.sfo_status == simgval.gSFO_ST_FILE_UPLOAD_START or sc_file_object.sfo_status == simgval.gSFO_ST_FILE_UPLOAD_COMPLETED:
storage_container_capacity += sc_file_object.sfo_size
if storage_container_capacity == s.sc_usage_volume:
total_storage_capacity += storage_container_capacity
else:
g_log_handler.error("[IaaS____] %s get_current_cloud_storage_capacity Error! - Storage Container (ID:%s, CR:%s, ST:%s, CT:%s, PR:$%s) Volume Mismatch - SC (%s kb) vs CAL (%s kb)" \
% (str(Global_Curr_Sim_Clock), s.sc_id, s.sc_creator, simgval.get_sim_code_str(s.sc_status), s.sc_create_time, s.sc_usage_price, s.sc_usage_volume, storage_container_capacity))
clib.sim_exit()
return total_storage_capacity
def get_total_number_of_current_SFOs ():
num_of_sfos = 0
for s in g_Activated_Storage_Containers:
num_of_sfos += len(s.sc_file_objects)
return num_of_sfos
def get_total_num_of_active_storage_containers ():
return len(g_Activated_Storage_Containers)
def create_storage_container (creator, region):
new_sc_uid = simobj.generate_SC_UID ()
new_sc_obj = simobj.Storage_Container (new_sc_uid, creator, region, Global_Curr_Sim_Clock)
new_sc_obj.set_sc_permission (simgval.gSC_PERMISSION_PUBLIC)
# insert SC into g_activated cloud storage list
insert_SC_into_Activated_Storage_Containers (new_sc_obj)
return new_sc_obj
def get_most_recent_storage_container ():
return g_Activated_Storage_Containers[-1]
def insert_SC_into_Activated_Storage_Containers (sc_obj):
global g_Activated_Storage_Containers
global g_Activated_Storage_Containers_CV
existing_sc_index = get_Index_from_activated_storage_container (sc_obj.sc_id)
if existing_sc_index < 0:
g_Activated_Storage_Containers_CV.acquire()
g_Activated_Storage_Containers.append (sc_obj)
g_Activated_Storage_Containers_CV.notify()
g_Activated_Storage_Containers_CV.release()
else:
existing_sco = g_Activated_Storage_Containers[existing_sc_index]
g_log_handler.error("[IaaS____] %s insert_SC_into_Activated_Storage_Containers Error! => Same SC Obj %s exists in g_Activated_Storage_Containers [%d]" % (str(Global_Curr_Sim_Clock), existing_sco.get_infos(), existing_sc_index))
clib.sim_exit()
def get_Index_from_activated_storage_container (sc_id):
global g_Activated_Storage_Containers
for sc in g_Activated_Storage_Containers:
if sc.sc_id == sc_id:
return g_Activated_Storage_Containers.index (sc)
return -1
def get_SC_from_activated_storage_container (sc_id):
global g_Activated_Storage_Containers
sc_obj = None
for sc in g_Activated_Storage_Containers:
if sc.sc_id == sc_id and sc.sc_status == simgval.gSC_ST_CREATE:
return sc
return sc_obj
def get_SFO_from_storage_container (sc_obj, sfo_id):
for sfo in sc_obj.sc_file_objects:
if sfo.sfo_id == sfo_id:
return sfo
return None
def error_check_for_data_update_to_cloud_storage (job_obj):
job_str = simobj.get_job_info_str(job_obj)
# job status must be "Started"
if job_obj.status != simgval.gJOB_ST_STARTED:
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - Job Status Error - %s" % (str(Global_Curr_Sim_Clock), job_str))
clib.sim_exit()
# job's use_out_file must TRUE
if job_obj.use_output_file != True:
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - %s not having output file to store in Cloud Storage - use_output_file (%s)" % (str(Global_Curr_Sim_Clock), job_str, job_obj.use_output_file))
clib.sim_exit()
# job's output_file_flow_direction must gOFTD_IC (OUTPUT FILE DIRECTION IN-CLOUD)
if job_obj.output_file_flow_direction != simgval.gOFTD_IC:
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - %s output file shouldn't be stored in Cloud Storage - output_file_flow_direction (%s)" % (str(Global_Curr_Sim_Clock), job_str, simgval.get_sim_code_str(job_obj.output_file_flow_direction)))
clib.sim_exit()
# job's output_file_size should be > 0 kb
if job_obj.output_file_size <= 0:
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - %s output file size error! - output_file_size (%s KB)" % (str(Global_Curr_Sim_Clock), job_str, job_obj.output_file_size))
clib.sim_exit()
curr_vm_type = get_VM_type_obj_by_type_name (job_obj.VM_type)
data_transfer_duration = broker.cal_actual_network_duration_on_VMs (curr_vm_type.vm_type_net_factor, simgval.gJOBFILE_OUTPUT, job_obj.output_file_flow_direction, job_obj.output_file_size)
if job_obj.act_nettime_on_VM[1] != data_transfer_duration:
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - %s Data Transfer Time Mismatch! - job(%s sec) vs. cal(%s sec)" \
%(str(Global_Curr_Sim_Clock), job_str, job_obj.act_nettime_on_VM[1], data_transfer_duration))
clib.sim_exit()
# storage simulation libs - created on 10/26/2014
def upload_data_to_cloud_storage (job_obj):
job_str = simobj.get_job_info_str(job_obj)
# this is for error detection.
error_check_for_data_update_to_cloud_storage (job_obj)
curr_cloud_storage_capacity = get_current_cloud_storage_capacity ()
max_cloud_storage_capacity = get_kb_storage_max_capacity ()
max_cap_of_sc = get_max_num_of_storage_containers()
# storage_container
storage_container = None
data_transfer_size = 0
sfo_data_status = None
if max_cloud_storage_capacity >= curr_cloud_storage_capacity + job_obj.output_file_size:
data_transfer_size = job_obj.output_file_size
else:
data_transfer_size = max_cloud_storage_capacity - curr_cloud_storage_capacity
if data_transfer_size > 0:
num_act_containers = get_total_num_of_active_storage_containers ()
if num_act_containers > max_cap_of_sc:
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - Current Num of Act Storage Containers (%d) exceeds Max Cap of Storage Containers (%d) - Job Infos:%s" % (str(Global_Curr_Sim_Clock), num_act_containers, max_cap_sc, job_str))
clib.sim_exit()
if num_act_containers == 0:
# create new one
storage_container = create_storage_container (job_obj.id, None)
else:
# use most recent one
storage_container = get_most_recent_storage_container ()
if storage_container is None:
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - Cannot find/create a proper Storage Container for %s" \
%(str(Global_Curr_Sim_Clock), job_str))
clib.sim_exit()
sfo_id = storage_container.upload_file_start (job_obj.id, Global_Curr_Sim_Clock, data_transfer_size, job_obj.output_file_size)
return data_transfer_size, storage_container.sc_id, sfo_id
elif data_transfer_size == 0:
# no capacity remained.
data_transfer_size = 0
return data_transfer_size, None, None
else:
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - Current Storage Volume exceeds (%s KB) Max Capacity of Storage (%s KB)" % (str(Global_Curr_Sim_Clock), curr_cloud_storage_capacity, max_cloud_storage_capacity))
clib.sim_exit()
def proc_file_transfer_to_cloud_storage (job_obj):
job_str = simobj.get_job_info_str(job_obj)
# temp validation - this might be changed at some point. 10/27/2014
# =================================================================================================================================
if job_obj.use_output_file is not True or job_obj.output_file_flow_direction != simgval.gOFTD_IC or job_obj.output_file_size <= 0:
job_str = simobj.get_job_info_str(job_obj)
g_log_handler.error("[IaaS____] %s upload_data_to_cloud_storage Error! - Invalid Job Object (%s) for File Transfer: UOF (%s), OFFD (%s), OFS (%s KB)" % (str(Global_Curr_Sim_Clock), job_str, job_obj.use_output_file, job_obj.output_file_flow_direction, job_obj.output_file_size))
clib.sim_exit()
# =================================================================================================================================
data_transfer_size, sc_id, sfo_id = upload_data_to_cloud_storage (job_obj)
full_data_transfer = True
if data_transfer_size == job_obj.output_file_size:
q_ft_tmr_time = job_obj.act_nettime_on_VM[1]
simobj.set_Job_output_file_transfer_infos (job_obj, simgval.gSFO_DST_FULL_UPLOAD, job_obj.output_file_size)
full_data_transfer = True
elif data_transfer_size < job_obj.output_file_size:
curr_vm_type = get_VM_type_obj_by_type_name (job_obj.VM_type)
q_ft_tmr_time = broker.cal_actual_network_duration_on_VMs (curr_vm_type.vm_type_net_factor, simgval.gJOBFILE_OUTPUT, job_obj.output_file_flow_direction, data_transfer_size)
updated_act_nettime_on_VM = [job_obj.act_nettime_on_VM[0], q_ft_tmr_time]
simobj.set_Job_act_nettime_on_VM (job_obj, updated_act_nettime_on_VM)
simobj.set_Job_output_file_transfer_infos (job_obj, simgval.gSFO_DST_PARTIAL_UPLOAD, data_transfer_size)
simobj.adjust_Job_act_duration_on_VM (job_obj)
# full data transfer is False because data_transfer_size < job_obj.output_file_size:
full_data_transfer = False
else:
g_log_handler.error ("[IaaS____] %s proc_file_transfer_to_cloud_storage Error! - Data Transfer Size (%s KB) to Cloud Storage exceeds File Size (%s KB) - %s" \
%(str(Global_Curr_Sim_Clock), data_transfer_size, job_obj.output_file_size, job_str))
clib.sim_exit()
# file transfer completion timer.
if q_ft_tmr_time > 0:
q_ft_tmr_evt_data = [sc_id, sfo_id, job_obj.id, job_obj.output_file_transfer_size]
q_ft_tmr_evt = clib.make_queue_event ( q_ft_tmr_time, # timer (job actual duration)
g_my_block_id, # src block id (will be returned to src block)
simgval.gBLOCK_SIM_EVENT_HANDLER, # dst block id (event handler block)
simgval.gEVT_SET_TIMER, # evt code: set timer
simgval.gEVT_SUB_FILE_TRANSFER_COMPLETE_CLOUD_STORAGE,
q_ft_tmr_evt_data)
gQ_OUT.put (q_ft_tmr_evt)
return True if data_transfer_size > 0 else False, full_data_transfer
# related to Event Processing - this execute job until cpu_duration
def iaas_evt_start_job_processing (q_msg):
# need to think of basic procedure of this ...
# [a] read the first job from vm's job queue
# [b] assign the job to vm's current job
# [c] job status change to gJOB_ST_STARTED
# [d] assign current clock to job's time_start
# [e] read duration and register timer event
# [f] send ack to event handler
evt_id = q_msg[0] # EVT_ID
evt_code = q_msg[4] # EVT_CODE
evt_data = q_msg[6] # EVT_DATA
if evt_code != simgval.gEVT_START_JOB:
g_log_handler.error("[IaaS____] %s EVT(%s) ERROR! - INVALID EVT_CODE (%s) => %s " \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(simgval.gEVT_START_JOB), simgval.get_sim_code_str(evt_code), q_msg))
clib.sim_exit()
job_id = evt_data[0]
vm = evt_data[1]
# vm can process a new job if its status is active
if vm.status != simgval.gVM_ST_ACTIVE:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - VM(%d)'s Status (%s) is NOT Active! => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, simgval.get_sim_code_str(vm.status), q_msg))
clib.sim_exit()
# vm can process a new job if its current job is None
if vm.current_job is not None:
if vm.current_job.id == job_id and vm.current_job.status == simgval.gJOB_ST_STARTED:
# this is error case that iaas gets duplicated events from broker but ok
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - VM(%d) gets Duplicated %s Start Event! => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, simobj.get_job_info_str(vm.current_job), q_msg))
clib.logwrite_vm_obj(g_log_handler, Global_Curr_Sim_Clock, vm)
# clib.sim_long_sleep()
# ttt need to be improved
clib.sim_exit()
# [f] send ack event to event handler
iaas_send_evt_ack(evt_id, evt_code)
return
else:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - VM(%d) has current Running %s! => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, simobj.get_job_info_str(vm.current_job), q_msg))
clib.sim_exit()
# [a] read the first job from vm's job queue
job = simobj.get_job_from_VM_job_queue (vm, job_id)
if job is None:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - VM (ID:%d) Job Queue has been corrupted! => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, vm.job_queue))
clib.logwrite_vm_obj(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), vm)
clib.sim_exit()
# [b] assign the job to vm's current job
upd_res = simobj.set_VM_current_job (Global_Curr_Sim_Clock, vm, job)
if upd_res is False:
clib.display_vm_obj(Global_Curr_Sim_Clock, vm)
clib.display_job_obj(Global_Curr_Sim_Clock, job)
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - VM (ID:%d) has current running job (%d, %s, %d, %d)!" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, vm.current_job.id, vm.current_job.name, vm.current_job.act_duration_on_VM, vm.current_job.deadline))
clib.sim_exit()
# [c] job status change to gJOB_ST_STARTED
simobj.set_Job_status (job, simgval.gJOB_ST_STARTED)
# [d] assign current clock to job's time_start
simobj.set_Job_time_start (job, Global_Curr_Sim_Clock)
job_str = simobj.get_job_info_str(job)
print "[IaaS____] %s Actual Duration for %s on VM (%d, %s, $%s, %s) : %d (CPU:%s, I/O:%s) sec." \
% (str(Global_Curr_Sim_Clock), job_str, vm.id, vm.type_name, vm.unit_price, simgval.get_sim_code_str(vm.status), job.act_duration_on_VM, job.act_cputime_on_VM, job.act_nettime_on_VM)
# [e] read duration and register timer event
q_tmr_evt_data = [job.id]
q_tmr_evt_data.append(vm)
if job.job_failure_recovered == True:
q_tmr_evt_sub_code = simgval.gEVT_SUB_JOB_IN_CPU_OP_COMPLETE
q_tmr_time = get_job_sub_runtime (job, q_tmr_evt_sub_code)
q_tmr_evt = clib.make_queue_event (q_tmr_time, # timer (job actual duration)
g_my_block_id, # src block id (will be returned to src block)
simgval.gBLOCK_SIM_EVENT_HANDLER, # dst block id (event handler block)
simgval.gEVT_SET_TIMER, # evt code: set timer
q_tmr_evt_sub_code, # evt sub code: job exec complete
q_tmr_evt_data) # event data = [job_id, vm_obj]
else:
# job failure happens
q_tmr_evt_sub_code = simgval.gEVT_SUB_JOB_FAILED
q_tmr_time = get_job_sub_runtime (job, q_tmr_evt_sub_code)
q_tmr_evt = clib.make_queue_event (q_tmr_time, # timer (job actual duration)
g_my_block_id, # src block id (will be returned to src block)
simgval.gBLOCK_SIM_EVENT_HANDLER, # dst block id (event handler block)
simgval.gEVT_SET_TIMER, # evt code: set timer
q_tmr_evt_sub_code, # evt sub code: job exec complete
q_tmr_evt_data) # event data = [job_id, vm_obj]
# send timer event to event handler
gQ_OUT.put (q_tmr_evt)
# [f] send ack event to event handler
iaas_send_evt_ack(evt_id, evt_code)
# this lib is related to restart failured job.
def iaas_evt_restart_job_processing (q_msg):
# need to think of basic procedure of this ...
# [a] read the first job from vm's job queue
# [b] assign the job to vm's current job
# [c] job status change to gJOB_ST_STARTED
# [d] assign current clock to job's time_start
# [e] read duration and register timer event
# [f] send ack to event handler
evt_id = q_msg[0] # EVT_ID
evt_code = q_msg[4] # EVT_CODE
evt_data = q_msg[6] # EVT_DATA
if evt_code != simgval.gEVT_RESTART_JOB:
g_log_handler.error("[IaaS____] %s EVT(%s) ERROR! - INVALID EVT_CODE (%s) => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(simgval.gEVT_START_JOB), simgval.get_sim_code_str(evt_code), q_msg))
clib.sim_exit()
job_id = evt_data[0]
vm = evt_data[1]
# vm can process a new job if its status is active
if vm.status != simgval.gVM_ST_ACTIVE:
g_log_handler.error ("[IaaS____] %s EVT (%s) ERROR! - VM(%d)'s Status (%s) is NOT Active! => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, simgval.get_sim_code_str(vm.status), q_msg))
clib.sim_exit()
# vm can process a new job if its current job is None
if vm.current_job is None:
# this is error case that iaas gets duplicated events from broker but ok
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - VM(%d) should HAVE current_job! => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, q_msg))
clib.logwrite_vm_obj(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), vm)
clib.sim_exit()
else: # vm.current_job is not None
if vm.current_job.id != job_id or vm.current_job.status != simgval.gJOB_ST_FAILED or vm.current_job.job_failure_recovered is not True:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - VM(%d)'s Current JobID (CURR:%s, INPUT:%s) Mismatch or Status (%s) Error or Recovery Status (%s) Error! => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, vm.current_job.id, job_id, simgval.get_sim_code_str(vm.current_job.status), simgval.get_sim_code_str(vm.current_job.job_failure_recovered), q_msg))
clib.logwrite_vm_obj(g_log_handler, "[IaaS____] " + str(Global_Curr_Sim_Clock), vm)
clib.sim_exit()
curr_job = vm.current_job
# [c] job status change to gJOB_ST_STARTED
simobj.set_Job_status (curr_job, simgval.gJOB_ST_STARTED)
# start time shouldn't be updated
# [d] assign current clock to job's time_start
simobj.set_Job_time_start (curr_job, Global_Curr_Sim_Clock)
job_str = simobj.get_job_info_str(curr_job)
print "[IaaS____] %s Actual Duration for %s on VM (%d, %s, $%s, %s) : %d (CPU:%s, I/O:%s) sec." \
% (str(Global_Curr_Sim_Clock), job_str, vm.id, vm.type_name, vm.unit_price, simgval.get_sim_code_str(vm.status), curr_job.act_duration_on_VM, curr_job.act_cputime_on_VM, curr_job.act_nettime_on_VM)
"""
clib.display_job_obj(Global_Curr_Sim_Clock, curr_job)
clib.sim_exit()
"""
# [e] read duration and register timer event
q_tmr_evt_data = [curr_job.id]
q_tmr_evt_data.append(vm)
q_tmr_evt_sub_code = simgval.gEVT_SUB_JOB_IN_CPU_OP_COMPLETE
q_tmr_time = get_job_sub_runtime (curr_job, q_tmr_evt_sub_code)
q_tmr_evt = clib.make_queue_event (q_tmr_time, # timer (job actual duration)
g_my_block_id, # src block id (will be returned to src block)
simgval.gBLOCK_SIM_EVENT_HANDLER, # dst block id (event handler block)
simgval.gEVT_SET_TIMER, # evt code: set timer
q_tmr_evt_sub_code, # evt sub code: job exec complete
q_tmr_evt_data) # event data = [job_id, vm_obj]
# send timer event to event handler
gQ_OUT.put (q_tmr_evt)
# [f] send ack event to event handler
iaas_send_evt_ack(evt_id, evt_code)
def iaas_evt_create_vm_processing (q_msg):
evt_id = q_msg[0] # EVT_ID
evt_src = q_msg[2] # EVT_SRC
evt_code = q_msg[4] # EVT_CODE
evt_sub_code = q_msg[5] # EVT_SUB_CODE
evt_data = q_msg[6] # EVT_DATA
if evt_code != simgval.gEVT_CREATE_VM:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - INVALID EVT_CODE (%s) => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(simgval.gEVT_CREATE_VM), simgval.get_sim_code_str(evt_code), q_msg))
clib.sim_exit()
evt_real_src = evt_sub_code
vm = evt_data[0]
if evt_real_src != simgval.gBLOCK_BROKER:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - EVT_REAL_SRC (%s) Should be BLOCK_BROKER => %s" \
% (str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), simgval.get_sim_code_str(evt_real_src), q_msg))
clib.sim_exit()
g_log_handler.info("[IaaS____] %s %s: ID:%d, IID:%s, TY:%s, PR:$%s, ST:%s, TC:%s, TA:%s" \
% (str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, vm.instance_id, vm.type_name, vm.unit_price, simgval.get_sim_code_str(vm.status), str(vm.time_create), str(vm.time_active)))
if vm.status != simgval.gVM_ST_CREATING or vm.time_active is not None:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - VM Data Corrupted - Discard Event" % (str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code)))
clib.sim_exit()
startup_lag = calculate_Startup_Lag()
print "[IaaS____] %s Startup Lag for VM (%d, %s, $%s, %s) : %d sec." \
% (str(Global_Curr_Sim_Clock), vm.id, vm.type_name, vm.unit_price, simgval.get_sim_code_str(vm.status), startup_lag)
# insert vm obj into creating vm list.
insert_result = insert_VM_into_Creating_VMs (vm)
if insert_result > -1:
# insert success
# create timer event since insert into creating vm list --> send timer event for startup lag
q_tmr_evt_data = [startup_lag]
q_tmr_evt_data.append(vm)
q_tmr_evt = clib.make_queue_event (startup_lag, # timer (startup time)
g_my_block_id, # src block id (will be returned to src block)
simgval.gBLOCK_SIM_EVENT_HANDLER, # dst block id (event handler block)
simgval.gEVT_SET_TIMER, # evt code: set timer
simgval.gEVT_SUB_VM_STARTUP_COMPLETE,# evt sub code: startup completed
q_tmr_evt_data) # event data = [startup lag, vm_obj]
# send timer event to event handler
gQ_OUT.put (q_tmr_evt)
else:
g_log_handler.error("[IaaS____] %s insert_VM_into_Creating_VMs Error! - res_code: %d" % (str(Global_Curr_Sim_Clock), insert_result))
clib.sim_exit()
# send ack event to event handler
iaas_send_evt_ack(evt_id, evt_code)
return
def iaas_evt_terminate_vm_processing (q_msg):
evt_id = q_msg[0] # EVT_ID
evt_src = q_msg[2] # EVT_SRC
evt_code = q_msg[4] # EVT_CODE
evt_sub_code = q_msg[5] # EVT_SUB_CODE
evt_data = q_msg[6] # EVT_DATA
if evt_code != simgval.gEVT_TERMINATE_VM:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - INVALID EVT_CODE (%s) => %s" \
%(str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(simgval.gEVT_TERMINATE_VM), simgval.get_sim_code_str(evt_code), q_msg))
clib.sim_exit()
evt_real_src = evt_sub_code
ref_vm = evt_data[0]
if evt_real_src != simgval.gBLOCK_BROKER:
g_log_handler.error("[IaaS____] %s EVT (%s) ERROR! - EVT_REAL_SRC (%s) Should be BLOCK_BROKER => %s" \
% (str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), simgval.get_sim_code_str(evt_real_src), q_msg))
clib.sim_exit()
# procedure for gEVT_TERMINATE_VM processing
# [a] find index from g_Running_VMs
# [b] extract from g_Running_VMs
# [b-1] check valid termination of vm
# [c] insert vm into g_Stopped_VMs
# [d] cost calculate - price and billing_time_unit, billing_time_period
# [e] write log -- vm info
# [f] sim entity term check
# [g] send ack
# [a] find index from g_Running_VMs
#clib.display_vm_obj(Global_Curr_Sim_Clock, ref_vm)
#clib.display_VM_Instances_List(Global_Curr_Sim_Clock, "BEFORE-Running", g_Running_VMs)
#clib.display_VM_Instances_List(Global_Curr_Sim_Clock, "BEFORE-Stopped", g_Stopped_VMs)
# [b] extract from g_Running_VMs
vm = extract_VM_from_Running_VM_List (ref_vm)
# [b-1] check valid termination of vm
# condition "or len (vm.job_history) < 1" is removed from the termination condition - on 11/07/2014 -- this is not relevant to now due to job failure model
if vm.status != simgval.gVM_ST_TERMINATE or vm.time_terminate is None or vm.current_job is not None or vm.current_job_start_clock != -1 or len (vm.job_queue) > 0 or vm.cost != 0:
g_log_handler.error("[IaaS____] %s %s Processing Error => Invalid VM Termination: ID:%d, IID:%s, ST:%s, TC:%s, TA:%s, TT:%s, CT:%f, CJ:%s, CJST:%d, LEN(JQ):%d, LEN(JH):%d" \
% (str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_sub_code), vm.id, vm.instance_id, simgval.get_sim_code_str(vm.status), vm.time_create, vm.time_active, vm.time_terminate, vm.cost, str(vm.current_job), vm.current_job_start_clock, len(vm.job_queue), len(vm.job_history)))
clib.sim_exit()
# [c] insert vm into g_Stopped_VMs
insert_VM_into_Stopped_VMs (vm)
g_log_handler.info("[IaaS____] %s %s: ID:%d, IID:%s, TY:%s, PR:$%s, ST:%s, TC:%s, TA:%s, TE:%s" \
% (str(Global_Curr_Sim_Clock), simgval.get_sim_code_str(evt_code), vm.id, vm.instance_id, vm.type_name, vm.unit_price, simgval.get_sim_code_str(vm.status), str(vm.time_create), str(vm.time_active), str(vm.time_terminate)))
# [d] cost calculate - price and billing_time_unit, billing_time_period
calculate_vm_cost (vm)
# [e] write log -- vm info
write_vm_usage_log (vm)
# [f] iaas sim entity term check
iaas_check_sim_entity_termination()
# [g] send ack
iaas_send_evt_ack(evt_id, evt_code)