11cb0ef41Sopenharmony_ci#!/usr/bin/env python 21cb0ef41Sopenharmony_ci# Copyright 2019 the V8 project authors. All rights reserved. 31cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be 41cb0ef41Sopenharmony_ci# found in the LICENSE file. 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci""" 71cb0ef41Sopenharmony_cipython %prog trace-file 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciParses output generated by v8 with flag --trace-regexp-bytecodes and generates 101cb0ef41Sopenharmony_cia list of the most common sequences. 111cb0ef41Sopenharmony_ci""" 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cifrom __future__ import print_function 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciimport sys 161cb0ef41Sopenharmony_ciimport re 171cb0ef41Sopenharmony_ciimport collections 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cidef parse(file, seqlen): 201cb0ef41Sopenharmony_ci # example: 211cb0ef41Sopenharmony_ci # pc = 00, sp = 0, curpos = 0, curchar = 0000000a ..., bc = PUSH_BT, 02, 00, 00, 00, e8, 00, 00, 00 ....... 221cb0ef41Sopenharmony_ci rx = re.compile(r'pc = (?P<pc>[0-9a-f]+), sp = (?P<sp>\d+), ' 231cb0ef41Sopenharmony_ci r'curpos = (?P<curpos>\d+), curchar = (?P<char_hex>[0-9a-f]+) ' 241cb0ef41Sopenharmony_ci r'(:?\.|\()(?P<char>\.|\w)(:?\.|\)), bc = (?P<bc>\w+), .*') 251cb0ef41Sopenharmony_ci total = 0 261cb0ef41Sopenharmony_ci bc_cnt = [None] * seqlen 271cb0ef41Sopenharmony_ci for i in range(seqlen): 281cb0ef41Sopenharmony_ci bc_cnt[i] = {} 291cb0ef41Sopenharmony_ci last = [None] * seqlen 301cb0ef41Sopenharmony_ci with open(file) as f: 311cb0ef41Sopenharmony_ci l = f.readline() 321cb0ef41Sopenharmony_ci while l: 331cb0ef41Sopenharmony_ci l = l.strip() 341cb0ef41Sopenharmony_ci if l.startswith("Start bytecode interpreter"): 351cb0ef41Sopenharmony_ci for i in range(seqlen): 361cb0ef41Sopenharmony_ci last[i] = collections.deque(maxlen=i+1) 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci match = rx.search(l) 391cb0ef41Sopenharmony_ci if match: 401cb0ef41Sopenharmony_ci total += 1 411cb0ef41Sopenharmony_ci bc = match.group('bc') 421cb0ef41Sopenharmony_ci for i in range(seqlen): 431cb0ef41Sopenharmony_ci last[i].append(bc) 441cb0ef41Sopenharmony_ci key = ' --> '.join(last[i]) 451cb0ef41Sopenharmony_ci bc_cnt[i][key] = bc_cnt[i].get(key,0) + 1 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci l = f.readline() 481cb0ef41Sopenharmony_ci return bc_cnt, total 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cidef print_most_common(d, seqlen, total): 511cb0ef41Sopenharmony_ci sorted_d = sorted(d.items(), key=lambda kv: kv[1], reverse=True) 521cb0ef41Sopenharmony_ci for (k,v) in sorted_d: 531cb0ef41Sopenharmony_ci if v*100/total < 1.0: 541cb0ef41Sopenharmony_ci return 551cb0ef41Sopenharmony_ci print("{}: {} ({} %)".format(k,v,(v*100/total))) 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_cidef main(argv): 581cb0ef41Sopenharmony_ci max_seq = 7 591cb0ef41Sopenharmony_ci bc_cnt, total = parse(argv[1],max_seq) 601cb0ef41Sopenharmony_ci for i in range(max_seq): 611cb0ef41Sopenharmony_ci print() 621cb0ef41Sopenharmony_ci print("Most common of length {}".format(i+1)) 631cb0ef41Sopenharmony_ci print() 641cb0ef41Sopenharmony_ci print_most_common(bc_cnt[i], i, total) 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciif __name__ == '__main__': 671cb0ef41Sopenharmony_ci main(sys.argv) 68