Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
[MUSIC PLAYING] PROFESSOR: Hello, and welcome back to our virtual memory and operating system support module.
We've introduced the page memory system and seen how our pages work.
They can reside either in DRAM or on the disk, and we have a page table that tells us whether the pages have been allocated and whether they are in DRAM or on the disk.
We're going to take a little bit more detailed look into how does the system actually work. But before that, let's recap some things and remove any confusion that we might have caused.
First, let's make sure that we understand what are these blocks and pages, and understand that they are generally just different ways of measuring how many bytes are we working with.
Like in the metric system where we use kilos and millis to tell us how much grams are we dealing with in weight, here, we are using different metrics for some amount of bytes that we are moving around.
So in general, in caches, we deal with individual blocks. And blocks are 64 bytes in most modern systems. Some of them may be 128.
The thing, also, to make sure here is don't confuse this block with the size of a block that sometimes is referred to in storage, in SSDs and disks.
Cache blocks are 64 bytes, in our case. In virtual memory, we generally deal with pages. And pages, in our case, are 4 KiB.
And in most other modern systems, they're also 4 KiB. So don't confuse bytes, words, blocks, and pages.
They're just different ways of measuring the amount of data that we have in the memory. Let's take a look at a quick example.
Let's say that we have a really, really simple computer that just has a tiny amount of DRAM — the total 16 KiB.
The 16 KiB will be organized into four pages, where each page is 4 KiB.
In a simple virtual memory system it would have, it would have 128 byte blocks for caches and 4 byte words for loads and stores.
So our memory system here would have 16 KiB. Those 16 KiB would be organized into four 4 KiB pages.
Each page would have 32 blocks. Each block would have 32 words. Each word would have 4 bytes.
So, to recap all of that, we can think of the entire memory as four pages, 128 blocks, 4,096 words, or 16,384 bytes.
We can think of a page as having 32 blocks or 1,024 words. And finally, each block can have 32 words or 128 bytes.
The other analogy that we would like to recall now, since we have seen how does the memory system work, is the library analogy. We generally, in our hands, we keep book titles.
So all what we remember is a book title. We don't remember the Library of Congress call number. So when we refer to a book, we refer to it by its title.
But going to the library, straight for the bookshelves to try to find that book, is usually not going to yield the reasonable result.
We may not find the book just going straight for the stacks.
Books are organized in different ways, and generally, we will have to go for the card catalog, like the page table what is in our virtual memory equivalents, that maps the titles to the Library of Congress call numbers.
On that card entry that is in our catalog, we would find out a valid bit, whether the book exists in this library system, and where is it?
Is it in the main memory, or is it on the disk? Meaning some external storage or some other library, some other facility.
Finally, there may be a reservation bit on that entry that tells us for how long we can check out that book — and can we check it out at all?
Another important thing here that should be brought up is, when a library brings a new book to its collection, it doesn't just go and shove it on the library stacks.
Every time a new book is brought, a new page is brought in our virtual memory system, we need to create a catalog page for it.
We need to create a catalog card for it and place it in the collection. All right. So back to our page memory system.
In our page memory system, what we have seen is that each process works with its own page table.
A page table has multiple entries that are pointing to the pages that may reside in DRAM or on the disk.
Now, in addition to these references to the DRAM or the disk, there are some of the status bits that are associated with each page table entry.
We have seen that we need to have a valid — whether the page is allocated, whether it exists — but also, we are seeing that there is a need for another one.
We need to know whether that page is in DRAM or is on the disk. This is just conceptual.
What we'll find out is that virtual memory systems are specific to the ISA and have a bit longer number, a large number of these control bits that are telling us what's the status of each page table entry.
We'll see some of those a bit later. But conceptually, we need to know whether the page is valid and whether it resides in DRAM or it's on the disk.
Now, there is an important mechanism that gets invoked every time we would like to perform this translation.
We see a virtual address, and then we would like to find out — we would like to get our data, and we need to find out whether we want to get it from the DRAM or the disk.
Or we actually need to create a new page table entry. So on each memory reference, loads and stores, we are going to check the page table entry.
If it is valid, we are going to proceed with checking where is that page. If it is in DRAM, that's great. We're just going to go ahead and rewrite the data.
If it is on a disk, that may be a lengthy procedure of getting that data into the DRAM. So we have to going to allocate a new page in DRAM.
If there is room, that's great.
If there is no room in — if all of the DRAM has been allocated, there is no free space in DRAM, we would have to evict a page from DRAM.
Store evicted page to disk. So move that page that is being evicted — and that's usually the one that was least recently used — would be moving to the disk.
We would read a page from the disk into the memory, and then proceed with reading and writing the data.
In other case, if the page table entry is not valid — that means that we haven't referenced that data before, so we are going to allocate a new page in DRAM.
If we're out of memory, we are going to do like we did before — we are going to evict a page and move it to disk, and then read and write data to the newly-created page in DRAM.
In these two cases, if the data is not on disk or not valid, we need an OS intervention. OS needs to handle all of that.
And it does it through a mechanism of a page fault. What is a page fault?
Well, page fault is a special exception that is invoked when we are handling one of these two conditions.
So it is handled by the same mechanism that we have seen before, that we have outlined before, of mechanism of handling the exceptions.
It does all the page table updates and initiates transfer from the DRAM to disk and disk to DRAM and updates the status bits.
This is something that is done by the OS in the supervisor mode.
Now, if this procedure involves moving the data to and from the disk — essentially a swap of a page with a disk — it will often, generally, perform the context switch, because that's a lengthy procedure.
That means that this process needs something from Pluto or Mars and it's going to take a while, so processes are going to be idle for tens of milliseconds.
It's a perfect time to initiate a context switch and have some other process use the processor while the data is coming in. Then, following the page fault, the instruction is re-executed.
Remember, when we raise the exception, we are canceling that instruction that is being in-flight load or store.
It has no hope of finishing, then, if the data is not valid, so it will wait for the data to be valid in the memory and complete the instruction.
A few other things. Even though our virtual memory system appears to be huge, it is not infinite.
So if you try to allocate something that is just really, really big — like try to allocate an array that is 1,024 times 1,024 times 1,024 times the word length — we may run out of memory.
We are going to get this kind of an exception — failed to allocate 131 tebibytes. We are out memory.
So if you don't have that much memory and that much disks together, we can't do it. We can't complete this.
Another point here that we would like — that is fairly straightforward, but we just want to make sure that it is clear.
Unlike caches, where you had different policies — how do we want to write data back to the memory from the cache — we discussed the write-through or write-back policies.
When we are dealing from data transfers, updates to the disk, we really have only one option — the cost of writing data back to disk is so huge that the only option that we can do is write-back.
Write-through is really not an option because it's going to be really, really slow. So the only policy here is going to be write-back of the least recently used page.
So write policies in virtual memory systems are significantly simpler than what we encounter in caches.
I think it's a good time now to pause. We are going to continue with some other interesting aspects of the virtual memory after the break.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.