diff options
author | Yabin Cui <yabinc@google.com> | 2017-05-08 14:23:19 -0700 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2017-05-08 14:24:44 -0700 |
commit | 98058a897528620916427e96485c58b8a534c6b3 (patch) | |
tree | b5f9c0f69755b4f718c3b81822868fc558e0ea1f | |
parent | f942f9570e526d6853fe0ab1af9b78dd196ea585 (diff) | |
download | extras-98058a897528620916427e96485c58b8a534c6b3.tar.gz |
simpleperf: fix scripts to be runnable by python3.
Bug: http://b/32834638
Test: run scripts with demo using both python and python3.
Change-Id: I5cb7eb41bed0e2a91f4cbc97c386dd527aa3186a
-rw-r--r-- | simpleperf/demo/SimpleperfExamplePureJava/.idea/misc.xml | 2 | ||||
-rw-r--r-- | simpleperf/demo/SimpleperfExampleWithNative/.idea/misc.xml | 2 | ||||
-rw-r--r-- | simpleperf/scripts/annotate.py | 22 | ||||
-rw-r--r-- | simpleperf/scripts/app_profiler.py | 2 | ||||
-rw-r--r-- | simpleperf/scripts/binary_cache_builder.py | 6 | ||||
-rw-r--r-- | simpleperf/scripts/pprof_proto_generator.py | 1 | ||||
-rw-r--r-- | simpleperf/scripts/report.py | 8 | ||||
-rw-r--r-- | simpleperf/scripts/simpleperf_report_lib.py | 10 | ||||
-rw-r--r-- | simpleperf/scripts/utils.py | 22 |
9 files changed, 46 insertions, 29 deletions
diff --git a/simpleperf/demo/SimpleperfExamplePureJava/.idea/misc.xml b/simpleperf/demo/SimpleperfExamplePureJava/.idea/misc.xml index 5d199810..fbb68289 100644 --- a/simpleperf/demo/SimpleperfExamplePureJava/.idea/misc.xml +++ b/simpleperf/demo/SimpleperfExamplePureJava/.idea/misc.xml @@ -37,7 +37,7 @@ <ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Remove" /> </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build/classes" /> </component> <component name="ProjectType"> diff --git a/simpleperf/demo/SimpleperfExampleWithNative/.idea/misc.xml b/simpleperf/demo/SimpleperfExampleWithNative/.idea/misc.xml index 5d199810..fbb68289 100644 --- a/simpleperf/demo/SimpleperfExampleWithNative/.idea/misc.xml +++ b/simpleperf/demo/SimpleperfExampleWithNative/.idea/misc.xml @@ -37,7 +37,7 @@ <ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Remove" /> </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build/classes" /> </component> <component name="ProjectType"> diff --git a/simpleperf/scripts/annotate.py b/simpleperf/scripts/annotate.py index d88a9169..a77f6a21 100644 --- a/simpleperf/scripts/annotate.py +++ b/simpleperf/scripts/annotate.py @@ -65,7 +65,7 @@ class Addr2Line(object): dso = self.dso_dict.get(dso_name) if dso is None: self.dso_dict[dso_name] = dso = dict() - if not dso.has_key(addr): + if addr not in dso: dso[addr] = None @@ -95,7 +95,8 @@ class Addr2Line(object): addr_str = '\n'.join(addr_str) subproc = subprocess.Popen([self.addr2line_path, '-e', dso_path, '-aifC'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) - (stdoutdata, _) = subproc.communicate(addr_str) + (stdoutdata, _) = subproc.communicate(str_to_bytes(addr_str)) + stdoutdata = bytes_to_str(stdoutdata) stdoutdata = stdoutdata.strip().split('\n') if len(stdoutdata) < len(addrs): log_fatal("addr2line didn't output enough lines") @@ -270,7 +271,7 @@ class SourceFileAnnotator(object): 'annotate_dest_dir', 'comm_filters', 'pid_filters', 'tid_filters', 'dso_filters', 'addr2line_path'] for name in config_names: - if not config.has_key(name): + if name not in config: log_fatal('config [%s] is missing' % name) symfs_dir = config['symfs_dir'] if symfs_dir and not os.path.isdir(symfs_dir): @@ -435,7 +436,7 @@ class SourceFileAnnotator(object): def _add_dso_period(self, dso_name, period, used_dso_dict): - if not used_dso_dict.has_key(dso_name): + if dso_name not in used_dso_dict: used_dso_dict[dso_name] = True dso_period = self.dso_periods.get(dso_name) if dso_period is None: @@ -444,7 +445,7 @@ class SourceFileAnnotator(object): def _add_file_period(self, source, period, used_file_dict): - if not used_file_dict.has_key(source.file_key): + if source.file_key not in used_file_dict: used_file_dict[source.file_key] = True file_period = self.file_periods.get(source.file) if file_period is None: @@ -453,14 +454,14 @@ class SourceFileAnnotator(object): def _add_line_period(self, source, period, used_line_dict): - if not used_line_dict.has_key(source.line_key): + if source.line_key not in used_line_dict: used_line_dict[source.line_key] = True file_period = self.file_periods[source.file] file_period.add_line_period(source.line, period) def _add_function_period(self, source, period, used_function_dict): - if not used_function_dict.has_key(source.function_key): + if source.function_key not in used_function_dict: used_function_dict[source.function_key] = True file_period = self.file_periods[source.file] file_period.add_function_period(source.function, source.line, period) @@ -471,14 +472,14 @@ class SourceFileAnnotator(object): with open(summary, 'w') as f: f.write('total period: %d\n\n' % self.period) dso_periods = sorted(self.dso_periods.values(), - cmp=lambda x, y: cmp(y.period.acc_period, x.period.acc_period)) + key=lambda x: x.period.acc_period, reverse=True) for dso_period in dso_periods: f.write('dso %s: %s\n' % (dso_period.dso_name, self._get_percentage_str(dso_period.period))) f.write('\n') file_periods = sorted(self.file_periods.values(), - cmp=lambda x, y: cmp(y.period.acc_period, x.period.acc_period)) + key=lambda x: x.period.acc_period, reverse=True) for file_period in file_periods: f.write('file %s: %s\n' % (file_period.file, self._get_percentage_str(file_period.period))) @@ -489,8 +490,7 @@ class SourceFileAnnotator(object): for func_name in file_period.function_dict.keys(): func_start_line, period = file_period.function_dict[func_name] values.append((func_name, func_start_line, period)) - values = sorted(values, - cmp=lambda x, y: cmp(y[2].acc_period, x[2].acc_period)) + values = sorted(values, key=lambda x: x[2].acc_period, reverse=True) for value in values: f.write('\tfunction (%s): line %d, %s\n' % ( value[0], value[1], self._get_percentage_str(value[2]))) diff --git a/simpleperf/scripts/app_profiler.py b/simpleperf/scripts/app_profiler.py index 6c889b44..05e9534f 100644 --- a/simpleperf/scripts/app_profiler.py +++ b/simpleperf/scripts/app_profiler.py @@ -49,7 +49,7 @@ class AppProfiler(object): 'record_options', 'perf_data_path', 'adb_path', 'readelf_path', 'binary_cache_dir'] for name in config_names: - if not config.has_key(name): + if name not in config: log_fatal('config [%s] is missing' % name) native_lib_dir = config.get('native_lib_dir') if native_lib_dir and not os.path.isdir(native_lib_dir): diff --git a/simpleperf/scripts/binary_cache_builder.py b/simpleperf/scripts/binary_cache_builder.py index aa0e6e69..950e2fb7 100644 --- a/simpleperf/scripts/binary_cache_builder.py +++ b/simpleperf/scripts/binary_cache_builder.py @@ -39,7 +39,7 @@ class BinaryCacheBuilder(object): config_names = ['perf_data_path', 'symfs_dirs', 'adb_path', 'readelf_path', 'binary_cache_dir'] for name in config_names: - if not config.has_key(name): + if name not in config: log_fatal('config for "%s" is missing' % name) self.perf_data_path = config.get('perf_data_path') @@ -82,7 +82,7 @@ class BinaryCacheBuilder(object): for symbol in symbols: dso_name = symbol.dso_name - if not binaries.has_key(dso_name): + if dso_name not in binaries: binaries[dso_name] = lib.GetBuildIdForPath(dso_name) self.binaries = binaries @@ -182,6 +182,7 @@ class BinaryCacheBuilder(object): if not self.readelf_path: return "" output = subprocess.check_output([self.readelf_path, '-n', file]) + output = bytes_to_str(output) result = re.search(r'Build ID:\s*(\S+)', output) if result: build_id = result.group(1) @@ -197,6 +198,7 @@ class BinaryCacheBuilder(object): if not self.readelf_path: return False output = subprocess.check_output([self.readelf_path, '-S', file]) + output = bytes_to_str(output) if output.find('.symtab') != -1: return True return False diff --git a/simpleperf/scripts/pprof_proto_generator.py b/simpleperf/scripts/pprof_proto_generator.py index c877a167..c09469ad 100644 --- a/simpleperf/scripts/pprof_proto_generator.py +++ b/simpleperf/scripts/pprof_proto_generator.py @@ -31,7 +31,6 @@ import os.path import profile_pb2 import re import shutil -import subprocess import sys import time diff --git a/simpleperf/scripts/report.py b/simpleperf/scripts/report.py index 5ed633b1..13104129 100644 --- a/simpleperf/scripts/report.py +++ b/simpleperf/scripts/report.py @@ -28,9 +28,11 @@ import os.path import re import subprocess import sys -from tkFont import * -from Tkinter import * -from ttk import * + +from tkinter import * +from tkinter.font import Font +from tkinter.ttk import * + from utils import * PAD_X = 3 diff --git a/simpleperf/scripts/simpleperf_report_lib.py b/simpleperf/scripts/simpleperf_report_lib.py index de904669..c540059e 100644 --- a/simpleperf/scripts/simpleperf_report_lib.py +++ b/simpleperf/scripts/simpleperf_report_lib.py @@ -37,17 +37,11 @@ def _is_null(p): def _char_pt(str): - if sys.version_info < (3, 0): - return str - # In python 3, str are wide strings whereas the C api expects 8 bit strings, hence we have to convert - # For now using utf-8 as the encoding. - return str.encode('utf-8') + return str_to_bytes(str) def _char_pt_to_str(char_pt): - if sys.version_info < (3, 0): - return char_pt - return char_pt.decode('utf-8') + return bytes_to_str(char_pt) class SampleStruct(ct.Structure): diff --git a/simpleperf/scripts/utils.py b/simpleperf/scripts/utils.py index d5515baf..01853744 100644 --- a/simpleperf/scripts/utils.py +++ b/simpleperf/scripts/utils.py @@ -31,6 +31,9 @@ def get_script_dir(): def is_windows(): return sys.platform == 'win32' or sys.platform == 'cygwin' +def is_python3(): + return sys.version_info >= (3, 0) + def log_debug(msg): logging.debug(msg) @@ -47,6 +50,17 @@ def log_warning(msg): def log_fatal(msg): raise Exception(msg) +def str_to_bytes(str): + if not is_python3(): + return str + # In python 3, str are wide strings whereas the C api expects 8 bit strings, hence we have to convert + # For now using utf-8 as the encoding. + return str.encode('utf-8') + +def bytes_to_str(bytes): + if not is_python3(): + return bytes + return bytes.decode('utf-8') def get_target_binary_path(arch, binary_name): if arch == 'aarch64': @@ -97,6 +111,7 @@ class AdbHelper(object): (stdoutdata, _) = subproc.communicate() result = (subproc.returncode == 0) if stdoutdata: + stdoutdata = bytes_to_str(stdoutdata) log_debug(stdoutdata) log_debug('run adb cmd: %s [result %s]' % (adb_args, result)) return (result, stdoutdata) @@ -143,7 +158,12 @@ def load_config(config_file): if not os.path.exists(config_file): log_fatal("can't find config_file: %s" % config_file) config = {} - execfile(config_file, config) + if is_python3(): + with open(config_file, 'r') as fh: + source = fh.read() + exec(source, config) + else: + execfile(config_file, config) return config |