diff options
author | Emma Vukelj <emmavukelj@google.com> | 2019-06-26 13:34:05 -0700 |
---|---|---|
committer | Emma Vukelj <emmavukelj@google.com> | 2019-06-28 15:19:43 +0000 |
commit | 46751c1e85e160ea4d4ce68f965314532649e0d0 (patch) | |
tree | bb3cef581b8afa71794d7629dcc63f4966e02683 /afdo_tools | |
parent | 5a14390efe9009979f4c5fe8134eab728f298ba1 (diff) | |
download | toolchain-utils-46751c1e85e160ea4d4ce68f965314532649e0d0.tar.gz |
AFDO-Bisect: Write a (very) basic AFDO prof parser
This CL introduces a script which does very basic parsing of AFDO
text-based profiles which associates each top-level function with its
associated profile data.
BUG=None
TEST=The added test is successful.
Change-Id: I6c8d3b3b1049b81a5890cc9699e2798dda641eb9
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1679371
Reviewed-by: Caroline Tice <cmtice@chromium.org>
Reviewed-by: George Burgess <gbiv@chromium.org>
Tested-by: Emma Vukelj <emmavukelj@google.com>
Diffstat (limited to 'afdo_tools')
-rwxr-xr-x | afdo_tools/bisection/afdo_parse.py | 61 | ||||
-rwxr-xr-x | afdo_tools/bisection/afdo_parse_test.py | 55 |
2 files changed, 116 insertions, 0 deletions
diff --git a/afdo_tools/bisection/afdo_parse.py b/afdo_tools/bisection/afdo_parse.py new file mode 100755 index 00000000..b9da2174 --- /dev/null +++ b/afdo_tools/bisection/afdo_parse.py @@ -0,0 +1,61 @@ +#!/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 = line.split(':')[0].strip() + 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) diff --git a/afdo_tools/bisection/afdo_parse_test.py b/afdo_tools/bisection/afdo_parse_test.py new file mode 100755 index 00000000..3e17b8f9 --- /dev/null +++ b/afdo_tools/bisection/afdo_parse_test.py @@ -0,0 +1,55 @@ +#!/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. + +"""Tests for afdo_parse.""" + +from __future__ import print_function + +import StringIO +import unittest + +import afdo_parse + + +class SimpleAfdoParseTest(unittest.TestCase): + """Test class for AFDO parsing.""" + + def test_parse_afdo(self): + test_data = StringIO.StringIO('deflate_slow:87460059:3\n' + ' 3: 24\n' + ' 14: 54767\n' + ' 15: 664 fill_window:22\n' + ' 16: 661\n' + ' 19: 637\n' + ' 41: 36692 longest_match:36863\n' + ' 44: 36692\n' + ' 44.2: 5861\n' + ' 46: 13942\n' + ' 46.1: 14003\n') + expected = { + 'deflate_slow': ' 3: 24\n' + ' 14: 54767\n' + ' 15: 664 fill_window:22\n' + ' 16: 661\n' + ' 19: 637\n' + ' 41: 36692 longest_match:36863\n' + ' 44: 36692\n' + ' 44.2: 5861\n' + ' 46: 13942\n' + ' 46.1: 14003\n' + } + actual = afdo_parse.parse_afdo(test_data) + self.assertEqual(actual, expected) + test_data.close() + + def test_parse_empty_afdo(self): + expected = {} + actual = afdo_parse.parse_afdo('') + self.assertEqual(actual, expected) + + +if __name__ == '__main__': + unittest.main() |