This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com
Yesterday, we did the live session on the internals of remote sampling profilers. We learned the internals that are required to build such tools. Building these tools is probably one of the most interesting systems programming projects that you can do to not only learn the internals of a programming language, but also learn the ELF file format.
Specifically, a remote sampling profiler for a language like Python is able to attach to a running Python process. It is then able to read the memory of that process to figure out what Python code is executing on the interpreter and extract its stack trace.
To be able to do this, three pieces are required:
* Ability to read the memory of an external process
* Knowledge of the Python runtime data structures that you need to traverse to extract the stack trace once you are able to read the memory of the Python process.
* Figuring out the memory addresses of these data structures in the memory of the running Python process. This is the most tricky and interesting part. It requires parsing the python executable binary file to read the memory address that the linker has specified where the runtime data structure should be loaded. And it also requires understanding of how the program loader loads the executable into memory.
We covered all of this yesterday. It was quite a bit of detail to go through but worth it!
You can also access the slides at the below link:
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_00Okay, so we are going to talk about how these remote sampling profilers work internally, and we'll also look at the internals or the implementation of a very bare-bones profiler for Python, and we'll go through what it takes to make something like this work. So let's start by looking at uh, how, uh, such a profiler actually works before we actually get into, into chain tunnels.
SPEAKER_00So what I'm going to do here is I'm going to run a Python program and I'll attach a sampling profiler to it. Uh, so I have to grab the process ID of this process. This one. Okay, so I'm running a sampling profiler for Python, which is called PySpy.
SPEAKER_00It's one of the third party open source profilers, which is available for Python. It is quite popular. So here we can see its output. So it is showing us the stack trace of the currently executing code in this Python program. So this is a very simple program. It's only executing... three functions. And on the leftmost column, it is showing the amount of CPU time spent inside each of these functions. So the topmost function, which is pass line bytes, is the most expensive function.
SPEAKER_00The interpreter is spending around 70% time inside that function. So as you can see, the way we started this profiler is by giving it the process ID of our already running process. And what the profiler would have done is it would have attached itself to the running process and then it started reading its memory and it started to sample stack traces from the running Python process.
SPEAKER_00And by collecting those stack traces, it is able to tell us all this information about what the interpreter is doing at any point of time and where it is spending all the time, all the CPU. So we are going to look at how such a profiler actually works. So specifically, we are going to look at a very simple barebones profiler, how it works at export. I just want to demo that as well.
SPEAKER_00So Python I think the current in-development version of Python has a test module, which is called test external inspection. It is essentially a test module for doing some unit testing within the CPython implementation. It is not supposed to be used by the users, but it is a very simple profiler. So you see,
SPEAKER_00what functions does it provide? So it provides a function called getStackTrace and getStackTrace takes a process ID of a running Python program. So it can say dot getStackTrace. I have to give it a process ID. So I'll start one process. I also print its process ID here. So this model simply prints the stack trace.
SPEAKER_00Essentially this, the program that I ran, it is very simple. So the main function starts, who calls bar and bar calls bash. And that's what we are seeing. So that's the stack trace that it has. produced. So we are by the end of this session, we'll be looking at the implementation of such a bare bones profiler and understand how it works. Okay.
SPEAKER_00So what do we need to build such a remote sampling profiler which can attach to a running process and collect its stack traces. So there are two main pieces that we need to figure out. The first thing is we need to be able to attach to a running process and we should be able to read its memory. So we need to get that out. And the other thing is once we are able to read the memory of the running process, how do we extract stack traces from it?
SPEAKER_00So how do we know what the Python interpreter is currently executing? What kind of, what Python code is the interpreter executing? So those are the two things we have to identify in order to build such a remote sampling profiler. And now attaching to a running process and reading its memory is simple uh it's a very straightforward system called that linux and other operating systems provide so
SPEAKER_00that is an easy piece the tricky piece is how do we extract uh how do we navigate the memory of this other process and uh extract the stack trace uh so that for for that we have to know Certain data structures, which are part of the CPython runtime, which contain information about what the interpreter is currently doing. That will help us extract this stack trace. So we have to learn what these data structures are and what fields they contain.
SPEAKER_00But even that is not enough. We also have to know where these data structures are actually loaded in the memory of this running Python process. So every time the Python process starts, these data structures would be... initialize and locate it in some part, at some address in the memory. And every time you run the process, these addresses would change. So how do we reliably identify where these addresses are so that we can read the
SPEAKER_00memory and then navigate the data switches? So this, This is a tricky part. We'll spend a lot of time just to unravel this. So the way I'm going to structure this session is I'm going to go bottom up. We'll first learn or how do we extract the stack trace from a running Python process. So let's assume that we are in the Python process and what data structures do we need to navigate to collect the stack trace.
SPEAKER_00So we'll start there. So in order to extract, a stack trace is essentially, we saw it here, essentially the chain of functions that are currently executing on the interpreter. So for example, here we can see the interpreter executing the bash function and bash was called a bar and bar was called a foo.
SPEAKER_00how do we know what function the interpreter is currently executing? So for that, there is a data structure called the stack frame. So every function that is running on the interpreter, it is wrapped in a data structure called the stack frame. And that stack frame object contains all this information. So it contains the compiled bytecode of the function. So that is exactly what the interpreter is actually reading and executing. But it also contains a lot of other information.
SPEAKER_00So it contains the name of the function. So we can read this name field and we can know what function exactly is running right now. And then it contains some other information that the interpreter needs, such as what are the locals of that function, what are the globals, built-ins, all that. And then the next crucial field is a pointer to the caller of this function. Caller's stack ring. So here, if I had... Sorry. Yeah. So if I had the stack frame of Baz, then Baz was for the bar.
SPEAKER_00So Baz's stack frame would contain a pointer to bar's stack frame. So I can traverse this pointer and go to bar's stack frame and read its name. And then I can read bars, previous stack frame pointer and arrive at foo. And then I can re-identify, okay, bar was called by foo. And foo was called by the main function of the module. So that is the end where the whole
SPEAKER_00chain of functions were originated. So if you can identify what the stack frame of the currently running function, then we can extract the stack trace. Is this clear? Stack frames are okay. Or any questions around the stack frames? So this is a Python construct, correct? This isn't a Linux construct. No, this is a C Python internal data structure.
SPEAKER_00So essentially, uh, whenever, so in our, uh, Python code, whenever we execute a function, so for example, uh, when I call food, uh, the runtime would create a stack frame object that would wrap the byte code of foo. And then it would give that stack frame object to the interpreter to go and execute it. And if foo calls another function, then that function would be wrapped in a stack frame and that would be given to the
SPEAKER_00interpreter and so on. So every function is wrapped in a stack frame. So a stack frame is Python's internal data structure. I should be able to show it. So ceval.c is where all the CPython virtual machine code is present.
SPEAKER_01So I develop a this one so file frame object okay
SPEAKER_00So this is the C-Python representation of the interpretive stacktrain. So the executable field contains the bytecode of that function and the previous field contains a pointer to the previous and the caller's stacktrain and then there are other fields.
SPEAKER_01Okay, is there a question?
SPEAKER_00Okay, so this is simple. So if we can find what the stack frame of the currently executing function on the interpreter, we can traverse the previous field and we can collect the stack trace. But how do we find the stack frame of the currently executing function? So we can do that through the thread state object. So every thread that is running in the interpreter,
SPEAKER_00its state is represented by a data subject called thread state. And that thread state structure contains a lot of fields to represent what is happening inside that thread. One of the key fields is it contains the pointer to the currently uh pointed to the stack frame of the currently executing function so the current frame field is points to this uh whatever function is running on the interpreter at this point of time so if we can find this uh the thread state object uh we can jump
SPEAKER_00to the stack frame and then we can get our stack trace uh now the question is how do we find the thread state object Uh, sorry about that. Uh, so to get the thread state object, uh, we need to, we need, we have another level of indirection. So the thread state is contained in the interpreter state object. So the Python runtime,
SPEAKER_00when it starts, it starts with one interpreter, which is called a main interpreter. Uh, Although it is possible that the interpreter could have multiple interpreters running inside it, those are parts of interpreters, but the main interpreter is what we are interested in here. Every interpreter's state is maintained in a data structure called interpreter state. And it also contains a lot of fields, but the fields that we are interested in here is the linked list of thread state objects.
SPEAKER_00So we know that within an interpreter, we can have many threads running. So, and every thread has thread state. So the interpreter state object maintains a linked list of all these thread states. At any point of time, one thread will be running in the interpreter and interpreter has this main field, which points to the thread state of the currently executing thread on the interpreter. But if you want to traverse what are all the threads running, you have the head of the linked list and the head gives you the next and the next
SPEAKER_00and next. So you can traverse all the threads and you can
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we learned about copy-on-write (CoW) and understood its mechanics by digging inside the mmap pathway in the...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we discussed demand paging. Now, let’s move towards an even more interesting topic: copy-on-write (CoW). Just...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the previous video in our virtual memory series, we discussed demand paging. But that was mostly theoretical, explaining how...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com Welcome back to the video series on virtual memory based on my article on the same topic. It is also available in ebook format on...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is a short aside in our virtual memory series. We have talked extensively about the size of a virtual address and the virtual...