• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 are based on the same test data simpleperf uses for its
22# testing
23# (https://android.googlesource.com/platform/system/extras/+/refs/heads/main/simpleperf/testdata).
24# Basically we load these perf files and make sure we can get the same data we
25# would get via `simpleperf report`
26class Simpleperf(TestSuite):
27  # simpleperf report -i perf.data --print-event-count --csv --cpu 2,6,7
28  def test_perf(self):
29    return DiffTestBlueprint(
30        trace=DataPath('simpleperf/perf.data'),
31        query=Path('perf_test.sql'),
32        out=Csv('''
33        "event_count","command","pid","tid","shared_object","symbol"
34        130707953,"t2",26130,26130,"/t2","t2[+51c]"
35        126249237,"elf",26083,26083,"/elf","elf[+51c]"
36        109687208,"t1",26124,26124,"/t1","t1[+523]"
37        107027760,"t1",26124,26124,"/t1","t1[+51c]"
38        101887409,"t2",26130,26130,"/t2","t2[+523]"
39        92421568,"elf",26083,26083,"/elf","elf[+523]"
40        61539363,"t1",26124,26124,"/t1","t1[+518]"
41        60355129,"elf",26083,26083,"/elf","elf[+513]"
42        54840659,"t1",26124,26124,"/t1","t1[+4ed]"
43        52233968,"elf",26083,26083,"/elf","elf[+4ed]"
44        50833094,"t1",26124,26124,"/t1","t1[+4f7]"
45        50746374,"t2",26130,26130,"/t2","t2[+4ed]"
46        49185691,"elf",26083,26083,"/elf","elf[+4f7]"
47        47520901,"t2",26130,26130,"/t2","t2[+513]"
48        45979652,"elf",26083,26083,"/elf","elf[+518]"
49        44834371,"t2",26130,26130,"/t2","t2[+4f7]"
50        42928068,"t2",26130,26130,"/t2","t2[+518]"
51        39608138,"t1",26124,26124,"/t1","t1[+513]"
52        1390415,"t1",26124,26124,"/t1","t1[+4fa]"
53        1390305,"t2",26130,26130,"/t2","t2[+4fa]"
54        1390173,"elf",26083,26083,"/elf","elf[+500]"
55        1389030,"t2",26130,26130,"/t2","t2[+500]"
56        693786,"t2",26130,26130,"/lib/modules/3.13.0-76-generic/kernel/drivers/ata/pata_acpi.ko","pata_acpi.ko[+ffffffffa05c4da4]"
57        '''))
58
59  def test_perf_tracks(self):
60    return DiffTestBlueprint(
61        trace=DataPath('simpleperf/perf.data'),
62        query='''
63        SELECT
64          name,
65          unit,
66          description,
67          cpu,
68          is_timebase
69        FROM perf_counter_track
70        ORDER BY perf_session_id, name, cpu;
71        ''',
72        out=Csv('''
73        "name","unit","description","cpu","is_timebase"
74        "","","",2,1
75        "","","",6,1
76        "","","",7,1
77        "","","",16,1
78        '''))
79
80  def test_perf_with_add_counter_tracks(self):
81    return DiffTestBlueprint(
82        trace=DataPath('simpleperf/perf_with_add_counter.data'),
83        query='''
84        SELECT
85          name,
86          unit,
87          description,
88          cpu,
89          is_timebase
90        FROM perf_counter_track
91        ORDER BY perf_session_id, name, cpu;
92        ''',
93        out=Csv('''
94        "name","unit","description","cpu","is_timebase"
95        "cpu-cycles","","",40,1
96        "instructions","","",40,0
97        '''))
98
99  # simpleperf report -i perf.data --print-event-count --csv
100  # The thread name in this trace changes over time. simpleperf shows samples
101  # with the old and new name. Perfetto does not support threads changing names,
102  # it only keeps the last name, thus there is a slight mismatch in the outputs.
103  def test_perf_with_add_counter(self):
104    return DiffTestBlueprint(
105        trace=DataPath('simpleperf/perf_with_add_counter.data'),
106        query=Path('perf_with_add_counter_test.sql'),
107        out=Csv('''
108        "cpu_cycles","instructions","others","command","pid","tid","shared_object","symbol"
109        1011567,1188389,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa8cc9d30]"
110        219490,233619,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa8e498c6]"
111        191017,157031,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa94d0901]"
112        175099,140443,0,"sleep",689664,689664,"/lib/x86_64-linux-gnu/libc-2.32.so","_dl_addr"
113        152310,130151,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa8e30c70]"
114        122439,87058,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa960015d]"
115        89368,68332,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa8e03757]"
116        40272,30457,0,"sleep",689664,689664,"/lib/x86_64-linux-gnu/ld-2.32.so","ld-2.32.so[+1767b]"
117        14742,7858,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa8ce7a78]"
118        7551,1953,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa8cc90c5]"
119        7080,2940,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa8cc8119]"
120        3520,295,0,"sleep",689664,689664,"[kernel.kallsyms]","[kernel.kallsyms][+ffffffffa8c6b3e6]"
121        '''))
122
123  def test_build_id_feature(self):
124    return DiffTestBlueprint(
125        trace=DataPath('simpleperf/perf.data'),
126        query='''
127        SELECT build_id, name
128        FROM stack_profile_mapping
129        WHERE build_id <> ""
130        ORDER BY name
131        ''',
132        out=Csv('''
133        "build_id","name"
134        "0b12a384a9f4a3f3659b7171ca615dbec3a81f71","/elf"
135        "0b12a384a9f4a3f3659b7171ca615dbec3a81f71","/elf"
136        "47111a47babdcd27ca2f9ff450dc1897ded761ed","/lib/modules/3.13.0-76-generic/kernel/drivers/ata/pata_acpi.ko"
137        "0b12a384a9f4a3f3659b7171ca615dbec3a81f71","/t1"
138        "0b12a384a9f4a3f3659b7171ca615dbec3a81f71","/t2"
139        '''))
140
141  def test_clocks_align(self):
142    return DiffTestBlueprint(
143        trace=DataPath('zip/perf_track_sym.zip'),
144        query=Path('clocks_align_test.sql'),
145        out=Csv('''
146        "misaligned_count"
147        0
148        '''))
149
150  def test_cmdline(self):
151    return DiffTestBlueprint(
152        trace=DataPath('simpleperf/perf.data'),
153        query='''
154        SELECT cmdline
155        FROM perf_session
156        ''',
157        out=Csv('''
158        "cmdline"
159        "/ssd/android/aosp_master/out/host/linux-x86/bin/simpleperf record -p 26083,26090,26124,26130 sleep 0.0001"
160        '''))
161