-
Notifications
You must be signed in to change notification settings - Fork 4
/
RAMification.py
376 lines (277 loc) · 16.8 KB
/
RAMification.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
# Copyright (C) 2018-2019 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
import argparse
import collections
import json
import math
import os
import re
import subprocess
import sys
jitTests = ["3d-cube-SP", "3d-raytrace-SP", "acorn-wtb", "ai-astar", "Air", "async-fs", "Babylon", "babylon-wtb", "base64-SP", "Basic", "Box2D", "cdjs", "chai-wtb", "coffeescript-wtb", "crypto", "crypto-aes-SP", "crypto-md5-SP", "crypto-sha1-SP", "date-format-tofte-SP", "date-format-xparb-SP", "delta-blue", "earley-boyer", "espree-wtb", "first-inspector-code-load", "FlightPlanner", "float-mm.c", "gaussian-blur", "gbemu", "gcc-loops-wasm", "hash-map", "HashSet-wasm", "jshint-wtb", "json-parse-inspector", "json-stringify-inspector", "lebab-wtb", "mandreel", "ML", "multi-inspector-code-load", "n-body-SP", "navier-stokes", "octane-code-load", "octane-zlib", "OfflineAssembler", "pdfjs", "prepack-wtb", "quicksort-wasm", "raytrace", "regex-dna-SP", "regexp", "richards", "richards-wasm", "splay", "stanford-crypto-aes", "stanford-crypto-pbkdf2", "stanford-crypto-sha256", "string-unpack-code-SP", "tagcloud-SP", "tsf-wasm", "typescript", "uglify-js-wtb", "UniPoker", "WSL"]
nonJITTests = ["3d-cube-SP", "3d-raytrace-SP", "acorn-wtb", "ai-astar", "Air", "async-fs", "Babylon", "babylon-wtb", "base64-SP", "Basic", "Box2D", "cdjs", "chai-wtb", "coffeescript-wtb", "crypto-aes-SP", "delta-blue", "earley-boyer", "espree-wtb", "first-inspector-code-load", "gaussian-blur", "gbemu", "hash-map", "jshint-wtb", "json-parse-inspector", "json-stringify-inspector", "lebab-wtb", "mandreel", "ML", "multi-inspector-code-load", "octane-code-load", "OfflineAssembler", "pdfjs", "prepack-wtb", "raytrace", "regex-dna-SP", "regexp", "splay", "stanford-crypto-aes", "string-unpack-code-SP", "tagcloud-SP", "typescript", "uglify-js-wtb"]
# Run two groups of tests with each group in a single JSC instance to see how well memory recovers between tests.
groupTests = ["typescript,acorn-wtb,Air,pdfjs,crypto-aes-SP", "splay,FlightPlanner,prepack-wtb,octane-zlib,3d-cube-SP"]
luaTests = [("hello_world-LJF", "LuaJSFight/hello_world.js", 5), ("list_search-LJF", "LuaJSFight/list_search.js", 5), ("lists-LJF", "LuaJSFight/lists.js", 5), ("string_lists-LJF", "LuaJSFight/string_lists.js", 5), ("richards", "LuaJSFight/richards.js", 5)]
oneMB = float(1024 * 1024)
footprintRE = re.compile(r"Current Footprint: (\d+(?:.\d+)?)")
peakFootprintRE = re.compile(r"Peak Footprint: (\d+(?:.\d+)?)")
TestResult = collections.namedtuple("TestResult", ["name", "returnCode", "footprint", "peakFootprint", "vmmapOutput", "smapsOutput"])
ramification_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
remoteHooks = {}
def mean(values):
if not len(values):
return None
return sum(values) / len(values)
def geomean(values):
if not len(values):
return None
product = 1.0
for x in values:
product *= x
return math.pow(product, (1.0 / len(values)))
def frameworkPathFromExecutablePath(execPath):
if not os.path.abspath(execPath):
execPath = os.path.isabs(execPath)
pathMatch = re.match("(.*?/WebKitBuild/(Release|Debug)+)/([a-zA-Z]+)$", execPath)
if pathMatch:
return pathMatch.group(1)
pathMatch = re.match("(.*?)/JavaScriptCore.framework/Resources/([a-zA-Z]+)$", execPath)
if pathMatch:
return pathMatch.group(1)
pathMatch = re.match("(.*?)/JavaScriptCore.framework/Helpers/([a-zA-Z]+)$", execPath)
if pathMatch:
return pathMatch.group(1)
pathMatch = re.match("(.*?/(Release|Debug)+)/([a-zA-Z]+)$", execPath)
if pathMatch:
return pathMatch.group(1)
pathMatch = re.match("(.*?)/JavaScriptCore.framework/(.*?)/Resources/([a-zA-Z]+)$", execPath)
if pathMatch:
return pathMatch.group(1)
def parseArgs(parser=None):
def optStrToBool(arg):
if arg.lower() in ("true", "t", "yes", "y"):
return True
if arg.lower() in ("false", "f", "no", "n"):
return False
raise argparse.ArgumentTypeError("Boolean value expected")
if not parser:
parser = argparse.ArgumentParser(description="RAMification benchmark driver script")
parser.set_defaults(runner=LocalRunner)
verbosityGroup = parser.add_mutually_exclusive_group()
verbosityGroup.add_argument("-q", "--quiet", dest="verbose", action="store_false", help="Provide less output")
verbosityGroup.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=True, help="Provide more output")
parser.add_argument("-c", "--jsc", dest="jscCommand", type=str, default="/usr/local/bin/jsc", metavar="path-to-jsc", help="Path to jsc command")
parser.add_argument("-d", "--jetstream3-dir", dest="testDir", type=str, default=ramification_dir, metavar="path-to-JetStream3-files", help="JetStream3 root directory")
parser.add_argument("-e", "--env-var", dest="extraEnvVars", action="append", default=[], metavar="env-var=value", help="Specify additional environment variables")
parser.add_argument("-f", "--format-json", dest="formatJSON", action="store_true", default=False, help="Format JSON with whitespace")
parser.add_argument("-g", "--run-grouped-tests", dest="runGroupedTests", nargs="?", const=True, default=None, type=optStrToBool, metavar="true / false", help="Run grouped tests [default]")
parser.add_argument("-j", "--run-jit", dest="runJITTests", nargs="?", const=True, default=None, type=optStrToBool, metavar="true / false", help="Run JIT tests [default]")
parser.add_argument("-l", "--lua", dest="runLuaTests", nargs="?", const=True, default=None, type=optStrToBool, metavar="true / false", help="Run Lua comparison tests [default]")
parser.add_argument("-n", "--run-no-jit", dest="runNoJITTests", nargs="?", const=True, default=None, type=optStrToBool, metavar="true / false", help="Run no JIT tests [default]")
parser.add_argument("-o", "--output", dest="jsonFilename", type=str, default=None, metavar="JSON-output-file", help="Path to JSON output")
parser.add_argument("-m", "--vmmap", dest="takeVmmap", action="store_true", default=False, help="Take a vmmap after each test")
parser.add_argument("--smaps", dest="takeSmaps", action="store_true", default=False, help="Take a smaps rollup after each test")
args = parser.parse_args()
subtestArgs = [args.runGroupedTests, args.runJITTests, args.runLuaTests, args.runNoJITTests]
allDefault = all([arg is None for arg in subtestArgs])
anyTrue = any([arg is True for arg in subtestArgs])
anyFalse = any([arg is False for arg in subtestArgs])
# Default behavior is to run all subtests.
# If a test is explicitly specified not to run, skip that test and use the default behavior for the remaining tests.
# If tests are explicitly specified to run, only run those tests.
# If there is a mix of tests specified to run and not to run, also do not run any unspecified tests.
getArgValue = lambda arg: True if allDefault else False if arg is None and anyTrue else True if arg is None and anyFalse else arg
args.runJITTests = getArgValue(args.runJITTests)
args.runNoJITTests = getArgValue(args.runNoJITTests)
args.runLuaTests = getArgValue(args.runLuaTests)
args.runGroupedTests = getArgValue(args.runGroupedTests)
return args
class BaseRunner:
def __init__(self, args):
self.rootDir = args.testDir
self.environmentVars = {}
self.vmmapOutput = "" if args.takeVmmap else None
self.smapsOutput = "" if args.takeSmaps else None
def setup(self):
pass
def setEnv(self, variable, value):
self.environmentVars[variable] = value
def unsetEnv(self, variable):
self.environmentVars.pop(variable, None)
def resetForTest(self, testName):
self.testName = testName
self.footprint = None
self.peakFootprint = None
self.returnCode = 0
def processLine(self, line):
line = str(line.strip())
footprintMatch = re.match(footprintRE, line)
if footprintMatch:
self.footprint = int(footprintMatch.group(1))
return
peakFootprintMatch = re.match(peakFootprintRE, line)
if peakFootprintMatch:
self.peakFootprint = int(peakFootprintMatch.group(1))
def setReturnCode(self, returnCode):
self.returnCode = returnCode
def getResults(self):
return TestResult(name=self.testName, returnCode=self.returnCode, footprint=self.footprint, peakFootprint=self.peakFootprint, vmmapOutput=self.vmmapOutput, smapsOutput=self.smapsOutput)
class LocalRunner(BaseRunner):
def __init__(self, args):
BaseRunner.__init__(self, args)
self.jscCommand = args.jscCommand
def runOneTest(self, test, extraOptions=None, useJetStream3Harness=True):
self.resetForTest(test)
args = [self.jscCommand]
if extraOptions:
args.extend(extraOptions)
if useJetStream3Harness:
args.extend(["-e", "testList='{test}'; runMode='RAMification'".format(test=test), "cli.js"])
else:
args.extend(["--footprint", "{test}".format(test=test)])
self.resetForTest(test)
proc = subprocess.Popen(args, cwd=self.rootDir, env=self.environmentVars, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=None, shell=False)
while True:
line = proc.stdout.readline()
if sys.version_info[0] >= 3:
line = str(line, "utf-8")
self.processLine(line)
if "js shell waiting for input to exit" in line:
if self.vmmapOutput is not None:
self.vmmapOutput = subprocess.Popen(['vmmap', '--summary', '{}'.format(proc.pid)], shell=False, stderr=subprocess.PIPE, stdout=subprocess.PIPE).stdout.read()
if sys.version_info[0] >= 3:
self.vmmapOutput = str(self.vmmapOutput, "utf-8")
if self.smapsOutput is not None:
self.smapsOutput = subprocess.Popen(['cat', '/proc/{}/smaps_rollup'.format(proc.pid)], shell=False, stderr=subprocess.PIPE, stdout=subprocess.PIPE).stdout.read()
proc.stdin.write(b"done\n")
proc.stdin.flush()
if line == "":
break
self.setReturnCode(proc.wait())
return self.getResults()
def main(parser=None):
footprintValues = []
peakFootprintValues = []
testResultsDict = {}
hasFailedRuns = False
args = parseArgs(parser=parser)
testRunner = args.runner(args)
if args.takeVmmap or args.takeSmaps:
testRunner.setEnv("JS_SHELL_WAIT_FOR_INPUT_TO_EXIT", "1")
dyldFrameworkPath = frameworkPathFromExecutablePath(args.jscCommand)
if dyldFrameworkPath:
testRunner.setEnv("DYLD_FRAMEWORK_PATH", dyldFrameworkPath)
for envVar in args.extraEnvVars:
envVarParts = envVar.split("=")
if len(envVarParts) == 1:
envVarParts[1] = "1"
testRunner.setEnv(envVarParts[0], envVarParts[1])
testRunner.setup()
def runTestList(testList, extraOptions=None, useJetStream3Harness=True):
testScoresDict = {}
for testInfo in testList:
footprintScores = []
peakFootprintScores = []
if isinstance(testInfo, tuple):
testName, test, weight = testInfo
else:
testName, test, weight = testInfo, testInfo, 1
sys.stdout.write("Running {}... ".format(testName))
testResult = testRunner.runOneTest(test, extraOptions, useJetStream3Harness)
if testResult.returnCode == 0 and testResult.footprint and testResult.peakFootprint:
if args.verbose:
print("footprint: {}, peak footprint: {}".format(testResult.footprint, testResult.peakFootprint))
if testResult.vmmapOutput:
print(testResult.vmmapOutput)
if testResult.smapsOutput:
print(testResult.smapsOutput)
else:
print
if testResult.footprint:
footprintScores.append(int(testResult.footprint))
for count in range(0, weight):
footprintValues.append(testResult.footprint / oneMB)
if testResult.peakFootprint:
peakFootprintScores.append(int(testResult.peakFootprint))
for count in range(0, weight):
peakFootprintValues.append(testResult.peakFootprint / oneMB)
else:
hasFailedRuns = True
print("failed")
if testResult.returnCode != 0:
print(" exit code = {}".format(testResult.returnCode))
if not testResult.footprint:
print(" footprint = {}".format(testResult.footprint))
if not testResult.peakFootprint:
print(" peak footprint = {}".format(testResult.peakFootprint))
print
testScoresDict[test] = {"metrics": {"Allocations": ["Geometric"]}, "tests": {"end": {"metrics": {"Allocations": {"current": footprintScores}}}, "peak": {"metrics": {"Allocations": {"current": peakFootprintScores}}}}}
return testScoresDict
current_path = os.getcwd()
os.chdir(ramification_dir) # To allow JS libraries to load
if args.runLuaTests:
if args.verbose:
print("== LuaJSFight No JIT tests ==")
# Use system malloc for LuaJSFight tests
testRunner.setEnv("Malloc", "X")
scoresDict = runTestList(luaTests, ["--useJIT=false", "--forceMiniVMMode=true"], useJetStream3Harness=False)
testResultsDict["LuaJSFight No JIT Tests"] = {"metrics": {"Allocations": ["Geometric"]}, "tests": scoresDict}
testRunner.unsetEnv("Malloc")
if args.runGroupedTests:
if args.verbose:
print("== Grouped tests ==")
scoresDict = runTestList(groupTests)
testResultsDict["Grouped Tests"] = {"metrics": {"Allocations": ["Geometric"]}, "tests": scoresDict}
if args.runJITTests:
if args.verbose:
print("== JIT tests ==")
scoresDict = runTestList(jitTests)
testResultsDict["JIT Tests"] = {"metrics": {"Allocations": ["Geometric"]}, "tests": scoresDict}
if args.runNoJITTests:
if args.verbose:
print("== No JIT tests ==")
scoresDict = runTestList(nonJITTests, ["--useJIT=false", "-e", "testIterationCount=1"])
testResultsDict["No JIT Tests"] = {"metrics": {"Allocations": ["Geometric"]}, "tests": scoresDict}
footprintGeomean = int(geomean(footprintValues) * oneMB)
peakFootprintGeomean = int(geomean(peakFootprintValues) * oneMB)
totalScore = int(geomean([footprintGeomean, peakFootprintGeomean]))
if footprintGeomean:
print("Footprint geomean: {} ({:.3f} MB)".format(footprintGeomean, footprintGeomean / oneMB))
if peakFootprintGeomean:
print("Peak Footprint geomean: {} ({:.3f} MB)".format(peakFootprintGeomean, peakFootprintGeomean / oneMB))
if footprintGeomean and peakFootprintGeomean:
print("Score: {} ({:.3f} MB)".format(totalScore, totalScore / oneMB))
resultsDict = {"RAMification": {"metrics": {"Allocations": {"current": [totalScore]}}, "tests": testResultsDict}}
os.chdir(current_path) # Reset the path back to what it was before
if args.jsonFilename:
with open(args.jsonFilename, "w") as jsonFile:
if args.formatJSON:
json.dump(resultsDict, jsonFile, indent=4, separators=(',', ': '))
else:
json.dump(resultsDict, jsonFile)
if hasFailedRuns:
print("Detected failed run(s), exiting with non-zero return code")
return hasFailedRuns
if __name__ == "__main__":
exit(main())