1*288bf522SAndroid Build Coastguard Worker# perf2cfg 2*288bf522SAndroid Build Coastguard Worker 3*288bf522SAndroid Build Coastguard Workerperf2cfg annotates a control-flow graph (CFG) file with profiling information 4*288bf522SAndroid Build Coastguard Workerfrom simpleperf data files. A CFG file can be generated by the Android Runtime 5*288bf522SAndroid Build Coastguard Workercompiler using the `--dump-cfg=<cfg-file>` option. The tool outputs an 6*288bf522SAndroid Build Coastguard Workerannotated CFG file with the following added information: 7*288bf522SAndroid Build Coastguard Worker- Methods are annotated with their contribution relative to the total profile. 8*288bf522SAndroid Build Coastguard Worker- Basic blocks and assembly instructions are annotated with their contribution 9*288bf522SAndroid Build Coastguard Worker relative to the method profile. 10*288bf522SAndroid Build Coastguard Worker- Basic blocks are colored according to their contribution to the method 11*288bf522SAndroid Build Coastguard Worker profile. 12*288bf522SAndroid Build Coastguard Worker 13*288bf522SAndroid Build Coastguard WorkerThe tool does not modify any input files and assumes the input CFG file can be 14*288bf522SAndroid Build Coastguard Workerparsed by c1visualizer. The input files must have all been generated for the 15*288bf522SAndroid Build Coastguard Workersame architecture. 16*288bf522SAndroid Build Coastguard Worker 17*288bf522SAndroid Build Coastguard Worker## Usage 18*288bf522SAndroid Build Coastguard Worker 19*288bf522SAndroid Build Coastguard Worker``` 20*288bf522SAndroid Build Coastguard Workerusage: perf2cfg [-h|--help] --cfg CFG --perf-data PERF_DATA [PERF_DATA ...] 21*288bf522SAndroid Build Coastguard Worker [--output-file OUTPUT_FILE] [-e|--events EVENTS] 22*288bf522SAndroid Build Coastguard Worker [--primary-event PRIMARY_EVENT] 23*288bf522SAndroid Build Coastguard Worker 24*288bf522SAndroid Build Coastguard WorkerAnnotates a CFG file with profiling information from simpleperf data files. 25*288bf522SAndroid Build Coastguard Worker 26*288bf522SAndroid Build Coastguard Workeroptional arguments: 27*288bf522SAndroid Build Coastguard Worker -h, --help Show this help message and exit. 28*288bf522SAndroid Build Coastguard Worker --output-file OUTPUT_FILE 29*288bf522SAndroid Build Coastguard Worker A path to the output CFG file. 30*288bf522SAndroid Build Coastguard Worker -e EVENTS, --events EVENTS 31*288bf522SAndroid Build Coastguard Worker A comma-separated list of events only to use for 32*288bf522SAndroid Build Coastguard Worker annotating a CFG (default: use all events found in 33*288bf522SAndroid Build Coastguard Worker perf data). An error is reported if the events are not 34*288bf522SAndroid Build Coastguard Worker present in perf data. 35*288bf522SAndroid Build Coastguard Worker --primary-event PRIMARY_EVENT 36*288bf522SAndroid Build Coastguard Worker The event to be used for basic blocks hotness analysis 37*288bf522SAndroid Build Coastguard Worker (default: cpu-cycles). Basic blocks are color 38*288bf522SAndroid Build Coastguard Worker highlighted according to their hotness. An error is 39*288bf522SAndroid Build Coastguard Worker reported if the primary event is not present in perf 40*288bf522SAndroid Build Coastguard Worker data. 41*288bf522SAndroid Build Coastguard Worker 42*288bf522SAndroid Build Coastguard Workerrequired arguments: 43*288bf522SAndroid Build Coastguard Worker --cfg CFG The CFG file to annotate. 44*288bf522SAndroid Build Coastguard Worker --perf-data PERF_DATA [PERF_DATA ...] 45*288bf522SAndroid Build Coastguard Worker The perf data files to extract information from. 46*288bf522SAndroid Build Coastguard Worker``` 47*288bf522SAndroid Build Coastguard Worker 48*288bf522SAndroid Build Coastguard Worker### Examples 49*288bf522SAndroid Build Coastguard Worker 50*288bf522SAndroid Build Coastguard WorkerAnnotate a CFG file: 51*288bf522SAndroid Build Coastguard Worker``` 52*288bf522SAndroid Build Coastguard Workerperf2cfg --cfg art.cfg --perf-data perf.data 53*288bf522SAndroid Build Coastguard Worker``` 54*288bf522SAndroid Build Coastguard Worker 55*288bf522SAndroid Build Coastguard WorkerAnnotate a CFG file with multiple simpleperf data files: 56*288bf522SAndroid Build Coastguard Worker``` 57*288bf522SAndroid Build Coastguard Workerperf2cfg --cfg art.cfg \ 58*288bf522SAndroid Build Coastguard Worker --perf-data perf_event1.data perf_event2.data perf_event3.data 59*288bf522SAndroid Build Coastguard Worker``` 60*288bf522SAndroid Build Coastguard Worker 61*288bf522SAndroid Build Coastguard WorkerColor basic blocks according to cache-misses events: 62*288bf522SAndroid Build Coastguard Worker``` 63*288bf522SAndroid Build Coastguard Workerperf2cfg --cfg art.cfg --perf-data perf.data \ 64*288bf522SAndroid Build Coastguard Worker --primary-event cache-misses 65*288bf522SAndroid Build Coastguard Worker``` 66*288bf522SAndroid Build Coastguard Worker 67*288bf522SAndroid Build Coastguard WorkerDisplay a subset of events from the simpleperf data file: 68*288bf522SAndroid Build Coastguard Worker``` 69*288bf522SAndroid Build Coastguard Workerperf2cfg --cfg art.cfg --perf-data perf.data \ 70*288bf522SAndroid Build Coastguard Worker --events cpu-cycles,cache-misses 71*288bf522SAndroid Build Coastguard Worker``` 72*288bf522SAndroid Build Coastguard Worker 73*288bf522SAndroid Build Coastguard Worker## Method annotations 74*288bf522SAndroid Build Coastguard Worker 75*288bf522SAndroid Build Coastguard WorkerOnce the annotated CFG file has been opened in c1visualizer, method annotations 76*288bf522SAndroid Build Coastguard Workercan be seen by enabling the "Show Package Names" and "Sort List of 77*288bf522SAndroid Build Coastguard WorkerCompilations" options in the top-left "Compiled Methods" panel. 78*288bf522SAndroid Build Coastguard Worker 79*288bf522SAndroid Build Coastguard Worker## Basic block coloring 80*288bf522SAndroid Build Coastguard Worker 81*288bf522SAndroid Build Coastguard Workerperf2cfg implements basic block coloring by adding specific flags to the output 82*288bf522SAndroid Build Coastguard WorkerCFG file. These flags have the following names and meanings: 83*288bf522SAndroid Build Coastguard Worker- `LO` (low): the basic block is responsible for 1 to 10% of its method primary 84*288bf522SAndroid Build Coastguard Worker event. 85*288bf522SAndroid Build Coastguard Worker- `MO` (moderate): for 10 to 30%. 86*288bf522SAndroid Build Coastguard Worker- `CO` (considerable): for 30 to 50% 87*288bf522SAndroid Build Coastguard Worker- `HI` (high): for 50 to 100%. 88*288bf522SAndroid Build Coastguard Worker 89*288bf522SAndroid Build Coastguard WorkerTo use this feature, custom flags have to be defined in c1visualizer: 90*288bf522SAndroid Build Coastguard Worker1. Open c1visualizer. 91*288bf522SAndroid Build Coastguard Worker2. Click on the "Tools" menu entry and "Options" to open the options window. 92*288bf522SAndroid Build Coastguard Worker3. Click on the "Control Flow Graph" button if it isn't already selected. 93*288bf522SAndroid Build Coastguard Worker4. On the right of the "Flags" list, click on the "New" button. 94*288bf522SAndroid Build Coastguard Worker5. Enter "LO" in the text field and press "OK". 95*288bf522SAndroid Build Coastguard Worker6. Select the newly created flag in the list and click on the color picker 96*288bf522SAndroid Build Coastguard Worker button. 97*288bf522SAndroid Build Coastguard Worker7. Select an appropriate color and press "OK". 98*288bf522SAndroid Build Coastguard Worker8. Repeat steps 4 to 7 for the remaining flags (MO, CO, and HI). 99*288bf522SAndroid Build Coastguard Worker 100*288bf522SAndroid Build Coastguard WorkerAlternatively, flags can be defined by editing a properties file located at: 101*288bf522SAndroid Build Coastguard Worker`~/.c1visualizer/dev/config/Preferences/at/ssw/visualizer/cfg/options/CfgPreferences.properties`. 102*288bf522SAndroid Build Coastguard WorkerThe directory hierarchy and the file itself might have to be created. 103*288bf522SAndroid Build Coastguard Worker 104*288bf522SAndroid Build Coastguard WorkerReplace the file contents with the following line to use a yellow to red 105*288bf522SAndroid Build Coastguard Workergradient: 106*288bf522SAndroid Build Coastguard Worker``` 107*288bf522SAndroid Build Coastguard WorkerflagsPreference=LO(255,210,0);MO(253,155,5);CO(253,100,5);HI(245,40,5) 108*288bf522SAndroid Build Coastguard Worker``` 109*288bf522SAndroid Build Coastguard Worker 110*288bf522SAndroid Build Coastguard WorkerFor colorblind people, this green gradient can be used as an alternative: 111*288bf522SAndroid Build Coastguard Worker``` 112*288bf522SAndroid Build Coastguard WorkerflagsPreference=LO(235,235,50);MO(210,210,40);CO(185,185,25);HI(155,155,15) 113*288bf522SAndroid Build Coastguard Worker``` 114*288bf522SAndroid Build Coastguard Worker 115*288bf522SAndroid Build Coastguard Worker## Hacking 116*288bf522SAndroid Build Coastguard Worker 117*288bf522SAndroid Build Coastguard WorkerA diagram of the finite state machine used to parse the input CFG file can be 118*288bf522SAndroid Build Coastguard Workergenerated with the following command (requires Graphviz): 119*288bf522SAndroid Build Coastguard Worker``` 120*288bf522SAndroid Build Coastguard Workerdot -Tpng doc/FSM.dot -o doc/FSM.png 121*288bf522SAndroid Build Coastguard Worker``` 122