Path: blob/master/AndroidRunner/Plugins/perfetto/trace_wrapper.py
907 views
import subprocess1import platform2import pandas as pd3import os4from pathlib import Path5from io import StringIO67class PerfettoTrace(object):8def __init__(self, trace_path, trace_processor_path=Path(__file__).resolve().parent / "trace_processor"):9""" Inits PerfettoTrace with the trace_path and trace_processor_path.1011Parameters12----------13trace_path : string14Path to the perfetto trace file you want to query.15trace_processor_path : string16Path to the trace_processor executable file.1718Returns19-------20None21"""22self.trace_path = trace_path23self.trace_processor_path = trace_processor_path2425# Since trace_processor executable only works on x86 based architectures give an error when running this script on ARM based machine.26if "arm" in platform.uname().machine:27raise PerfettoTraceException("Trace processor is not yet supported on ARM.")2829def query(self, query):30""" Runs query on trace file and returns data as Pandas object.3132Parameters33----------34query : string35Query that is run on the perfetto trace file.3637Returns38-------39pandas.DataFrame40Pandas dataframe containing the results of the query.41"""42proc = subprocess.Popen([self.trace_processor_path, "-q", "/dev/stdin", self.trace_path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)43stdout, stderr = proc.communicate(input=query.encode("ascii"))4445if stderr:46raise PerfettoTraceException(stderr.decode("ascii"))47data = pd.read_csv(StringIO(stdout.decode('ascii')))48return data4950class PerfettoTraceException(Exception):51pass52535455565758