xref: /aosp_15_r20/system/extras/simpleperf/scripts/test/inferno_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 collections
18*288bf522SAndroid Build Coastguard Workerimport json
19*288bf522SAndroid Build Coastguard Workerimport os
20*288bf522SAndroid Build Coastguard Workerimport re
21*288bf522SAndroid Build Coastguard Workerimport tempfile
22*288bf522SAndroid Build Coastguard Workerfrom typing import Any, Dict, List, Set
23*288bf522SAndroid Build Coastguard Worker
24*288bf522SAndroid Build Coastguard Workerfrom . test_utils import INFERNO_SCRIPT, TestBase, TestHelper
25*288bf522SAndroid Build Coastguard Worker
26*288bf522SAndroid Build Coastguard Worker
27*288bf522SAndroid Build Coastguard Workerclass TestInferno(TestBase):
28*288bf522SAndroid Build Coastguard Worker    def get_report(self, options: List[str]) -> str:
29*288bf522SAndroid Build Coastguard Worker        self.run_cmd([INFERNO_SCRIPT] + options)
30*288bf522SAndroid Build Coastguard Worker        with open('report.html', 'r') as fh:
31*288bf522SAndroid Build Coastguard Worker            return fh.read()
32*288bf522SAndroid Build Coastguard Worker
33*288bf522SAndroid Build Coastguard Worker    def test_proguard_mapping_file(self):
34*288bf522SAndroid Build Coastguard Worker        """ Test --proguard-mapping-file option. """
35*288bf522SAndroid Build Coastguard Worker        testdata_file = TestHelper.testdata_path('perf_need_proguard_mapping.data')
36*288bf522SAndroid Build Coastguard Worker        proguard_mapping_file = TestHelper.testdata_path('proguard_mapping.txt')
37*288bf522SAndroid Build Coastguard Worker        original_methodname = 'androidx.fragment.app.FragmentActivity.startActivityForResult'
38*288bf522SAndroid Build Coastguard Worker        # Can't show original method name without proguard mapping file.
39*288bf522SAndroid Build Coastguard Worker        self.assertNotIn(original_methodname, self.get_report(
40*288bf522SAndroid Build Coastguard Worker            ['--record_file', testdata_file, '-sc']))
41*288bf522SAndroid Build Coastguard Worker        # Show original method name with proguard mapping file.
42*288bf522SAndroid Build Coastguard Worker        self.assertIn(original_methodname, self.get_report(
43*288bf522SAndroid Build Coastguard Worker            ['--record_file', testdata_file, '-sc', '--proguard-mapping-file', proguard_mapping_file]))
44*288bf522SAndroid Build Coastguard Worker
45*288bf522SAndroid Build Coastguard Worker    def test_trace_offcpu(self):
46*288bf522SAndroid Build Coastguard Worker        """ Test --trace-offcpu option. """
47*288bf522SAndroid Build Coastguard Worker        testdata_file = TestHelper.testdata_path('perf_with_trace_offcpu_v2.data')
48*288bf522SAndroid Build Coastguard Worker        report = self.get_report(['--record_file', testdata_file,
49*288bf522SAndroid Build Coastguard Worker                                  '-sc', '--trace-offcpu', 'off-cpu'])
50*288bf522SAndroid Build Coastguard Worker        self.assertIn('Thread 6525 (com.google.samples.apps.sunflower) (42 samples)', report)
51*288bf522SAndroid Build Coastguard Worker
52*288bf522SAndroid Build Coastguard Worker    def test_sample_filters(self):
53*288bf522SAndroid Build Coastguard Worker        def get_threads_for_filter(filter: str) -> Set[int]:
54*288bf522SAndroid Build Coastguard Worker            report = self.get_report(
55*288bf522SAndroid Build Coastguard Worker                ['--record_file', TestHelper.testdata_path('perf_display_bitmaps.data'),
56*288bf522SAndroid Build Coastguard Worker                 '-sc'] + filter.split())
57*288bf522SAndroid Build Coastguard Worker            threads = set()
58*288bf522SAndroid Build Coastguard Worker            pattern = re.compile(r'Thread\s+(\d+)\s+')
59*288bf522SAndroid Build Coastguard Worker            threads = set()
60*288bf522SAndroid Build Coastguard Worker            for m in re.finditer(pattern, report):
61*288bf522SAndroid Build Coastguard Worker                threads.add(int(m.group(1)))
62*288bf522SAndroid Build Coastguard Worker            return threads
63*288bf522SAndroid Build Coastguard Worker
64*288bf522SAndroid Build Coastguard Worker        self.assertNotIn(31850, get_threads_for_filter('--exclude-pid 31850'))
65*288bf522SAndroid Build Coastguard Worker        self.assertIn(31850, get_threads_for_filter('--include-pid 31850'))
66*288bf522SAndroid Build Coastguard Worker        self.assertNotIn(31881, get_threads_for_filter('--exclude-tid 31881'))
67*288bf522SAndroid Build Coastguard Worker        self.assertIn(31881, get_threads_for_filter('--include-tid 31881'))
68*288bf522SAndroid Build Coastguard Worker        self.assertNotIn(31881, get_threads_for_filter(
69*288bf522SAndroid Build Coastguard Worker            '--exclude-process-name com.example.android.displayingbitmaps'))
70*288bf522SAndroid Build Coastguard Worker        self.assertIn(31881, get_threads_for_filter(
71*288bf522SAndroid Build Coastguard Worker            '--include-process-name com.example.android.displayingbitmaps'))
72*288bf522SAndroid Build Coastguard Worker        self.assertNotIn(31850, get_threads_for_filter(
73*288bf522SAndroid Build Coastguard Worker            '--exclude-thread-name com.example.android.displayingbitmaps'))
74*288bf522SAndroid Build Coastguard Worker        self.assertIn(31850, get_threads_for_filter(
75*288bf522SAndroid Build Coastguard Worker            '--include-thread-name com.example.android.displayingbitmaps'))
76*288bf522SAndroid Build Coastguard Worker
77*288bf522SAndroid Build Coastguard Worker        with tempfile.NamedTemporaryFile('w', delete=False) as filter_file:
78*288bf522SAndroid Build Coastguard Worker            filter_file.write('GLOBAL_BEGIN 684943449406175\nGLOBAL_END 684943449406176')
79*288bf522SAndroid Build Coastguard Worker            filter_file.flush()
80*288bf522SAndroid Build Coastguard Worker            threads = get_threads_for_filter('--filter-file ' + filter_file.name)
81*288bf522SAndroid Build Coastguard Worker            self.assertIn(31881, threads)
82*288bf522SAndroid Build Coastguard Worker            self.assertNotIn(31850, threads)
83*288bf522SAndroid Build Coastguard Worker        os.unlink(filter_file.name)
84*288bf522SAndroid Build Coastguard Worker
85*288bf522SAndroid Build Coastguard Worker    def test_show_art_frames(self):
86*288bf522SAndroid Build Coastguard Worker        art_frame_str = 'art::interpreter::DoCall'
87*288bf522SAndroid Build Coastguard Worker        options = ['--record_file',
88*288bf522SAndroid Build Coastguard Worker                   TestHelper.testdata_path('perf_with_interpreter_frames.data'), '-sc']
89*288bf522SAndroid Build Coastguard Worker        report = self.get_report(options)
90*288bf522SAndroid Build Coastguard Worker        self.assertNotIn(art_frame_str, report)
91*288bf522SAndroid Build Coastguard Worker        report = self.get_report(options + ['--show-art-frames'])
92*288bf522SAndroid Build Coastguard Worker        self.assertIn(art_frame_str, report)
93