Skip to content

Commit cc9dd47

Browse files
authored
Merge pull request #5607 from graydon/scale-test-improvements
2 parents 8b07e54 + c6d38f5 commit cc9dd47

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

test/lit.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ config.swift_utils = os.path.join(config.swift_src_root, 'utils')
292292
config.line_directive = os.path.join(config.swift_utils, 'line-directive')
293293
config.gyb = os.path.join(config.swift_utils, 'gyb')
294294
config.rth = os.path.join(config.swift_utils, 'rth') # Resilience test helper
295+
config.scale_test = os.path.join(config.swift_utils, 'scale-test')
295296
config.PathSanitizingFileCheck = os.path.join(config.swift_utils, 'PathSanitizingFileCheck')
296297
config.swift_lib_dir = os.path.join(os.path.dirname(os.path.dirname(config.swift)), 'lib')
297298

@@ -975,6 +976,7 @@ config.substitutions.append(('%utils', config.swift_utils))
975976
config.substitutions.append(('%line-directive', config.line_directive))
976977
config.substitutions.append(('%gyb', config.gyb))
977978
config.substitutions.append(('%rth', config.rth))
979+
config.substitutions.append(('%scale-test', config.scale_test))
978980

979981
config.substitutions.append(('%target-sil-opt', config.target_sil_opt))
980982
config.substitutions.append(('%target-sil-extract', config.target_sil_extract))

utils/scale-test

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ def write_input_file(args, ast, d, n):
4444
return ifile
4545

4646

47-
def run_once(args, ast, rng):
47+
def run_once_with_primary(args, ast, rng, primary_idx):
4848
import sys, shutil, tempfile, json
4949
r = {}
5050
try:
5151
d = tempfile.mkdtemp()
5252
inputs = [write_input_file(args, ast, d, i) for i in rng]
53-
primary = inputs[-1]
53+
primary = inputs[primary_idx]
5454
ofile = os.path.join(d, "out.o")
5555
mode = "-c"
5656
if args.parse:
@@ -72,11 +72,13 @@ def run_once(args, ast, rng):
7272
[line.split() for line in open(trace)]
7373
if len(fields) == 2}
7474
else:
75+
if args.debug:
76+
command = ["lldb", "--"] + command
7577
stats = os.path.join(d, "stats.json")
76-
subprocess.check_call(
77-
command + ["-Xllvm", "-stats",
78-
"-Xllvm", "-stats-json",
79-
"-Xllvm", "-info-output-file=" + stats])
78+
argv = command + ["-Xllvm", "-stats",
79+
"-Xllvm", "-stats-json",
80+
"-Xllvm", "-info-output-file=" + stats]
81+
subprocess.check_call(argv)
8082
with open(stats) as f:
8183
r = json.load(f)
8284
finally:
@@ -85,6 +87,20 @@ def run_once(args, ast, rng):
8587
return {k:v for (k,v) in r.items() if args.select in k}
8688

8789

90+
def run_once(args, ast, rng):
91+
if args.sum_multi:
92+
cumulative = {}
93+
for i in range(len(rng)):
94+
tmp = run_once_with_primary(args, ast, rng, i)
95+
for (k, v) in tmp.items():
96+
if k in cumulative:
97+
cumulative[k] += v
98+
else:
99+
cumulative[k] = v
100+
return cumulative
101+
else:
102+
return run_once_with_primary(args, ast, rng, -1)
103+
88104
def run_many(args):
89105

90106
if args.dtrace and has_debuginfo(args.swiftc_binary):
@@ -101,23 +117,51 @@ def run_many(args):
101117

102118
ast = gyb.parse_template(args.file.name, args.file.read())
103119
rng = range(args.begin, args.end, args.step)
104-
if args.multi_file:
105-
return (rng, [run_once(args, ast, rng[0:i+1]) for i in range(len(rng))])
120+
if args.multi_file or args.sum_multi:
121+
return (rng, [run_once(args, ast, range(i)) for i in rng])
106122
else:
107123
return (rng, [run_once(args, ast, [r]) for r in rng])
108124

109125

126+
def linear_regression(x, y):
127+
# By the book: https://en.wikipedia.org/wiki/Simple_linear_regression
128+
n = len(x)
129+
assert n == len(y)
130+
if n == 0:
131+
return 0, 0
132+
prod_sum = 0
133+
sum_x = sum(x)
134+
sum_y = sum(y)
135+
sum_prod = sum(a * b for a, b in zip(x, y))
136+
sum_x_sq = sum(a ** 2 for a in x)
137+
mean_x = sum_x/n
138+
mean_y = sum_y/n
139+
mean_prod = sum_prod/n
140+
mean_x_sq = sum_x_sq/n
141+
covar_xy = mean_prod - mean_x * mean_y
142+
var_x = mean_x_sq - mean_x**2
143+
slope = covar_xy / var_x
144+
inter = mean_y - slope * mean_x
145+
return slope, inter
146+
147+
110148
def report(args, rng, runs):
111-
import numpy as np
149+
import math
112150
bad = False
113151
keys = set.intersection(*[set(j.keys()) for j in runs])
114-
A = np.vstack([np.log(rng), np.ones(len(rng))]).T
152+
if len(keys) == 0:
153+
print "No data found"
154+
if len(args.select) != 0:
155+
"(perhaps try a different --select?)"
156+
return True
157+
x = [math.log(n) for n in rng]
115158
rows = []
116159
for k in keys:
117160
vals = [r[k] for r in runs]
118161
bounded = [max(v, 1) for v in vals]
119-
b, a = np.linalg.lstsq(A, np.log(bounded))[0]
120-
b = 0 if np.isclose(b, 0) else b
162+
y = [math.log(b) for b in bounded]
163+
b, a = linear_regression(x, y)
164+
b = 0 if abs(b) < 1e-9 else b
121165
rows.append((b, k, vals))
122166
rows.sort()
123167
tolerance = 1.2
@@ -153,6 +197,9 @@ def main():
153197
parser.add_argument(
154198
'--multi-file', action='store_true',
155199
default=False, help='vary number of input files as well')
200+
parser.add_argument(
201+
'--sum-multi', action='store_true',
202+
default=False, help='simulate a multi-primary run and sum stats')
156203
parser.add_argument(
157204
'--begin', type=int,
158205
default=10, help='first value for N')
@@ -168,6 +215,9 @@ def main():
168215
parser.add_argument(
169216
'--select',
170217
default="", help='substring of counters/symbols to restrict attention to')
218+
parser.add_argument(
219+
'--debug', action='store_true',
220+
default=False, help='invoke lldb on each scale test')
171221

172222
args = parser.parse_args(sys.argv[1:])
173223
(rng, runs) = run_many(args)

validation-test/lit.site.cfg.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ else:
5252
if "@CMAKE_BUILD_TYPE@" in ["Debug", "RelWithDebInfo"]:
5353
config.available_features.add('tools-debuginfo')
5454

55+
# If tools are release-mode, set the tools-release flag.
56+
if "@CMAKE_BUILD_TYPE@" in ["Release", "RelWithDebInfo"]:
57+
config.available_features.add('tools-release')
58+
5559
if "@SWIFT_STDLIB_ASSERTIONS@" == "TRUE":
5660
config.available_features.add('swift_stdlib_asserts')
5761
else:

0 commit comments

Comments
 (0)