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