1 # vim: set ts=4 sw=4 tw=99 et:
11 return os.path.realpath(os.path.normpath(k))
14 def __init__(self, loop):
20 def addChild(self, child):
21 if self.children == None:
23 self.children.append(child)
25 def computeUCB(self, coeff):
26 return (self.score / self.visits) + math.sqrt(coeff / self.visits)
29 def __init__(self, benchmark, bestTime, enableLoops, loops, fd, playouts):
32 self.numPlayouts = playouts
33 self.maxNodes = self.numPlayouts * 20
35 self.enableLoops = enableLoops
36 self.maturityThreshold = 20
37 self.originalBest = bestTime
38 self.bestTime = bestTime
44 def expandNode(self, node, pending):
46 node.addChild(UCTNode(loop))
48 if self.numNodes >= self.maxNodes:
52 def findBestChild(self, node):
53 coeff = self.bias * math.log(node.visits)
55 bestUCB = -float('Infinity')
57 for child in node.children:
58 ucb = child.computeUCB(coeff)
65 def playout(self, history):
67 for i in range(0, len(self.loops)):
68 queue.append(random.randint(0, 1))
70 queue[node.loop] = not self.enableLoops
72 for i in range(0, len(queue)):
75 if zash in self.zobrist:
76 return self.zobrist[zash]
78 self.bm.generateBanList(self.loops, queue)
79 result = self.bm.treeSearchRun(self.fd, ['-m', '-j'], 3)
80 self.zobrist[zash] = result
83 def step(self, loopList):
89 # If this is a leaf node...
90 if node.children == None:
91 # And the leaf node is mature...
92 if node.visits >= self.maturityThreshold:
93 # If the node can be expanded, keep spinning.
94 if self.expandNode(node, pending) and node.children != None:
97 # Otherwise, this is a leaf node. Run a playout.
98 score = self.playout(history)
101 # Find the best child.
102 node = self.findBestChild(node)
104 pending.remove(node.loop)
106 # Normalize the score.
108 score = (self.originalBest - score) / self.originalBest
114 if int(origScore) < int(self.bestTime):
115 print('New best score: {0:f}ms'.format(origScore))
116 self.combos = [history]
117 self.bestTime = origScore
118 elif int(origScore) == int(self.bestTime):
119 self.combos.append(history)
122 loopList = [i for i in range(0, len(self.loops))]
124 self.root = UCTNode(-1)
125 self.expandNode(self.root, loopList)
127 for i in range(0, self.numPlayouts):
130 # Build the expected combination vector.
132 for combo in self.combos:
134 for i in range(0, len(self.loops)):
135 vec.append(int(self.enableLoops))
137 vec[node.loop] = int(not self.enableLoops)
140 return [self.bestTime, combos]
143 def __init__(self, JS, fname):
149 def run(self, fd, eargs):
153 return subprocess.check_output(args).decode()
155 # self.stats[name] = { }
156 # self.runList.append(name)
157 # for line in output.split('\n'):
158 # m = re.search('line (\d+): (\d+)', line)
160 # self.stats[name][int(m.group(1))] = int(m.group(2))
162 # m = re.search('total: (\d+)', line)
164 # self.stats[name]['total'] = m.group(1)
166 def winnerForLine(self, line):
167 best = self.runList[0]
168 bestTime = self.stats[best][line]
169 for run in self.runList[1:]:
170 x = self.stats[run][line]
177 sys.stdout.write('{0:7s}'.format(''))
178 sys.stdout.write('{0:15s}'.format('line'))
179 for run in self.runList:
180 sys.stdout.write('{0:15s}'.format(run))
181 sys.stdout.write('{0:15s}\n'.format('best'))
182 for c in self.counters:
183 sys.stdout.write('{0:10d}'.format(c))
184 for run in self.runList:
185 sys.stdout.write('{0:15d}'.format(self.stats[run][c]))
186 sys.stdout.write('{0:12s}'.format(''))
187 sys.stdout.write('{0:15s}'.format(self.winnerForLine(c)))
188 sys.stdout.write('\n')
190 def preprocess(self, lines, onBegin, onEnd):
193 rd = open(self.fname, 'rt')
195 if re.search('\/\* BEGIN LOOP \*\/', line):
196 stack.append([len(lines), len(counters)])
197 counters.append([len(lines), 0])
198 onBegin(lines, len(lines))
199 elif re.search('\/\* END LOOP \*\/', line):
201 onEnd(lines, old[0], len(lines))
202 counters[old[1]][1] = len(lines)
205 return [lines, counters]
207 def treeSearchRun(self, fd, args, count = 5):
209 for i in range(0, count):
210 output = self.run(fd, args)
214 def generateBanList(self, counters, queue):
215 if os.path.exists('/tmp/permabans'):
216 os.unlink('/tmp/permabans')
217 fd = open('/tmp/permabans', 'wt')
218 for i in range(0, len(counters)):
219 for j in range(counters[i][0], counters[i][1] + 1):
220 fd.write('{0:d} {1:d}\n'.format(j, int(queue[i])))
223 def internalExhaustiveSearch(self, params):
224 counters = params['counters']
226 # iterative algorithm to explore every combination
227 ncombos = 2 ** len(counters)
233 bestTime = float('Infinity')
239 for j in range(0, len(counters)):
242 self.generateBanList(counters, queue)
244 t = self.treeSearchRun(fd, ['-m', '-j'])
247 bestCombos = [queue[:]]
248 print('New best time: {0:f}ms'.format(t))
249 elif int(t) == int(bestTime):
250 bestCombos.append(queue[:])
254 return [bestTime, bestCombos]
256 def internalTreeSearch(self, params):
258 methodTime = params['methodTime']
259 tracerTime = params['tracerTime']
260 combinedTime = params['combinedTime']
261 counters = params['counters']
263 # Build the initial loop data.
264 # If the method JIT already wins, disable tracing by default.
265 # Otherwise, enable tracing by default.
266 if methodTime < combinedTime:
273 uct = UCT(self, combinedTime, enableLoops, counters[:], fd, 50000)
276 def treeSearch(self):
277 fd, counters = self.ppForTreeSearch()
279 os.system("cat " + fd.name + " > /tmp/k.js")
281 if os.path.exists('/tmp/permabans'):
282 os.unlink('/tmp/permabans')
283 methodTime = self.treeSearchRun(fd, ['-m'])
284 tracerTime = self.treeSearchRun(fd, ['-j'])
285 combinedTime = self.treeSearchRun(fd, ['-m', '-j'])
287 #Get a rough estimate of how long this benchmark will take to fully compute.
288 upperBound = max(methodTime, tracerTime, combinedTime)
289 upperBound *= 2 ** len(counters)
290 upperBound *= 5 # Number of runs
292 if (upperBound < 1000):
293 print('Estimating {0:d}ms to test, so picking exhaustive '.format(int(upperBound)) +
296 upperBound = int(upperBound / 1000)
297 delta = datetime.timedelta(seconds = upperBound)
299 print('Estimating {0:d}s to test, so picking exhaustive '.format(int(upperBound)))
301 print('Estimating {0:s} to test, so picking tree search '.format(str(delta)))
304 best = min(methodTime, tracerTime, combinedTime)
308 'counters': counters,
309 'methodTime': methodTime,
310 'tracerTime': tracerTime,
311 'combinedTime': combinedTime
314 print('Method JIT: {0:d}ms'.format(int(methodTime)))
315 print('Tracing JIT: {0:d}ms'.format(int(tracerTime)))
316 print('Combined: {0:d}ms'.format(int(combinedTime)))
319 results = self.internalTreeSearch(params)
321 results = self.internalExhaustiveSearch(params)
323 bestTime = results[0]
324 bestCombos = results[1]
325 print('Search found winning time {0:d}ms!'.format(int(bestTime)))
326 print('Combos at this time: {0:d}'.format(len(bestCombos)))
328 #Find loops that traced every single time
329 for i in range(0, len(counters)):
330 start = counters[i][0]
335 print('\tloop @ {0:d}-{1:d} traced {2:d}% of the time'.format(
336 start, end, int(n / len(bestCombos) * 100)))
338 def ppForTreeSearch(self):
339 def onBegin(lines, lineno):
340 lines.append('GLOBAL_THINGY = 1;\n')
341 def onEnd(lines, old, lineno):
342 lines.append('GLOBAL_THINGY = 1;\n')
344 lines = ['var JINT_START_TIME = Date.now();\n',
345 'var GLOBAL_THINGY = 0;\n']
347 lines, counters = self.preprocess(lines, onBegin, onEnd)
348 fd = tempfile.NamedTemporaryFile('wt')
351 fd.write('print(Date.now() - JINT_START_TIME);\n')
353 return [fd, counters]
355 def preprocessForLoopCounting(self):
356 def onBegin(lines, lineno):
357 lines.append('JINT_TRACKER.line_' + str(lineno) + '_start = Date.now();\n')
359 def onEnd(lines, old, lineno):
360 lines.append('JINT_TRACKER.line_' + str(old) + '_end = Date.now();\n')
361 lines.append('JINT_TRACKER.line_' + str(old) + '_total += ' + \
362 'JINT_TRACKER.line_' + str(old) + '_end - ' + \
363 'JINT_TRACKER.line_' + str(old) + '_start;\n')
365 lines, counters = self.preprocess(onBegin, onEnd)
366 fd = tempfile.NamedTemporaryFile('wt')
367 fd.write('var JINT_TRACKER = { };\n')
369 fd.write('JINT_TRACKER.line_' + str(c) + '_start = 0;\n')
370 fd.write('JINT_TRACKER.line_' + str(c) + '_end = 0;\n')
371 fd.write('JINT_TRACKER.line_' + str(c) + '_total = 0;\n')
372 fd.write('JINT_TRACKER.begin = Date.now();\n')
375 fd.write('JINT_TRACKER.total = Date.now() - JINT_TRACKER.begin;\n')
376 for c in self.counters:
377 fd.write('print("line ' + str(c) + ': " + JINT_TRACKER.line_' + str(c) +
379 fd.write('print("total: " + JINT_TRACKER.total);')
383 if __name__ == '__main__':
384 script_path = os.path.abspath(__file__)
385 script_dir = os.path.dirname(script_path)
386 test_dir = os.path.join(script_dir, 'tests')
387 lib_dir = os.path.join(script_dir, 'lib')
389 # The [TESTS] optional arguments are paths of test files relative
390 # to the jit-test/tests directory.
392 from optparse import OptionParser
393 op = OptionParser(usage='%prog [options] JS_SHELL test')
394 (OPTIONS, args) = op.parse_args()
396 op.error('missing JS_SHELL and test argument')
397 # We need to make sure we are using backslashes on Windows.
398 JS = realpath(args[0])
399 test = realpath(args[1])
401 bm = Benchmark(JS, test)
404 # bm.run('mjit', ['-m'])
405 # bm.run('tjit', ['-j'])
406 # bm.run('m+tjit', ['-m', '-j'])