forked from DependableSystemsLab/LLTFI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.py
executable file
·199 lines (164 loc) · 6.61 KB
/
profile.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
#! /usr/bin/env python
"""
%(prog)s takes a profiling executable and executes it
Usage: %(prog)s --CLI/--GUI <profiling executable> <the same options that you use to run the excutable before>
%(prog)s --help(-h): show help information
Prerequisite:
0. You need to specify --CLI or --GUI depending on whether you're invoking it from the command line or from the GUI.
1. You need to be at the parent directory of the <profiling executable> to invoke %(prog)s. This is to make it easier for LLFI to track the outputs generated by <profiling executable>
2. (prog)s only checks recursively at the current directory for possible outputs, if your output is not under current directory, you need to store that output by yourself
3. You need to put your input files (if any) under current directory
4. You need to have 'input.yaml' under your current directory, which contains appropriate options for LLFI.
"""
# This script profiles the program to produce llfi.stat.prof.txt
import sys
import yaml
import os
import time
import subprocess
import shutil
optionlist = []
prog = os.path.basename(sys.argv[0])
# basedir is assigned in parseArgs(args)
basedir = ""
profiling_exe = ""
def usage(msg = None):
retval = 0
if msg is not None:
retval = 1
msg = "ERROR: " + msg
print(msg, file=sys.stderr)
print(__doc__ % globals(), file=sys.stderr)
sys.exit(retval)
def parseArgs(args):
global optionlist, profiling_exe, env
profiling_exe = os.path.realpath(args[0])
basedir = os.path.abspath(os.path.dirname(os.path.dirname(profiling_exe)))
optionlist = args[1:]
# remove the directory prefix for input files, this is to make it easier for the program
# to take a snapshot
for index, opt in enumerate(optionlist):
if os.path.isfile(opt):
if os.path.realpath(os.path.dirname(opt)) != basedir:
usage("File %s passed through option is not under current directory" % opt)
else:
optionlist[index] = os.path.basename(opt)
if basedir != os.getcwd():
print("Change directory to:", basedir)
os.chdir(basedir)
def checkInputYaml():
#Check for input.yaml's presence
yamldir = os.path.dirname(os.path.dirname(profiling_exe))
try:
f = open(os.path.join(yamldir, 'input.yaml'), 'r')
except:
usage("No input.yaml file in the parent directory of profiling executable")
exit(1)
#Check for input.yaml's correct formmating
try:
doc = yaml.load(f, Loader = yaml.FullLoader)
#doc = yaml.load(f)
f.close()
except:
usage("input.yaml is not formatted in proper YAML (reminder: use spaces, not tabs)")
exit(1)
################################################################################
def config():
global inputdir, outputdir, baselinedir, errordir
# config
llfi_dir = os.path.dirname(profiling_exe)
inputdir = os.path.join(llfi_dir, "prog_input")
outputdir = os.path.join(llfi_dir, "prog_output")
baselinedir = os.path.join(llfi_dir, "baseline")
errordir = os.path.join(llfi_dir, "error_output")
if not os.path.isdir(outputdir):
os.mkdir(outputdir)
if not os.path.isdir(baselinedir):
os.mkdir(baselinedir)
if not os.path.isdir(errordir):
os.mkdir(errordir)
if not os.path.isdir(inputdir):
os.mkdir(inputdir)
################################################################################
def execute(execlist):
#print "Begin"
#inputFile = open(inputfile, "r")
global outputfile
print('\t' + ' '.join(execlist))
#get state of directory
dirSnapshot()
p = subprocess.Popen(execlist, stdout=subprocess.PIPE)
start_time = time.time()
#communicate() blocks until program exit, but does not fill stdout and cause
(p_stdout,p_stderr) = p.communicate() #the target program to block
elapsetime = int(time.time() - start_time + 1) # round up to the nearest sec.
#Continue with normal execution
moveOutput()
print("\t program finish", p.returncode)
print("\t time taken", elapsetime,"\n")
outputFile = open(outputfile, "wb")
outputFile.write(p_stdout)
outputFile.close()
replenishInput() #for cases where program deletes input or alters them each run
#inputFile.close()
return p.returncode
################################################################################
def storeInputFiles():
global inputList
inputList=[]
##========Consider comma as separator of arguments ==================================
temp_optionlist = []
for item in optionlist:
if item.count(',') == 0:
temp_optionlist.append(item)
else:
temp_optionlist.extend(item.split(','))
##===================================================================================
for opt in temp_optionlist:
if os.path.isfile(opt):#stores all files in inputList and copy over to inputdir
shutil.copy2(opt, os.path.join(inputdir, opt))
inputList.append(opt)
################################################################################
def replenishInput():#TODO make condition to skip this if input is present
for each in inputList:
if not os.path.isfile(each):#copy deleted inputfiles back to basedir
shutil.copy2(os.path.join(inputdir, each), each)
################################################################################
def moveOutput():
#move all newly created files that are not "llfi.stat.prof.txt" < -- since this is a product of profiling
newfiles = [_file for _file in os.listdir(".")]
for each in newfiles:
if each not in dirBefore and each != "llfi.stat.prof.txt":
fileSize = os.stat(each).st_size
if fileSize == 0 and each.startswith("llfi"):
#empty library output, can delete
print(each + " is going to be deleted for having size of " + str(fileSize))
os.remove(each)
else:
flds = each.split(".")
newName = '.'.join(flds[0:-1])
newName+='.prof.'+flds[-1]
os.rename(each, os.path.join(baselinedir, newName))
################################################################################
def dirSnapshot():
#snapshot of directory before each execute() is performed
global dirBefore
dirBefore = [_file for _file in os.listdir(".")]
################################################################################
def main(args):
global optionlist, outputfile
parseArgs(args)
checkInputYaml()
config()
storeInputFiles()
# baseline
outputfile = os.path.join(baselinedir, "golden_std_output")
execlist = [profiling_exe]
execlist.extend(optionlist)
return execute(execlist)
################################################################################
if __name__=="__main__":
if len(sys.argv) == 1:
usage('Must provide the profiling executable and its options')
exit(1)
exit(main(sys.argv[1:]))