1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2024 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker //
17*288bf522SAndroid Build Coastguard Worker // Test if simpleperf -> create_llvm_prof can convert addresses in ETM data correctly to
18*288bf522SAndroid Build Coastguard Worker // symbols and source lines in llvm prof data.
19*288bf522SAndroid Build Coastguard Worker //
20*288bf522SAndroid Build Coastguard Worker // To test address conversion for userspace binaries:
21*288bf522SAndroid Build Coastguard Worker // 1. Generate branch list file on device:
22*288bf522SAndroid Build Coastguard Worker // (device) $ simpleperf record -e cs-etm:u --delay 10 --duration 1 ./autofdo_addr_test
23*288bf522SAndroid Build Coastguard Worker // (device) $ simpleperf inject -i perf.data --output branch-list -o branch_user.data
24*288bf522SAndroid Build Coastguard Worker //
25*288bf522SAndroid Build Coastguard Worker // 2. Generate llvm prof data on host:
26*288bf522SAndroid Build Coastguard Worker // (host) $ simpleperf inject -i branch_user.data --symdir .
27*288bf522SAndroid Build Coastguard Worker // (host) $ create_llvm_prof --profiler text --binary=autofdo_addr_test \
28*288bf522SAndroid Build Coastguard Worker // --profile=perf_inject.data --out=user.profdata --format text
29*288bf522SAndroid Build Coastguard Worker //
30*288bf522SAndroid Build Coastguard Worker // 3. Check llvm prof data. It should show something like the following content:
31*288bf522SAndroid Build Coastguard Worker // (host) $ cat user.profdata
32*288bf522SAndroid Build Coastguard Worker // main:4289758:0
33*288bf522SAndroid Build Coastguard Worker // 0: 0
34*288bf522SAndroid Build Coastguard Worker // 1: 428974
35*288bf522SAndroid Build Coastguard Worker // 2: 428976
36*288bf522SAndroid Build Coastguard Worker //
37*288bf522SAndroid Build Coastguard Worker // To test address conversion for vmlinux:
38*288bf522SAndroid Build Coastguard Worker // 1. Generate branch list file on device:
39*288bf522SAndroid Build Coastguard Worker // (device) $ simpleperf inject -e cs-etm:k --delay 10 --duration 1 ./autofdo_addr_test
40*288bf522SAndroid Build Coastguard Worker // (device) $ simpleperf inject -i perf.data --output branch-list -o branch.data
41*288bf522SAndroid Build Coastguard Worker //
42*288bf522SAndroid Build Coastguard Worker // 2. Generate llvm prof data on host:
43*288bf522SAndroid Build Coastguard Worker // (device) $ simpleperf inject -i branch.data --symdir .
44*288bf522SAndroid Build Coastguard Worker // (device) $ create_llvm_prof --profiler text --binary=vmlinux --profile=perf_inject.data \
45*288bf522SAndroid Build Coastguard Worker // --out=kernel.profdata --format text
46*288bf522SAndroid Build Coastguard Worker //
47*288bf522SAndroid Build Coastguard Worker // 3. Check llvm prof data. The top functions should be like the following content:
48*288bf522SAndroid Build Coastguard Worker // (host) $ cat kernel.profdata | grep -v "^ "
49*288bf522SAndroid Build Coastguard Worker // __arm64_sys_getpriority:24967795:0
50*288bf522SAndroid Build Coastguard Worker // do_el0_svc:13978399:185833
51*288bf522SAndroid Build Coastguard Worker // el0_svc:11968147:184885
52*288bf522SAndroid Build Coastguard Worker // invoke_syscall:8962484:2843
53*288bf522SAndroid Build Coastguard Worker // __rcu_read_unlock:8667849:200748
54*288bf522SAndroid Build Coastguard Worker // el0_svc_common:7142648:0
55*288bf522SAndroid Build Coastguard Worker //
56*288bf522SAndroid Build Coastguard Worker // [TODO] Test address conversion for kernel modules
57*288bf522SAndroid Build Coastguard Worker //
58*288bf522SAndroid Build Coastguard Worker //
59*288bf522SAndroid Build Coastguard Worker //
60*288bf522SAndroid Build Coastguard Worker // Test if simpleperf -> perf2bolt can convert addresses in ETM data correctly to symbols and
61*288bf522SAndroid Build Coastguard Worker // and offsets in perf2bolt.
62*288bf522SAndroid Build Coastguard Worker //
63*288bf522SAndroid Build Coastguard Worker // To test address conversion for userspace binaries:
64*288bf522SAndroid Build Coastguard Worker // 1. Generate branch list file on device (as for create_llvm_prof)
65*288bf522SAndroid Build Coastguard Worker // 2. Generate perf2bolt profile on host:
66*288bf522SAndroid Build Coastguard Worker // (host) $ simpleperf inject -i branch_user.data --output bolt -o perf_inject_bolt.data \
67*288bf522SAndroid Build Coastguard Worker // --symdir .
68*288bf522SAndroid Build Coastguard Worker // (host) $ sed -i '1d' perf_inject_bolt.data
69*288bf522SAndroid Build Coastguard Worker // (host) $ perf2bolt --pa -p perf_inject_bolt.data -o perf.fdata autofdo_addr_test
70*288bf522SAndroid Build Coastguard Worker //
71*288bf522SAndroid Build Coastguard Worker // 3. Check perf.fdata. The output should be like the following content:
72*288bf522SAndroid Build Coastguard Worker // (host) $ cat perf.fdata
73*288bf522SAndroid Build Coastguard Worker // 1 getpriority@PLT c 0 [unknown] 0 0 429112
74*288bf522SAndroid Build Coastguard Worker // 1 main 10 1 getpriority@PLT 0 0 429110
75*288bf522SAndroid Build Coastguard Worker // 1 main 14 1 main 8 0 429109
76*288bf522SAndroid Build Coastguard Worker //
77*288bf522SAndroid Build Coastguard Worker //
78*288bf522SAndroid Build Coastguard Worker
79*288bf522SAndroid Build Coastguard Worker #include <sys/resource.h>
80*288bf522SAndroid Build Coastguard Worker
main()81*288bf522SAndroid Build Coastguard Worker int main() {
82*288bf522SAndroid Build Coastguard Worker while (true) {
83*288bf522SAndroid Build Coastguard Worker getpriority(PRIO_PROCESS, 0);
84*288bf522SAndroid Build Coastguard Worker }
85*288bf522SAndroid Build Coastguard Worker return 0;
86*288bf522SAndroid Build Coastguard Worker }
87