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 demand paging works inside the Linux kernel and the mechanisms through which it handles page faults. In this follow-up video, we make it more concrete by learning to inspect the virtual memory of a running process to see demand paging in action.
If you watch this video, you will learn a few cool things:
* What is demand paging (the compressed version; watch the previous video for a more in-depth explanation)
* What is the /proc file system in the Linux kernel
* How to read /proc/ /maps to inspect the VMAs of a live process
* How to read /proc/ /smaps to inspect which VMA is consuming how much physical memory
* How to use mmap
* How to use getrusage to track the number of page faults of a process
If you are new to this series, it is based on my ebook called “Virtual Memory from First Principles”. It is available to read for free online and also available to purchase from Gumroad (PDF/Epub) and Amazon (Kindle edition).
And, if you want to watch the previous videos in this series, the following is what has been published so far:
* What is virtual memory and why do we need it
* Size of the virtual address space of a process
* Address space of a process
* Structure of page tables and how page table walks work
* What is inside a page table entry
* Size of a physical address
* Demand paging
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_01Hey everyone, this is Abhinav. Welcome back to our series on virtual memory. In the previous video, we learned about demand paging and covered it in quite a bit of depth. And towards the end of the video, I promise that in the next video, we will do a more hands-on Demonstration of how demand paging really works. So we will see it in action. And we will do that by writing code and then observing the virtual address space over process. We will look at the VMAs of that process and then we will see how calling
SPEAKER_01a map Results in the new VMA being created and then how writing or reading those pages results in the page actually getting mapped to physical memory and the RSS or the resident set size of the process going up. So we will do all of that in this video. So as if you watch the entire video, you learn a bunch of things. So whether you know how to use MMAP itself. How to inspect the memory of the process of a running process.
SPEAKER_01So we will learn the debug and observability tools that Linux provides. So looking at the inspecting the files inside the slash proc file system and navigating the virtual address space for the process. All those things we will learn. So yeah, so let's get started with that. To start with, I would want to do a quick recap of the last video just so that we have a common context.
SPEAKER_01Although I still recommend that you go and watch the previous video because that covers everything in quite a bit of depth. So demand pitching is this idea that whenever you ask the kernel for some You know, new memory. So you want to allocate some memory. The kernel just allocates virtual memory pages for you and returns immediately. But it does not do physical memory allocation. So those virtual memory pages that the kernel reserves for the process, those are yet not mapped to any physical frames at that point of time.
SPEAKER_01Which means that the first time you access one of those pages at that point of time, The kernel gets a page fault, and while handling that page fault, it actually allocates physical memory against that page, which caused the page fault. So this is basically lazy allocation of memory. So the kernel does not want to waste time allocating physical memory right at the point you are calling mmap. Rather, it does physical memory allocation when the first time you actually access that memory. So that's the idea of mmap.
SPEAKER_01And how does that really work? So inside the kernel at the process level, the kernel maintains a data structure called the VMA. VMA stands for virtual memory area and it represents a contiguous range of virtual memory address. So for example, you know, and that basically it can be one page, two pages, three pages, ten pages and so whatever. A VMA can present any number of pages contiguous. And all those pages inside a VMA, they share a bunch of common properties with each other.
SPEAKER_01So they all have the same properties. And a VMA simply holds those properties at the VMA level. So the kernel does not have to track those properties at the page level. A single VMA can tell the kernel that, okay, all these pages within this VMA are, for example, readable and writable, but not executable. So that's the VMA in a nutshell. So I'll just quickly list down some of the key fields inside the VMA that are
SPEAKER_00important for us to be aware about. So VMA has, first of all, So it has a starting and ending address. So what is the starting address and what is the ending address?
SPEAKER_01Then it has information about the protection bits. So for all the pages, what are the protection bits within this VMA? This means whether these pages are readable, writable, executable.
SPEAKER_00It also has information about flags. So whether those pages inside that VMA are private,
SPEAKER_01P for private, or shared, S for shared. If it is a file-backed VMA, so if you're mapping a file,
SPEAKER_00then the VMA would also contain a file path. And if it is file-backed, it may also have an offset within the file.
SPEAKER_01So, starting from which offset, you are trying to map the file. For example, let's say 4096 offset. If it is an anonymous VMA, all the pages are anonymous, then the file information will be empty or null, and the offset will be zero. And then, apart from that, a VMA can have more information inside it. But these are the key fields that we care about in this video. So now what we are going to do is, first of all, what we are going to learn,
SPEAKER_01how do we look at the, you know, debug the VMAs of a process? What are the VMAs within a process? And then, you know, which VMAs are consuming, how much physical memory and so on? So how do we do that? So for that, I'll use Python REPL. So I'll just start a Python process of Python REPL.
SPEAKER_00And I'll print its process ID. And what I'm going to do is, so I'm on a Linux machine here.
SPEAKER_01Everything I'm talking about here is very specific to Linux. So this is the process ID. And on Linux, you have the slash proc file system that the kernel exposes as a way to observe the internals on the kernel and the hardware. So you can observe the virtual memory, you can look at CP-related information, a lot of other things that you can inspect in order to debug and observe the system. So one of the files in the proc file system is slash proc slash pid slash maps.
SPEAKER_01So PID you have to replace. So you have to know your process is PID and you have to use that. And then with slash maps. So this is a special file that dumps information about all the VMAs within that process. So what are all the VMAs that that process contains and what are their attributes? So this is all the VMAs of the Python process at this point of time. So one row per VMA. So this is one VMA.
SPEAKER_00This is second VMA. Now, how do we read this output? So, I'll go column by column. So, the first column here is
SPEAKER_01basically the starting and ending virtual address range for this VMA. So, this VMA starts at this particular address and ends at this address. These are virtual addresses in hexadecimal. Then the next second column is the protection bits and the flags of that VMA. So as I said, you know, the pages within the VMA can be readable, writable, executable and so on. So these first three values here, they represent R, W and X.
SPEAKER_01So here only R is set, which means all these pages will be readable, but not writable and not executable. A hyphen means that those particular bits are not set. The final, the fourth bit here is whether these pages are private or shared. So P for private, if this was a shared page, then you will see S. For example, here you can see this is a shared page. So this is loading some shared cache here. So these pages are shared. After this, the information is mostly about the file.
SPEAKER_01So if you're loading a file back page, then it has information about that. So for example, in this case, this is the file being mapped. So this is the Python binary itself, which is being mapped. This is the inode of the file on the file system. This is the device ID on which device this file is stored, so device ID. And this is the offset within the file. So which offset is being mapped. So this starts at the beginning of the file. Whereas you can see the next VMA is mapping the same file.
SPEAKER_01It has different protection bits. So these pages are readable and executable. And the offset is 1000. So it starts 1000 bytes later. Similarly, if we go scroll down, We can find some anonymous VMAs as well. For example, here, this is an anonymous VMA. So, the file information is empty. And the device is 0, inode is 0, offset is 0. So, this is a clearly anonymous memory.
SPEAKER_01So, this is how we inspect the VMAs of our process. So, just look, just if you do cat on the maps file, you can just... You can get the VMAs. If you do cat on the smaps file, so this is another file, then it provides additional information apart from the VMAs. So here it gives us per VMA information. So for a specific VMA, so for example, for this VMA, it is telling us a bunch of things. So what is the size of the VMA?
SPEAKER_01So how many virtual pages are allocated inside this? So this is 4KV, which means only one page. Then the RSS. So How many pages are actually mapped to physical memories? In this case, nothing is mapped. So this is zero. RSS stands over the resident set size, which means how many pages are actually resident in physical memory. If you go scroll up, we can probably find VMAs, which have non-zero RSS.
SPEAKER_01So here, you have this VMA, which has Consists of two virtual pages and RSS is also 8KB, which means that both the pages are actually mapped to physical memory. So this is how you, you know, if you want to look at if a process is using a lot of memory, then you can look at the smaps file and can see which VMA is actually mapping a lot of pages and so on. And whether those are filed back or...
SPEAKER_01I mean, basically you can start debugging, you can start analyzing what is really happening behind the scenes. So now we understand the difference between maps and smaps file and how to read those. Now the next thing I want to do is I want to write a small C program where we will call mmap to allocate some memory and then we will inspect these files
SPEAKER_00to see demand paging in action. So, to bear with me, I'm not using AI here. So, writing by hand. Just a bunch of usual includes.
SPEAKER_00So, one thing that I want to do in this program is that I want it to
SPEAKER_01pause for a moment so that For example, after calling mmap, I want the program to stop so that I can go and look at the maps file and see a new VMA has been created. I don't want the program to exit. I don't want it to continue executing. I want it to pause at that point of time. So I need a small utility function that will do that.
SPEAKER_00So I'll write that function here. I'll call it pause. So, I'll just print something, some message, press any key to continue, like that.
SPEAKER_00Then, what I'm going to do is, I'll have an infinite loop, where I'll
SPEAKER_01read a character from the
SPEAKER_00standard input. And until that is not a new line, I will run an infinite loop. So this will wait until I type a character and hit enter. Now we can write our main.
SPEAKER_01So the first thing I want to do is, first of all, I want to know the PID of the process. Because in order to inspect the slash proc file system, we have to know the PID. So we have to call the getPID system call.
SPEAKER_00So I need to include a
SPEAKER_01header here.
SPEAKER_00I call getPID and print it out. And at this point of time, we can do a pause. So we can go and look at the existing VMA.
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 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...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is the 5th video in our virtual memory series. In the previous video, we learned about the page table and how the hardware...