diff options
Diffstat (limited to 'heatmaps')
-rwxr-xr-x | heatmaps/heat_map.py | 4 | ||||
-rwxr-xr-x | heatmaps/heat_map_test.py | 13 | ||||
-rw-r--r-- | heatmaps/heatmap_generator.py | 39 | ||||
-rwxr-xr-x | heatmaps/heatmap_generator_test.py | 9 |
4 files changed, 33 insertions, 32 deletions
diff --git a/heatmaps/heat_map.py b/heatmaps/heat_map.py index 2fd742d2..a989ab70 100755 --- a/heatmaps/heat_map.py +++ b/heatmaps/heat_map.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2015 The Chromium OS Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be @@ -15,7 +15,7 @@ import sys import tempfile from cros_utils import command_executer -import heatmap_generator +from heatmaps import heatmap_generator def IsARepoRoot(directory): diff --git a/heatmaps/heat_map_test.py b/heatmaps/heat_map_test.py index 21f90d41..ad62cd91 100755 --- a/heatmaps/heat_map_test.py +++ b/heatmaps/heat_map_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. @@ -9,14 +9,15 @@ from __future__ import print_function -import mock +import unittest.mock as mock import unittest import os from cros_utils import command_executer -import heat_map +from heatmaps import heat_map +from heatmaps import heatmap_generator def make_heatmap(chromeos_root='/path/to/fake/chromeos_root/', @@ -41,7 +42,7 @@ def fake_generate_perf_report_exception(_): class HeatmapTest(unittest.TestCase): """All of our tests for heat_map.""" - #pylint: disable=protected-access + # pylint: disable=protected-access @mock.patch('shutil.copy2') @mock.patch('tempfile.mkdtemp') def test_EnsureFileInChrootAlreadyInside(self, mock_mkdtemp, mock_copy): @@ -81,7 +82,7 @@ class HeatmapTest(unittest.TestCase): self.assertEqual(heatmap.perf_report, '/fake/chroot/inchroot_path/perf_report.txt') - @mock.patch('heatmap_generator.HeatmapGenerator') + @mock.patch.object(heatmap_generator, 'HeatmapGenerator') def test_GetHeatMap(self, mock_heatmap_generator): heatmap = make_heatmap() heatmap._GetHeatMap(10) @@ -107,7 +108,7 @@ class HeatmapTest(unittest.TestCase): new=fake_generate_perf_report_exception) @mock.patch.object(heat_map.HeatMapProducer, '_GetHeatMap') @mock.patch.object(heat_map.HeatMapProducer, '_RemoveFiles') - @mock.patch('__builtin__.print') + @mock.patch('builtins.print') def test_Run_with_exception(self, mock_print, mock_remove_files, mock_get_heatmap, mock_ensure_file_in_chroot): heatmap = make_heatmap() diff --git a/heatmaps/heatmap_generator.py b/heatmaps/heatmap_generator.py index 42fd6352..0dd6ad28 100644 --- a/heatmaps/heatmap_generator.py +++ b/heatmaps/heatmap_generator.py @@ -13,7 +13,7 @@ performed by another script perf-to-inst-page.sh). It can also analyze the symbol names in hot pages. """ -from __future__ import print_function +from __future__ import division, print_function import bisect import collections @@ -93,10 +93,10 @@ class HeatmapGenerator(object): self.max_addr = 1024 * 1024 * 1024 self.ce = command_executer.GetCommandExecuter(log_level=log_level) self.dir = os.path.dirname(os.path.realpath(__file__)) - with open(perf_report) as f: + with open(perf_report, 'r', encoding='utf-8') as f: self.perf_report_contents = f.readlines() # Write histogram results to a text file, in order to use gnu plot to draw - self.hist_temp_output = open('out.txt', 'w') + self.hist_temp_output = open('out.txt', 'w', encoding='utf-8') self.processes = {} self.deleted_processes = {} self.count = 0 @@ -277,7 +277,7 @@ class HeatmapGenerator(object): if address < self.max_addr: self.count += 1 line = '%d/%d: %d %d' % (pid[0], pid[1], self.count, - address / self.page_size * self.page_size) + address // self.page_size * self.page_size) if self.hugepage: if self.hugepage.start <= address < self.hugepage.end: line += ' hugepage' @@ -321,7 +321,7 @@ class HeatmapGenerator(object): names = [x for x in os.listdir('.') if 'inst-histo' in x and '.txt' in x] hist = {} for n in names: - with open(n) as f: + with open(n, encoding='utf-8') as f: for l in f.readlines(): num, addr = l.strip().split(' ') assert int(addr) not in hist @@ -354,7 +354,7 @@ class HeatmapGenerator(object): if 't' not in symbol_type and 'T' not in symbol_type: # Filter out symbols not in text sections continue - if len(self.symbol_addresses) == 0: + if not self.symbol_addresses: # The first symbol in text sections text_section_start = addr self.symbol_addresses.append(0) @@ -374,9 +374,9 @@ class HeatmapGenerator(object): def _map_addr_to_symbol(self, addr): # Find out the symbol name - assert len(self.symbol_addresses) > 0 + assert self.symbol_addresses index = bisect.bisect(self.symbol_addresses, addr) - assert index > 0 and index <= len(self.symbol_names), \ + assert 0 < index <= len(self.symbol_names), \ 'Failed to find an index (%d) in the list (len=%d)' % ( index, len(self.symbol_names)) return self.symbol_names[index - 1] @@ -387,7 +387,7 @@ class HeatmapGenerator(object): print( '----------------------------------------------------------', file=fp) print( - 'Page Offset: %d MB, Count: %d' % (page_num / 1024 / 1024, + 'Page Offset: %d MB, Count: %d' % (page_num // 1024 // 1024, sample_num), file=fp) @@ -400,8 +400,8 @@ class HeatmapGenerator(object): if pid is None: # The sampling is not on Chrome continue - if addr / self.page_size != ( - self.processes[pid].start_address + page_num) / self.page_size: + if addr // self.page_size != ( + self.processes[pid].start_address + page_num) // self.page_size: # Sampling not in the current page continue @@ -410,14 +410,14 @@ class HeatmapGenerator(object): assert name, 'Failed to find symbol name of addr %x' % addr symbol_counts[name] += 1 - assert sum(symbol_counts.itervalues()) == sample_num, \ + assert sum(symbol_counts.values()) == sample_num, \ 'Symbol name matching missing for some addresses: %d vs %d' % ( - sum(symbol_counts.itervalues()), sample_num) + sum(symbol_counts.values()), sample_num) # Print out the symbol names sorted by the number of samples in # the page for name, count in sorted( - symbol_counts.iteritems(), key=lambda kv: kv[1], reverse=True): + symbol_counts.items(), key=lambda kv: kv[1], reverse=True): if count == 0: break print('> %s : %d' % (name, count), file=fp) @@ -434,20 +434,19 @@ class HeatmapGenerator(object): # Read histogram from histo.txt hist = self._restore_histogram() # Sort the pages in histogram - sorted_hist = sorted( - hist.iteritems(), key=lambda value: value[1], reverse=True) + sorted_hist = sorted(hist.items(), key=lambda value: value[1], reverse=True) # Generate symbolizations self._read_symbols_from_binary(binary) # Write hottest pages - with open('addr2symbol.txt', 'w') as fp: + with open('addr2symbol.txt', 'w', encoding='utf-8') as fp: if self.hugepage: # Print hugepage region first print( 'Hugepage top %d hot pages (%d MB - %d MB):' % - (top_n, self.hugepage.start / 1024 / 1024, - self.hugepage.end / 1024 / 1024), + (top_n, self.hugepage.start // 1024 // 1024, + self.hugepage.end // 1024 // 1024), file=fp) pages_to_print = [(k, v) for k, v in sorted_hist @@ -464,5 +463,5 @@ class HeatmapGenerator(object): self._print_symbols_in_hot_pages(fp, pages_to_print) else: # Print top_n hottest pages. - pages_to_print = [(k, v) for k, v in sorted_hist][:top_n] + pages_to_print = sorted_hist[:top_n] self._print_symbols_in_hot_pages(fp, pages_to_print) diff --git a/heatmaps/heatmap_generator_test.py b/heatmaps/heatmap_generator_test.py index 0c0bbfc8..5008c653 100755 --- a/heatmaps/heatmap_generator_test.py +++ b/heatmaps/heatmap_generator_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,11 +8,12 @@ from __future__ import division, print_function -import mock -import os +import unittest.mock as mock import unittest -import heatmap_generator +import os + +from heatmaps import heatmap_generator def _write_perf_mmap(pid, tid, addr, size, fp): |