1#!/usr/bin/env python 2# Copyright 2019 the V8 project authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6""" 7python %prog trace-file 8 9Parses output generated by v8 with flag --trace-regexp-bytecodes and generates 10a list of the most common sequences. 11""" 12 13from __future__ import print_function 14 15import sys 16import re 17import collections 18 19def parse(file, seqlen): 20 # example: 21 # pc = 00, sp = 0, curpos = 0, curchar = 0000000a ..., bc = PUSH_BT, 02, 00, 00, 00, e8, 00, 00, 00 ....... 22 rx = re.compile(r'pc = (?P<pc>[0-9a-f]+), sp = (?P<sp>\d+), ' 23 r'curpos = (?P<curpos>\d+), curchar = (?P<char_hex>[0-9a-f]+) ' 24 r'(:?\.|\()(?P<char>\.|\w)(:?\.|\)), bc = (?P<bc>\w+), .*') 25 total = 0 26 bc_cnt = [None] * seqlen 27 for i in range(seqlen): 28 bc_cnt[i] = {} 29 last = [None] * seqlen 30 with open(file) as f: 31 l = f.readline() 32 while l: 33 l = l.strip() 34 if l.startswith("Start bytecode interpreter"): 35 for i in range(seqlen): 36 last[i] = collections.deque(maxlen=i+1) 37 38 match = rx.search(l) 39 if match: 40 total += 1 41 bc = match.group('bc') 42 for i in range(seqlen): 43 last[i].append(bc) 44 key = ' --> '.join(last[i]) 45 bc_cnt[i][key] = bc_cnt[i].get(key,0) + 1 46 47 l = f.readline() 48 return bc_cnt, total 49 50def print_most_common(d, seqlen, total): 51 sorted_d = sorted(d.items(), key=lambda kv: kv[1], reverse=True) 52 for (k,v) in sorted_d: 53 if v*100/total < 1.0: 54 return 55 print("{}: {} ({} %)".format(k,v,(v*100/total))) 56 57def main(argv): 58 max_seq = 7 59 bc_cnt, total = parse(argv[1],max_seq) 60 for i in range(max_seq): 61 print() 62 print("Most common of length {}".format(i+1)) 63 print() 64 print_most_common(bc_cnt[i], i, total) 65 66if __name__ == '__main__': 67 main(sys.argv) 68