aboutsummaryrefslogtreecommitdiff
path: root/afdo_redaction
diff options
context:
space:
mode:
Diffstat (limited to 'afdo_redaction')
-rwxr-xr-xafdo_redaction/redact_profile.py38
-rwxr-xr-xafdo_redaction/redact_profile_test.py30
-rwxr-xr-xafdo_redaction/remove_indirect_calls.py3
-rwxr-xr-xafdo_redaction/remove_indirect_calls_test.py8
4 files changed, 43 insertions, 36 deletions
diff --git a/afdo_redaction/redact_profile.py b/afdo_redaction/redact_profile.py
index 96375fee..02bae928 100755
--- a/afdo_redaction/redact_profile.py
+++ b/afdo_redaction/redact_profile.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
@@ -30,6 +30,7 @@ import collections
import re
import sys
+
def _count_samples(samples):
"""Count the total number of samples in a function."""
line_re = re.compile(r'^(\s*)\d+(?:\.\d+)?: (\d+)\s*$')
@@ -132,7 +133,7 @@ def _read_textual_afdo_profile(stream):
continue
if line[0].isspace():
- assert function_line is not None, "sample exists outside of a function?"
+ assert function_line is not None, 'sample exists outside of a function?'
samples.append(line)
continue
@@ -170,12 +171,13 @@ def dedup_records(profile_records, summary_file, max_repeats=100):
counts[_normalize_samples(record.samples)].append(record)
# Be sure that we didn't see any duplicate functions, since that's bad...
- total_functions_recorded = sum(len(records)
- for records in counts.itervalues())
+ total_functions_recorded = sum(len(records) for records in counts.values())
- unique_function_names = set(record.function_line.split(':')[0]
- for records in counts.itervalues()
- for record in records)
+ unique_function_names = {
+ record.function_line.split(':')[0]
+ for records in counts.values()
+ for record in records
+ }
assert len(unique_function_names) == total_functions_recorded, \
'duplicate function names?'
@@ -187,7 +189,7 @@ def dedup_records(profile_records, summary_file, max_repeats=100):
num_samples_total = 0
num_top_samples_total = 0
- for normalized_samples, records in counts.iteritems():
+ for normalized_samples, records in counts.items():
top_sample_count, all_sample_count = _count_samples(normalized_samples)
top_sample_count *= len(records)
all_sample_count *= len(records)
@@ -205,11 +207,13 @@ def dedup_records(profile_records, summary_file, max_repeats=100):
for record in records:
yield record
- print('Retained {:,}/{:,} functions'.format(num_kept, num_total),
- file=summary_file)
- print('Retained {:,}/{:,} samples, total'.format(num_samples_kept,
- num_samples_total),
- file=summary_file)
+ print(
+ 'Retained {:,}/{:,} functions'.format(num_kept, num_total),
+ file=summary_file)
+ print(
+ 'Retained {:,}/{:,} samples, total'.format(num_samples_kept,
+ num_samples_total),
+ file=summary_file)
print('Retained {:,}/{:,} top-level samples' \
.format(num_top_samples_kept, num_top_samples_total),
file=summary_file)
@@ -220,15 +224,17 @@ def run(profile_input_file, summary_output_file, profile_output_file):
# Sort this so we get deterministic output. AFDO doesn't care what order it's
# in.
- deduped = sorted(dedup_records(profile_records, summary_output_file),
- key=lambda r: r.function_line)
+ deduped = sorted(
+ dedup_records(profile_records, summary_output_file),
+ key=lambda r: r.function_line)
for function_line, samples in deduped:
print(function_line, file=profile_output_file)
print('\n'.join(samples), file=profile_output_file)
def _main():
- run(profile_input_file=sys.stdin, summary_output_file=sys.stderr,
+ run(profile_input_file=sys.stdin,
+ summary_output_file=sys.stderr,
profile_output_file=sys.stdout)
diff --git a/afdo_redaction/redact_profile_test.py b/afdo_redaction/redact_profile_test.py
index 27fb534e..e2438972 100755
--- a/afdo_redaction/redact_profile_test.py
+++ b/afdo_redaction/redact_profile_test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
@@ -8,12 +8,12 @@
from __future__ import division, print_function
-import StringIO
+import io
import unittest
-import redact_profile
+from afdo_redaction import redact_profile
-_redact_limit = redact_profile.dedup_records.func_defaults[0]
+_redact_limit = redact_profile.dedup_records.__defaults__[0]
def _redact(input_lines, summary_to=None):
@@ -21,17 +21,18 @@ def _redact(input_lines, summary_to=None):
input_lines = input_lines.splitlines()
if summary_to is None:
- summary_to = StringIO.StringIO()
+ summary_to = io.StringIO()
- output_to = StringIO.StringIO()
- redact_profile.run(profile_input_file=input_lines,
- summary_output_file=summary_to,
- profile_output_file=output_to)
+ output_to = io.StringIO()
+ redact_profile.run(
+ profile_input_file=input_lines,
+ summary_output_file=summary_to,
+ profile_output_file=output_to)
return output_to.getvalue()
def _redact_with_summary(input_lines):
- summary = StringIO.StringIO()
+ summary = io.StringIO()
result = _redact(input_lines, summary_to=summary)
return result, summary.getvalue()
@@ -64,6 +65,7 @@ def _generate_repeated_function_body(repeats, fn_name='_some_name'):
class Tests(unittest.TestCase):
"""All of our tests for redact_profile."""
+
def test_no_input_works(self):
self.assertEqual(_redact(''), '')
@@ -93,13 +95,13 @@ class Tests(unittest.TestCase):
result_file = '\n'.join(kept_lines) + '\n'
- lines = _generate_repeated_function_body(_redact_limit,
- fn_name='_discard_me')
+ lines = _generate_repeated_function_body(
+ _redact_limit, fn_name='_discard_me')
self.assertEqual(_redact(kept_lines + lines), result_file)
self.assertEqual(_redact(lines + kept_lines), result_file)
- more_lines = _generate_repeated_function_body(_redact_limit,
- fn_name='_and_discard_me')
+ more_lines = _generate_repeated_function_body(
+ _redact_limit, fn_name='_and_discard_me')
self.assertEqual(_redact(lines + kept_lines + more_lines), result_file)
self.assertEqual(_redact(lines + more_lines), '')
diff --git a/afdo_redaction/remove_indirect_calls.py b/afdo_redaction/remove_indirect_calls.py
index b879b2f0..0dc15077 100755
--- a/afdo_redaction/remove_indirect_calls.py
+++ b/afdo_redaction/remove_indirect_calls.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# -*- 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
@@ -21,7 +21,6 @@ from __future__ import division, print_function
import argparse
import re
-import sys
def _remove_indirect_call_targets(lines):
diff --git a/afdo_redaction/remove_indirect_calls_test.py b/afdo_redaction/remove_indirect_calls_test.py
index 1499af25..164b284f 100755
--- a/afdo_redaction/remove_indirect_calls_test.py
+++ b/afdo_redaction/remove_indirect_calls_test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
# -*- 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
@@ -11,12 +11,12 @@ from __future__ import print_function
import io
import unittest
-import remove_indirect_calls
+from afdo_redaction import remove_indirect_calls
def _run_test(input_lines):
- input_buf = io.BytesIO('\n'.join(input_lines))
- output_buf = io.BytesIO()
+ input_buf = io.StringIO('\n'.join(input_lines))
+ output_buf = io.StringIO()
remove_indirect_calls.run(input_buf, output_buf)
return output_buf.getvalue().splitlines()