1# Copyright 2016 the V8 project authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import sys
7import json
8import re
9import argparse
10
11sys.path.append(os.environ['PERF_EXEC_PATH'] + \
12  '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
13
14from perf_trace_context import *
15from Core import *
16
17def trace_begin():
18  json_obj['eventCounts'] = {}
19  prog = re.compile(r'0x[0-9a-fA-F]+')
20  for phase in reversed(json_obj['phases']):
21    if phase['name'] == "disassembly":
22      for line in phase['data'].splitlines():
23        result = re.match(prog, line)
24        if result:
25          known_addrs.add(result.group(0))
26
27def trace_end():
28  print json.dumps(json_obj)
29
30def process_event(param_dict):
31  addr = "0x%x" % int(param_dict['sample']['ip'])
32
33  # Only count samples that belong to the function
34  if addr not in known_addrs:
35    return
36
37  ev_name = param_dict['ev_name']
38  if ev_name not in json_obj['eventCounts']:
39    json_obj['eventCounts'][ev_name] = {}
40  if addr not in json_obj['eventCounts'][ev_name]:
41    json_obj['eventCounts'][ev_name][addr] = 0
42  json_obj['eventCounts'][ev_name][addr] += 1
43
44if __name__ == "__main__":
45  parser = argparse.ArgumentParser(
46      description="Perf script to merge profiling data with turbofan compiler "
47                  "traces.")
48  parser.add_argument("file_name", metavar="JSON File",
49      help="turbo trace json file.")
50
51  args = parser.parse_args()
52
53  with open(args.file_name, 'r') as json_file:
54    json_obj = json.load(json_file)
55
56  known_addrs = set()
57