12
12
# values.
13
13
#
14
14
15
- import gyb , os , os .path , shutil , subprocess
15
+ import argparse
16
+ import json
17
+ import os
18
+ import os .path
19
+ import shutil
20
+ import subprocess
21
+ import sys
22
+ import tempfile
23
+ import gyb
24
+
16
25
17
26
def find_which (p ):
18
27
for d in os .environ ["PATH" ].split (os .pathsep ):
19
- full = os .path .join (d ,p )
28
+ full = os .path .join (d , p )
20
29
if os .path .isfile (full ) and os .access (full , os .X_OK ):
21
30
return full
22
31
return p
23
32
33
+
24
34
# Evidently the debug-symbol reader in dtrace is sufficiently slow and/or buggy
25
35
# that attempting to inject probes into a binary w/ debuginfo is asking for a
26
36
# failed run (possibly racing with probe insertion, or probing the stabs
27
37
# entries, see rdar://problem/7037927 or rdar://problem/11490861 respectively),
28
38
# so we sniff the presence of debug symbols here.
29
39
def has_debuginfo (swiftc ):
30
40
swiftc = find_which (swiftc )
31
- for line in subprocess .check_output (["dwarfdump" , "--file-stats" , swiftc ]).splitlines ():
41
+ for line in subprocess .check_output (
42
+ ["dwarfdump" , "--file-stats" , swiftc ]).splitlines ():
32
43
if '%' not in line :
33
44
continue
34
45
fields = line .split ()
@@ -40,16 +51,15 @@ def has_debuginfo(swiftc):
40
51
def write_input_file (args , ast , d , n ):
41
52
fname = "in%d.swift" % n
42
53
pathname = os .path .join (d , fname )
43
- with open (pathname ,'w+' ) as f :
54
+ with open (pathname , 'w+' ) as f :
44
55
f .write (gyb .execute_template (ast , '' , N = n ))
45
56
return fname
46
57
47
58
48
59
def run_once_with_primary (args , ast , rng , primary_idx ):
49
- import sys , tempfile , json
50
60
r = {}
51
61
try :
52
- if args .tmpdir != None and not os .path .exists (args .tmpdir ):
62
+ if args .tmpdir is not None and not os .path .exists (args .tmpdir ):
53
63
os .makedirs (args .tmpdir , 0700 )
54
64
d = tempfile .mkdtemp (dir = args .tmpdir )
55
65
inputs = [write_input_file (args , ast , d , i ) for i in rng ]
@@ -85,7 +95,8 @@ def run_once_with_primary(args, ast, rng, primary_idx):
85
95
86
96
if args .dtrace :
87
97
trace = "trace.txt"
88
- script = "pid$target:swiftc:*%s*:entry { @[probefunc] = count() }" % args .select
98
+ script = ("pid$target:swiftc:*%s*:entry { @[probefunc] = count() }"
99
+ % args .select )
89
100
subprocess .check_call (
90
101
["sudo" , "dtrace" , "-q" ,
91
102
"-o" , trace ,
@@ -108,7 +119,7 @@ def run_once_with_primary(args, ast, rng, primary_idx):
108
119
finally :
109
120
shutil .rmtree (d )
110
121
111
- return {k :v for (k ,v ) in r .items () if args .select in k }
122
+ return {k : v for (k , v ) in r .items () if args .select in k }
112
123
113
124
114
125
def run_once (args , ast , rng ):
@@ -125,6 +136,7 @@ def run_once(args, ast, rng):
125
136
else :
126
137
return run_once_with_primary (args , ast , rng , - 1 )
127
138
139
+
128
140
def run_many (args ):
129
141
130
142
if args .dtrace and has_debuginfo (args .swiftc_binary ):
@@ -148,25 +160,24 @@ def run_many(args):
148
160
149
161
150
162
def linear_regression (x , y ):
151
- # By the book: https://en.wikipedia.org/wiki/Simple_linear_regression
152
- n = len (x )
153
- assert n == len (y )
154
- if n == 0 :
155
- return 0 , 0
156
- prod_sum = 0
157
- sum_x = sum (x )
158
- sum_y = sum (y )
159
- sum_prod = sum (a * b for a , b in zip (x , y ))
160
- sum_x_sq = sum (a ** 2 for a in x )
161
- mean_x = sum_x / n
162
- mean_y = sum_y / n
163
- mean_prod = sum_prod / n
164
- mean_x_sq = sum_x_sq / n
165
- covar_xy = mean_prod - mean_x * mean_y
166
- var_x = mean_x_sq - mean_x ** 2
167
- slope = covar_xy / var_x
168
- inter = mean_y - slope * mean_x
169
- return slope , inter
163
+ # By the book: https://en.wikipedia.org/wiki/Simple_linear_regression
164
+ n = len (x )
165
+ assert n == len (y )
166
+ if n == 0 :
167
+ return 0 , 0
168
+ sum_x = sum (x )
169
+ sum_y = sum (y )
170
+ sum_prod = sum (a * b for a , b in zip (x , y ))
171
+ sum_x_sq = sum (a ** 2 for a in x )
172
+ mean_x = sum_x / n
173
+ mean_y = sum_y / n
174
+ mean_prod = sum_prod / n
175
+ mean_x_sq = sum_x_sq / n
176
+ covar_xy = mean_prod - mean_x * mean_y
177
+ var_x = mean_x_sq - mean_x ** 2
178
+ slope = covar_xy / var_x
179
+ inter = mean_y - slope * mean_x
180
+ return slope , inter
170
181
171
182
172
183
def report (args , rng , runs ):
@@ -199,7 +210,6 @@ def report(args, rng, runs):
199
210
200
211
201
212
def main ():
202
- import argparse , sys
203
213
parser = argparse .ArgumentParser ()
204
214
parser .add_argument (
205
215
'file' , type = argparse .FileType (),
@@ -249,7 +259,7 @@ def main():
249
259
default = None , help = 'directory to create tempfiles in' )
250
260
parser .add_argument (
251
261
'--select' ,
252
- default = "" , help = 'substring of counters/symbols to restrict attention to' )
262
+ default = "" , help = 'substring of counters/symbols to limit attention to' )
253
263
parser .add_argument (
254
264
'--debug' , action = 'store_true' ,
255
265
default = False , help = 'invoke lldb on each scale test' )
@@ -279,5 +289,6 @@ def main():
279
289
exit (1 )
280
290
exit (0 )
281
291
292
+
282
293
if __name__ == '__main__' :
283
294
main ()
0 commit comments