1*288bf522SAndroid Build Coastguard Worker# Pin Tool 2*288bf522SAndroid Build Coastguard Worker 3*288bf522SAndroid Build Coastguard WorkerThis tool is currently used mainly for: 4*288bf522SAndroid Build Coastguard Worker 5*288bf522SAndroid Build Coastguard Worker1) Inspecting resident memory and locality 6*288bf522SAndroid Build Coastguard Worker2) Generating and inspecting pinlist.meta used by Android's PinnerService 7*288bf522SAndroid Build Coastguard Worker 8*288bf522SAndroid Build Coastguard WorkerFor memory inspection, it allows probing live memory and providing the memory 9*288bf522SAndroid Build Coastguard Workerlocations that are resident which can be used for diagnosis or as part of PGO 10*288bf522SAndroid Build Coastguard Workeroptimizations. 11*288bf522SAndroid Build Coastguard Worker 12*288bf522SAndroid Build Coastguard Worker## Build and Install the tool 13*288bf522SAndroid Build Coastguard Worker 14*288bf522SAndroid Build Coastguard WorkerTo build and push the binary to device 15*288bf522SAndroid Build Coastguard Worker``` 16*288bf522SAndroid Build Coastguard Workermm pintool 17*288bf522SAndroid Build Coastguard Workerand push $ANDROID_REPO/out/target/product/<lunchtarget>/system/bin/pintool /data/local/tmp/pintool 18*288bf522SAndroid Build Coastguard Workeradb shell 19*288bf522SAndroid Build Coastguard Workercd /data/local/tmp/pintool 20*288bf522SAndroid Build Coastguard Worker``` 21*288bf522SAndroid Build Coastguard Worker 22*288bf522SAndroid Build Coastguard Worker 23*288bf522SAndroid Build Coastguard Worker## How to use the tool to generate pinner files 24*288bf522SAndroid Build Coastguard Worker 25*288bf522SAndroid Build Coastguard WorkerHere are some sample use cases for this tool. 26*288bf522SAndroid Build Coastguard Worker 27*288bf522SAndroid Build Coastguard Worker### Sample Use Case 1: Probe Resident Memory for a mapped file or library and dump to console 28*288bf522SAndroid Build Coastguard Worker 29*288bf522SAndroid Build Coastguard WorkerExecuting the following command and providing the path to a library mapped by a process 30*288bf522SAndroid Build Coastguard Workerit will dump the resident memory ranges. 31*288bf522SAndroid Build Coastguard Worker 32*288bf522SAndroid Build Coastguard Worker``` 33*288bf522SAndroid Build Coastguard Worker./pintool file <path_to_your_library> --gen-probe --dump 34*288bf522SAndroid Build Coastguard Worker``` 35*288bf522SAndroid Build Coastguard Worker 36*288bf522SAndroid Build Coastguard WorkerNote: you can use any kind of mapped file such as .so, .apk, etc. 37*288bf522SAndroid Build Coastguard Worker 38*288bf522SAndroid Build Coastguard Worker### Sample Use Case 2: Probe per-file Resident Memory for a mapped apk file and dump to console 39*288bf522SAndroid Build Coastguard Worker 40*288bf522SAndroid Build Coastguard WorkerExecuting this command will inspect resident memory for a zip file and dump 41*288bf522SAndroid Build Coastguard Workerper-file breakdowns. 42*288bf522SAndroid Build Coastguard Worker 43*288bf522SAndroid Build Coastguard Worker``` 44*288bf522SAndroid Build Coastguard Worker./pintool file <path_to_myfile.apk> --zip --gen-probe --dump 45*288bf522SAndroid Build Coastguard Worker``` 46*288bf522SAndroid Build Coastguard Worker 47*288bf522SAndroid Build Coastguard Worker### Sample Use Case 3: Probe per-file resident memory for a mapped apk, dump and generate pinlist.meta 48*288bf522SAndroid Build Coastguard Worker``` 49*288bf522SAndroid Build Coastguard Worker./pintool file <path_to_myfile.apk> --zip --gen-probe --dump -o pinlist.meta 50*288bf522SAndroid Build Coastguard Worker``` 51*288bf522SAndroid Build Coastguard Worker 52*288bf522SAndroid Build Coastguard Worker### Sample Use Case 4: Probe per-file resident memory and filter it with a provided pinconfig 53*288bf522SAndroid Build Coastguard Worker``` 54*288bf522SAndroid Build Coastguard Worker./pintool file <path_to_myfile.apk> --zip --gen-probe --pinconfig pinconfig.txt --dump -o pinlist.meta 55*288bf522SAndroid Build Coastguard Worker``` 56*288bf522SAndroid Build Coastguard Worker 57*288bf522SAndroid Build Coastguard Worker### Sample Use Case 5: Dump contents of a provided pinlist.meta file 58*288bf522SAndroid Build Coastguard Worker``` 59*288bf522SAndroid Build Coastguard Worker./pintool pinlist pinlist.meta --dump -v 60*288bf522SAndroid Build Coastguard Worker``` 61*288bf522SAndroid Build Coastguard Worker 62*288bf522SAndroid Build Coastguard Worker### Sample Use Case 6: Read a zip and filter based on a pinconfig file and generate pinlist.meta without probing 63*288bf522SAndroid Build Coastguard Worker 64*288bf522SAndroid Build Coastguard WorkerThis will skip doing any probing and it will just apply filtering based on the pinconfig.txt, this is helpful 65*288bf522SAndroid Build Coastguard Workerin cases where you do not intend to do any kind of PGO probing and know exactly what ranges you want to pin within your file 66*288bf522SAndroid Build Coastguard Worker 67*288bf522SAndroid Build Coastguard Worker``` 68*288bf522SAndroid Build Coastguard Worker./pintool file <path_to_myfile.apk> --zip --pinconfig pinconfig.txt --dump -o pinlist.meta 69*288bf522SAndroid Build Coastguard Worker``` 70*288bf522SAndroid Build Coastguard Worker 71*288bf522SAndroid Build Coastguard Worker### Sample Use Case 7: Load an existing zip probe and inspect its per-file contents 72*288bf522SAndroid Build Coastguard Worker 73*288bf522SAndroid Build Coastguard Worker``` 74*288bf522SAndroid Build Coastguard Worker./pintool file /data/app/~~tmTrs5_XINwbpYWroRu5rA==/org.chromium.trichromelibrary_602300034-EFoOwMgVNBbwkMnp9zcWbg==/base.apk --zip --use-probe pinlist.meta --dump 75*288bf522SAndroid Build Coastguard Worker``` 76*288bf522SAndroid Build Coastguard Worker 77*288bf522SAndroid Build Coastguard Worker 78*288bf522SAndroid Build Coastguard Worker## Pinconfig File Structure 79*288bf522SAndroid Build Coastguard Worker 80*288bf522SAndroid Build Coastguard WorkerPinconfig files specify a custom filter to be applied on top of a generated or provided memory probe 81*288bf522SAndroid Build Coastguard Workerit should specify a subset of files and optionally ranges within those files to be matched against 82*288bf522SAndroid Build Coastguard Workerand subsequently kept in case a pinlist.meta is generated. 83*288bf522SAndroid Build Coastguard Worker 84*288bf522SAndroid Build Coastguard WorkerA `pinconfig.txt` is just a list of files with a key value pair separated by a newline. 85*288bf522SAndroid Build Coastguard Worker 86*288bf522SAndroid Build Coastguard Worker`pinconfig.txt` structure pattern: 87*288bf522SAndroid Build Coastguard Worker``` 88*288bf522SAndroid Build Coastguard Worker(file <file> 89*288bf522SAndroid Build Coastguard Worker[offset <value> 90*288bf522SAndroid Build Coastguard Workerlength <value>])* 91*288bf522SAndroid Build Coastguard Worker``` 92*288bf522SAndroid Build Coastguard Workerwhere: 93*288bf522SAndroid Build Coastguard Worker<file> 94*288bf522SAndroid Build Coastguard Worker Filename as a string, the parser will do a contains like operation (e.g. GLOB(*<file>*)) to match against files 95*288bf522SAndroid Build Coastguard Worker within the zip file and stop on first match. 96*288bf522SAndroid Build Coastguard Worker<value> 97*288bf522SAndroid Build Coastguard Worker Unsigned integer value 98*288bf522SAndroid Build Coastguard Worker 99*288bf522SAndroid Build Coastguard WorkerNote: `offset` and `length` tokens are optional and if ommited, the whole file will be considered desired. 100*288bf522SAndroid Build Coastguard Worker 101*288bf522SAndroid Build Coastguard Worker 102*288bf522SAndroid Build Coastguard WorkerExample `pinconfig.txt`: 103*288bf522SAndroid Build Coastguard Worker``` 104*288bf522SAndroid Build Coastguard Workerfile lib/arm64-v8a/libcrashpad_handler_trampoline.so 105*288bf522SAndroid Build Coastguard Workerfile libmonochrome_64.so 106*288bf522SAndroid Build Coastguard Workeroffset 1000 107*288bf522SAndroid Build Coastguard Workerlen 50000 108*288bf522SAndroid Build Coastguard Workerfile libarcore_sdk_c.so 109*288bf522SAndroid Build Coastguard Worker``` 110*288bf522SAndroid Build Coastguard Worker 111*288bf522SAndroid Build Coastguard Worker## Pinlist.meta files 112*288bf522SAndroid Build Coastguard Worker 113*288bf522SAndroid Build Coastguard Worker"pinlist.meta" files are consumed by PinnerService. 114*288bf522SAndroid Build Coastguard Worker 115*288bf522SAndroid Build Coastguard WorkerThese files specify a list of memory ranges to be pinned (mlocked). 116*288bf522SAndroid Build Coastguard WorkerIf Android's PinnerService allows your app pinning, it will read the pinlist.meta 117*288bf522SAndroid Build Coastguard Workerfile from inside your apk's assets folder (assets/pinlist.meta) and pin based 118*288bf522SAndroid Build Coastguard Workeron the specified ranges. 119*288bf522SAndroid Build Coastguard Worker 120*288bf522SAndroid Build Coastguard WorkerNote: The PinnerService will need to support pinning your apk in order for the 121*288bf522SAndroid Build Coastguard Workerpinlist.meta file to be used. 122*288bf522SAndroid Build Coastguard Worker 123*288bf522SAndroid Build Coastguard WorkerA pinlist.meta file is a binary file with a set of tuples of OFFSET and LENGTH 124*288bf522SAndroid Build Coastguard Workerstored in Big Endian format. 125*288bf522SAndroid Build Coastguard Worker 126*288bf522SAndroid Build Coastguard Worker4 byte: OFFSET 127*288bf522SAndroid Build Coastguard Worker4 byte: LEN 128*288bf522SAndroid Build Coastguard Worker 129*288bf522SAndroid Build Coastguard Workerpinlist.meta 130*288bf522SAndroid Build Coastguard Worker``` 131*288bf522SAndroid Build Coastguard WorkerOFFSET LEN* 132*288bf522SAndroid Build Coastguard Worker``` 133*288bf522SAndroid Build Coastguard Worker 134*288bf522SAndroid Build Coastguard WorkerSo to read those files, it is usually helpful to use the `pintool`. 135*288bf522SAndroid Build Coastguard Worker 136*288bf522SAndroid Build Coastguard Worker## Other potential uses 137*288bf522SAndroid Build Coastguard Worker 138*288bf522SAndroid Build Coastguard WorkerOutside of pinner service, the tool can be used to inspect resident memory for 139*288bf522SAndroid Build Coastguard Workerany file in memory. 140*288bf522SAndroid Build Coastguard Worker 141*288bf522SAndroid Build Coastguard Worker## Extra information 142*288bf522SAndroid Build Coastguard Worker 143*288bf522SAndroid Build Coastguard Workerthe pinlist.meta depends on the apk contents and needs to be regenrated if 144*288bf522SAndroid Build Coastguard Workeryou are pushing a new version of your apk. 145*288bf522SAndroid Build Coastguard Worker 146*288bf522SAndroid Build Coastguard Worker## Tips for better pinning 147*288bf522SAndroid Build Coastguard Worker 148*288bf522SAndroid Build Coastguard WorkerTake the steps listed below before generating your pinlist to improve its quality. 149*288bf522SAndroid Build Coastguard Worker 150*288bf522SAndroid Build Coastguard Worker### Disable disk read-ahead before generating the pinlist 151*288bf522SAndroid Build Coastguard Worker 152*288bf522SAndroid Build Coastguard WorkerDisk read-ahead will add pages to your pinlist that are not actually accessed. 153*288bf522SAndroid Build Coastguard WorkerDisable disk read-ahead to reduce the size of your pinned memory by dropping 154*288bf522SAndroid Build Coastguard Workerunnecessary regions from the pinlist. 155*288bf522SAndroid Build Coastguard Worker 156*288bf522SAndroid Build Coastguard WorkerBefore invoking `pintool --gen-probe`, disable disk read-ahead: 157*288bf522SAndroid Build Coastguard Worker``` 158*288bf522SAndroid Build Coastguard Workerecho 1 > /sys/block/<block_device>/queue/read_ahead_kb 159*288bf522SAndroid Build Coastguard Worker``` 160*288bf522SAndroid Build Coastguard WorkerReplace <block_device> with the filesystem where the path to your `.apk` or library is hosted. You can find this information in the output of `df`. 161*288bf522SAndroid Build Coastguard Worker 162*288bf522SAndroid Build Coastguard WorkerIf you are unsure if read ahead is currently enabled, you can just query the value instead: 163*288bf522SAndroid Build Coastguard Worker``` 164*288bf522SAndroid Build Coastguard Workercat /sys/block/<block_device>/queue/read_ahead_kb 165*288bf522SAndroid Build Coastguard Worker``` 166*288bf522SAndroid Build Coastguard Worker 167*288bf522SAndroid Build Coastguard WorkerThis setting is reverted to the default after a reboot. 168*288bf522SAndroid Build Coastguard Worker 169*288bf522SAndroid Build Coastguard Worker### Drop the page cache before generating the pinlist 170*288bf522SAndroid Build Coastguard Worker 171*288bf522SAndroid Build Coastguard WorkerBefore taking a probe, it is a good idea to drop the page cache to reduce the amount of pages that 172*288bf522SAndroid Build Coastguard Workerare unrelated to your CUJ and start fresh loading new memory. 173*288bf522SAndroid Build Coastguard Worker 174*288bf522SAndroid Build Coastguard WorkerIn order to drop the page cache you can run this command: 175*288bf522SAndroid Build Coastguard Worker``` 176*288bf522SAndroid Build Coastguard Workerecho 3 > /proc/sys/vm/drop_caches 177*288bf522SAndroid Build Coastguard Worker```