-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_function.py
More file actions
685 lines (608 loc) · 36.8 KB
/
simulation_function.py
File metadata and controls
685 lines (608 loc) · 36.8 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
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
try:
import sys
except ImportError:
print("Error: Package 'sys' not found or failed to import.")
try:
import subprocess
except ImportError:
print("Error: Package 'subprocess' not found or failed to import.")
import os
import signal
import psutil
import re
directories = [
'stamp-master/bayes',
'stamp-master/yada',
'stamp-master/intruder',
'stamp-master/vacation',
'stamp-master/kmeans',
'stamp-master/labyrinth',
'stamp-master/genome',
'stamp-master/ssca2'
]
def parse_simulation_times():
i = 0
simulation_times = 1
while i < len(sys.argv):
if sys.argv[i] == "-simulation_times":
i += 1
simulation_times = int(sys.argv[i])
break
i += 1
return simulation_times if (simulation_times != 1) else 1
def parse_threads_list():
threads = []
i = 0
while i < len(sys.argv):
if sys.argv[i] == "-thread":
i += 1
while i < len(sys.argv) and not sys.argv[i].startswith("-"):
threads.append(int(sys.argv[i]))
i += 1
threads = [thread for thread in threads if (thread & (thread - 1)) == 0]
break
i += 1
return threads if threads else [16]
def reset_makefile_config_to_suicide():
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# DEFINES += -DCM=CM_SUICIDE','DEFINES += -DCM=CM_SUICIDE') \
.replace('# DEFINES += -DCM=CM_MODULAR','DEFINES += -DCM=CM_MODULAR') \
.replace('# DEFINES += -DSWITCH_STM','DEFINES += -DSWITCH_STM') \
.replace('# DEFINES += -USWITCH_STM','DEFINES += -USWITCH_STM') \
.replace('# DEFINES += -DCONTENTION_INTENSITY','DEFINES += -DCONTENTION_INTENSITY') \
.replace('# DEFINES += -UCONTENTION_INTENSITY','DEFINES += -UCONTENTION_INTENSITY') \
.replace('# DEFINES += -DCM_POLKA','DEFINES += -DCM_POLKA') \
.replace('# DEFINES += -UCM_POLKA','DEFINES += -UCM_POLKA') \
.replace('# DEFINES += -DSHRINK_ENABLE','DEFINES += -DSHRINK_ENABLE') \
.replace('# DEFINES += -USHRINK_ENABLE','DEFINES += -USHRINK_ENABLE') \
.replace('# DEFINES += -DATS_ENABLE','DEFINES += -DATS_ENABLE') \
.replace('# DEFINES += -UATS_ENABLE','DEFINES += -UATS_ENABLE')
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('DEFINES += -DCM=CM_SUICIDE','# DEFINES += -DCM=CM_SUICIDE') \
.replace('DEFINES += -DCM=CM_MODULAR','# DEFINES += -DCM=CM_MODULAR') \
.replace('DEFINES += -DSWITCH_STM','# DEFINES += -DSWITCH_STM') \
.replace('DEFINES += -DCONTENTION_INTENSITY','# DEFINES += -DCONTENTION_INTENSITY') \
.replace('DEFINES += -DCM_POLKA','# DEFINES += -DCM_POLKA') \
.replace('DEFINES += -DSHRINK_ENABLE','# DEFINES += -DSHRINK_ENABLE') \
.replace('DEFINES += -DATS_ENABLE','# DEFINES += -DATS_ENABLE')
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
def reset_makefile_stm_config_to_suicide():
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -I$(STM)/libaco -I$(STM)/src -I$(STM)/src/atomic_ops -DSWITCH_STM','CFLAGS += -I$(STM)/libaco -I$(STM)/src -I$(STM)/src/atomic_ops -DSWITCH_STM') \
.replace('# CFLAGS += -DCONTENTION_INTENSITY','CFLAGS += -DCONTENTION_INTENSITY') \
.replace('# CFLAGS += -DSWITCH_STM_TIME_PROFILE','CFLAGS += -DSWITCH_STM_TIME_PROFILE') \
.replace('# CFLAGS += -DSHRINK_ENABLE','CFLAGS += -DSHRINK_ENABLE') \
.replace('# CFLAGS += -DATS_ENABLE','CFLAGS += -DATS_ENABLE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('CFLAGS += -I$(STM)/libaco -I$(STM)/src -I$(STM)/src/atomic_ops -DSWITCH_STM','# CFLAGS += -I$(STM)/libaco -I$(STM)/src -I$(STM)/src/atomic_ops -DSWITCH_STM') \
.replace('CFLAGS += -DCONTENTION_INTENSITY','# CFLAGS += -DCONTENTION_INTENSITY') \
.replace('CFLAGS += -DSWITCH_STM_TIME_PROFILE','# CFLAGS += -DSWITCH_STM_TIME_PROFILE') \
.replace('CFLAGS += -DSHRINK_ENABLE','# CFLAGS += -DSHRINK_ENABLE') \
.replace('CFLAGS += -DATS_ENABLE','# CFLAGS += -DATS_ENABLE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
def run_tests(log_file="output.stm", threads=16, benchmarks=None, RSS=False):
benchmark_map = {
"yada": "./yada/yada -a15 -i yada/inputs/ttimeu1000000.2 -t",
"intruder": "./intruder/intruder -a10 -l128 -n262144 -s1 -t",
"kmeans_low": "./kmeans/kmeans -m40 -n40 -t0.00001 -i kmeans/inputs/random-n65536-d32-c16.txt -p",
"kmeans_high": "./kmeans/kmeans -m15 -n15 -t0.00001 -i kmeans/inputs/random-n65536-d32-c16.txt -p",
"bayes": "./bayes/bayes -v32 -r4096 -n10 -p40 -i2 -e8 -s1 -t",
"vacation_low": "./vacation/vacation -n2 -q90 -u98 -r1048576 -t4194304 -c",
"vacation_high": "./vacation/vacation -n4 -q60 -u90 -r1048576 -t4194304 -c",
"genome": "./genome/genome -g16384 -s64 -n16777216 -t",
"labyrinth": "./labyrinth/labyrinth -i labyrinth/inputs/random-x512-y512-z7-n512.txt -t",
"ssca2": "./ssca2/ssca2 -s20 -i1.0 -u1.0 -l3 -p3 -t"
}
tests = []
if benchmarks:
for b in benchmarks:
if b in benchmark_map:
tests.append(benchmark_map[b])
else:
print(f"Warning: Benchmark '{b}' not found.")
else:
tests = [
benchmark_map["yada"],
benchmark_map["intruder"],
benchmark_map["kmeans_low"],
benchmark_map["kmeans_high"],
benchmark_map["bayes"],
benchmark_map["vacation_low"],
benchmark_map["vacation_high"],
benchmark_map["genome"],
benchmark_map["labyrinth"],
benchmark_map["ssca2"]
]
for test in tests:
timeout = 600
print(f"Executing: {test} {threads}")
with open(log_file, "a") as f:
f.write(f"Executing: {test} {threads}\n")
try:
cmd = f"{test} {threads}"
if RSS:
cmd = f"/usr/bin/time -v {cmd}"
process = subprocess.Popen(cmd, cwd="stamp-master/", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, preexec_fn=os.setsid)
stdout, _ = process.communicate(timeout=timeout)
with open(log_file, "a") as f:
f.write(stdout)
print(stdout)
except subprocess.TimeoutExpired:
# Kill the entire process group
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
# Wait for a short time to allow for graceful termination
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
# If the process is still running, force kill it and its children
try:
parent = psutil.Process(process.pid)
for child in parent.children(recursive=True):
child.terminate()
parent.terminate()
gone, alive = psutil.wait_procs([parent] + parent.children(recursive=True), timeout=3)
for p in alive:
p.kill()
except psutil.NoSuchProcess:
pass
stdout, _ = process.communicate() # Collect any remaining output
with open(log_file, "a") as f:
f.write(f"Timeout occurred after {timeout} seconds.\n")
f.write(stdout) # Write any output that was produced before the timeout
print(f"Timeout occurred after {timeout} seconds.")
except Exception as e:
with open(log_file, "a") as f:
f.write(f"An error occurred: {str(e)}\n")
print(f"An error occurred: {str(e)}")
finally:
# Ensure all related processes are terminated
try:
parent = psutil.Process(process.pid)
children = parent.children(recursive=True)
for child in children:
child.terminate()
parent.terminate()
gone, alive = psutil.wait_procs([parent] + children, timeout=3)
for p in alive:
p.kill()
except psutil.NoSuchProcess:
pass # Process already terminated
# After all tests, clean up any remaining zombie processes
try:
current_process = psutil.Process()
children = current_process.children(recursive=True)
for child in children:
if child.status() == psutil.STATUS_ZOMBIE:
child.wait()
except psutil.NoSuchProcess:
pass
def simulate_suicide(simulation_times=1, threads_list=[16], log_path="./log", TP=False, benchmarks=None, RSS=False):
###############SUICIDE###############
print("###############SUICIDE###############")
print("###############SUICIDE###############")
print("###############SUICIDE###############")
print("###############SUICIDE###############")
print("###############SUICIDE###############")
print("###############SUICIDE###############")
print("###############SUICIDE###############")
print("###############SUICIDE###############")
print("###############SUICIDE###############")
print("###############SUICIDE###############")
#reset the configuration to suicide
reset_makefile_config_to_suicide()
reset_makefile_stm_config_to_suicide()
# Explicitly enable CM_SUICIDE
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# DEFINES += -DCM=CM_SUICIDE','DEFINES += -DCM=CM_SUICIDE') \
.replace('DEFINES += -DCM=CM_MODULAR','# DEFINES += -DCM=CM_MODULAR')
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
# If TP is true ,emable SWITCH_STM time profile
if (TP == True):
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-DSWITCH_STM_TIME_PROFILE', 'DEFINES += -DSWITCH_STM_TIME_PROFILE', makefile_content, flags=re.MULTILINE)
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-USWITCH_STM_TIME_PROFILE', '# DEFINES += -USWITCH_STM_TIME_PROFILE', modified_makefile_content, flags=re.MULTILINE)
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -DSWITCH_STM_TIME_PROFILE','CFLAGS += -DSWITCH_STM_TIME_PROFILE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
#Compile tinySTM
subprocess.run(['make'],cwd='tinySTM')
#Compile STAMP
for directory in directories:
subprocess.run(["make", "-f", f"Makefile.stm", "clean"], cwd=directory)
subprocess.run(["make", "-f", f"Makefile.stm"], cwd=directory)
#Run the test
for i in range(simulation_times):
for threads in threads_list:
log_name = f"suicide_{threads}.stm"
if TP:
log_name = f"suicide_TP_{threads}.stm"
log_file = os.path.join(log_path, log_name)
run_tests(log_file=log_file, threads=threads, benchmarks=benchmarks, RSS=RSS)
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done the iteration {i} - Current time: {current_time}")
#Print current time
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done all the iterations of suicide! - Current time: {current_time}")
def simulate_polka(simulation_times=1, threads_list=[16], log_path="./log", TP=False, benchmarks=None, RSS=False):
###############POLKA###############
print("###############POLKA###############")
print("###############POLKA###############")
print("###############POLKA###############")
print("###############POLKA###############")
print("###############POLKA###############")
print("###############POLKA###############")
print("###############POLKA###############")
print("###############POLKA###############")
print("###############POLKA###############")
print("###############POLKA###############")
#reset the configuration to suicide
reset_makefile_config_to_suicide()
reset_makefile_stm_config_to_suicide()
# Modify the configuration of tinySTM to POLKA (Polka requires CM_MODULAR and WRITE_BACK_ETL)
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
# Set DESIGN to WRITE_BACK_ETL (required for CM_MODULAR)
modified_makefile_content = makefile_content.replace('DEFINES += -DDESIGN=WRITE_BACK_CTL','# DEFINES += -DDESIGN=WRITE_BACK_CTL') \
.replace('DEFINES += -DDESIGN=WRITE_THROUGH','# DEFINES += -DDESIGN=WRITE_THROUGH') \
.replace('DEFINES += -DDESIGN=MODULAR','# DEFINES += -DDESIGN=MODULAR') \
.replace('# DEFINES += -DDESIGN=WRITE_BACK_ETL','DEFINES += -DDESIGN=WRITE_BACK_ETL')
# Enable CM_MODULAR and CM_POLKA
modified_makefile_content = modified_makefile_content.replace('# DEFINES += -DCM=CM_MODULAR','DEFINES += -DCM=CM_MODULAR') \
.replace('DEFINES += -DCM=CM_SUICIDE','# DEFINES += -DCM=CM_SUICIDE') \
.replace('# DEFINES += -DCM_POLKA','DEFINES += -DCM_POLKA') \
.replace('DEFINES += -UCM_POLKA','# DEFINES += -UCM_POLKA')
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
# If TP is true ,emable SWITCH_STM time profile
if (TP == True):
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-DSWITCH_STM_TIME_PROFILE', 'DEFINES += -DSWITCH_STM_TIME_PROFILE', makefile_content, flags=re.MULTILINE)
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-USWITCH_STM_TIME_PROFILE', '# DEFINES += -USWITCH_STM_TIME_PROFILE', modified_makefile_content, flags=re.MULTILINE)
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -DSWITCH_STM_TIME_PROFILE','CFLAGS += -DSWITCH_STM_TIME_PROFILE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
#Compile tinySTM
subprocess.run(['make'],cwd='tinySTM')
#Compile STAMP
for directory in directories:
subprocess.run(["make", "-f", f"Makefile.stm", "clean"], cwd=directory)
subprocess.run(["make", "-f", f"Makefile.stm"], cwd=directory)
#Run the test
for i in range(simulation_times):
for threads in threads_list:
log_name = f"polka_{threads}.stm"
if TP:
log_name = f"polka_TP_{threads}.stm"
log_file = os.path.join(log_path, log_name)
run_tests(log_file=log_file, threads=threads, benchmarks=benchmarks, RSS=RSS)
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done the iteration {i} - Current time: {current_time}")
#Print current time
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done all the iterations of polka! - Current time: {current_time}")
def simulate_ats(simulation_times=1, threads_list=[16], log_path="./log", TP=False, benchmarks=None, RSS=False):
###############ATS#################
print("#################ATS#################")
print("#################ATS#################")
print("#################ATS#################")
print("#################ATS#################")
print("#################ATS#################")
print("#################ATS#################")
print("#################ATS#################")
print("#################ATS#################")
print("#################ATS#################")
#reset the configuration to suicide
reset_makefile_config_to_suicide()
reset_makefile_stm_config_to_suicide()
# Modify the configuration of tinySTM to ATS
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# DEFINES += -DCM=CM_SUICIDE','DEFINES += -DCM=CM_SUICIDE') \
.replace('DEFINES += -DCM=CM_MODULAR','# DEFINES += -DCM=CM_MODULAR') \
.replace('# DEFINES += -DATS_ENABLE','DEFINES += -DATS_ENABLE') \
.replace('DEFINES += -UATS_ENABLE','# DEFINES += -UATS_ENABLE')
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
# If TP is true ,emable SWITCH_STM time profile
if (TP == True):
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-DSWITCH_STM_TIME_PROFILE', 'DEFINES += -DSWITCH_STM_TIME_PROFILE', makefile_content, flags=re.MULTILINE)
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-USWITCH_STM_TIME_PROFILE', '# DEFINES += -USWITCH_STM_TIME_PROFILE', modified_makefile_content, flags=re.MULTILINE)
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -DSWITCH_STM_TIME_PROFILE','CFLAGS += -DSWITCH_STM_TIME_PROFILE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
#Compile tinySTM
subprocess.run(['make'],cwd='tinySTM')
#Compile STAMP
for directory in directories:
subprocess.run(["make", "-f", f"Makefile.stm", "clean"], cwd=directory)
subprocess.run(["make", "-f", f"Makefile.stm"], cwd=directory)
#Run the test
for i in range(simulation_times):
for threads in threads_list:
log_name = f"ats_{threads}.stm"
if TP:
log_name = f"ats_TP_{threads}.stm"
log_file = os.path.join(log_path, log_name)
run_tests(log_file=log_file, threads=threads, benchmarks=benchmarks, RSS=RSS)
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done the iteration {i} - Current time: {current_time}")
#Print current time
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done all the iterations of ats! - Current time: {current_time}")
def simulate_shrink(simulation_times=1, threads_list=[16], log_path="./log", TP=False, benchmarks=None, RSS=False):
###############SHRINK###############
print("###############SHRINK###############")
print("###############SHRINK###############")
print("###############SHRINK###############")
print("###############SHRINK###############")
print("###############SHRINK###############")
print("###############SHRINK###############")
print("###############SHRINK###############")
print("###############SHRINK###############")
print("###############SHRINK###############")
print("###############SHRINK###############")
#reset the configuration to suicide
reset_makefile_config_to_suicide()
reset_makefile_stm_config_to_suicide()
# Modify the configuration of tinySTM to SHRINK
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# DEFINES += -DCM=CM_SUICIDE','DEFINES += -DCM=CM_SUICIDE') \
.replace('DEFINES += -DCM=CM_MODULAR','# DEFINES += -DCM=CM_MODULAR') \
.replace('# DEFINES += -DSHRINK_ENABLE','DEFINES += -DSHRINK_ENABLE') \
.replace('DEFINES += -USHRINK_ENABLE','# DEFINES += -USHRINK_ENABLE')
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
# If TP is true ,emable SWITCH_STM time profile
if (TP == True):
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-DSWITCH_STM_TIME_PROFILE', 'DEFINES += -DSWITCH_STM_TIME_PROFILE', makefile_content, flags=re.MULTILINE)
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-USWITCH_STM_TIME_PROFILE', '# DEFINES += -USWITCH_STM_TIME_PROFILE', modified_makefile_content, flags=re.MULTILINE)
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -DSWITCH_STM_TIME_PROFILE','CFLAGS += -DSWITCH_STM_TIME_PROFILE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
#Compile tinySTM
subprocess.run(['make'],cwd='tinySTM')
# Modify the configuration of STAMP to SHRINK
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -DSHRINK_ENABLE','CFLAGS += -DSHRINK_ENABLE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
#Compile STAMP
for directory in directories:
subprocess.run(["make", "-f", f"Makefile.stm", "clean"], cwd=directory)
subprocess.run(["make", "-f", f"Makefile.stm"], cwd=directory)
#Run the test
for i in range(simulation_times):
for threads in threads_list:
log_name = f"shrink_{threads}.stm"
if TP:
log_name = f"shrink_TP_{threads}.stm"
log_file = os.path.join(log_path, log_name)
run_tests(log_file=log_file, threads=threads, benchmarks=benchmarks, RSS=RSS)
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done the iteration {i} - Current time: {current_time}")
#Print current time
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done all the iterations of shrink! - Current time: {current_time}")
# Undo the modification of configuration of STAMP
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('CFLAGS += -DSHRINK_ENABLE','# CFLAGS += -DSHRINK_ENABLE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
def simulate_switch_stm(simulation_times=1, threads_list=[16], schedule_policy='seq', CI=True, TP=False, PROFILE=False, log_path="./log", benchmarks=None, RSS=False):
###############SWITCH_STM###############
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
print("###############SWITCH_STM###############")
#reset the configuration to suicide
reset_makefile_config_to_suicide()
reset_makefile_stm_config_to_suicide()
# Modify the configuration of tinySTM to SWITCH_STM
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# DEFINES += -DCM=CM_SUICIDE','DEFINES += -DCM=CM_SUICIDE') \
.replace('DEFINES += -DCM=CM_MODULAR','# DEFINES += -DCM=CM_MODULAR') \
.replace('# DEFINES += -DSWITCH_STM','DEFINES += -DSWITCH_STM') \
.replace('DEFINES += -USWITCH_STM','# DEFINES += -USWITCH_STM')
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
# set the schedule policy
if (schedule_policy == 'rnd'):
with open('tinySTM/include/param.h', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('#define SCHEDULE_POLICY 1 ','#define SCHEDULE_POLICY 0 ') \
.replace('#define SCHEDULE_POLICY 2 ','#define SCHEDULE_POLICY 0 ')
with open('tinySTM/include/param.h', 'w') as f:
f.write(modified_makefile_content)
elif (schedule_policy == 'seq'):
with open('tinySTM/include/param.h', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('#define SCHEDULE_POLICY 0 ','#define SCHEDULE_POLICY 1 ') \
.replace('#define SCHEDULE_POLICY 2 ','#define SCHEDULE_POLICY 1 ')
with open('tinySTM/include/param.h', 'w') as f:
f.write(modified_makefile_content)
elif (schedule_policy == 'laf'):
with open('tinySTM/include/param.h', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('#define SCHEDULE_POLICY 0 ','#define SCHEDULE_POLICY 2 ') \
.replace('#define SCHEDULE_POLICY 1 ','#define SCHEDULE_POLICY 2 ') \
.replace('#define SCHEDULE_POLICY 3 ','#define SCHEDULE_POLICY 2 ')
with open('tinySTM/include/param.h', 'w') as f:
f.write(modified_makefile_content)
elif (schedule_policy == 'maf'):
with open('tinySTM/include/param.h', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('#define SCHEDULE_POLICY 0 ','#define SCHEDULE_POLICY 3 ') \
.replace('#define SCHEDULE_POLICY 1 ','#define SCHEDULE_POLICY 3 ') \
.replace('#define SCHEDULE_POLICY 2 ','#define SCHEDULE_POLICY 3 ')
with open('tinySTM/include/param.h', 'w') as f:
f.write(modified_makefile_content)
# To modify param.h to select the desired strategy,
# you have to replace the SCHEDULE_POLICY value
# Change SCHEDULE_POLICY from any other value to the case number of your switch strategy
# you can use the following template:
'''
elif schedule_policy == 'YOUR_POLICY':
with open('tinySTM/include/param.h', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('#define SCHEDULE_POLICY 0 ','#define SCHEDULE_POLICY 2 ') \
.replace('#define SCHEDULE_POLICY 1 ','#define SCHEDULE_POLICY 2 ')
with open('tinySTM/include/param.h', 'w') as f:
f.write(modified_makefile_content)
'''
else:
print("No schedule policy is set, the simulation might not correct!!!")
# If CI is true ,set the flag of CI
if (CI == True):
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# DEFINES += -DCONTENTION_INTENSITY','DEFINES += -DCONTENTION_INTENSITY') \
.replace('DEFINES += -UCONTENTION_INTENSITY','# DEFINES += -UCONTENTION_INTENSITY')
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
# If TP is true ,emable SWITCH_STM time profile
if (TP == True):
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-DSWITCH_STM_TIME_PROFILE', 'DEFINES += -DSWITCH_STM_TIME_PROFILE', makefile_content, flags=re.MULTILINE)
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-USWITCH_STM_TIME_PROFILE', '# DEFINES += -USWITCH_STM_TIME_PROFILE', modified_makefile_content, flags=re.MULTILINE)
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -DSWITCH_STM_TIME_PROFILE','CFLAGS += -DSWITCH_STM_TIME_PROFILE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
# If PROFILE is true, enable SWITCH_STM_METRIC_PROFILE
if (PROFILE == True):
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-DSWITCH_STM_METRIC_PROFILE', 'DEFINES += -DSWITCH_STM_METRIC_PROFILE', makefile_content, flags=re.MULTILINE)
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-USWITCH_STM_METRIC_PROFILE', '# DEFINES += -USWITCH_STM_METRIC_PROFILE', modified_makefile_content, flags=re.MULTILINE)
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -DSWITCH_STM_METRIC_PROFILE','CFLAGS += -DSWITCH_STM_METRIC_PROFILE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
#Compile tinySTM
subprocess.run(['make', 'clean'],cwd='tinySTM')
subprocess.run(['make'],cwd='tinySTM')
# Modify the configuration of STAMP to SWITCH_STM
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -I$(STM)/libaco -I$(STM)/src -I$(STM)/src/atomic_ops -DSWITCH_STM','CFLAGS += -I$(STM)/libaco -I$(STM)/src -I$(STM)/src/atomic_ops -DSWITCH_STM')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
# If CI is true , set the flag of CI
if (CI == True):
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('# CFLAGS += -DCONTENTION_INTENSITY','CFLAGS += -DCONTENTION_INTENSITY')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
#Compile STAMP
for directory in directories:
subprocess.run(["make", "-f", f"Makefile.stm", "clean"], cwd=directory)
subprocess.run(["make", "-f", f"Makefile.stm"], cwd=directory)
#specify log name
if (schedule_policy == 'rnd' and CI == False):
switch_log_name = 'switch_rnd'
elif (schedule_policy == 'rnd' and CI == True):
switch_log_name = 'switch_rnd_CI'
elif (schedule_policy == 'seq' and CI == False):
switch_log_name = 'switch_seq'
elif (schedule_policy == 'seq' and CI == True):
switch_log_name = 'switch_seq_CI'
elif (schedule_policy == 'laf' and CI == False):
switch_log_name = 'switch_laf'
elif (schedule_policy == 'laf' and CI == True):
switch_log_name = 'switch_laf_CI'
elif (schedule_policy == 'maf' and CI == False):
switch_log_name = 'switch_maf'
elif (schedule_policy == 'maf' and CI == True):
switch_log_name = 'switch_maf_CI'
else:
switch_log_name = 'some_wierd_switch_mode'
if PROFILE:
switch_log_name += '_Profile'
#Run the test
for i in range(simulation_times):
for threads in threads_list:
log_file = os.path.join(log_path, f"{switch_log_name}_{threads}.stm")
run_tests(log_file=log_file, threads=threads, benchmarks=benchmarks, RSS=RSS)
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done the iteration {i} - Current time: {current_time}")
#Print current time
current_time = subprocess.run(["date", "+%Y-%m-%d %H:%M:%S"], capture_output=True, text=True).stdout.strip()
print(f"Done all the iterations of {switch_log_name}! - Current time: {current_time}")
if (PROFILE == True):
with open('tinySTM/Makefile', 'r') as f:
makefile_content = f.read()
# Disable definition
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-DSWITCH_STM_METRIC_PROFILE', '# DEFINES += -DSWITCH_STM_METRIC_PROFILE', makefile_content, flags=re.MULTILINE)
modified_makefile_content = re.sub(r'^\s*#?\s*DEFINES\s*\+=\s*-USWITCH_STM_METRIC_PROFILE', 'DEFINES += -USWITCH_STM_METRIC_PROFILE', modified_makefile_content, flags=re.MULTILINE)
with open('tinySTM/Makefile', 'w') as f:
f.write(modified_makefile_content)
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('CFLAGS += -DSWITCH_STM_METRIC_PROFILE', '# CFLAGS += -DSWITCH_STM_METRIC_PROFILE')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
# Undo the modification of configuration of STAMP
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('CFLAGS += -I$(STM)/libaco -I$(STM)/src -I$(STM)/src/atomic_ops -DSWITCH_STM','# CFLAGS += -I$(STM)/libaco -I$(STM)/src -I$(STM)/src/atomic_ops -DSWITCH_STM')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)
# If CI is true , reset the flag of CI
if (CI == True):
with open('stamp-master/common/Makefile.stm', 'r') as f:
makefile_content = f.read()
modified_makefile_content = makefile_content.replace('CFLAGS += -DCONTENTION_INTENSITY','# CFLAGS += -DCONTENTION_INTENSITY')
with open('stamp-master/common/Makefile.stm', 'w') as f:
f.write(modified_makefile_content)