Reputation: 17
I have files, describing structure of 32 bit virtual address space of process. For example:
There is a two-level virtual memory on x86 processors. Size of one page is 4096 bytes. One page directory contains 1024 records, having 4 bytes sized each.
How can I count overall size of virtual memory tables?
Upvotes: 0
Views: 268
Reputation: 129374
So, there is ONE page-table entry (PTE) for every 4096 bytes. And one page directory entry for each 1024 PTEs.
So the number of entries of each would be:
ptes = (x + 4095) / 4096;
pdes = (ptes + 1023) / 1024
The +4095 and + 1023 is to "make sure we have at least one for any non-zero value", and the above assumes integer values for x
and ptes
.
I'll leave it to you to add the two numbers together and multiply up to give you bytes.
Of course, if you want to be efficient, use >> 12
instead of / 4096
and >> 10
instead of / 1024
- this will guarantee that the compiler doesn't actually perform a divide operation.
When we have multiple memory ranges, you need to one PDE for each "big lump" (1024 x 4096 bytes, so even 4MB ranges will have a PDE), then one PTE for even 4K region.
So from your example: 08048000-08053000 r-xp 00000000 08:03 18877 /usr/bin/cat No memory. Reserved.
08053000-08054000 r--p 0000a000 08:03 18877 /usr/bin/cat
1 PDE, 10 PTE (0xa000 = 10 * 4096 bytes)
08054000-08055000 rw-p 0000b000 08:03 18877 /usr/bin/cat
0 PDE, 1 PTE (0xb000 - 0xa000 = 0x1000 = 4096 bytes)
091e3000-09204000 rw-p 00000000 00:00 0 [heap]
No memory, reserved. 4f2d0000-4f2ef000 r-xp 00000000 08:03 1857 /usr/lib/ld-2.15.so No memory, reserved
4f2ef000-4f2f0000 r--p 0001e000 08:03 1857 /usr/lib/ld-2.15.so
1 PDE, 1 PTE (0xf0000 - 0xef000 = 4096 bytes)
4f2f0000-4f2f1000 rw-p 0001f000 08:03 1857 /usr/lib/ld-2.15.so
0 PDE, 1 PTE
4f2f7000-4f4a2000 r-xp 00000000 08:03 1858 /usr/lib/libc-2.15.so
No memory, reserved range
4f4a2000-4f4a3000 ---p 001ab000 08:03 1858 /usr/lib/libc-2.15.so
0 PDE, 1 PTE
4f4a3000-4f4a5000 r--p 001ab000 08:03 1858 /usr/lib/libc-2.15.so
0 PDE, 2 PTE (0xa5000 - 0xa3000 = 2000)
4f4a5000-4f4a6000 rw-p 001ad000 08:03 1858 /usr/lib/libc-2.15.so
0 PDE, 1 PTE
4f4a6000-4f4a9000 rw-p 00000000 00:00 0
b75c0000-b77c0000 r--p 00000000 08:03 57661 /usr/lib/locale/locale-archive
b77c0000-b77c1000 rw-p 00000000 00:00 0
b77d9000-b77da000 rw-p 00000000 00:00 0
b77da000-b77db000 r-xp 00000000 00:00 0 [vdso]
bf819000-bf83a000 rw-p 00000000 00:00 0 [stack]
So, I would argue that in total, this particular executable has 2 PDEs entries and 18 PTEs. I may have counted something wrong, but in principle that's how you'd do it for this particular example (which appears to be part-way through loading, as it's not using any stack or heap, which is unlikely for a fully running program - this may be that the statistic was gathered before the program actually got running fully, or some such)
Upvotes: 1