• 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 json
18import os
19import re
20import tempfile
21from typing import Dict, List, Optional, Set
22from gecko_profile_generator import Category, StackFrame
23
24from . test_utils import TestBase, TestHelper
25
26
27class TestGeckoProfileGenerator(TestBase):
28    def run_generator(self, testdata_file: str, options: Optional[List[str]] = None) -> str:
29        testdata_path = TestHelper.testdata_path(testdata_file)
30        args = ['gecko_profile_generator.py', '-i', testdata_path]
31        if options:
32            args.extend(options)
33        return self.run_cmd(args, return_output=True)
34
35    def generate_profile(self, testdata_file: str, options: Optional[List[str]] = None) -> Dict:
36        output = self.run_generator(testdata_file, options)
37        return json.loads(output)
38
39    def test_golden(self):
40        output = self.run_generator('perf_with_interpreter_frames.data', ['--remove-gaps', '0'])
41        got = json.loads(output)
42        golden_path = TestHelper.testdata_path('perf_with_interpreter_frames.gecko.json')
43        with open(golden_path) as f:
44            want = json.load(f)
45        # Golden data is formatted with `jq` tool (https://stedolan.github.io/jq/).
46        # Regenerate golden data by running:
47        # $ apt install jq
48        # $ ./gecko_profile_generator.py --remove-gaps 0 -i ../testdata/perf_with_interpreter_frames.data | jq > test/script_testdata/perf_with_interpreter_frames.gecko.json
49        self.assertEqual(
50            json.dumps(got, sort_keys=True, indent=2),
51            json.dumps(want, sort_keys=True, indent=2))
52
53    def test_golden_offcpu(self):
54        output = self.run_generator('perf_with_tracepoint_event.data', ['--remove-gaps', '0'])
55        got = json.loads(output)
56        golden_path = TestHelper.testdata_path('perf_with_tracepoint_event.gecko.json')
57        with open(golden_path) as f:
58            want = json.load(f)
59        # Golden data is formatted with `jq` tool (https://stedolan.github.io/jq/).
60        # Regenerate golden data by running:
61        # $ apt install jq
62        # $ ./gecko_profile_generator.py --remove-gaps 0 -i ../testdata/perf_with_tracepoint_event.data | jq > test/script_testdata/perf_with_tracepoint_event.gecko.json
63        self.assertEqual(
64            json.dumps(got, sort_keys=True, indent=2),
65            json.dumps(want, sort_keys=True, indent=2))
66
67    def test_golden_jit(self):
68        output = self.run_generator('perf_with_jit_symbol.data', ['--remove-gaps', '0'])
69        got = json.loads(output)
70        golden_path = TestHelper.testdata_path('perf_with_jit_symbol.gecko.json')
71        with open(golden_path) as f:
72            want = json.load(f)
73        # Golden data is formatted with `jq` tool (https://stedolan.github.io/jq/).
74        # Regenerate golden data by running:
75        # $ apt install jq
76        # $ ./gecko_profile_generator.py --remove-gaps 0 -i ../testdata/perf_with_jit_symbol.data | jq > test/script_testdata/perf_with_jit_symbol.gecko.json
77        self.assertEqual(
78            json.dumps(got, sort_keys=True, indent=2),
79            json.dumps(want, sort_keys=True, indent=2))
80
81    def test_sample_filters(self):
82        def get_threads_for_filter(filter: str) -> Set[int]:
83            report = self.run_generator('perf_display_bitmaps.data',
84                                        filter.split() + ['--remove-gaps', '0'])
85            pattern = re.compile(r'"tid":\s+(\d+),')
86            threads = set()
87            for m in re.finditer(pattern, report):
88                threads.add(int(m.group(1)))
89            return threads
90
91        self.assertNotIn(31850, get_threads_for_filter('--exclude-pid 31850'))
92        self.assertIn(31850, get_threads_for_filter('--include-pid 31850'))
93        self.assertIn(31850, get_threads_for_filter('--pid 31850'))
94        self.assertNotIn(31881, get_threads_for_filter('--exclude-tid 31881'))
95        self.assertIn(31881, get_threads_for_filter('--include-tid 31881'))
96        self.assertIn(31881, get_threads_for_filter('--tid 31881'))
97        self.assertNotIn(31881, get_threads_for_filter(
98            '--exclude-process-name com.example.android.displayingbitmaps'))
99        self.assertIn(31881, get_threads_for_filter(
100            '--include-process-name com.example.android.displayingbitmaps'))
101        self.assertNotIn(31850, get_threads_for_filter(
102            '--exclude-thread-name com.example.android.displayingbitmaps'))
103        self.assertIn(31850, get_threads_for_filter(
104            '--include-thread-name com.example.android.displayingbitmaps'))
105
106        with tempfile.NamedTemporaryFile('w', delete=False) as filter_file:
107            filter_file.write('GLOBAL_BEGIN 684943449406175\nGLOBAL_END 684943449406176')
108            filter_file.flush()
109            threads = get_threads_for_filter('--filter-file ' + filter_file.name)
110            self.assertIn(31881, threads)
111            self.assertNotIn(31850, threads)
112        os.unlink(filter_file.name)
113
114    def test_show_art_frames(self):
115        art_frame_str = 'art::interpreter::DoCall'
116        report = self.run_generator('perf_with_interpreter_frames.data')
117        self.assertNotIn(art_frame_str, report)
118        report = self.run_generator('perf_with_interpreter_frames.data', ['--show-art-frames'])
119        self.assertIn(art_frame_str, report)
120
121    def test_remove_gaps(self):
122        testdata = 'perf_with_interpreter_frames.data'
123
124        def get_sample_count(options: Optional[List[str]] = None) -> int:
125            data = self.generate_profile(testdata, options)
126            sample_count = 0
127            for thread in data['threads']:
128                sample_count += len(thread['samples']['data'])
129            return sample_count
130        # By default, the gap sample is removed.
131        self.assertEqual(4031, get_sample_count())
132        # Use `--remove-gaps 0` to disable removing gaps.
133        self.assertEqual(4032, get_sample_count(['--remove-gaps', '0']))
134
135    def test_categories(self):
136        want = [
137            (StackFrame("do_translation_fault", "[kernel.kallsyms]"), Category.KERNEL),
138            (StackFrame("ufshcd_queuecommand", "/vendor/lib/modules/ufshcd-core.ko"), Category.KERNEL),
139            (StackFrame("pread64", "/apex/com.android.runtime/lib64/bionic/libc.so"), Category.NATIVE),
140            (StackFrame("sqlite3_step", "/data/app/~~wuHphp3RYz860st7j_csbg==/com.google.android.apps.maps-Ly1kpqXI4YEFCsPE36jq5A==/split_config.arm64_v8a.apk!/lib/arm64-v8a/libgmm-jni.so"), Category.NATIVE),
141            (StackFrame("__schedule", "[kernel.kallsyms]"), Category.OFF_CPU),
142            # b/362131906 regression: Collection.sort was classified as NATIVE
143            # due to having .so substring.
144            (StackFrame("java.util.Collections.sort", "/data/misc/apexdata/com.android.art/dalvik-cache/arm64/boot.oat"), Category.OAT),
145            (StackFrame("java.util.ArrayList.sort", "/data/misc/apexdata/com.android.art/dalvik-cache/arm64/boot.oat"), Category.OAT),
146            (StackFrame("java.lang.Thread.run", "/data/misc/apexdata/com.android.art/dalvik-cache/arm64/boot.oat"), Category.OAT),
147            (StackFrame("art_quick_alloc_object_initialized_region_tlab", "/apex/com.android.art/lib64/libart.so"), Category.NATIVE),
148            (StackFrame("java.lang.System.arraycopy", "/apex/com.android.art/javalib/core-oj.jar"), Category.DEX),
149            (StackFrame("com.google.protobuf.MessageSchema.parseMessage", "/data/app/~~wuHphp3RYz860st7j_csbg==/com.google.android.apps.maps-Ly1kpqXI4YEFCsPE36jq5A==/oat/arm64/base.odex"), Category.OAT),
150            (StackFrame("art_quick_invoke_stub", "/apex/com.android.art/lib64/libart.so"), Category.NATIVE),
151            (StackFrame("android.net.NetworkInfo.<init>", "[JIT app cache]"), Category.JIT),
152            (StackFrame("unknown", "noextension"), Category.USER),
153        ]
154        got = [(testcase[0], testcase[0].category()) for testcase in want]
155
156        self.assertEqual(want, got)
157