1# Trace Processor (Python) 2 3The trace processor Python API is built on the trace procesor 4[C++ library](/docs/analysis/trace-processor.md). By integrating with Python, 5the library allows using Python's rich data analysis ecosystem to process 6traces. 7 8## Setup 9``` 10pip install perfetto 11``` 12NOTE: The API is only compatible with Python3. 13 14```python 15from perfetto.trace_processor import TraceProcessor 16# Initialise TraceProcessor with a trace file 17tp = TraceProcessor(trace='trace.perfetto-trace') 18``` 19 20NOTE: The TraceProcessor can be initialized in a combination of ways including: 21 <br> - An address at which there exists a running instance of `trace_processor` with a 22 loaded trace (e.g.`TraceProcessor(addr='localhost:9001')`) 23 <br> - An address at which there exists a running instance of `trace_processor` and 24 needs a trace to be loaded in 25 (e.g. `TraceProcessor(trace='trace.perfetto-trace', addr='localhost:9001')`) 26 <br> - A path to a `trace_processor` binary and the trace to be loaded in 27 (e.g. `TraceProcessor(trace='trace.perfetto-trace', config=TraceProcessorConfig(bin_path='./trace_processor'))`) 28 29 30## API 31 32The `trace_processor.api` module contains the `TraceProcessor` class which provides various 33functions that can be called on the loaded trace. For more information on how to use 34these functions, see this [`example`](/python/example.py). 35 36#### Query 37The query() function takes an SQL query as input and returns an iterator through the rows 38of the result. 39 40```python 41from perfetto.trace_processor import TraceProcessor 42tp = TraceProcessor(trace='trace.perfetto-trace') 43 44qr_it = tp.query('SELECT ts, dur, name FROM slice') 45for row in qr_it: 46 print(row.ts, row.dur, row.name) 47``` 48**Output** 49``` 50261187017446933 358594 eglSwapBuffersWithDamageKHR 51261187017518340 357 onMessageReceived 52261187020825163 9948 queueBuffer 53261187021345235 642 bufferLoad 54261187121345235 153 query 55... 56``` 57The QueryResultIterator can also be converted to a Pandas DataFrame, although this 58requires you to have both the `NumPy` and `Pandas` modules installed. 59```python 60from perfetto.trace_processor import TraceProcessor 61tp = TraceProcessor(trace='trace.perfetto-trace') 62 63qr_it = tp.query('SELECT ts, dur, name FROM slice') 64qr_df = qr_it.as_pandas_dataframe() 65print(qr_df.to_string()) 66``` 67**Output** 68``` 69ts dur name 70-------------------- -------------------- --------------------------- 71 261187017446933 358594 eglSwapBuffersWithDamageKHR 72 261187017518340 357 onMessageReceived 73 261187020825163 9948 queueBuffer 74 261187021345235 642 bufferLoad 75 261187121345235 153 query 76 ... 77``` 78Furthermore, you can use the query result in a Pandas DataFrame format to easily 79make visualisations from the trace data. 80```python 81from perfetto.trace_processor import TraceProcessor 82tp = TraceProcessor(trace='trace.perfetto-trace') 83 84qr_it = tp.query('SELECT ts, value FROM counter WHERE track_id=50') 85qr_df = qr_it.as_pandas_dataframe() 86qr_df = qr_df.replace(np.nan,0) 87qr_df = qr_df.set_index('ts')['value'].plot() 88``` 89**Output** 90 91 92 93 94### Metric 95The metric() function takes in a list of trace metrics and returns the results as a Protobuf. 96 97```python 98from perfetto.trace_processor import TraceProcessor 99tp = TraceProcessor(trace='trace.perfetto-trace') 100 101ad_cpu_metrics = tp.metric(['android_cpu']) 102print(ad_cpu_metrics) 103``` 104**Output** 105``` 106metrics { 107 android_cpu { 108 process_info { 109 name: "/system/bin/init" 110 threads { 111 name: "init" 112 core { 113 id: 1 114 metrics { 115 mcycles: 1 116 runtime_ns: 570365 117 min_freq_khz: 1900800 118 max_freq_khz: 1900800 119 avg_freq_khz: 1902017 120 } 121 } 122 core { 123 id: 3 124 metrics { 125 mcycles: 0 126 runtime_ns: 366406 127 min_freq_khz: 1900800 128 max_freq_khz: 1900800 129 avg_freq_khz: 1902908 130 } 131 } 132 ... 133 } 134 ... 135 } 136 process_info { 137 name: "/system/bin/logd" 138 threads { 139 name: "logd.writer" 140 core { 141 id: 0 142 metrics { 143 mcycles: 8 144 runtime_ns: 33842357 145 min_freq_khz: 595200 146 max_freq_khz: 1900800 147 avg_freq_khz: 1891825 148 } 149 } 150 core { 151 id: 1 152 metrics { 153 mcycles: 9 154 runtime_ns: 36019300 155 min_freq_khz: 1171200 156 max_freq_khz: 1900800 157 avg_freq_khz: 1887969 158 } 159 } 160 ... 161 } 162 ... 163 } 164 ... 165 } 166} 167``` 168 169## HTTP 170The `trace_processor.http` module contains the `TraceProcessorHttp` class which 171provides methods to make HTTP requests to an address at which there already 172exists a running instance of `trace_processor` with a trace loaded in. All 173results are returned in Protobuf format 174(see [`trace_processor_proto`](/protos/perfetto/trace_processor/trace_processor.proto)). 175Some functions include: 176* `execute_query()` - Takes in an SQL query and returns a `QueryResult` Protobuf 177 message 178* `compute_metric()` - Takes in a list of trace metrics and returns a 179 `ComputeMetricResult` Protobuf message 180* `status()` - Returns a `StatusResult` Protobuf message