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 18from pathlib import Path 19import re 20import tempfile 21 22from binary_cache_builder import BinaryCacheBuilder 23from . test_utils import TestBase, TestHelper 24 25 26class TestAnnotate(TestBase): 27 def test_annotate(self): 28 testdata_file = TestHelper.testdata_path('runtest_two_functions_arm64_perf.data') 29 30 # Build binary_cache. 31 binary_cache_builder = BinaryCacheBuilder(TestHelper.ndk_path, False) 32 binary_cache_builder.build_binary_cache(testdata_file, [TestHelper.testdata_dir]) 33 34 # Generate annotated files. 35 source_dir = TestHelper.testdata_dir 36 self.run_cmd(['annotate.py', '-i', testdata_file, '-s', 37 str(source_dir), '--summary-width', '1000']) 38 39 # Check annotated files. 40 annotate_dir = Path('annotated_files') 41 summary_file = annotate_dir / 'summary' 42 check_items = [ 43 re.compile(r'100.00% \| 100.00% \| .+two_functions.cpp'), 44 '100.00% | 0.00% | main (line 20)', 45 '50.06% | 50.06% | line 16', 46 ] 47 self.check_strings_in_file(summary_file, check_items) 48 49 source_files = list(annotate_dir.glob('**/*.cpp')) 50 self.assertEqual(len(source_files), 1) 51 source_file = source_files[0] 52 self.assertEqual(source_file.name, 'two_functions.cpp') 53 check_items = ['/* Total 50.06%, Self 50.06% */ *p = i;'] 54 self.check_strings_in_file(source_file, check_items) 55 56 def test_sample_filters(self): 57 def get_report(filter: str): 58 self.run_cmd(['annotate.py', '-i', TestHelper.testdata_path( 59 'perf_display_bitmaps.data')] + filter.split()) 60 61 get_report('--exclude-pid 31850') 62 get_report('--include-pid 31850') 63 get_report('--pid 31850') 64 get_report('--exclude-tid 31881') 65 get_report('--include-tid 31881') 66 get_report('--tid 31881') 67 get_report('--exclude-process-name com.example.android.displayingbitmaps') 68 get_report('--include-process-name com.example.android.displayingbitmaps') 69 get_report('--exclude-thread-name com.example.android.displayingbitmaps') 70 get_report('--include-thread-name com.example.android.displayingbitmaps') 71 72 with tempfile.NamedTemporaryFile('w', delete=False) as filter_file: 73 filter_file.write('GLOBAL_BEGIN 684943449406175\nGLOBAL_END 684943449406176') 74 filter_file.flush() 75 get_report('--filter-file ' + filter_file.name) 76 os.unlink(filter_file.name) 77 78 def test_show_art_frames(self): 79 self.run_cmd( 80 ['annotate.py', '-i', TestHelper.testdata_path('perf_with_interpreter_frames.data'), 81 '--show-art-frames']) 82 summary = Path('annotated_files') / 'summary' 83 self.check_strings_in_file(summary, 'total period: 9800649') 84