Skip to content

Commit afec0bb

Browse files
authored
Merge pull request #14 from arthurscchan/Development
Merge non-deterministic handling to master branch
2 parents eeede85 + 5e40b5b commit afec0bb

File tree

15 files changed

+158
-73
lines changed

15 files changed

+158
-73
lines changed

benchmarkProgram/concurrentSyscall/grpReadWrite/cmdForkReadWrite/prepare

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,25 @@ CODE=$(cat <<EOF
1818
#include <stdlib.h>
1919
#include <fcntl.h>
2020
#include <unistd.h>
21-
int main() { srand(time(NULL));
22-
if (fork()) {int id=open("`realpath $1`/test.txt", O_RDWR); char buf[1]; int i;
23-
#ifdef READ
24-
for (i=0;i<READ;i++) {
25-
#ifdef RANDOM
26-
sleep(rand()%20);
27-
#endif
28-
#ifdef PROGRAM
29-
read(id, buf, 1);
30-
#endif
31-
}
21+
int main() {
22+
int id=open("`realpath $1`/test.txt", O_RDWR); char buf[1]; int i;
23+
fork();
24+
#ifdef READ
25+
for (i=0;i<READ;i++) {
26+
#ifdef PROGRAM
27+
read(id, buf, 1);
3228
#endif
33-
close(id);
34-
} else {int id=open("`realpath $1`/test.txt", O_RDWR); int i;
35-
#ifdef WRITE
36-
for (i=0;i<WRITE;i++) {
37-
#ifdef RANDOM
38-
sleep(rand()%20);
39-
#endif
40-
#ifdef PROGRAM
41-
write(id, "TEST", 1);
42-
#endif
43-
}
29+
}
30+
#endif
31+
#ifdef WRITE
32+
for (i=0;i<WRITE;i++) {
33+
#ifdef PROGRAM
34+
write(id, "TEST", 1);
4435
#endif
45-
close(id);
46-
}}
36+
}
37+
#endif
38+
close(id);
39+
}
4740
EOF
4841
)
4942

config/config.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
[spg]
22
stage1tool = startSpade.py -d
33
stage2handler = dot2Clingo.py
4-
template = output.dot-%%s
54

65
[spn]
76
stage1tool = startSpade.py -n
87
stage2handler = neo4j2Clingo.py -d
9-
template = output.db-%%s
108

119
[opu]
1210
stage1tool = startOpus.py
1311
stage2handler = neo4j2Clingo.py -d
14-
template = output.db-%%s
1512

1613
[cam]
1714
stage1tool = startCamflow.py
1815
stage2handler = provJson2Clingo.py
19-
template = output.provjson-%%s

fullAutomation.py

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def prepareDir(directory):
3535
trial = 2
3636
baseDir = os.path.abspath(os.path.dirname(sys.argv[0]))
3737
tool = sys.argv[1]
38+
if tool != 'cam':
39+
print ('Temporary disable spade and opus for new concurrency testing, please revert to git version a28cc39585c6e74dbeccfc3dccfeef537857ff22 for testing spade and opus in a stable version')
40+
quit()
3841
toolBaseDir = os.path.abspath(sys.argv[2])
3942
benchmarkDir = os.path.abspath(sys.argv[3])
4043
stageDir = os.path.abspath('%s/stage/' % baseDir)
@@ -51,19 +54,22 @@ def prepareDir(directory):
5154

5255
stage1Tool = config[tool]['stage1tool']
5356
stage2Handler = config[tool]['stage2handler']
54-
template = config[tool]['template']
5557

5658
#Stage 1 - Start the tools and generate graph (neo4j / dot / provjson)
5759
start = time.time()
5860
print ('Starting stage 1...Generating provenance from native tools')
5961

6062
os.system('sudo chmod +x %s/startTool/%s' % (baseDir, stage1Tool.split()[0]))
6163
stage1Command = 'sudo %s/startTool/%s %s %s %s %s %s %s %d' % (baseDir, stage1Tool, stageDir, workingDir, '%s' , '%s', toolBaseDir , '%s', trial)
64+
6265
print ('Program')
63-
subprocess.call((stage1Command % (benchmarkDir, 'PROGRAM,RANDOM,READ=2,WRITE=2', 'program')).split())
66+
#programFingerprint = subprocess.check_output((stage1Command % (benchmarkDir, 'PROGRAM,RANDOM,READ=2,WRITE=2', 'program')).split())
67+
programFingerprint = subprocess.check_output((stage1Command % (benchmarkDir, 'PROGRAM,READ=2,WRITE=2', 'program')).split()).decode().split()
6468
print ('End Program')
69+
6570
print ('Control')
66-
subprocess.call((stage1Command % (benchmarkDir, 'CONTROL,RANDOM,READ=2,WRITE=2', 'control')).split())
71+
#controlFingerprint = subprocess.check_output((stage1Command % (benchmarkDir, 'CONTROL,RANDOM,READ=2,WRITE=2', 'control')).split())
72+
controlFingerprint = subprocess.check_output((stage1Command % (benchmarkDir, 'CONTROL,READ=2,WRITE=2', 'control')).split()).decode().split()
6773
print ('End Control')
6874

6975
print ('End of stage 1\n')
@@ -75,13 +81,26 @@ def prepareDir(directory):
7581
print ('Starting stage 2...Transforming provenance result to Clingo graph')
7682

7783
os.system('sudo chmod +x %s/genClingoGraph/%s' % (baseDir, stage2Handler.split()[0]))
78-
stage2Command = 'sudo %s/genClingoGraph/%s %s %s %s' % (baseDir, stage2Handler, '%s', template, workingDir)
79-
for i in range(1,trial+1):
80-
suffix = 'control-%d' % i
81-
subprocess.call((stage2Command % (suffix,suffix)).split())
82-
for i in range(1,trial+1):
83-
suffix = 'program-%d' % i
84-
subprocess.call((stage2Command % (suffix,suffix)).split())
84+
stage2Command = 'sudo %s/genClingoGraph/%s %s %s %s' % (baseDir, stage2Handler, '%s', '%s', '%s')
85+
86+
for fingerprint in controlFingerprint:
87+
dir = os.path.abspath('%s/%s' % (workingDir, fingerprint))
88+
if os.path.isdir(dir):
89+
i = 1
90+
for file in os.listdir(dir):
91+
suffix = 'control-%d' % i
92+
subprocess.call((stage2Command % (suffix,file,dir)).split())
93+
i += 1
94+
95+
for fingerprint in programFingerprint:
96+
dir = os.path.abspath('%s/%s' % (workingDir, fingerprint))
97+
if os.path.isdir(dir):
98+
i = 1
99+
for file in os.listdir(dir):
100+
suffix = 'program-%d' % i
101+
subprocess.call((stage2Command % (suffix,file,dir)).split())
102+
i += 1
103+
85104
print ('End of stage 2\n')
86105
end = time.time()
87106
t2 = end-start
@@ -93,32 +112,52 @@ def prepareDir(directory):
93112
os.system('sudo chmod +x %s/processGraph/generalizeGraph.py' % baseDir)
94113
stage3Command = 'sudo %s/processGraph/generalizeGraph.py %s %s %s' % (baseDir, workingDir, ('%s/processGraph/template.lp' % baseDir), '%s')
95114

96-
command = stage3Command % ('control %s')
97-
for i in range(1,trial+1):
98-
suffix = 'control-%d' % i
99-
command = command % ('%s/clingo-%s %s' % (workingDir, suffix, '%s'))
100-
subprocess.call((command % ' ').split())
101-
102-
command = stage3Command % ('program %s')
103-
for i in range(1,trial+1):
104-
suffix = 'program-%d' % i
105-
command = command % ('%s/clingo-%s %s' % (workingDir, suffix,'%s'))
106-
subprocess.call((command % '').split())
115+
for fingerprint in controlFingerprint:
116+
dir = os.path.abspath('%s/%s' % (workingDir, fingerprint))
117+
if os.path.isdir(dir):
118+
command = stage3Command % ('control-%s %s' % (fingerprint, '%s'))
119+
for file in ('%s/%s' % (dir,name) for name in os.listdir(dir) if name.startswith('clingo-control')):
120+
command = command % ('%s %s' % (file, '%s'))
121+
subprocess.call((command % '').split())
122+
123+
for fingerprint in programFingerprint:
124+
dir = os.path.abspath('%s/%s' % (workingDir, fingerprint))
125+
if os.path.isdir(dir):
126+
command = stage3Command % ('program-%s %s' % (fingerprint, '%s'))
127+
for file in ('%s/%s' % (dir,name) for name in os.listdir(dir) if name.startswith('clingo-program')):
128+
command = command % ('%s %s' % (file, '%s'))
129+
subprocess.call((command % '').split())
107130

108131
print ('End of stage 3\n')
109132
end = time.time()
110133
t3 = end-start
111134

112-
shutil.copyfile('%s/general.clingo-program' % workingDir, '%s/general.clingo-program' % outDir)
113-
shutil.copyfile('%s/general.clingo-control' % workingDir, '%s/general.clingo-control' % outDir)
135+
for file in (file for file in os.listdir(workingDir) if file.startswith('general.clingo')):
136+
shutil.copyfile('%s/%s' % (workingDir,file), '%s/%s' % (outDir,file))
114137

115138
#Stage 4 - Compare and generate benchmark
116139
start = time.time()
117140
print ('Starting stage 4...Generating benchmark')
118141

119142
os.system('sudo chmod +x %s/processGraph/findSubgraph.py' % baseDir)
120-
stage4Command = 'sudo %s/processGraph/findSubgraph.py %s %s 1 general.clingo-control general.clingo-program %s' % (baseDir, workingDir, ('%s/processGraph/template.lp' % baseDir), ('%s/result.clingo' % outDir))
121-
subprocess.call(stage4Command.split())
143+
for fingerprint in programFingerprint:
144+
if len(controlFingerprint) > 1:
145+
editDistance = sys.maxsize
146+
for backgroundFingerprint in controlFingerprint:
147+
preStage4Command = '''sudo %s/processGraph/calEditDistance.py %s %s/general.clingo-control-%s
148+
%s/general.clingo-program-%s''' % (baseDir, ('''%s/processGraph/editdist.lp
149+
''' % baseDir), workingDir, backgroundFingerprint, workingDir, fingerprint)
150+
newEditDistance = int(subprocess.check_output(preStage4Command.split()))
151+
if newEditDistance < editDistance:
152+
editDistance = newEditDistance
153+
background = backgroundFingerprint
154+
else:
155+
background = controlFingerprint[0]
156+
157+
stage4Command = '''sudo %s/processGraph/findSubgraph.py %s %s 1 general.clingo-control-%s general.clingo-program-%s %s
158+
''' % (baseDir, workingDir, ('%s/processGraph/template.lp' % baseDir), background, fingerprint, ('''%s/result-%s.clingo
159+
''' % (outDir,fingerprint)))
160+
subprocess.call(stage4Command.split())
122161

123162
print ('End of stage 4\n')
124163
end = time.time()
3.22 KB
Binary file not shown.

processGraph/calEditDistance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
result = dict()
1818

19-
print (processGraph(sys.argv[2], sys.argv[3], clingoCode, False))
19+
print (processGraph(sys.argv[2], sys.argv[3], clingoCode, os.path.abspath('%s/../' % os.path.dirname(sys.argv[0])), False))

processGraph/clingoFunction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def compareProps(graph1Props, graph2Props, isGeneral):
125125
return result
126126

127127
#Graph Process
128-
def processGraph(graph1Path, graph2Path, clingoCode, isMapping):
128+
def processGraph(graph1Path, graph2Path, clingoCode, baseDir, isMapping):
129129
#Read Graph
130130
file = open(graph1Path, 'r')
131131
graph1 = fixIdentifier(file.read(), 1)
@@ -142,7 +142,7 @@ def processGraph(graph1Path, graph2Path, clingoCode, isMapping):
142142

143143
#Clingo Operation
144144
inputString = '%s\n%s\n%s'%(clingoCode, graph1, graph2)
145-
pipe = subprocess.Popen(['../clingo/clingo', '--time-limit=30'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
145+
pipe = subprocess.Popen(['%s/clingo/clingo' % baseDir, '--time-limit=30'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
146146
mapResult = pipe.communicate(input=inputString.encode())[0]
147147
if isMapping:
148148
map = decodeClingoResult(mapResult.decode())

processGraph/findSubgraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
os.chdir(workingDir)
2222

2323
#Process Graph
24-
graph2Node, graph2Edge, graph1Props, graph2Props, map = processGraph(sys.argv[4], sys.argv[5], clingoCode, True)
24+
graph2Node, graph2Edge, graph1Props, graph2Props, map = processGraph(sys.argv[4], sys.argv[5], clingoCode, os.path.abspath('../'), True)
2525

2626
# Properties are present if present in graph 2 and not in linked part of graph 1
2727
diffProps = dict()

processGraph/generalizeGraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
quit()
2929

3030
for i in range(4,len(sys.argv)-1):
31-
graph2Node, graph2Edge, graph1Props, graph2Props, map = processGraph(sys.argv[i], sys.argv[i+1], clingoCode, True)
31+
graph2Node, graph2Edge, graph1Props, graph2Props, map = processGraph(sys.argv[i], sys.argv[i+1], clingoCode, os.path.abspath('../'), True)
3232

3333
#Generalize properties
3434
if map:

startTool/startCamflow.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import re
45
import sys
56
import time
67
import shutil
78
import subprocess
89
import json
10+
import hashlib
911

1012
#Merger Json
1113
def mergeJson(base, line):
@@ -92,14 +94,21 @@ def startCamflow(stagePath, workingPath, suffix, isModel):
9294
subprocess.call('camflow --duplicate true'.split())
9395
subprocess.call('camflow -e true'.split())
9496
# subprocess.call('camflow -a true'.split())
95-
os.system('%s/test' % stagePath)
97+
subprocess.call(('trace-cmd record -e syscalls %s/test' % stagePath).split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
9698
# subprocess.call('camflow -a false'.split())
9799
subprocess.call('camflow -e false'.split())
98100
# subprocess.call('camflow --duplicate false'.split())
99101
subprocess.call(('camflow --track-file %s/test false' % stagePath).split())
100102
time.sleep(1)
101103
subprocess.call('service camflowd stop'.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
102104

105+
#Handle FTrace Fingerprint
106+
ftraceResult = subprocess.check_output('trace-cmd report'.split())
107+
if ftraceResult:
108+
syscallList = [line.split(':')[1].strip() for line in ftraceResult.decode('ascii').split('\n') if re.match(r'^\s*test-((?!wait4).)*$',line)]
109+
110+
fingerprint = hashlib.md5(''.join(syscallList).encode()).hexdigest()
111+
103112
#Process provenance result into 1 json
104113
result={}
105114
try:
@@ -124,9 +133,14 @@ def startCamflow(stagePath, workingPath, suffix, isModel):
124133
# file.write(json.dumps(mergeNode(oldNode,result)))
125134
# file.close()
126135

136+
#Handle fingerprint folder
137+
if not os.path.exists('%s/%s' %(workingPath, fingerprint)):
138+
os.makedirs('%s/%s' %(workingPath, fingerprint))
139+
os.chown('%s/%s' %(workingPath, fingerprint), 1000, 1000)
140+
127141
if not isModel:
128142
#Writing result to json
129-
file = open('%s/output.provjson-%s' %(workingPath, suffix), 'w')
143+
file = open('%s/%s/output.provjson-%s' %(workingPath, fingerprint, suffix), 'w')
130144
file.write(json.dumps(result))
131145
file.close()
132146

@@ -135,6 +149,7 @@ def startCamflow(stagePath, workingPath, suffix, isModel):
135149
except IOError:
136150
pass
137151

152+
return fingerprint
138153

139154
#Retrieve arguments
140155
trial = 0
@@ -160,12 +175,16 @@ def startCamflow(stagePath, workingPath, suffix, isModel):
160175
for item in macroOpt.split(','):
161176
gccMacro = "%s -D%s" %(gccMacro,item)
162177

178+
fingerprintSet = set()
179+
163180
#Create Model Data
164181
#subprocess.check_output(('%s/prepare %s %s --static' %(progPath, stagePath, gccMacro)).split())
165182
#startCamflow(stagePath, workingPath, '', True)
166183

167184
for i in range(1, trial+1):
168185
#Prepare the benchmark program
169186
subprocess.check_output(('%s/prepare %s %s --static' %(progPath, stagePath, gccMacro)).split())
170-
startCamflow(stagePath, workingPath, '%s-%d' % (suffix, i), False)
171-
187+
fingerprintSet.add(startCamflow(stagePath, workingPath, '%s-%d' % (suffix, i), False))
188+
189+
for fingerprint in fingerprintSet:
190+
print (fingerprint)

startTool/startOpus.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import re
45
import sys
56
import time
7+
import hashlib
68
import subprocess
79

810
#Retrieve arguments
@@ -35,6 +37,8 @@
3537

3638
os.chdir(workingPath)
3739

40+
fingerprintSet = set()
41+
3842
#Get Audit Log
3943
for i in range(1, trial+1):
4044
#Prepare the benchmark program
@@ -69,10 +73,25 @@
6973
subprocess.call((baseCommand % 'server start').split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
7074

7175
#Capture Provenance
72-
subprocess.call((baseCommand % ('process launch %s/test' % stagePath)).split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
76+
subprocess.call((baseCommand % ('process launch trace-cmd record -e syscalls %s/test' % stagePath)).split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
7377

7478
#Stop Opus Server
7579
subprocess.call((baseCommand % 'server stop').split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
7680

77-
#Wait for Data Flushing
78-
time.sleep(2)
81+
#Handle FTrace Fingerprint
82+
ftraceResult = subprocess.check_output('trace-cmd report'.split())
83+
if ftraceResult:
84+
syscallList = [line.split(':')[1].strip() for line in ftraceResult.decode('ascii').split('\n') if re.match(r'^\s*test-((?!wait4).)*$',line)]
85+
86+
fingerprint = hashlib.md5(''.join(syscallList).encode()).hexdigest()
87+
fingerprintSet.add(fingerprint)
88+
89+
#Handle fingerprint folder
90+
if not os.path.exists('%s/%s' %(workingPath, fingerprint)):
91+
os.makedirs('%s/%s' %(workingPath, fingerprint))
92+
os.chown('%s/%s' %(workingPath, fingerprint), 1000, 1000)
93+
94+
os.rename('%s/output.db-%s-%d' % (workingPath,suffix,i),'%s/%s/output.db-%s-%d' % (workingPath,fingerprint,suffix,i))
95+
96+
for fingerprint in fingerprintSet:
97+
print(fingerprint)

0 commit comments

Comments
 (0)