README.md
1# Profcollect
2
3Profcollect is a system daemon that facilitates sampling profile collection and reporting for native
4platform applications.
5
6Profcollect can only be enabled on `userdebug` or `eng` builds.
7
8## Supported Platforms
9
10Currently Profcollect only supports collecting profiles from Coresight ETM enabled ARM devices.
11
12Instructions to enable Coresight ETM can be found from the
13[simpleperf manual](https://android.googlesource.com/platform/system/extras/+/refs/heads/master/simpleperf/doc/collect_etm_data_for_autofdo.md).
14
15## Usage
16
17Profcollect has two components: `profcollectd`, the system daemon, and `profcollectctl`, the command
18line interface.
19
20### Collection
21
22`profcollectd` can be started from `adb` directly (under root), or automatically on system boot by
23setting system property through:
24
25```
26adb shell device_config put profcollect_native_boot enabled true
27```
28
29Profcollect collects profiles periodically, as well as through triggers like app launch events. Only
30a percentage of these events result in a profile collection to avoid using too much resource, these
31are controlled by the following configurations:
32
33| Event | Config |
34|------------|------------------------|
35| Periodic | collection\_interval |
36| App launch | applaunch\_trace\_freq |
37
38Setting the frequency value to `0` disables collection for the corresponding event.
39
40#### Custom configuration
41
42Under adb root:
43
44```
45# Record every 60s (By default, record every 10m). The actual interval will be longer than the
46# set value if the device goes to hibernation.
47oriole:/ # device_config put profcollect_native_boot collection_interval 60
48
49# Each time recording, record ETM data for 1s (By default, it's 0.5s).
50oriole:/ # device_config put profcollect_native_boot sampling_period 1000
51
52# Set ETM data storage limit to 50G (By default, it is 512M).
53oriole:/ # device_config put profcollect_native_boot max_trace_limit 53687091200
54
55# After adjusting configuration, need to restart profcollectd
56oriole:/ # setprop ctl.stop profcollectd
57# Wait for a few seconds.
58oriole:/ # setprop ctl.start profcollectd
59
60# Check if profcollectd is running
61oriole:/ # ps -e | grep profcollectd
62root 918 1 10945660 47040 binder_wait_for_work 0 S profcollectd
63
64# Check if the new configuration takes effect.
65oriole:/ # cat /data/misc/profcollectd/output/config.json
66{"version":1,"node_id":[189,15,145,225,97,167],"build_fingerprint":"google/oriole/oriole:Tiramisu/TP1A.220223.002/8211650:userdebug/dev-keys","collection_interval":{"secs":60,"nanos":0},"sampling_period":{"secs":1,"nanos":0},"binary_filter":"^/(system|apex/.+)/(bin|lib|lib64)/.+","max_trace_limit":53687091200}
67```
68
69To check existing collected ETM data:
70```
71oriole:/ # cd data/misc/profcollectd/trace/
72oriole:/data/misc/profcollectd/trace # ls
73```
74
75To check if ETM data can be collected successfully:
76```
77# Trigger one collection manually.
78oriole:/ # profcollectctl once
79Trace once
80
81# Check trace directory to see if there is a recent manual trace file.
82oriole:/ # ls /data/misc/profcollectd/trace/
8320220224-222946_manual.etmtrace
84```
85
86If there are too many trace files, we need to processing them to avoid reaching storage limit.
87It may take a long time.
88```
89oriole:/ # profcollectctl process
90Processing traces
91```
92
93### Processing
94
95The raw tracing data needs to be combined with the original binary to create the AutoFDO branch
96list. This is a costly process, thus it is done separately from the profile collection. Profcollect
97attempts to process all the traces when the device is idle and connected to a power supply. It can
98also be initiated by running:
99
100```
101adb shell profcollectctl process
102```
103
104### Reporting
105
106#### Manual
107
108After actively using the device for a period of time, the device should have gathered enough data to
109generate a good quality PGO profile that represents typical system usage. Run the following command
110to create a profile report:
111
112```
113$ adb shell profcollectctl report
114Creating profile report
115Report created at: 12345678-0000-abcd-8000-12345678abcd
116```
117
118You can then fetch the report by running (under root):
119
120```
121adb pull /data/misc/profcollectd/report/12345678-0000-abcd-8000-12345678abcd.zip
122```
123
124#### Automated Uploading to Server
125
126*In development*
127
128### Post Processing
129
130For each trace file, run:
131
132```
133simpleperf inject \
134 -i {TRACE_FILE_NAME} \
135 -o {OUTPUT_FILE_NAME}.data \
136 --binary {BINARY_NAME} \
137 --symdir out/target/product/{PRODUCT_NAME}/symbols
138```
139
140Afterwards, run [AutoFDO](https://github.com/google/autofdo) to generate Clang PGO profiles:
141
142```
143create_llvm_prof \
144 --profiler text \
145 --binary=${BINARY_PATH} \
146 --profile=${INPUT_FILE_NAME} \
147 --out={OUTPUT_FILE_NAME}.profdata
148```
149
150Finally, merge all the PGO profiles into one profile:
151
152```
153find {INPUT_DIR} -name *.profdata > proflist
154prebuilts/clang/host/linux-x86/llvm-binutils-stable/llvm-profdata merge \
155 --binary \
156 --sample \
157 --input-files proflist \
158 --output merged.profdata
159```
160
161More profile data usually generates better quality profiles. You may combine data from multiple
162devices running the same build to improve profile quality, and/or reduce the performance impact for
163each device (by reducing collection frequency).
164