Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
S2-group
GitHub Repository: S2-group/android-runner
Path: blob/master/AndroidRunner/Plugins/perfetto/trace_wrapper.py
630 views
1
import subprocess
2
import platform
3
import pandas as pd
4
from io import StringIO
5
6
class PerfettoTrace(object):
7
def __init__(self, trace_path, trace_processor_path):
8
""" Inits PerfettoTrace with the trace_path and trace_processor_path.
9
10
Parameters
11
----------
12
trace_path : string
13
Path to the perfetto trace file you want to query.
14
trace_processor_path : string
15
Path to the trace_processor executable file.
16
17
Returns
18
-------
19
None
20
"""
21
self.trace_path = trace_path
22
self.trace_processor_path = trace_processor_path
23
24
# Since trace_processor executable only works on x86 based architectures give an error when running this script on ARM based machine.
25
if "arm" in platform.uname().machine:
26
raise PerfettoTraceException("Trace processor is not yet supported on ARM.")
27
28
def query(self, query):
29
""" Runs query on trace file and returns data as Pandas object.
30
31
Parameters
32
----------
33
query : string
34
Query that is run on the perfetto trace file.
35
36
Returns
37
-------
38
pandas.DataFrame
39
Pandas dataframe containing the results of the query.
40
"""
41
proc = subprocess.Popen([self.trace_processor_path, "-q", "/dev/stdin", self.trace_path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
42
stdout, stderr = proc.communicate(input=query.encode("ascii"))
43
44
if stderr:
45
raise PerfettoTraceException(stderr.decode("ascii"))
46
data = pd.read_csv(StringIO(stdout.decode('ascii')))
47
return data
48
49
class PerfettoTraceException(Exception):
50
pass
51
52
53
54
55
56