xref: /aosp_15_r20/system/extras/simpleperf/scripts/test/annotate_test.py (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*288bf522SAndroid Build Coastguard Worker#
3*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2021 The Android Open Source Project
4*288bf522SAndroid Build Coastguard Worker#
5*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*288bf522SAndroid Build Coastguard Worker#
9*288bf522SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*288bf522SAndroid Build Coastguard Worker#
11*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*288bf522SAndroid Build Coastguard Worker# limitations under the License.
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Workerimport os
18*288bf522SAndroid Build Coastguard Workerfrom pathlib import Path
19*288bf522SAndroid Build Coastguard Workerimport re
20*288bf522SAndroid Build Coastguard Workerimport tempfile
21*288bf522SAndroid Build Coastguard Worker
22*288bf522SAndroid Build Coastguard Workerfrom binary_cache_builder import BinaryCacheBuilder
23*288bf522SAndroid Build Coastguard Workerfrom . test_utils import TestBase, TestHelper
24*288bf522SAndroid Build Coastguard Worker
25*288bf522SAndroid Build Coastguard Worker
26*288bf522SAndroid Build Coastguard Workerclass TestAnnotate(TestBase):
27*288bf522SAndroid Build Coastguard Worker    def test_annotate(self):
28*288bf522SAndroid Build Coastguard Worker        testdata_file = TestHelper.testdata_path('runtest_two_functions_arm64_perf.data')
29*288bf522SAndroid Build Coastguard Worker
30*288bf522SAndroid Build Coastguard Worker        # Build binary_cache.
31*288bf522SAndroid Build Coastguard Worker        binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False)
32*288bf522SAndroid Build Coastguard Worker        binary_cache_builder.build_binary_cache(testdata_file, [TestHelper.testdata_dir])
33*288bf522SAndroid Build Coastguard Worker
34*288bf522SAndroid Build Coastguard Worker        # Generate annotated files.
35*288bf522SAndroid Build Coastguard Worker        source_dir = TestHelper.testdata_dir
36*288bf522SAndroid Build Coastguard Worker        self.run_cmd(['annotate.py', '-i', testdata_file, '-s',
37*288bf522SAndroid Build Coastguard Worker                      str(source_dir), '--summary-width', '1000'])
38*288bf522SAndroid Build Coastguard Worker
39*288bf522SAndroid Build Coastguard Worker        # Check annotated files.
40*288bf522SAndroid Build Coastguard Worker        annotate_dir = Path('annotated_files')
41*288bf522SAndroid Build Coastguard Worker        summary_file = annotate_dir / 'summary'
42*288bf522SAndroid Build Coastguard Worker        check_items = [
43*288bf522SAndroid Build Coastguard Worker            re.compile(r'100.00% \| 100.00% \| .+two_functions.cpp'),
44*288bf522SAndroid Build Coastguard Worker            '100.00% | 0.00%  | main (line 20)',
45*288bf522SAndroid Build Coastguard Worker            '50.06%  | 50.06% | line 16',
46*288bf522SAndroid Build Coastguard Worker        ]
47*288bf522SAndroid Build Coastguard Worker        self.check_strings_in_file(summary_file, check_items)
48*288bf522SAndroid Build Coastguard Worker
49*288bf522SAndroid Build Coastguard Worker        source_files = list(annotate_dir.glob('**/*.cpp'))
50*288bf522SAndroid Build Coastguard Worker        self.assertEqual(len(source_files), 1)
51*288bf522SAndroid Build Coastguard Worker        source_file = source_files[0]
52*288bf522SAndroid Build Coastguard Worker        self.assertEqual(source_file.name, 'two_functions.cpp')
53*288bf522SAndroid Build Coastguard Worker        check_items = ['/* Total 50.06%, Self 50.06%          */    *p = i;']
54*288bf522SAndroid Build Coastguard Worker        self.check_strings_in_file(source_file, check_items)
55*288bf522SAndroid Build Coastguard Worker
56*288bf522SAndroid Build Coastguard Worker    def test_sample_filters(self):
57*288bf522SAndroid Build Coastguard Worker        def get_report(filter: str):
58*288bf522SAndroid Build Coastguard Worker            self.run_cmd(['annotate.py', '-i', TestHelper.testdata_path(
59*288bf522SAndroid Build Coastguard Worker                'perf_display_bitmaps.data')] + filter.split())
60*288bf522SAndroid Build Coastguard Worker
61*288bf522SAndroid Build Coastguard Worker        get_report('--exclude-pid 31850')
62*288bf522SAndroid Build Coastguard Worker        get_report('--include-pid 31850')
63*288bf522SAndroid Build Coastguard Worker        get_report('--pid 31850')
64*288bf522SAndroid Build Coastguard Worker        get_report('--exclude-tid 31881')
65*288bf522SAndroid Build Coastguard Worker        get_report('--include-tid 31881')
66*288bf522SAndroid Build Coastguard Worker        get_report('--tid 31881')
67*288bf522SAndroid Build Coastguard Worker        get_report('--exclude-process-name com.example.android.displayingbitmaps')
68*288bf522SAndroid Build Coastguard Worker        get_report('--include-process-name com.example.android.displayingbitmaps')
69*288bf522SAndroid Build Coastguard Worker        get_report('--exclude-thread-name com.example.android.displayingbitmaps')
70*288bf522SAndroid Build Coastguard Worker        get_report('--include-thread-name com.example.android.displayingbitmaps')
71*288bf522SAndroid Build Coastguard Worker
72*288bf522SAndroid Build Coastguard Worker        with tempfile.NamedTemporaryFile('w', delete=False) as filter_file:
73*288bf522SAndroid Build Coastguard Worker            filter_file.write('GLOBAL_BEGIN 684943449406175\nGLOBAL_END 684943449406176')
74*288bf522SAndroid Build Coastguard Worker            filter_file.flush()
75*288bf522SAndroid Build Coastguard Worker            get_report('--filter-file ' + filter_file.name)
76*288bf522SAndroid Build Coastguard Worker        os.unlink(filter_file.name)
77*288bf522SAndroid Build Coastguard Worker
78*288bf522SAndroid Build Coastguard Worker    def test_show_art_frames(self):
79*288bf522SAndroid Build Coastguard Worker        self.run_cmd(
80*288bf522SAndroid Build Coastguard Worker            ['annotate.py', '-i', TestHelper.testdata_path('perf_with_interpreter_frames.data'),
81*288bf522SAndroid Build Coastguard Worker             '--show-art-frames'])
82*288bf522SAndroid Build Coastguard Worker        summary = Path('annotated_files') / 'summary'
83*288bf522SAndroid Build Coastguard Worker        self.check_strings_in_file(summary, 'total period: 9800649')
84