-
Notifications
You must be signed in to change notification settings - Fork 32
/
ace.py
executable file
·1578 lines (1267 loc) · 64.4 KB
/
ace.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
#!/usr/bin/env python3
# To run : python3 ace.py -l <seq_length> -n <nested : True|False> -d <demo : True|False>
import os
import re
import sys
import stat
import subprocess
import argparse
import time
import itertools
import json
import pprint
import collections
import threading
from shutil import copyfile
from multiprocessing import Pool
from progress.bar import FillingCirclesBar
# Import list of all function options
from common import (FallocOptions, FsyncOptions, FileOptions, SecondFileOptions, DirOptions,
TestDirOptions, SecondDirOptions, WriteOptions, dWriteOptions, TruncateOptions,
OperationSet)
# The sequences we want to reach to, to reproduce known bugs.
expected_sequence = []
expected_sync_sequence = []
# return sibling of a file/directory
def SiblingOf(file):
if file == 'foo':
return 'bar'
elif file == 'bar' :
return 'foo'
elif file == 'A/foo':
return 'A/bar'
elif file == 'A/bar':
return 'A/foo'
elif file == 'B/foo':
return 'B/bar'
elif file == 'B/bar' :
return 'B/foo'
elif file == 'AC/foo':
return 'AC/bar'
elif file == 'AC/bar' :
return 'AC/foo'
elif file == 'A' :
return 'B'
elif file == 'B':
return 'A'
elif file == 'AC' :
return 'AC'
elif file == 'test':
return 'test'
# Return parent of a file/directory
def Parent(file):
if file == 'foo' or file == 'bar':
return 'test'
if file == 'A/foo' or file == 'A/bar' or file == 'AC':
return 'A'
if file == 'B/foo' or file == 'B/bar':
return 'B'
if file == 'A' or file == 'B' or file == 'test':
return 'test'
if file == 'AC/foo' or file == 'AC/bar':
return 'AC'
# Given a list of files, return a list of related files.
# These are optimizations to reduce the effective workload set, by persisting only related files during workload generation.
def file_range(file_list):
file_set = list(file_list)
for i in range(0, len(file_list)):
file_set.append(SiblingOf(file_list[i]))
file_set.append(Parent(file_list[i]))
return list(set(file_set))
#----------------------Known Bug summary-----------------------#
# Length 1 = 3
# Length 2 = 14
# length 3 = 9
# Total encoded = 26
#--------------------------------------------------------------#
# TODO: Update this list carefully.
# If we don't allow dependency ops on same file, we'll miss this in seq2
# This is actually seq 2 = [link foo-bar, 'sync', unlink bar, 'fsync-bar']
# 1. btrfs_link_unlink 3 (yes finds in 2)
expected_sequence.append([('link', ('foo', 'bar')), ('unlink', ('bar')), ('creat', ('bar'))])
expected_sync_sequence.append([('sync'), ('none'), ('fsync', 'bar')])
# 2. btrfs_rename_special_file 3 (yes in 3)
expected_sequence.append([('mknod', ('foo')), ('rename', ('foo', 'bar')), ('link', ('bar', 'foo'))])
expected_sync_sequence.append([('fsync', 'bar'), ('none'), ('fsync', 'bar')])
# 3. new_bug1_btrfs 2 (Yes finds in 2)
expected_sequence.append([('write', ('foo', 'append')), ('falloc', ('foo', 'FALLOC_FL_ZERO_RANGE|FALLOC_FL_KEEP_SIZE', 'append'))])
expected_sync_sequence.append([('fsync', 'foo'), ('fsync', 'foo')])
# 4. new_bug2_f2fs 3 (Yes finds in 2)
expected_sequence.append([('write', ('foo', 'append')), ('falloc', ('foo', 'FALLOC_FL_ZERO_RANGE|FALLOC_FL_KEEP_SIZE', 'append')), ('fdatasync', ('foo'))])
expected_sync_sequence.append([('sync'), ('none'), ('none')])
# We miss this in seq-2, because we disallow workloads of sort creat, creat
# 5. generic_034 2
expected_sequence.append([('creat', ('A/foo')), ('creat', ('A/bar'))])
expected_sync_sequence.append([('sync'), ('fsync', 'A')])
# 6. generic_039 2 (Yes finds in 2)
expected_sequence.append([('link', ('foo', 'bar')), ('remove', ('bar'))])
expected_sync_sequence.append([('sync'), ('fsync', 'foo')])
# 7. generic_059 2 (yes finds in 2)
expected_sequence.append([('write', ('foo', 'append')), ('falloc', ('foo', 'FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE', 'overlap_unaligned'))])
expected_sync_sequence.append([('sync'), ('fsync', 'foo')])
# 8. generic_066 2 (Yes finds in 2)
expected_sequence.append([('fsetxattr', ('foo')), ('removexattr', ('foo'))])
expected_sync_sequence.append([('sync'), ('fsync', 'foo')])
# Reachable from current seq 2 generator (#1360 : creat A/foo, rename A,B) (sync, fsync A)
# We will miss this, if we restrict that op2 reuses files from op1
# 9. generic_341 3 (Yes finds in 2)
expected_sequence.append([('creat', ('A/foo')), ('rename', ('A', 'B')), ('mkdir', ('A'))])
expected_sync_sequence.append([('sync'), ('none'), ('fsync', 'A')])
# 10. generic_348 1 (yes finds in 1)
expected_sequence.append([('symlink', ('foo', 'A/bar'))])
expected_sync_sequence.append([('fsync', 'A')])
# 11. generic_376 2 (yes finds in 2)
expected_sequence.append([('rename', ('foo', 'bar')), ('creat', ('foo'))])
expected_sync_sequence.append([('none'), ('fsync', 'bar')])
# Yes reachable from sseeq2 - (falloc (foo, append), fdatasync foo)
# 12. generic_468 3 (yes, finds in 2)
expected_sequence.append([('write', ('foo', 'append')), ('falloc', ('foo', 'FALLOC_FL_KEEP_SIZE', 'append')), ('fdatasync', ('foo'))])
expected_sync_sequence.append([('sync'), ('none'), ('none')])
# We miss this if we sync only used file set - or we need an option 'none' to end the file with
# 13. ext4_direct_write 2
expected_sequence.append([('write', ('foo', 'append')), ('dwrite', ('foo', 'overlap'))])
expected_sync_sequence.append([('none'), ('fsync', 'bar')])
#14 btrfs_EEXIST (Seq 1)
# creat foo, fsync foo
# write foo 0-4K, fsync foo
# btrfs use -O extref during mkfs
#15. generic 041 (If we consider the 3000 as setup, then seq length 3)
# create 3000 link(foo, foo_i), sync, unlink(foo_0), link(foo, foo_3001), link(foo, foo_0), fsync foo
#16. generic 056 (seq2)
# write(foo, 0-4K), fsync foo, link(foo, bar), fsync some random file/dir
# requires that we allow repeated operations (check if mmap write works here)
#17 generic 090 (seq3)
# write(foo 0-4K), sync, link(foo, bar), sync, append(foo, 4K-8K), fsync foo
#18 generic_104 (seq2) larger file set
# link(foo, foo1), link(bar, bar1), fsync(bar)
#19 generic 106 (seq 2)
# link(foo, bar), sync, unlink(bar) *drop cache* fsync foo
#20 generic 107 (seq 3)
# link(foo, A/foo), link(foo, A/bar), sync, unlink(A/bar), fsync(foo)
#21 generic 177
# write(foo, 0-32K), sync, punch_hole(foo, 24K-32K), punch_hole(foo, 4K-64K) fsync foo
#22 generic 321 2 fsyncs?
# rename(foo, A/foo), fsync A, fsync A/foo
#23 generic 322 (yes, seq1)
# rename(A/foo, A/bar), fsync(A/bar)
#24 generic 335 (seq 2) but larger file set
# rename(A/foo, foo), creat bar, fsync(test)
#25 generic 336 (seq 4)
# link(A/foo, B/foo), creat B/bar, sync, unlink(B/foo), mv(B/bar, C/bar), fsync A/foo
#26 generic 342 (seq 3)
# write foo 0-4K, sync, rename(foo,bar), write(foo) fsync(foo)
#27 generic 343 (seq 2)
# link(A/foo, A/bar) , rename(B/foo_new, A/foo_new), fsync(A/foo)
#28 generic 325 (seq3)
# write,(foo, 0-256K), mmapwrite(0-4K), mmapwrite(252-256K), msync(0-64K), msync(192-256K)
VALID_TEST_TYPES = ['crashmonkey', 'xfstest', 'xfstest-concise']
def build_parser():
parser = argparse.ArgumentParser(description='Automatic Crash Explorer v0.1')
# global args
parser.add_argument('--sequence_len', '-l', default='3', help='Number of critical ops in the bugy workload')
parser.add_argument('--nested', '-n', default='False', help='Add an extra level of nesting?')
parser.add_argument('--demo', '-d', default='False', help='Create a demo workload set?')
parser.add_argument('--test-type', '-t', default='crashmonkey', required=False,
help='Type of test to generate <{}>. (Default: crashmonkey)'.format("/".join(VALID_TEST_TYPES)))
return parser
def print_setup(parsed_args):
print('\n{: ^50s}'.format('Automatic Crash Explorer v0.1\n'))
print('='*20, 'Setup' , '='*20, '\n')
print('{0:20} {1}'.format('Sequence length', parsed_args.sequence_len))
print('{0:20} {1}'.format('Nested', parsed_args.nested))
print('{0:20} {1}'.format('Demo', parsed_args.demo))
print('{0:20} {1}'.format('Test Type', parsed_args.test_type))
print('\n', '='*48, '\n')
# Helper to build all possible combination of parameters to a given file-system operation
def buildTuple(command, expand_combinations=True):
if command == 'creat':
d = tuple(FileOptions)
elif command == 'mkdir' or command == 'rmdir':
d = tuple(DirOptions)
elif command == 'mknod':
d = tuple(FileOptions)
elif command == 'falloc':
d_tmp = list()
d_tmp.append(FileOptions)
d_tmp.append(FallocOptions)
d_tmp.append(WriteOptions)
if not expand_combinations:
return d_tmp
d = list()
for i in itertools.product(*d_tmp):
d.append(i)
elif command == 'write':
d_tmp = list()
d_tmp.append(FileOptions)
d_tmp.append(WriteOptions)
if not expand_combinations:
return d_tmp
d = list()
for i in itertools.product(*d_tmp):
d.append(i)
elif command == 'dwrite':
d_tmp = list()
d_tmp.append(FileOptions)
d_tmp.append(dWriteOptions)
if not expand_combinations:
return d_tmp
d = list()
for i in itertools.product(*d_tmp):
d.append(i)
elif command == 'link' or command == 'symlink':
d_tmp = list()
d_tmp.append(FileOptions + SecondFileOptions)
d_tmp.append(SecondFileOptions)
if not expand_combinations:
return d_tmp
d = list()
for i in itertools.product(*d_tmp):
if len(set(i)) == 2:
d.append(i)
elif command == 'rename':
d_tmp = list()
d_tmp.append(FileOptions + SecondFileOptions)
d_tmp.append(SecondFileOptions)
if not expand_combinations:
return d_tmp
d = list()
for i in itertools.product(*d_tmp):
if len(set(i)) == 2:
d.append(i)
d_tmp = list()
d_tmp.append(DirOptions + SecondDirOptions)
d_tmp.append(SecondDirOptions)
for i in itertools.product(*d_tmp):
if len(set(i)) == 2:
d.append(i)
elif command == 'remove' or command == 'unlink':
d = tuple(FileOptions +SecondFileOptions)
elif command == 'fdatasync' or command == 'fsetxattr' or command == 'removexattr':
d = tuple(FileOptions)
elif command == 'fsync':
d = tuple(FileOptions + DirOptions + TestDirOptions + SecondFileOptions + SecondDirOptions)
elif command == 'truncate':
d_tmp = list()
d_tmp.append(FileOptions)
d_tmp.append(TruncateOptions)
if not expand_combinations:
return d_tmp
d = list()
for i in itertools.product(*d_tmp):
d.append(i)
elif command == 'mmapwrite':
d_tmp = list()
d_tmp.append(FileOptions)
d_tmp.append(dWriteOptions)
if not expand_combinations:
return d_tmp
d = list()
for i in itertools.product(*d_tmp):
d.append(i)
else:
d=()
return d
# Given a restricted list of files, this function builds all combinations of input parameters to persistence operations.
# Once the parameters to core-ops are picked, it is not required to persist a file totally unrelated to the set of used files in the workload. So we can restrict the set of files for persistence to either related files(includes the parent and siblings of files used in the workload) or further restrict it to strictly pick from the set of used files only.
# We can optionally add a persistence point after each core-FS op, except for the last one. The last core-op must be followed by a persistence op, so that we don't truncate it to a workload of lower sequence.
def buildCustomTuple(file_list):
global num_ops
d = list(file_list)
fsync = ('fsync',)
sync = ('sync')
none = ('none')
SyncSetCustom = list()
SyncSetNoneCustom = list()
for i in range(0, len(d)):
tup = list(fsync)
tup.append(d[i])
SyncSetCustom.append(tuple(tup))
SyncSetNoneCustom.append(tuple(tup))
SyncSetCustom.append(sync)
SyncSetNoneCustom.append(sync)
SyncSetCustom.append(none)
SyncSetCustom = tuple(SyncSetCustom)
SyncSetNoneCustom = tuple(SyncSetNoneCustom)
syncPermutationsCustom = list()
if int(num_ops) == 1:
for i in itertools.product(SyncSetNoneCustom):
syncPermutationsCustom.append(i)
elif int(num_ops) == 2:
for i in itertools.product(SyncSetCustom, SyncSetNoneCustom):
syncPermutationsCustom.append(i)
elif int(num_ops) == 3:
for i in itertools.product(SyncSetCustom, SyncSetCustom, SyncSetNoneCustom):
syncPermutationsCustom.append(i)
elif int(num_ops) == 4:
for i in itertools.product(SyncSetCustom, SyncSetCustom, SyncSetCustom, SyncSetNoneCustom):
syncPermutationsCustom.append(i)
return syncPermutationsCustom
# Find the auto-generated workload that matches the necoded sequence of known bugs. This is to sanity check that Ace can indeed generate workloads to reproduce the bug, if run on appropriate kernel veersions.
def isBugWorkload(opList, paramList, syncList):
for i in range(0,len(expected_sequence)):
if len(opList) != len(expected_sequence[i]):
continue
flag = 1
for j in range(0, len(expected_sequence[i])):
if opList[j] == expected_sequence[i][j][0] and paramList[j] == expected_sequence[i][j][1] and tuple(syncList[j]) == tuple(expected_sync_sequence[i][j]):
continue
else:
flag = 0
break
if flag == 1:
print('Found match to Bug # ', i+1, ' : in file # ' , global_count)
print('Length of seq : ', len(expected_sequence[i]))
print('Expected sequence = ' , expected_sequence[i])
print('Expected sync sequence = ', expected_sync_sequence[i])
print('Auto generator found : ')
print(opList)
print(paramList)
print(syncList)
print('\n\n')
return True
# A bunch of functions to insert ops into the j-lang file.
def insertUnlink(file_name, open_dir_map, open_file_map, file_length_map, modified_pos):
open_file_map.pop(file_name, None)
return ('unlink', file_name)
def insertRmdir(file_name,open_dir_map, open_file_map, file_length_map, modified_pos):
open_dir_map.pop(file_name, None)
return ('rmdir', file_name)
def insertXattr(file_name, open_dir_map, open_file_map, file_length_map, modified_pos):
return ('fsetxattr', file_name)
def insertOpen(file_name, open_dir_map, open_file_map, file_length_map, modified_pos):
if file_name in FileOptions or file_name in SecondFileOptions:
open_file_map[file_name] = 1
elif file_name in DirOptions or file_name in SecondDirOptions or file_name in TestDirOptions:
open_dir_map[file_name] = 1
return ('open', file_name)
def insertMkdir(file_name, open_dir_map, open_file_map, file_length_map, modified_pos):
if file_name in DirOptions or file_name in SecondDirOptions or file_name in TestDirOptions:
open_dir_map[file_name] = 0
return ('mkdir', file_name)
def insertClose(file_name, open_dir_map, open_file_map, file_length_map, modified_pos):
if file_name in FileOptions or file_name in SecondFileOptions:
open_file_map[file_name] = 0
elif file_name in DirOptions or file_name in SecondDirOptions or file_name in TestDirOptions:
open_dir_map[file_name] = 0
return ('close', file_name)
def insertWrite(file_name, open_dir_map, open_file_map, file_length_map, modified_pos):
if file_name not in file_length_map:
file_length_map[file_name] = 0
file_length_map[file_name] += 1
return ('write', (file_name, 'append'))
# Dependency checks : Creat - file should not exist. If it does, remove it.
def checkCreatDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map):
file_name = current_sequence[pos][1]
# Either open or closed doesn't matter. File should not exist at all
if file_name in open_file_map:
# Insert dependency before the creat command
modified_sequence.insert(modified_pos, insertUnlink(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
return modified_pos
# Dependency checks : Mkdir
def checkDirDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map):
file_name = current_sequence[pos][1]
if file_name not in DirOptions and file_name not in SecondDirOptions:
print('Invalid param list for mkdir')
# Either open or closed doesn't matter. Directory should not exist at all
# TODO : We heavily depend on the pre-defined file list. Need to generalize it at some point.
if file_name in open_dir_map and file_name != 'test':
# if dir is A, remove contents within it too
if file_name == 'A':
if 'A/foo' in open_file_map and open_file_map['A/foo'] == 1:
file = 'A/foo'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
elif 'A/foo' in open_file_map and open_file_map['A/foo'] == 0:
file = 'A/foo'
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if 'A/bar' in open_file_map and open_file_map['A/bar'] == 1:
file = 'A/bar'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
elif 'A/bar' in open_file_map and open_file_map['A/bar'] == 0:
file = 'A/bar'
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if 'AC' in open_dir_map and open_dir_map['AC'] == 1:
file = 'AC'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if 'AC' in open_dir_map:
if 'AC/foo' in open_file_map and open_file_map['AC/foo'] == 1:
file = 'AC/foo'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
elif 'AC/foo' in open_file_map and open_file_map['AC/foo'] == 0:
file = 'AC/foo'
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if 'AC/bar' in open_file_map and open_file_map['AC/bar'] == 1:
file = 'AC/bar'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
elif 'AC/bar' in open_file_map and open_file_map['AC/bar'] == 0:
file = 'AC/bar'
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
file = 'AC'
modified_sequence.insert(modified_pos, insertRmdir(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if file_name == 'B':
if 'B/foo' in open_file_map and open_file_map['B/foo'] == 1:
file = 'B/foo'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
elif 'B/foo' in open_file_map and open_file_map['B/foo'] == 0:
file = 'B/foo'
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if 'B/bar' in open_file_map and open_file_map['B/bar'] == 1:
file = 'B/bar'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
elif 'B/bar' in open_file_map and open_file_map['B/bar'] == 0:
file = 'B/bar'
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if file_name == 'AC':
if 'AC/foo' in open_file_map and open_file_map['AC/foo'] == 1:
file = 'AC/foo'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
elif 'AC/foo' in open_file_map and open_file_map['AC/foo'] == 0:
file = 'AC/foo'
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if 'AC/bar' in open_file_map and open_file_map['AC/bar'] == 1:
file = 'AC/bar'
modified_sequence.insert(modified_pos, insertClose(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
elif 'AC/bar' in open_file_map and open_file_map['AC/bar'] == 0:
file = 'AC/bar'
modified_sequence.insert(modified_pos, insertUnlink(file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
# Insert dependency before the creat command
modified_sequence.insert(modified_pos, insertRmdir(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
return modified_pos
# Check if parent directories exist, if not create them.
def checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map):
file_names = current_sequence[pos][1]
if isinstance(file_names, str):
file_name = file_names
# Parent dir doesn't exist
if (Parent(file_name) == 'A' or Parent(file_name) == 'B') and Parent(file_name) not in open_dir_map:
modified_sequence.insert(modified_pos, insertMkdir(Parent(file_name), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if Parent(file_name) == 'AC' and Parent(file_name) not in open_dir_map:
if Parent(Parent(file_name)) not in open_dir_map:
modified_sequence.insert(modified_pos, insertMkdir(Parent(Parent(file_name)), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertMkdir(Parent(file_name), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
else:
file_name = file_names[0]
file_name2 = file_names[1]
# Parent dir doesn't exist
if (Parent(file_name) == 'A' or Parent(file_name) == 'B') and Parent(file_name) not in open_dir_map:
modified_sequence.insert(modified_pos, insertMkdir(Parent(file_name), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if Parent(file_name) == 'AC' and Parent(file_name) not in open_dir_map:
if Parent(Parent(file_name)) not in open_dir_map:
modified_sequence.insert(modified_pos, insertMkdir(Parent(Parent(file_name)), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertMkdir(Parent(file_name), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
# Parent dir doesn't exist
if (Parent(file_name2) == 'A' or Parent(file_name2) == 'B') and Parent(file_name2) not in open_dir_map:
modified_sequence.insert(modified_pos, insertMkdir(Parent(file_name2), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if Parent(file_name2) == 'AC' and Parent(file_name2) not in open_dir_map:
if Parent(Parent(file_name2)) not in open_dir_map:
modified_sequence.insert(modified_pos, insertMkdir(Parent(Parent(file_name2)), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
modified_sequence.insert(modified_pos, insertMkdir(Parent(file_name2), open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
return modified_pos
# Check the dependency that file already exists and is open, eg. before writing to a file
def checkExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map):
file_names = current_sequence[pos][1]
if isinstance(file_names, str):
file_name = file_names
else:
file_name = file_names[0]
# If we are trying to fsync a dir, ensure it exists
if file_name in DirOptions or file_name in SecondDirOptions or file_name in TestDirOptions:
if file_name not in open_dir_map:
modified_sequence.insert(modified_pos, insertMkdir(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if file_name in open_dir_map and open_dir_map[file_name] == 0:
modified_sequence.insert(modified_pos, insertOpen(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if file_name in FileOptions or file_name in SecondFileOptions:
if file_name not in open_file_map or open_file_map[file_name] == 0:
# Insert dependency - open before the command
modified_sequence.insert(modified_pos, insertOpen(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
return modified_pos
# Ensures that the file is closed. If not, closes it.
def checkClosed(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map):
file_names = current_sequence[pos][1]
if isinstance(file_names, str):
file_name = file_names
else:
file_name = file_names[0]
if file_name in open_file_map and open_file_map[file_name] == 1:
modified_sequence.insert(modified_pos, insertClose(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
if file_name in open_dir_map and open_dir_map[file_name] == 1:
modified_sequence.insert(modified_pos, insertClose(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
return modified_pos
# If the op is remove xattr, we need to ensure, there's atleast one associated xattr to the file
def checkXattr(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map):
file_name = current_sequence[pos][1]
if open_file_map[file_name] == 1:
modified_sequence.insert(modified_pos, insertXattr(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
return modified_pos
# For overwrites ensure that the file is not empty.
def checkFileLength(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map):
file_names = current_sequence[pos][1]
if isinstance(file_names, str):
file_name = file_names
else:
file_name = file_names[0]
# 0 length file
if file_name not in file_length_map:
modified_sequence.insert(modified_pos, insertWrite(file_name, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
return modified_pos
# Handles satisfying dependencies, for a given core FS op
def satisfyDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map):
if isinstance(current_sequence[pos], str):
command = current_sequence[pos]
else:
command = current_sequence[pos][0]
# print 'Command = ', command
if command == 'creat' or command == 'mknod':
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
modified_pos = checkCreatDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
file = current_sequence[pos][1]
open_file_map[file] = 1
elif command == 'mkdir':
modified_pos = checkDirDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
dir = current_sequence[pos][1]
open_dir_map[dir] = 0
elif command == 'falloc':
file = current_sequence[pos][1][0]
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# if file doesn't exist, has to be created and opened
modified_pos = checkExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# Whatever the op is, let's ensure file size is non zero
modified_pos = checkFileLength(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
elif command == 'write' or command == 'dwrite' or command == 'mmapwrite':
file = current_sequence[pos][1][0]
option = current_sequence[pos][1][1]
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# if file doesn't exist, has to be created and opened
modified_pos = checkExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# if we chose to do an append, let's not care about the file size
# however if its an overwrite or unaligned write, then ensure file is atleast one page long
if option == 'append':
if file not in file_length_map:
file_length_map[file] = 0
file_length_map[file] += 1
# elif option == 'overlap_unaligned_start' or 'overlap_unaligned_end' or 'overlap_start' or 'overlap_end' or 'overlap_extend':
elif option == 'overlap' or 'overlap_aligned' or 'overlap_unaligned':
modified_pos = checkFileLength(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# If we do a dwrite, let's close the file after that
if command == 'dwrite':
if file in FileOptions or file in SecondFileOptions:
open_file_map[file] = 0
elif command == 'link':
second_file = current_sequence[pos][1][1]
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
modified_pos = checkExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
if second_file in open_file_map and open_file_map[second_file] == 1:
# Insert dependency - open before the command
modified_sequence.insert(modified_pos, insertClose(second_file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
# if we have a closed file, remove it
if second_file in open_file_map and open_file_map[second_file] == 0:
# Insert dependency - open before the command
modified_sequence.insert(modified_pos, insertUnlink(second_file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
# We have created a new file, but it isn't open yet
open_file_map[second_file] = 0
elif command == 'rename':
# If the file was open during rename, does the handle now point to new file?
first_file = current_sequence[pos][1][0]
second_file = current_sequence[pos][1][1]
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
modified_pos = checkExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# Checks if first file is closed
modified_pos = checkClosed(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
if second_file in open_file_map and open_file_map[second_file] == 1:
# Insert dependency - close the second file
modified_sequence.insert(modified_pos, insertClose(second_file, open_dir_map, open_file_map, file_length_map, modified_pos))
modified_pos += 1
# We have removed the first file, and created a second file
if first_file in FileOptions or first_file in SecondFileOptions:
open_file_map.pop(first_file, None)
open_file_map[second_file] = 0
elif first_file in DirOptions or first_file in SecondDirOptions:
open_dir_map.pop(first_file, None)
open_dir_map[second_file] = 0
elif command == 'symlink':
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# No dependency checks
pass
elif command == 'remove' or command == 'unlink':
# Close any open file handle and then unlink
file = current_sequence[pos][1]
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
modified_pos = checkExistsDep(current_sequence, pos, modified_sequence, modified_pos,open_dir_map, open_file_map, file_length_map)
modified_pos = checkClosed(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# Remove file from map
open_file_map.pop(file, None)
elif command == 'removexattr':
# Check that file exists
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
modified_pos = checkExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# setxattr
modified_pos = checkXattr(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
elif command == 'fsync' or command == 'fdatasync' or command == 'fsetxattr':
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
modified_pos = checkExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
elif command == 'none' or command == 'sync':
pass
elif command == 'truncate':
file = current_sequence[pos][1][0]
option = current_sequence[pos][1][1]
modified_pos = checkParentExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# if file doesn't exist, has to be created and opened
modified_pos = checkExistsDep(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
# Put some data into the file
modified_pos = checkFileLength(current_sequence, pos, modified_sequence, modified_pos, open_dir_map, open_file_map, file_length_map)
else:
print(command)
print('Invalid command')
return modified_pos
# Helper to merge lists
def flatList(op_list):
flat_list = list()
if not isinstance(op_list, str):
for sublist in op_list:
if not isinstance(sublist, str):
for item in sublist:
flat_list.append(item)
else:
flat_list.append(sublist)
else:
flat_list.append(op_list)
return flat_list
# Returns a list of lines to output into J2lang file.
def buildJ2lang(sequence):
# Python2 does not support nonlocal variables,
# workaround by using dictionary.
d = dict(lines=["# J2-Lang\n"], file_num=1, options_num=1)
def add_line(line):
d['lines'].append(line + "\n")
def new_file(options):
fname = "file" + str(d['file_num'])
d['file_num'] += 1
options = list(map(lambda f: f.replace('/', ''), options))
add_line("{} {}".format(fname, " ".join(options)))
return fname
def new_option(options):
optname = "option" + str(d['options_num'])
d['options_num'] += 1
options = list(map(str, options))
add_line("{} {}".format(optname, " ".join(options)))
return optname
for op, parameters in sequence:
if op == "creat":
f1 = new_file(parameters[0])
add_line("open ${} O_RDWR|O_CREAT 0777".format(f1))
elif op == "mkdir":
f1 = new_file(parameters[0])
add_line("mkdir ${} 0777".format(f1))
elif op == "falloc":
f1 = new_file(parameters[0])
op1 = new_option(parameters[1])
op2 = new_option(parameters[2])
add_line("falloc ${} ${} ${}".format(f1, op1, op2))
elif op == "write":
f1 = new_file(parameters[0])
op1 = new_option(parameters[1])
add_line("write ${} ${}".format(f1, op1))
elif op == "dwrite":
f1 = new_file(parameters[0])
op1 = new_option(parameters[1])
add_line("dwrite ${} ${}".format(f1, op1))
elif op == "mmapwrite":
f1 = new_file(parameters[0])
op1 = new_option(parameters[1])
add_line("mmapwrite ${} ${}".format(f1, op1))
elif (op == "link" or op == "rename"):
f1, f2 = new_file(parameters[0]), new_file(parameters[1])
add_line("{} ${} ${}".format(op, f1, f2))
elif (op == "unlink" or op == "remove" or op == "fsetxattr" or op == "removexattr" or op == "fdatasync"):
f1 = new_file(parameters[0])
add_line("{} ${}".format(op, f1))
elif op == "truncate":
def map_truncate_option(opt):
if opt == "aligned": return "0"
else: return "2500"
f1 = new_file(parameters[0])
opt = new_option(list(map(map_truncate_option, parameters[1])))
add_line("truncate ${} ${}".format(f1, opt))
else:
raise ValueError("Operation '{}' with parameters {} is unsupported".format(op, parameters))
return d['lines']
# Creates the actual J-lang file.
def buildJlang(op_list, length_map):
flat_list = list()
if not isinstance(op_list, str):
for sublist in op_list:
if not isinstance(sublist, str):
for item in sublist:
flat_list.append(item)
else:
flat_list.append(sublist)
else:
flat_list.append(op_list)
command_str = ''
command = flat_list[0]
if command == 'open':
file = flat_list[1]
if file in DirOptions or file in SecondDirOptions or file in TestDirOptions:
command_str = command_str + 'opendir ' + file.replace('/','') + ' 0777'
else:
command_str = command_str + 'open ' + file.replace('/','') + ' O_RDWR|O_CREAT 0777'
if command == 'creat':
file = flat_list[1]
command_str = command_str + 'open ' + file.replace('/','') + ' O_RDWR|O_CREAT 0777'
if command == 'mkdir':
file = flat_list[1]
command_str = command_str + 'mkdir ' + file.replace('/','') + ' 0777'
if command == 'mknod':
file = flat_list[1]
command_str = command_str + 'mknod ' + file.replace('/','') + ' TEST_FILE_PERMS|S_IFCHR|S_IFBLK' + ' 0'
if command == 'falloc':
file = flat_list[1]
option = flat_list[2]
write_op = flat_list[3]
command_str = command_str + 'falloc ' + file.replace('/','') + ' ' + str(option) + ' '
if write_op == 'append':
off = str(length_map[file])
lenn = '32768'
length_map[file] += 32768
elif write_op == 'overlap_unaligned_start':
off = '0'
lenn = '5000'
elif write_op == 'overlap_unaligned_end':
size = length_map[file]
off = str(size-5000)
lenn = '5000'
elif write_op == 'overlap_extend':
size = length_map[file]
off = str(size-2000)
lenn = '5000'
length_map[file] += 3000
command_str = command_str + off + ' ' + lenn
if command == 'write':
file = flat_list[1]
write_op = flat_list[2]
command_str = command_str + 'write ' + file.replace('/','') + ' '
if write_op == 'append':
lenn = '32768'
if file not in length_map:
length_map[file] = 0
off = '0'
else:
off = str(length_map[file])
length_map[file] += 32768
elif write_op == 'overlap_unaligned_start':
off = '0'
lenn = '5000'
elif write_op == 'overlap_unaligned_end':
size = length_map[file]
off = str(size-5000)
lenn = '5000'