aboutsummaryrefslogtreecommitdiff
path: root/afdo_tools/bisection/afdo_parse.py
blob: 876c2c8f9de50184df8646bb5e8666dd17a19f9b (plain)
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Performs basic parsing of an AFDO text-based profile.

This short script performs very basic parsing of an AFDO text-based profile
into a dictionary which associates each top-level function with its profile
data (as plain text), and these results are dumped to a pickle file.
"""

from __future__ import print_function
from absl import app
from absl import flags

import json
import pprint

flags.DEFINE_string('afdo_text', None, 'AFDO text-based profile to be parsed')
flags.DEFINE_string('output_file', None, 'File to write JSON results to')
FLAGS = flags.FLAGS


def parse_afdo(f):
  """Performs basic parsing of an AFDO text-based profile.

  This parsing expects an input file of the form generated by bin/llvm-profdata
  (within an LLVM build).
  """
  results = {}
  curr_func = None
  curr_data = []
  for line in f:
    if not line.startswith(' '):
      if curr_func:
        results[curr_func] = ''.join(curr_data)
        curr_data = []
      curr_func, rest = line.split(':', 1)
      curr_func = curr_func.strip()
      curr_data.append(':' + rest)
    else:
      curr_data.append(line)

  if curr_func:
    results[curr_func] = ''.join(curr_data)
  return results


def main(_):
  with open(FLAGS.afdo_text) as f:
    results = parse_afdo(f)
    if FLAGS.output_file:
      with open(FLAGS.output_file, 'wb') as f_out:
        json.dump(results, f_out, indent=2)
    else:
      pprint.pprint(results)


if __name__ == '__main__':
  flags.mark_flag_as_required('afdo_text')
  app.run(main)