0% found this document useful (0 votes)
11 views2 pages

Studies Python

The document describes the use of SystemTap tapsets, which are higher-level wrappers around static markers for tracing Python function calls. It provides examples of how to implement probes for function entry and return events in CPython, allowing for cleaner tracing of function-call hierarchies and monitoring of frequently-entered bytecode frames. Additionally, it includes a script that displays the top 20 most frequently-entered functions across the system every second.

Uploaded by

xacabo4439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Studies Python

The document describes the use of SystemTap tapsets, which are higher-level wrappers around static markers for tracing Python function calls. It provides examples of how to implement probes for function entry and return events in CPython, allowing for cleaner tracing of function-call hierarchies and monitoring of frequently-entered bytecode frames. Additionally, it includes a script that displays the top 20 most frequently-entered functions across the system every second.

Uploaded by

xacabo4439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

SystemTap Tapsets¶

The higher-level way to use the SystemTap integration is to use a “tapset”:


SystemTap’s equivalent of a library, which hides some of the lower-level details of
the static markers.

Here is a tapset file, based on a non-shared build of CPython:

/*
Provide a higher-level wrapping around the function__entry and
function__return markers:
\*/
probe python.function.entry = process("python").mark("function__entry")
{
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
frameptr = $arg4
}
probe python.function.return = process("python").mark("function__return")
{
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
frameptr = $arg4
}
If this file is installed in SystemTap’s tapset directory (e.g.
/usr/share/systemtap/tapset), then these additional probepoints become available:

python.function.entry(str filename, str funcname, int lineno, frameptr)


This probe point indicates that execution of a Python function has begun. It is
only triggered for pure-Python (bytecode) functions.

python.function.return(str filename, str funcname, int lineno, frameptr)


This probe point is the converse of python.function.return(), and indicates that
execution of a Python function has ended (either via return, or via an exception).
It is only triggered for pure-Python (bytecode) functions.

Examples
This SystemTap script uses the tapset above to more cleanly implement the example
given above of tracing the Python function-call hierarchy, without needing to
directly name the static markers:

probe python.function.entry
{
printf("%s => %s in %s:%d\n",
thread_indent(1), funcname, filename, lineno);
}

probe python.function.return
{
printf("%s <= %s in %s:%d\n",
thread_indent(-1), funcname, filename, lineno);
}
The following script uses the tapset above to provide a top-like view of all
running CPython code, showing the top 20 most frequently-entered bytecode frames,
each second, across the whole system:

global fn_calls;
probe python.function.entry
{
fn_calls[pid(), filename, funcname, lineno] += 1;
}

probe timer.ms(1000) {
printf("\033[2J\033[1;1H") /* clear screen \*/
printf("%6s %80s %6s %30s %6s\n",
"PID", "FILENAME", "LINE", "FUNCTION", "CALLS")
foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) {
printf("%6d %80s %6d %30s %6d\n",
pid, filename, lineno, funcname,
fn_calls[pid, filename, funcname, lineno]);
}
delete fn_calls;
}

You might also like