xref: /aosp_15_r20/external/perfetto/test/trace_processor/diff_tests/parser/instruments/tests.py (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1#!/usr/bin/env python3
2# Copyright (C) 2024 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License a
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from python.generators.diff_tests.testing import Csv, Path, DataPath
17from python.generators.diff_tests.testing import DiffTestBlueprint
18from python.generators.diff_tests.testing import TestSuite
19
20
21# These diff tests use some locally collected trace.
22class Instruments(TestSuite):
23
24  def test_xml_stacks(self):
25    return DiffTestBlueprint(
26        trace=DataPath('instruments_trace.xml'),
27        query='''
28          WITH
29            child AS (
30              SELECT
31                spc.id AS root,
32                spc.id,
33                spc.parent_id,
34                rel_pc AS path
35              FROM
36                instruments_sample s
37                JOIN stack_profile_callsite spc ON (s.callsite_id = spc.id)
38                JOIN stack_profile_frame f ON (f.id = frame_id)
39              UNION ALL
40              SELECT
41                child.root,
42                parent.id,
43                parent.parent_id,
44                COALESCE(f.rel_pc || ',', '') || child.path AS path
45              FROM
46                child
47                JOIN stack_profile_callsite parent ON (child.parent_id = parent.id)
48                LEFT JOIN stack_profile_frame f ON (f.id = frame_id)
49            )
50          SELECT
51            s.id,
52            s.ts,
53            s.utid,
54            c.path
55          FROM
56            instruments_sample s
57            JOIN child c ON s.callsite_id = c.root
58          WHERE
59            c.parent_id IS NULL
60        ''',
61        out=Csv('''
62          "id","ts","utid","path"
63          0,175685291,1,"23999,34891,37935,334037"
64          1,176684208,1,"24307,28687,265407,160467,120123,391295,336787,8955,340991,392555,136711,5707,7603,10507,207839,207495,23655,17383,23211,208391,6225"
65          2,177685166,1,"24915,16095,15891,32211,91151,26907,87887,60651,28343,29471,30159,11087,36269"
66          3,178683916,1,"24915,16107,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16021"
67          4,179687000,1,"24915,16107,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16005"
68          5,180683708,1,"24915,16107,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16047,16005"
69        '''))
70
71  def test_symbolized_frames(self):
72    return DiffTestBlueprint(
73        trace=DataPath('instruments_trace_with_symbols.zip'),
74        query='''
75          SELECT
76            f.id,
77            m.name,
78            m.build_id,
79            f.rel_pc,
80            s.name,
81            s.source_file,
82            s.line_number
83          FROM
84            stack_profile_frame f
85            JOIN stack_profile_mapping m ON f.mapping = m.id
86            JOIN stack_profile_symbol s ON f.symbol_set_id = s.symbol_set_id
87        ''',
88        out=Csv('''
89          "id","name","build_id","rel_pc","name","source_file","line_number"
90          26,"/private/tmp/test","c3b3bdbd348730f18f9ddd08b7708d49",16095,"main","/tmp/test.cpp",25
91          27,"/private/tmp/test","c3b3bdbd348730f18f9ddd08b7708d49",15891,"EmitSignpost()","/tmp/test.cpp",8
92          38,"/private/tmp/test","c3b3bdbd348730f18f9ddd08b7708d49",16107,"main","/tmp/test.cpp",27
93          39,"/private/tmp/test","c3b3bdbd348730f18f9ddd08b7708d49",16047,"fib(int)","/tmp/test.cpp",21
94          40,"/private/tmp/test","c3b3bdbd348730f18f9ddd08b7708d49",16021,"fib(int)","/tmp/test.cpp",22
95          41,"/private/tmp/test","c3b3bdbd348730f18f9ddd08b7708d49",16005,"fib(int)","/tmp/test.cpp",15
96        '''))
97
98  def test_symbolized_stacks(self):
99    return DiffTestBlueprint(
100        trace=DataPath('instruments_trace_with_symbols.zip'),
101        query='''
102          WITH
103            frame AS (
104              SELECT
105                f.id AS frame_id,
106                COALESCE(s.name || ':' || s.line_number, f.rel_pc) as name
107              FROM
108                stack_profile_frame f
109                LEFT JOIN stack_profile_symbol s USING (symbol_set_id)
110            ),
111            child AS (
112              SELECT
113                spc.id AS root,
114                spc.id,
115                spc.parent_id,
116                name AS path
117              FROM
118                instruments_sample s
119                JOIN stack_profile_callsite spc ON (s.callsite_id = spc.id)
120                LEFT JOIN frame f USING (frame_id)
121              UNION ALL
122              SELECT
123                child.root,
124                parent.id,
125                parent.parent_id,
126                COALESCE(f.name || ',', '') || child.path AS path
127              FROM
128                child
129                JOIN stack_profile_callsite parent ON (child.parent_id = parent.id)
130                LEFT JOIN frame f USING (frame_id)
131            )
132          SELECT
133            s.id,
134            s.ts,
135            s.utid,
136            c.path
137          FROM
138            instruments_sample s
139            JOIN child c ON s.callsite_id = c.root
140          WHERE
141            c.parent_id IS NULL
142        ''',
143        out=Csv('''
144          "id","ts","utid","path"
145          0,175685291,1,"23999,34891,37935,334037"
146          1,176684208,1,"24307,28687,265407,160467,120123,391295,336787,8955,340991,392555,136711,5707,7603,10507,207839,207495,23655,17383,23211,208391,6225"
147          2,177685166,1,"24915,main:25,EmitSignpost():8,32211,91151,26907,87887,60651,28343,29471,30159,11087,36269"
148          3,178683916,1,"24915,main:27,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):22"
149          4,179687000,1,"24915,main:27,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):15"
150          5,180683708,1,"24915,main:27,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):21,fib(int):15"
151        '''))
152