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 json 18import os 19from pathlib import Path 20import re 21import tempfile 22from typing import List, Optional, Set 23 24from . test_utils import TestBase, TestHelper 25 26 27class TestStackCollapse(TestBase): 28 def get_report(self, testdata_file: str, options: Optional[List[str]] = None) -> str: 29 args = ['stackcollapse.py', '-i', TestHelper.testdata_path(testdata_file)] 30 if options: 31 args.extend(options) 32 report = self.run_cmd(args, return_output=True) 33 return report.replace('\r', '') 34 35 def test_jit_annotations(self): 36 got = self.get_report('perf_with_jit_symbol.data', ['--jit']) 37 golden_path = TestHelper.testdata_path('perf_with_jit_symbol.foldedstack') 38 self.assertEqual(got, Path(golden_path).read_text()) 39 40 def test_kernel_annotations(self): 41 got = self.get_report('perf_with_jit_symbol.data', ['--kernel']) 42 golden_path = TestHelper.testdata_path('perf_with_jit_symbol.foldedstack_with_kernel') 43 self.assertEqual(got, Path(golden_path).read_text()) 44 45 def test_with_pid(self): 46 got = self.get_report('perf_with_jit_symbol.data', ['--jit', '--pid']) 47 golden_path = TestHelper.testdata_path('perf_with_jit_symbol.foldedstack_with_pid') 48 self.assertEqual(got, Path(golden_path).read_text()) 49 50 def test_with_tid(self): 51 got = self.get_report('perf_with_jit_symbol.data', ['--jit', '--tid']) 52 golden_path = TestHelper.testdata_path('perf_with_jit_symbol.foldedstack_with_tid') 53 self.assertEqual(got, Path(golden_path).read_text()) 54 55 def test_two_event_types_chooses_first(self): 56 got = self.get_report('perf_with_two_event_types.data') 57 golden_path = TestHelper.testdata_path('perf_with_two_event_types.foldedstack') 58 self.assertEqual(got, Path(golden_path).read_text()) 59 60 def test_two_event_types_chooses_with_event_filter(self): 61 got = self.get_report('perf_with_two_event_types.data', ['--event-filter', 'cpu-clock']) 62 golden_path = TestHelper.testdata_path('perf_with_two_event_types.foldedstack_cpu_clock') 63 self.assertEqual(got, Path(golden_path).read_text()) 64 65 def test_unknown_symbol_addrs(self): 66 got = self.get_report('perf_with_jit_symbol.data', ['--addrs']) 67 golden_path = TestHelper.testdata_path('perf_with_jit_symbol.foldedstack_addrs') 68 self.assertEqual(got, Path(golden_path).read_text()) 69 70 def test_sample_filters(self): 71 def get_threads_for_filter(filter: str) -> Set[int]: 72 report = self.get_report('perf_display_bitmaps.data', ['--tid'] + filter.split()) 73 pattern = re.compile(r'-31850/(\d+);') 74 threads = set() 75 for m in re.finditer(pattern, report): 76 threads.add(int(m.group(1))) 77 return threads 78 79 self.assertNotIn(31850, get_threads_for_filter('--exclude-pid 31850')) 80 self.assertIn(31850, get_threads_for_filter('--include-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.assertNotIn(31881, get_threads_for_filter( 84 '--exclude-process-name com.example.android.displayingbitmaps')) 85 self.assertIn(31881, get_threads_for_filter( 86 '--include-process-name com.example.android.displayingbitmaps')) 87 self.assertNotIn(31850, get_threads_for_filter( 88 '--exclude-thread-name com.example.android.displayingbitmaps')) 89 self.assertIn(31850, get_threads_for_filter( 90 '--include-thread-name com.example.android.displayingbitmaps')) 91 92 with tempfile.NamedTemporaryFile('w', delete=False) as filter_file: 93 filter_file.write('GLOBAL_BEGIN 684943449406175\nGLOBAL_END 684943449406176') 94 filter_file.flush() 95 threads = get_threads_for_filter('--filter-file ' + filter_file.name) 96 self.assertIn(31881, threads) 97 self.assertNotIn(31850, threads) 98 os.unlink(filter_file.name) 99 100 def test_show_art_frames(self): 101 art_frame_str = 'art::interpreter::DoCall' 102 report = self.get_report('perf_with_interpreter_frames.data') 103 self.assertNotIn(art_frame_str, report) 104 report = self.get_report('perf_with_interpreter_frames.data', ['--show-art-frames']) 105 self.assertIn(art_frame_str, report) 106