Files
sunnypilot/selfdrive/test/profiling/profiler.py

102 lines
3.0 KiB
Python
Raw Normal View History

2020-04-30 15:15:19 -07:00
#!/usr/bin/env python3
import os
2020-10-04 15:55:25 -07:00
import sys
import cProfile # pylint: disable=import-error
import pprofile # pylint: disable=import-error
import pyprof2calltree # pylint: disable=import-error
2020-04-30 16:18:52 -07:00
2020-10-05 21:34:57 -07:00
from common.params import Params
2020-04-30 15:15:19 -07:00
from tools.lib.logreader import LogReader
from selfdrive.test.profiling.lib import SubMaster, PubMaster, SubSocket, ReplayDone
from selfdrive.test.process_replay.process_replay import CONFIGS
from selfdrive.car.toyota.values import CAR as TOYOTA
from selfdrive.car.honda.values import CAR as HONDA
from selfdrive.car.volkswagen.values import CAR as VW
2020-04-30 15:15:19 -07:00
BASE_URL = "https://commadataci.blob.core.windows.net/openpilotci/"
CARS = {
'toyota': ("0982d79ebb0de295|2021-01-03--20-03-36/6", TOYOTA.RAV4),
'honda': ("0982d79ebb0de295|2021-01-08--10-13-10/6", HONDA.CIVIC),
"vw": ("ef895f46af5fd73f|2021-05-22--14-06-35/6", VW.AUDI_A3_MK3),
}
2020-04-30 15:15:19 -07:00
def get_inputs(msgs, process, fingerprint):
for config in CONFIGS:
if config.proc_name == process:
sub_socks = list(config.pubs)
trigger = sub_socks[0]
break
2020-10-05 21:34:57 -07:00
# some procs block on CarParams
for msg in msgs:
if msg.which() == 'carParams':
m = msg.as_builder()
m.carParams.carFingerprint = fingerprint
2021-12-26 21:02:12 -08:00
Params().put("CarParams", m.carParams.copy().to_bytes())
2020-10-05 21:34:57 -07:00
break
sm = SubMaster(msgs, trigger, sub_socks)
pm = PubMaster()
2020-10-05 20:40:32 -07:00
if 'can' in sub_socks:
can_sock = SubSocket(msgs, 'can')
else:
can_sock = None
2020-05-01 10:49:02 -07:00
return sm, pm, can_sock
def profile(proc, func, car='toyota'):
segment, fingerprint = CARS[car]
segment = segment.replace('|', '/')
2020-04-30 15:15:19 -07:00
rlog_url = f"{BASE_URL}{segment}/rlog.bz2"
2020-10-05 21:34:57 -07:00
msgs = list(LogReader(rlog_url)) * int(os.getenv("LOOP", "1"))
2020-04-30 15:15:19 -07:00
os.environ['FINGERPRINT'] = fingerprint
os.environ['SKIP_FW_QUERY'] = "1"
os.environ['REPLAY'] = "1"
2020-10-05 20:48:23 -07:00
def run(sm, pm, can_sock):
2020-04-30 16:18:52 -07:00
try:
2020-10-05 20:40:32 -07:00
if can_sock is not None:
func(sm, pm, can_sock)
else:
func(sm, pm)
2020-04-30 16:18:52 -07:00
except ReplayDone:
pass
2020-10-05 20:40:32 -07:00
# Statistical
sm, pm, can_sock = get_inputs(msgs, proc, fingerprint)
2020-10-05 20:40:32 -07:00
with pprofile.StatisticalProfile()(period=0.00001) as pr:
2020-10-05 20:48:23 -07:00
run(sm, pm, can_sock)
2020-10-04 15:55:25 -07:00
pr.dump_stats(f'cachegrind.out.{proc}_statistical')
# Deterministic
sm, pm, can_sock = get_inputs(msgs, proc, fingerprint)
with cProfile.Profile() as pr:
2020-10-05 20:48:23 -07:00
run(sm, pm, can_sock)
2020-10-04 15:55:25 -07:00
pyprof2calltree.convert(pr.getstats(), f'cachegrind.out.{proc}_deterministic')
if __name__ == '__main__':
from selfdrive.controls.controlsd import main as controlsd_thread
from selfdrive.controls.radard import radard_thread
2020-10-05 20:40:32 -07:00
from selfdrive.locationd.paramsd import main as paramsd_thread
from selfdrive.controls.plannerd import main as plannerd_thread
from selfdrive.locationd.laikad import main as laikad_thread
2020-10-04 15:55:25 -07:00
procs = {
'radard': radard_thread,
'controlsd': controlsd_thread,
2020-10-05 20:40:32 -07:00
'paramsd': paramsd_thread,
'plannerd': plannerd_thread,
'laikad': laikad_thread,
2020-10-04 15:55:25 -07:00
}
2020-10-04 15:55:25 -07:00
proc = sys.argv[1]
if proc not in procs:
print(f"{proc} not available")
sys.exit(0)
else:
profile(proc, procs[proc])