Tracing System Calls in Python

Final time we counted CPU
instructions,
let’s have a look at syscalls now!
I’ll present you just a little tiny software I added to
Cirron that allows you to see precisely what
syscalls a chunk of Python code is looking and the way to analyze the hint extra successfully.
Let’s begin with print("Whats up")
as earlier than:
from cirron import Tracer
t = Tracer()
t.begin()
print("Whats up")
hint = t.finish()
print(hint)
# write(1, "Hellon", 6) = 6 <0.000150s>
You may see print
makes use of solely a single
write to put in writing the string
"Hellon"
to stdout (that’s what the 1
stands for) and asks it to put in writing at
most 6
bytes. Write then returns 6
, that means it managed to put in writing all of the
bytes we requested it to. You may also see it took 0.00015s or 150μs (that’s simply the
write
name, not the entire print
assertion).
Fairly cool!
How does Tracer
work? I initially needed to make use of the
ptrace syscall to
implement it, however that turned out to be just a little extra difficult that what I
needed, so in the long run I simply used the strace
software, which additionally makes use of ptrace
however handles all of the complexity. Tracer
merely starts tracing
itself with
it, redirecting output to a file, which is then parsed when it’s requested to cease.
Let’s hint import seaborn
now:
from cirron import Tracer
t = Tracer()
t.begin()
import seaborn
hint = t.finish()
print(len(hint))
# 20462
Seems importing Seaborn takes ~20k syscalls! That’s clearly too many to
simply print out, so what’s a greater option to analyze what it’s doing?
Visualizing traces with Perfetto
Perfetto Trace Viewer let’s you visualize every kind
of traces. It might’t ingest strace
output straight, however I’ve included a
operate that converts Tracer output to Trace Event
Format,
one thing Perfetto can load:
from cirron import to_tef
(...)
open("/tmp/hint", "w").write(to_tef(hint))
This will get you a file you possibly can open with Perfetto. I’m not going to explain all it might do; I uploaded a hint of import seaborn
here, go play with it!
I used to be stunned to search out it makes use of 4 threads, which largely spend time wanting up recordsdata and studying them, however one of many threads appears to be very interested by your CPU particulars!