diff options
author | Yabin Cui <yabinc@google.com> | 2021-10-21 09:43:32 -0700 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2021-10-21 10:41:33 -0700 |
commit | 3d20a0b64d71db24cbd9955dc0fe6d641ff66ca1 (patch) | |
tree | 41b318be79d97c488e2b09ee1bad348ea18cf7ff | |
parent | e99cc04becd6f56a9ee93dc0cdedce20fdfa24cd (diff) | |
download | extras-3d20a0b64d71db24cbd9955dc0fe6d641ff66ca1.tar.gz |
simpleperf: fix host python tests.
1. Fix parse_line_output in Addr2Nearestline, add new test for it.
2. Replace WithNative app with Cpp app in tests.
3. Use llvm-strip instead of strip. The latter can't understand test
binaries.
Bug: 203246914
Test: run scripts/test/test.py
Change-Id: Iffc4847dcf2b5a4ced172f7d9f49d4027b21aff5
-rw-r--r-- | simpleperf/scripts/simpleperf_utils.py | 21 | ||||
-rw-r--r-- | simpleperf/scripts/test/app_profiler_test.py | 2 | ||||
-rw-r--r-- | simpleperf/scripts/test/binary_cache_builder_test.py | 2 | ||||
-rw-r--r-- | simpleperf/scripts/test/tools_test.py | 70 |
4 files changed, 78 insertions, 17 deletions
diff --git a/simpleperf/scripts/simpleperf_utils.py b/simpleperf/scripts/simpleperf_utils.py index 0519b5fa..79a91e12 100644 --- a/simpleperf/scripts/simpleperf_utils.py +++ b/simpleperf/scripts/simpleperf_utils.py @@ -141,11 +141,10 @@ class ToolFinder: 'path_in_ndk': lambda platform: 'toolchains/llvm/prebuilt/%s-x86_64/bin/llvm-symbolizer' % platform, }, - 'objdump': { - 'is_binutils': True, - }, - 'strip': { - 'is_binutils': True, + 'llvm-strip': { + 'is_binutils': False, + 'path_in_ndk': + lambda platform: 'toolchains/llvm/prebuilt/%s-x86_64/bin/llvm-strip' % platform, }, } @@ -610,7 +609,7 @@ class Addr2Nearestline(object): stdoutdata = bytes_to_str(stdoutdata) except OSError: return - addr_map = self._parse_line_output(stdoutdata, dso) + addr_map = self.parse_line_output(stdoutdata, dso) # 3. Fill line info in dso.addrs. for addr in dso.addrs: @@ -634,8 +633,8 @@ class Addr2Nearestline(object): args.append('--functions=none') return args - def _parse_line_output(self, output: str, dso: Addr2Nearestline.Dso) -> Dict[int, - List[Tuple[int]]]: + def parse_line_output(self, output: str, dso: Addr2Nearestline.Dso) -> Dict[int, + List[Tuple[int]]]: """ The output is a list of lines. address1 @@ -648,7 +647,7 @@ class Addr2Nearestline(object): """ addr_map: Dict[int, List[Tuple[int]]] = {} - lines = output.strip().split('\n') + lines = output.strip().splitlines() i = 0 while i < len(lines): address = self._parse_line_output_address(lines[i]) @@ -665,6 +664,10 @@ class Addr2Nearestline(object): # no more frames break i += 1 + elif not lines[i]: + i += 1 + break + file_path, line_number = self._parse_line_output_source_location(lines[i]) i += 1 if not file_path or not line_number: diff --git a/simpleperf/scripts/test/app_profiler_test.py b/simpleperf/scripts/test/app_profiler_test.py index 72c68d24..9fc4bcc5 100644 --- a/simpleperf/scripts/test/app_profiler_test.py +++ b/simpleperf/scripts/test/app_profiler_test.py @@ -73,7 +73,7 @@ class TestNativeLibDownloader(TestBase): # Sync all native libs on device. downloader = NativeLibDownloader(self.ndk_path, 'arm64', self.adb) downloader.collect_native_libs_on_host(TestHelper.testdata_path( - 'SimpleperfExampleWithNative/app/build/intermediates/cmake/profiling')) + 'SimpleperfExampleCpp/app/build/intermediates/cmake/debug')) self.assertEqual(len(downloader.host_build_id_map), 2) for entry in downloader.host_build_id_map.values(): self.assertEqual(entry.score, 3) diff --git a/simpleperf/scripts/test/binary_cache_builder_test.py b/simpleperf/scripts/test/binary_cache_builder_test.py index cf71783c..21b21339 100644 --- a/simpleperf/scripts/test/binary_cache_builder_test.py +++ b/simpleperf/scripts/test/binary_cache_builder_test.py @@ -28,7 +28,7 @@ from . test_utils import TestBase, TestHelper class TestBinaryCacheBuilder(TestBase): def test_copy_binaries_from_symfs_dirs(self): readelf = ReadElf(TestHelper.ndk_path) - strip = ToolFinder.find_tool_path('strip', arch='arm') + strip = ToolFinder.find_tool_path('llvm-strip', arch='arm') self.assertIsNotNone(strip) symfs_dir = os.path.join(self.test_dir, 'symfs_dir') remove(symfs_dir) diff --git a/simpleperf/scripts/test/tools_test.py b/simpleperf/scripts/test/tools_test.py index f0455ad3..83ccf481 100644 --- a/simpleperf/scripts/test/tools_test.py +++ b/simpleperf/scripts/test/tools_test.py @@ -141,6 +141,64 @@ class TestTools(TestBase): 'for %s:0x%x, expected source %s, actual source %s' % (dso_path, test_addr['addr'], expected_source, actual_source)) + def test_addr2nearestline_parse_output(self): + output = """ +0x104c +system/extras/simpleperf/runtest/two_functions.cpp:6:0 + +0x1094 +system/extras/simpleperf/runtest/two_functions.cpp:9:10 + +0x10bb +system/extras/simpleperf/runtest/two_functions.cpp:11:1 + +0x10bc +system/extras/simpleperf/runtest/two_functions.cpp:13:0 + +0x1104 +system/extras/simpleperf/runtest/two_functions.cpp:16:10 + +0x112b +system/extras/simpleperf/runtest/two_functions.cpp:18:1 + +0x112c +system/extras/simpleperf/runtest/two_functions.cpp:20:0 + +0x113c +system/extras/simpleperf/runtest/two_functions.cpp:22:5 + +0x1140 +system/extras/simpleperf/runtest/two_functions.cpp:23:5 + +0x1147 +system/extras/simpleperf/runtest/two_functions.cpp:21:3 + """ + dso = Addr2Nearestline.Dso(None) + binary_finder = BinaryFinder(TestHelper.testdata_dir, ReadElf(TestHelper.ndk_path)) + addr2line = Addr2Nearestline(TestHelper.ndk_path, binary_finder, False) + addr_map = addr2line.parse_line_output(output, dso) + expected_addr_map = { + 0x104c: [('system/extras/simpleperf/runtest/two_functions.cpp', 6)], + 0x1094: [('system/extras/simpleperf/runtest/two_functions.cpp', 9)], + 0x10bb: [('system/extras/simpleperf/runtest/two_functions.cpp', 11)], + 0x10bc: [('system/extras/simpleperf/runtest/two_functions.cpp', 13)], + 0x1104: [('system/extras/simpleperf/runtest/two_functions.cpp', 16)], + 0x112b: [('system/extras/simpleperf/runtest/two_functions.cpp', 18)], + 0x112c: [('system/extras/simpleperf/runtest/two_functions.cpp', 20)], + 0x113c: [('system/extras/simpleperf/runtest/two_functions.cpp', 22)], + 0x1140: [('system/extras/simpleperf/runtest/two_functions.cpp', 23)], + 0x1147: [('system/extras/simpleperf/runtest/two_functions.cpp', 21)], + } + self.assertEqual(len(expected_addr_map), len(addr_map)) + for addr in expected_addr_map: + expected_source_list = expected_addr_map[addr] + source_list = addr_map[addr] + self.assertEqual(len(expected_source_list), len(source_list)) + for expected_source, source in zip(expected_source_list, source_list): + file_path = dso.file_id_to_name[source[0]] + self.assertEqual(file_path, expected_source[0]) + self.assertEqual(source[1], expected_source[1]) + def test_objdump(self): test_map = { '/simpleperf_runtest_two_functions_arm64': { @@ -239,24 +297,24 @@ class TestTools(TestBase): def test_source_file_searcher(self): searcher = SourceFileSearcher( - [TestHelper.testdata_path('SimpleperfExampleWithNative'), + [TestHelper.testdata_path('SimpleperfExampleCpp'), TestHelper.testdata_path('SimpleperfExampleOfKotlin')]) def format_path(path): return os.path.join(TestHelper.testdata_dir, path.replace('/', os.sep)) # Find a C++ file with pure file name. self.assertEqual( - format_path('SimpleperfExampleWithNative/app/src/main/cpp/native-lib.cpp'), + format_path('SimpleperfExampleCpp/app/src/main/cpp/native-lib.cpp'), searcher.get_real_path('native-lib.cpp')) # Find a C++ file with an absolute file path. self.assertEqual( - format_path('SimpleperfExampleWithNative/app/src/main/cpp/native-lib.cpp'), + format_path('SimpleperfExampleCpp/app/src/main/cpp/native-lib.cpp'), searcher.get_real_path('/data/native-lib.cpp')) # Find a Java file. self.assertEqual( - format_path('SimpleperfExampleWithNative/app/src/main/java/com/example/' + - 'simpleperf/simpleperfexamplewithnative/MainActivity.java'), - searcher.get_real_path('simpleperfexamplewithnative/MainActivity.java')) + format_path( + 'SimpleperfExampleCpp/app/src/main/java/simpleperf/example/cpp/MainActivity.java'), + searcher.get_real_path('cpp/MainActivity.java')) # Find a Kotlin file. self.assertEqual( format_path('SimpleperfExampleOfKotlin/app/src/main/java/com/example/' + |