Path: blob/master/AndroidRunner/Plugins/perfetto/trace_wrapper.py
630 views
import subprocess1import platform2import pandas as pd3from io import StringIO45class PerfettoTrace(object):6def __init__(self, trace_path, trace_processor_path):7""" Inits PerfettoTrace with the trace_path and trace_processor_path.89Parameters10----------11trace_path : string12Path to the perfetto trace file you want to query.13trace_processor_path : string14Path to the trace_processor executable file.1516Returns17-------18None19"""20self.trace_path = trace_path21self.trace_processor_path = trace_processor_path2223# Since trace_processor executable only works on x86 based architectures give an error when running this script on ARM based machine.24if "arm" in platform.uname().machine:25raise PerfettoTraceException("Trace processor is not yet supported on ARM.")2627def query(self, query):28""" Runs query on trace file and returns data as Pandas object.2930Parameters31----------32query : string33Query that is run on the perfetto trace file.3435Returns36-------37pandas.DataFrame38Pandas dataframe containing the results of the query.39"""40proc = subprocess.Popen([self.trace_processor_path, "-q", "/dev/stdin", self.trace_path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)41stdout, stderr = proc.communicate(input=query.encode("ascii"))4243if stderr:44raise PerfettoTraceException(stderr.decode("ascii"))45data = pd.read_csv(StringIO(stdout.decode('ascii')))46return data4748class PerfettoTraceException(Exception):49pass50515253545556