Skip to content

Instantly share code, notes, and snippets.

@cournape
Created June 24, 2013 21:20
Show Gist options
  • Save cournape/5853705 to your computer and use it in GitHub Desktop.
Save cournape/5853705 to your computer and use it in GitHub Desktop.
Poor man runtime callgraph. You need linux perf (perf-tools under debian/ubuntu) and valgrind for the corresponding functions
import contextlib
import os
import signal
import subprocess
import numpy as np
def enable_callgrind():
subprocess.check_call(["callgrind_control", "--instr=on", str(os.getpid())])
def disable_callgrind():
subprocess.check_call(["callgrind_control", "--instr=off", str(os.getpid())])
def reset_callgrind():
subprocess.check_call(["callgrind_control", "-z", str(os.getpid())])
@contextlib.contextmanager
def under_callgrind(reset=True):
if reset:
reset_callgrind()
enable_callgrind()
yield
disable_callgrind()
@contextlib.contextmanager
def under_perf():
pid = str(os.getpid())
p = subprocess.Popen(["perf", "record", "-g", "-a", "-F", "1000", "-p", pid])
yield
os.kill(p.pid, signal.SIGINT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment