• 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
17import os
18import re
19import tempfile
20from typing import List, Optional, Set
21
22from . test_utils import TestBase, TestHelper
23
24
25class TestReportSample(TestBase):
26    def get_record_data_string(self, record_file: str, options: Optional[List[str]] = None):
27        args = ['report_sample.py', '-i', TestHelper.testdata_path(record_file)]
28        if options:
29            args += options
30        report = self.run_cmd(args, return_output=True)
31        return report.replace('\r', '')
32
33    def test_no_flags(self):
34        got = self.get_record_data_string('perf_display_bitmaps.data')
35        with open(TestHelper.testdata_path('perf_display_bitmaps.perf-script')) as f:
36            want = f.read()
37        self.assertEqual(got, want)
38
39    def test_comm_filter_to_renderthread(self):
40        got = self.get_record_data_string('perf_display_bitmaps.data', ['--comm', 'RenderThread'])
41        self.assertIn('RenderThread', got)
42        self.assertNotIn('com.example.android.displayingbitmaps', got)
43
44        with open(TestHelper.testdata_path('perf_display_bitmaps.RenderThread.perf-script')) as f:
45            want = f.read()
46        self.assertEqual(got, want)
47
48    def test_comm_filter_to_ui_thread(self):
49        got = self.get_record_data_string('perf_display_bitmaps.data', [
50                                          '--comm', 'com.example.android.displayingbitmaps'])
51        self.assertIn('com.example.android.displayingbitmaps', got)
52        self.assertNotIn('RenderThread', got)
53        with open(TestHelper.testdata_path('perf_display_bitmaps.UiThread.perf-script')) as f:
54            want = f.read()
55        self.assertEqual(got, want)
56
57    def test_header(self):
58        got = self.get_record_data_string('perf_display_bitmaps.data', ['--header'])
59        with open(TestHelper.testdata_path('perf_display_bitmaps.header.perf-script')) as f:
60            want = f.read()
61        self.assertEqual(got, want)
62
63    def test_trace_offcpu(self):
64        got = self.get_record_data_string('perf_with_trace_offcpu_v2.data', [
65                                          '--trace-offcpu', 'on-cpu'])
66        self.assertIn('cpu-clock:u', got)
67        self.assertNotIn('sched:sched_switch', got)
68
69    def test_sample_filters(self):
70        def get_threads_for_filter(filter: str) -> Set[int]:
71            report = self.get_record_data_string('perf_display_bitmaps.data', filter.split())
72            pattern = re.compile(r'\s+31850/(\d+)\s+')
73            threads = set()
74            for m in re.finditer(pattern, report):
75                threads.add(int(m.group(1)))
76            return threads
77
78        self.assertNotIn(31850, get_threads_for_filter('--exclude-pid 31850'))
79        self.assertIn(31850, get_threads_for_filter('--include-pid 31850'))
80        self.assertIn(31850, get_threads_for_filter('--pid 31850'))
81        self.assertNotIn(31881, get_threads_for_filter('--exclude-tid 31881'))
82        self.assertIn(31881, get_threads_for_filter('--include-tid 31881'))
83        self.assertIn(31881, get_threads_for_filter('--tid 31881'))
84        self.assertNotIn(31881, get_threads_for_filter(
85            '--exclude-process-name com.example.android.displayingbitmaps'))
86        self.assertIn(31881, get_threads_for_filter(
87            '--include-process-name com.example.android.displayingbitmaps'))
88        self.assertNotIn(31850, get_threads_for_filter(
89            '--exclude-thread-name com.example.android.displayingbitmaps'))
90        self.assertIn(31850, get_threads_for_filter(
91            '--include-thread-name com.example.android.displayingbitmaps'))
92
93        with tempfile.NamedTemporaryFile('w', delete=False) as filter_file:
94            filter_file.write('GLOBAL_BEGIN 684943449406175\nGLOBAL_END 684943449406176')
95            filter_file.flush()
96            threads = get_threads_for_filter('--filter-file ' + filter_file.name)
97            self.assertIn(31881, threads)
98            self.assertNotIn(31850, threads)
99        os.unlink(filter_file.name)
100
101    def test_show_art_frames(self):
102        art_frame_str = 'art::interpreter::DoCall'
103        report = self.get_record_data_string('perf_with_interpreter_frames.data')
104        self.assertNotIn(art_frame_str, report)
105        report = self.get_record_data_string(
106            'perf_with_interpreter_frames.data', ['--show-art-frames'])
107        self.assertIn(art_frame_str, report)
108