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