• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from collections import namedtuple
18import google.protobuf
19import os
20import re
21import tempfile
22from typing import List, Optional, Set
23
24from binary_cache_builder import BinaryCacheBuilder
25from pprof_proto_generator import load_pprof_profile, PprofProfileGenerator
26from . test_utils import TestBase, TestHelper
27from simpleperf_utils import ReportLibOptions
28
29
30class TestPprofProtoGenerator(TestBase):
31    def run_generator(self, options=None, testdata_file='perf_with_interpreter_frames.data'):
32        testdata_path = TestHelper.testdata_path(testdata_file)
33        options = options or []
34        self.run_cmd(['pprof_proto_generator.py', '-i', testdata_path] + options)
35        return self.run_cmd(['pprof_proto_generator.py', '--show'], return_output=True)
36
37    def generate_profile(self, options: Optional[List[str]], testdata_files: List[str]):
38        testdata_paths = [TestHelper.testdata_path(f) for f in testdata_files]
39        options = options or []
40        self.run_cmd(['pprof_proto_generator.py', '-i'] + testdata_paths + options)
41        return load_pprof_profile('pprof.profile')
42
43    def test_show_art_frames(self):
44        art_frame_str = 'art::interpreter::DoCall'
45        # By default, don't show art frames.
46        self.assertNotIn(art_frame_str, self.run_generator())
47        # Use --show_art_frames to show art frames.
48        self.assertIn(art_frame_str, self.run_generator(['--show_art_frames']))
49
50    def test_pid_filter(self):
51        key = 'PlayScene::DoFrame()'  # function in process 10419
52        self.assertIn(key, self.run_generator())
53        self.assertIn(key, self.run_generator(['--pid', '10419']))
54        self.assertIn(key, self.run_generator(['--pid', '10419', '10416']))
55        self.assertNotIn(key, self.run_generator(['--pid', '10416']))
56
57    def test_thread_labels(self):
58        output = self.run_generator()
59        self.assertIn('label[0] = thread:Binder:10419_1', output)
60        self.assertIn('label[0] = thread:Binder:10419_2', output)
61        self.assertIn('label[0] = thread:Binder:10419_3', output)
62        self.assertIn('label[0] = thread:Binder:10419_4', output)
63        self.assertIn('label[1] = threadpool:Binder:%d_%d', output)
64        self.assertIn('label[2] = pid:10419', output)
65        self.assertIn('label[3] = tid:10459', output)
66
67    def test_tid_filter(self):
68        key1 = 'art::ProfileSaver::Run()'  # function in thread 10459
69        key2 = 'PlayScene::DoFrame()'  # function in thread 10463
70        for options in ([], ['--tid', '10459', '10463']):
71            output = self.run_generator(options)
72            self.assertIn(key1, output)
73            self.assertIn(key2, output)
74        output = self.run_generator(['--tid', '10459'])
75        self.assertIn(key1, output)
76        self.assertNotIn(key2, output)
77        output = self.run_generator(['--tid', '10463'])
78        self.assertNotIn(key1, output)
79        self.assertIn(key2, output)
80
81    def test_comm_filter(self):
82        key1 = 'art::ProfileSaver::Run()'  # function in thread 'Profile Saver'
83        key2 = 'PlayScene::DoFrame()'  # function in thread 'e.sample.tunnel'
84        for options in ([], ['--comm', 'Profile Saver', 'e.sample.tunnel']):
85            output = self.run_generator(options)
86            self.assertIn(key1, output)
87            self.assertIn(key2, output)
88        output = self.run_generator(['--comm', 'Profile Saver'])
89        self.assertIn(key1, output)
90        self.assertNotIn(key2, output)
91        output = self.run_generator(['--comm', 'e.sample.tunnel'])
92        self.assertNotIn(key1, output)
93        self.assertIn(key2, output)
94
95    def test_build_id(self):
96        """ Test the build ids generated are not padded with zeros. """
97        self.assertIn('build_id: e3e938cc9e40de2cfe1a5ac7595897de(', self.run_generator())
98
99    def test_time_nanos(self):
100        """ Test the timestamp is adjusted to be nanoseconds. """
101        self.assertIn('time_nanos: 1516268753000000000\n', self.run_generator())
102
103    def test_build_id_with_binary_cache(self):
104        """ Test the build ids for elf files in binary_cache are not padded with zero. """
105        # Test with binary_cache.
106        testdata_file = TestHelper.testdata_path('runtest_two_functions_arm64_perf.data')
107
108        # Build binary_cache.
109        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
110        binary_cache_builder.build_binary_cache(testdata_file, [TestHelper.testdata_dir])
111
112        # Generate profile.
113        output = self.run_generator(testdata_file=testdata_file)
114        self.assertIn('build_id: b4f1b49b0fe9e34e78fb14e5374c930c(', output)
115
116    def test_location_address(self):
117        """ Test if the address of a location is within the memory range of the corresponding
118            mapping.
119        """
120        profile = self.generate_profile(None, ['perf_with_interpreter_frames.data'])
121        # pylint: disable=no-member
122        for location in profile.location:
123            mapping = profile.mapping[location.mapping_id - 1]
124            self.assertLessEqual(mapping.memory_start, location.address)
125            self.assertGreaterEqual(mapping.memory_limit, location.address)
126
127    def test_sample_type(self):
128        """Test sample types have the right units."""
129        output = self.run_generator()
130        self.assertIn('type=cpu-cycles_samples, unit=samples', output)
131        self.assertIn('type=cpu-cycles, unit=cpu-cycles', output)
132
133    def test_multiple_perf_data(self):
134        """ Test reporting multiple recording file. """
135        profile1 = self.generate_profile(None, ['aggregatable_perf1.data'])
136        profile2 = self.generate_profile(None, ['aggregatable_perf2.data'])
137        profile_both = self.generate_profile(
138            None, ['aggregatable_perf1.data', 'aggregatable_perf2.data'])
139        # pylint: disable=no-member
140        self.assertGreater(len(profile_both.sample), len(profile1.sample))
141        self.assertGreater(len(profile_both.sample), len(profile2.sample))
142
143    def test_proguard_mapping_file(self):
144        """ Test --proguard-mapping-file option. """
145        testdata_file = 'perf_need_proguard_mapping.data'
146        proguard_mapping_file = TestHelper.testdata_path('proguard_mapping.txt')
147        original_methodname = 'androidx.fragment.app.FragmentActivity.startActivityForResult'
148        # Can't show original method name without proguard mapping file.
149        self.assertNotIn(original_methodname, self.run_generator(testdata_file=testdata_file))
150        # Show original method name with proguard mapping file.
151        self.assertIn(original_methodname, self.run_generator(
152            ['--proguard-mapping-file', proguard_mapping_file], testdata_file))
153
154    def test_use_binary_cache(self):
155        testdata_file = TestHelper.testdata_path('runtest_two_functions_arm64_perf.data')
156
157        # Build binary_cache.
158        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
159        binary_cache_builder.build_binary_cache(testdata_file, [TestHelper.testdata_dir])
160
161        # Generate profile.
162        output = self.run_generator(testdata_file=testdata_file)
163        self.assertIn('simpleperf_runtest_two_functions_arm64', output)
164        self.assertIn('two_functions.cpp', output)
165
166    def test_line_info(self):
167        """ Check line numbers generated in profile. """
168        testdata_file = TestHelper.testdata_path('runtest_two_functions_arm64_perf.data')
169
170        # Build binary_cache.
171        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
172        binary_cache_builder.build_binary_cache(testdata_file, [TestHelper.testdata_dir])
173
174        # Generate profile.
175        profile = self.generate_profile(None, [testdata_file])
176
177        CheckItem = namedtuple(
178            'CheckItem', ['addr', 'source_file', 'source_line', 'func_name', 'func_start_line'])
179
180        check_items = [
181            CheckItem(0x113c, 'two_functions.cpp', 22, 'main', 20),
182            CheckItem(0x1140, 'two_functions.cpp', 23, 'main', 20),
183            CheckItem(0x1094, 'two_functions.cpp', 9, 'Function1', 6),
184            CheckItem(0x1104, 'two_functions.cpp', 16, 'Function2', 13),
185        ]
186        mapping = None
187        for mapping in profile.mapping:
188            binary_path = profile.string_table[mapping.filename]
189            if 'runtest_two_functions_arm64' in binary_path:
190                self.assertTrue(mapping.has_line_numbers)
191                mapping = mapping
192                break
193        self.assertIsNotNone(mapping)
194
195        for check_item in check_items:
196            found = False
197            for location in profile.location:
198                if location.mapping_id != mapping.id:
199                    continue
200                addr = location.address - mapping.memory_start + mapping.file_offset
201                if addr == check_item.addr:
202                    found = True
203                    self.assertEqual(len(location.line), 1)
204                    line = location.line[0]
205                    function = profile.function[line.function_id - 1]
206                    self.assertIn(check_item.source_file, profile.string_table[function.filename])
207                    self.assertEqual(line.line, check_item.source_line)
208                    self.assertIn(check_item.func_name, profile.string_table[function.name])
209                    self.assertEqual(function.start_line, check_item.func_start_line)
210                    break
211            self.assertTrue(found, check_item)
212
213    def test_function_name_not_changed_by_line_info(self):
214        """ Adding line info shouldn't override function names from report library, which are more
215            accurate when proguard mapping file is given.
216        """
217        testdata_file = TestHelper.testdata_path('runtest_two_functions_arm64_perf.data')
218
219        # Build binary_cache.
220        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
221        binary_cache_builder.build_binary_cache(testdata_file, [TestHelper.testdata_dir])
222
223        # Read recording file.
224        config = {'ndk_path': TestHelper.ndk_path, 'max_chain_length': 1000000,
225                  'report_lib_options': ReportLibOptions(False, None, '', None, None, None),
226                  'show_event_counters': False}
227        generator = PprofProfileGenerator(config)
228        generator.load_record_file(testdata_file)
229
230        # Change function name.
231        sample = generator.sample_list[0]
232        self.assertGreaterEqual(len(sample.location_ids), 1)
233        location = generator.location_list[sample.location_ids[0] - 1]
234        self.assertGreaterEqual(len(location.lines), 1)
235        function = generator.get_function(location.lines[0].function_id)
236        function_name = generator.get_string(function.name_id)
237        self.assertEqual(function_name, 'Function1()')
238        location.lines[0].function_id = generator.get_function_id(
239            'NewFunction1()', generator.get_string(function.dso_name_id), function.vaddr_in_dso)
240
241        # Add line info.
242        generator.gen_source_lines(1)
243
244        # Check function name and line info.
245        sample = generator.sample_list[0]
246        self.assertGreaterEqual(len(sample.location_ids), 1)
247        location = generator.location_list[sample.location_ids[0] - 1]
248        self.assertGreaterEqual(len(location.lines), 1)
249        function = generator.get_function(location.lines[0].function_id)
250        function_name = generator.get_string(function.name_id)
251        self.assertEqual(function_name, 'NewFunction1()')
252        self.assertNotEqual(function.source_filename_id, 0)
253        source_filename = generator.get_string(function.source_filename_id)
254        self.assertIn('two_functions.cpp', source_filename)
255
256    def test_inlined_function_names(self):
257        """ Test that we are getting correct function callstacks for inlined functions.
258        """
259        testdata_file = TestHelper.testdata_path("runtest_two_functions_arm64_inlined.data")
260        # Build binary_cache.
261        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
262        binary_cache_builder.build_binary_cache(testdata_file, [TestHelper.testdata_dir])
263
264        # Generate profile.
265        profile = self.generate_profile(None, [testdata_file])
266
267        CheckItem = namedtuple('CheckItem', ['func_name1', 'line1', 'func_name2', 'line2'])
268
269        check_items = {
270            0x40c8: CheckItem('Function1()', 9, 'main', 22),
271            0x40dc: CheckItem('Function2()', 16, 'main', 23),
272        }
273        results = {}
274
275        for location in profile.location:
276            mapping = profile.mapping[location.mapping_id - 1]
277            addr = location.address - mapping.memory_start + mapping.file_offset
278            if check_item := check_items.get(addr):
279                self.assertEqual(len(location.line), 2)
280                function1 = profile.function[location.line[0].function_id - 1]
281                line1 = location.line[0].line
282                function2 = profile.function[location.line[1].function_id - 1]
283                line2 = location.line[1].line
284                self.assertEqual(profile.string_table[function1.name], check_item.func_name1)
285                self.assertEqual(line1, check_item.line1)
286                self.assertEqual(profile.string_table[function2.name], check_item.func_name2)
287                self.assertEqual(line2, check_item.line2)
288                results[addr] = True
289        self.assertEqual(len(results), len(check_items))
290
291    def test_comments(self):
292        profile = self.generate_profile(None, ['perf_with_interpreter_frames.data'])
293        comments = "\n".join([profile.string_table[i] for i in profile.comment])
294        comments = comments.replace('\\', '/')
295        self.assertIn('Simpleperf Record Command:\n/data/data/com.google.sample.tunnel/simpleperf record --in-app --tracepoint-events /data/local/tmp/tracepoint_events --app com.google.sample.tunnel -g --no-post-unwind --duration 30', comments)
296        self.assertIn('Converted to pprof with:', comments)
297        # The full path changes per-machine, so only assert on a subset of the
298        # path.
299        self.assertIn('testdata/perf_with_interpreter_frames.data', comments)
300        self.assertIn('Architecture:\naarch64', comments)
301
302    def test_sample_filters(self):
303        def get_threads_for_filter(filter: str) -> Set[int]:
304            report = self.run_generator(filter.split(), testdata_file='perf_display_bitmaps.data')
305            threads = set()
306            pattern = re.compile(r'\s+tid:(\d+)')
307            threads = set()
308            for m in re.finditer(pattern, report):
309                threads.add(int(m.group(1)))
310            return threads
311
312        self.assertNotIn(31850, get_threads_for_filter('--exclude-pid 31850'))
313        self.assertIn(31850, get_threads_for_filter('--include-pid 31850'))
314        self.assertIn(31850, get_threads_for_filter('--pid 31850'))
315        self.assertNotIn(31881, get_threads_for_filter('--exclude-tid 31881'))
316        self.assertIn(31881, get_threads_for_filter('--include-tid 31881'))
317        self.assertIn(31881, get_threads_for_filter('--tid 31881'))
318        self.assertNotIn(31881, get_threads_for_filter(
319            '--exclude-process-name com.example.android.displayingbitmaps'))
320        self.assertIn(31881, get_threads_for_filter(
321            '--include-process-name com.example.android.displayingbitmaps'))
322        self.assertNotIn(31850, get_threads_for_filter(
323            '--exclude-thread-name com.example.android.displayingbitmaps'))
324        self.assertIn(31850, get_threads_for_filter(
325            '--include-thread-name com.example.android.displayingbitmaps'))
326
327        with tempfile.NamedTemporaryFile('w', delete=False) as filter_file:
328            filter_file.write('GLOBAL_BEGIN 684943449406175\nGLOBAL_END 684943449406176')
329            filter_file.flush()
330            threads = get_threads_for_filter('--filter-file ' + filter_file.name)
331            self.assertIn(31881, threads)
332            self.assertNotIn(31850, threads)
333        os.unlink(filter_file.name)
334
335    def test_report_sample_proto_file(self):
336        self.run_generator('', testdata_file='display_bitmaps.proto_data')
337
338    def test_tagroot(self):
339        data = self.run_generator(['--tagroot', 'comm', 'thread_comm'])
340        self.assertIn('process:e.sample.tunnel', data)
341        self.assertIn('thread:e.sample.tunnel', data)
342        self.assertIn('thread:Binder:10419_3', data)
343
344    def test_show_event_counters(self):
345        output = self.run_generator(
346            ['--show_event_counters'],
347            testdata_file='perf_with_add_counter.data')
348        self.assertIn('type=cpu-cycles_counter, unit=count', output)
349        self.assertIn('type=cpu-cycles_counter_samples, unit=samples', output)
350        self.assertIn('type=instructions_counter, unit=count', output)
351        self.assertIn('type=instructions_counter_samples, unit=samples', output)
352