def start_profiling(func, filepath, statistical=True): import pprofile, signal if statistical: prof = pprofile.StatisticalProfile() else: prof = pprofile.Profile() def stop_profiling(prof, filepath): print('Writing profiling data: %s' % filepath, file=sys.stderr) print('You can use kcachegrind to analyze it.', file=sys.stderr) with open(filepath, 'w') as f: prof.callgrind(f) # This makes the `finally` block work as expected if we're terminated by # SIGTERM, which happens by default when running `timeout 30 stig ...`. # https://stackoverflow.com/a/42200623 # https://mail.python.org/pipermail/python-ideas/2016-February/038474.html import signal class SigTerm(SystemExit): pass def sigterm(sig, frame): raise SigTerm signal.signal(15, sigterm) try: with prof(): func() finally: stop_profiling(prof, filepath)
def profile(proc, func, car='toyota'): segment, fingerprint = CARS[car] segment = segment.replace('|', '/') rlog_url = f"{BASE_URL}{segment}/rlog.bz2" msgs = list(LogReader(rlog_url)) * int(os.getenv("LOOP", "1")) os.environ['FINGERPRINT'] = fingerprint def run(sm, pm, can_sock): try: if can_sock is not None: func(sm, pm, can_sock) else: func(sm, pm) except ReplayDone: pass # Statistical sm, pm, can_sock = get_inputs(msgs, proc, fingerprint) with pprofile.StatisticalProfile()(period=0.00001) as pr: run(sm, pm, can_sock) pr.dump_stats(f'cachegrind.out.{proc}_statistical') # Deterministic sm, pm, can_sock = get_inputs(msgs, proc, fingerprint) with cProfile.Profile() as pr: run(sm, pm, can_sock) pyprof2calltree.convert(pr.getstats(), f'cachegrind.out.{proc}_deterministic')
def inner_wrapper(*args, **kwargs): r.incr(key) call_num = int(r.get(key)) if call_num > max_call_num or (call_num - 1) % step != 0: return func(*args, **kwargs) print_title = ( ' ' * 30 + f"-*-pprofile_print_statistical_stats-*-|{call_num}") prof = pprofile.StatisticalProfile() with prof(period=0.001, single=True): result = func(*args, **kwargs) print('-' * 100) print(print_title) print('-' * 100) print('') _pprofile_dump(prof, f"{PROFILE_ROOT_PATH}/cachegrind.out.{call_num}") return result
def line_profiler(): profiler = None try: if settings.PROFILER["line-profiler"]: import pprofile if settings.PROFILER["line-profiler-type"] == "deterministic": profiler = pprofile.Profile() elif settings.PROFILER["line-profiler-type"] == "statistic": prof = pprofile.StatisticalProfile() profiler = prof( period=0.001, # Sample every 1ms single=True, # Only sample current thread ) except ImportError: print("Unable to create line_profiler : ImportError") except Exception as e: print("Unable to create line_profiler : " + str(e)) return profiler
pm = PubMaster() can_sock = SubSocket(msgs, 'can') return sm, pm, can_sock if __name__ == "__main__": segment, fingerprint = CARS['toyota'] segment = segment.replace('|', '/') rlog_url = f"{BASE_URL}{segment}/rlog.bz2" msgs = list(LogReader(rlog_url)) os.environ['FINGERPRINT'] = fingerprint # Statistical sm, pm, can_sock = get_inputs(msgs, 'controlsd') with pprofile.StatisticalProfile()(period=0.00001) as pr: try: controlsd_thread(sm, pm, can_sock) except ReplayDone: pass pr.dump_stats('cachegrind.out.controlsd_statistical') # Deterministic sm, pm, can_sock = get_inputs(msgs, 'controlsd') with cProfile.Profile() as pr: try: controlsd_thread(sm, pm, can_sock) except ReplayDone: pass pyprof2calltree.convert(pr.getstats(), 'cachegrind.out.controlsd_deterministic')