Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent pycallgraph from entering standard library functions?

I'm using pycallgraph from the command line to profile and draw the call graph of a relatively simple program. However, the resulting image includes the internals of standard libraries (threading, json, socket) even though I don't use the -s command line option. Using the -e option to exclude those modules has no effect and using -i results in an empty call graph. I've also tried cProfile, but it only graphs the main thread.

How can I make pycallgraph to only show calls in my code? The current messy result is useless.

Edit: I'm using 0.5.1, available via easy_install. Running pycallgraph ./cursesclient.py outputs this: a messy call graph. As you can see, pycallgraph show the internals of modules json, re, encodings, socket and threading. Re and encodings are never even called directly in my code, but through json and socket, respectively.

like image 338
andyn Avatar asked Oct 31 '12 10:10

andyn


2 Answers

Pycallgraph provides filtering capabilities to filter out any module, class or function you would like to exclude from call graph. Following function should be defined before you start the trace and passed to pycallgraph

Example

def filtercalls(call_stack, modul, clas, func, full):
    mod_ignore = ['shutil','scipy.optimize','re','os','sys','json']
    func_ignore = ['CustomFunctionName','pdbcall']
    clas_ignore = ['pdb']
    return modul not in mod_ignore and func not in func_ignore and clas not in clas_ignore

The pycallgraph trace start is

pycallgraph.start_trace(filter_func=filtercalls)

This way, any module, class or function you provide in filtercalls will be removed. Please note that many time in standard libraries providing just the module name is not enough. Thus, including numpy in mod_ignore will still result in numpy.core being included

like image 169
Apurva Samudra Avatar answered Oct 23 '22 21:10

Apurva Samudra


pycallgraph has an undocumented stop_trace() method that you can use to exclude sections of code. Something like

import pycallgraph
import mycode
import stuff_i_dont_want_to_see

pycallgraph.start_trace()
#Initializations

pycallgraph.stop_trace()
stuff_i_dont_want_to_see()
pycallgraph.start_trace()

mycode.things()
pycallgraph.make_dot_graph('cleaner_graph.png')

Is that what you're after?

source

like image 22
Nat Knight Avatar answered Oct 23 '22 21:10

Nat Knight