Created
June 24, 2013 21:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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