 Now it's going. Whoops. Okay Now we should be good Okay, sorry so Access page one what happens? so Yeah Yeah, so if we look back so we check the reference hand currently at zero So we place the page there and advance the hand So currently we're pointing at something. It's reference bit zero. In fact, it doesn't represent anything So we place page one. So that's the number at the top It's reference bit would be one because we just used it and we advanced the iterator to the next spot So everything goes clockwise. Hey, it's a clock out. All right. What happens if we access page two? Hopefully exactly the same thing, right? So not pointing anything valid. It's reference bit is zero. So I'll place it there Place page two there reference bit is one advance the hand Can we skip two steps? So should not be terribly surprising. So access page three. I insert it right there Reference bit is one advance the hand Same thing for page four Insert it right where it's pointing because it's reference bit zero advance the hand and its reference bit is now one Now what happens if I access page five? Eventually, wow, we're already fast at this. So Excruciating detail. What do I have to do if I follow it one step at a time? Yeah Yeah, so the solar version of exactly what you said so currently pointing at something the reference bit is one Can't get rid of it I essentially give it a second chance change it from a one to a zero and well Because it's set up like this. It's gonna be boring. It's gonna go around so one goes to zero advance it One goes to zero advance it one goes to zero advance it finally We wrapped all the way back around to the beginning now. We can replace page one right its reference bit is zero So it's out of here So we replace page one with paid or replace page one with page five So it's reference bit to one and move the hand. So now our clock looks like this after we insert five Now what happens if I access page two? Yeah Yes, just set the reference bit to one So before when we had five four or whatever we weren't keeping track of things We just said it was a cash hit carried on in this case. We're setting a reference bit So in this case since we just use page two kind of makes sense We accessed it so we'd set its reference bit to one the idea being while we just used it So we wouldn't want to get rid of it immediately because we just used it So just take page two so that's reference bit from zero to one Good All right access page three what happens Same thing says reference bit from zero to one. All right page one what happens Yeah Yeah, so the idea behind this is we want accesses to be as fast as possible So you can think about it that way that well all I'm doing is updating a bit. I'm not touching the iterator I'm not traversing anything. I'm just setting a bit and that's all I'm doing So even doesn't matter what the iterator is currently pointing to that's only for replacement for keeping track of accesses Doesn't matter. Let's flip the bit Yep No, just gonna follow exactly whatever the current hands point for or just follow the algorithm Whatever it is it should should replace it the first time it hit zero replaces that one, right? Oh, you wouldn't have two threes. No only one page in memory at a time So you wouldn't have two entries where one's referenced and one's not it's just is three in memory. Yes or no Yeah Yeah, yeah iterator only moves when we're actually doing the replacement because the idea behind that is on accesses We want to be fast, right? Can't do anything but faster than just setting a bit without doing nothing so Only change the iterer once we're actually doing the replacement algorithm once it is not in memory So in this case What's good? So now I access page one and well previously I set three from a zero to one So now what do what happens when I access page one? So it's not currently in memory. I have to kick something out Yeah, yeah, I would eventually kick four but first step Well, if I follow the algorithm then I'm currently pointing out a page page two its reference bit is one So I can't kick that out. So I change its reference bit from a one to a zero so It essentially you give it a last chance. So It's I move the iterator to page three Well, it's reference bit is one. I change it from zero or one to zero Move the hand and now I point at page four its reference bit is zero So it is now gone. So I replace page four with page one Set its reference bit to one and advance the hand. So now after I have inserted page one. It looks like this So any questions about that? All right, so now I access page two what happens Yeah, so it's a reference bit to one don't need to touch the iterator We want to be as fast as possible So to its reference bit goes from zero to one and for the last access page three It goes from a zero to a one and it looks like that at the end of the day now If I draw that out in terms of page faults and what we had before this is what it would look like so it would Begin looking exactly like FIFO all of our page replacements, but if I just write them out wherever we Access to well, it didn't look like we did anything. We actually updated a reference bit But if you write it out like this Just all the pages to stay in memory like we had before Same for three although we update a reference bit and then when we access page one We replace page four and then we went ahead and access page two and then page three How many page faults was that? six If I want to compare that to the best we could possibly do what would I compare it to? for what? Six all right, so the best thing we could do what algorithm is that called? The optimal algorithm right so same page accesses, but optimal So we can do that. Am I okay to skip the four same four steps of optimal just bring it into memory All right, so we are going to have some extra time at the end of the lecture so these are a page we just load everything in the memory and This is the state after the first four accesses now. We have to bring in page five So what do I kick out to bring in page five if I'm doing the optimal thing? Four right look in the future. Well, I can't kick out two. I can't kick out three I can't kick out one that leaves me with four, so I would replace four with five Oh, and then guess what after that point? Everything is a hit the dot. How many page faults is that? Five. Yeah, so five So got kind of close to optimal, but soft or clock got six Optimal got five. So hey, we did pretty decent. All right if we really really want to evaluate this This is a sequence we used in the last lecture when we did like FIFO at least recently used all them So what do we want to redo this with the clock? Sure may as well. Otherwise, I mean, I guess you can go home right now if you don't want to do that because that's pretty much all the lecture So if I do that Before when we had all those accesses we could fit four things in the memory So if we're doing the clock we can draw ourselves some boxes one for each frame we can hold and Let's just draw some nice arrows between them Just to show that it's circular So I can just leave it empty if I want so what happens if oh, let's point out something Jail make it blue So the iterator I will do in blue So faxes page one what happens? Yeah, we insert it in reference bit is one advanced the hand right dude So one goes in its reference bit is one. So I'll write the reference bit in blue and then we advanced the hand so It would look like this All right. Can't skip a few steps All right, we can skip a few steps. This lecture is going to be real quick. All right. So eventually whoops We will insert page two then three then four and The hand will wrap all the way around back to the beginning. We'll have reference bits like this two three four All right good Any questions about how we got to that point? All right. This is boring. That's good because guess what should be free marks on the final on the Horrible day we got chosen for it So what happens now if I access page one? Nothing, right? Yeah, it's reference bit is already one However, you want to think about it up to you so changes one to one or this does nothing Doesn't matter at the end of the day means the same thing All right accesses page two what happens? Whole lot of nothing whole lot of nothing again All right now things get interesting page five happens Or at least as interesting as it's going to get So what happens now? Yeah, if I want to do a shortcut if everything is Currently a one to do a shortcut. I'll just change all the ones to zero and just Go on from there. So So change all the ones you said to a zero Then go on from there. So now I'm trying to insert page five So currently the hand's pointing at page one. So I should probably kick page one out kick page one out for page five So I kick page one All right, am I done Yeah, I should advance the hand after I put it in because otherwise I try and kick out something. I immediately just put in So that was our big trick of the day. All right, so Now what happens I access page two Yeah, reference bit just goes from zero to one Nothing else happens All right access page three. Hopefully same thing. Hopefully it's like Get me out of here level All right So if you can do this as fast you can hopefully do this in like five minutes on the final You will be given a lot more than five minutes. All right So that's what it looks like. All right access page five. What happens? Nothing or changes one to one. Whatever you want to think of it as All right access page two what happens Nothing nothing happening is actually good. It means we're not swapping things in and out of memory No, so when we access five for accesses all we do is update the reference bit nothing else Doesn't matter what it's pointing to Yeah, so the reference bit There'll be some hardware support probably with the two So it knows exactly where it is and it can update it really quick doesn't have to use the iterator The iterator is only for replacement. All right. Uh, all right. Yeah, now we access page one What we do so do I throw up page two? No, all right if I follow it well It's currently pointing at the iterator This is where I use an iterator because I have to do the replacement currently pointing at an element the reference bit is one So I give it another shot at life. I change the zero the one to a zero Advance it. I do the same thing again. It's currently pointing at a one So I change it to a zero and then advance it Oh now it's pointing at an element where the reference bit is zero, so it is now gone So see you later for we just replaced you with What one replace you with one reference bit is one and then we advanced the hand So now would we replace four? All right, turns out that was a poor decision to make so now we're accessing page four Currently not in memory. So if I follow the steps, I shouldn't get rid of page five. I should change the one to a zero Then advance the hand. Oh I'm pointing at page two. Well, it's reference bit is zero. So now it is gone. I Put in page four reference bit is one Advance the hand and I replaced what I've replaced page two All right, how many page faults happened? Seven we remember how many we had last lecture for like I don't know FIFO FIFO was pretty bad I think it was like 10 LRU last time was eight or six. I forget optimal was six. So hey did pretty good and Guess what we can actually implement it So any questions about that? Yeah, yeah, it just as soon as you hit the end you go back to the beginning Sorry. Yeah, it might might loop back to the beginning and the pens of everything's full. So initially Every single time if it's empty then we load it in reference bit is one then we have to circle around the whole thing So sometimes we might have to do that but that's the worst case generally after the first time it kind of settles down a little bit and We don't really have to do that. Yeah. Yeah, it might be slow But it just goes until the first time it hits zero and then it goes and if all the bits are like right next to each other And it's actually not that bad So there's things you can do to implement to go faster But just a bunch of bits will be quite fast and you'll see You'll be using a bunch of bits in lab six to Yay lab six All right, any other questions for this? Yep So high-level exclamation is one So so far it's about the only thing we can actually implement So right now we can only implement FIFO and We can implement this we can't really implement LRU because it's just too slow So I think you could argue that this is better than FIFO by a long margin but for this point is accesses I do something really really quick and It kind of approximates least recently used because of just the reference bits They don't keep track of like when I reference something so it's not exactly least recently used but because I go around in the same order and I essentially give things a shot and Whatever their access to I set it back to one it kind of approximates that it's kind of the idea So it looks like least recently used ish with using only one bit of information to keep track of When something was used Yep So yeah, we get slightly different orders depending on this if we tried to do least recently used So I forget what we had last time, but I think it was Six page faults, maybe so least recently used I think was better So last lecture we did the exact same sequence I think But in general least recently used is probably going to be slightly better, but we can't implement it All right, so any other things for the last point? All right, so For the last point for performance. I think I touched on last lecture. I'll touch on it again so for performance This whole last two lectures might not also be important to you because well Goes really slow if you start swapping the disk instead of using memory you're using disk and you can't really control it Suddenly if you kind of run out of memory your program just starts running slow You might not want that especially if you're Google you would rather just Run out of memory so if I don't have swap and I run out of memory what happens in Your program so if I ran out of in your program you ran out of memory you tried to malloc something What would happen? so exit seg faults, so All right, so none of us actually check for errors do we? malloc returns null and you should be able to detect that and like Quit gracefully, but I guess no one actually does that so Remember I've told you in this course to check for errors It will probably take you about depending on what job you go to probably like Two to four years to actually start listening to me, so just remember I told you so whenever it finally clicks so Yeah, if I don't check for errors and I would get a null and then I would probably say fault and would probably be hard to debug In fact also while the kernel Depending on the out-of-memory thing the kernel can also check So the kernel will know if it has run out of memory because well, it's a kernel It's managing memory. So what it will do is run something called the OOM killer any ideas Oh, I put on the slide so OOM stands for out of memory So the out-of-memory killer is not very complicated Can just look through all the process control blocks see what's using the most memory and essentially SIG kill it and then it's gone and just freed memory So that's an easy way to resolve an out-of-memory issue if you're the operating system in your process It's a bit harder because you can't just do whatever you want, but the kernel can just kill process freeze the memory Process it kills probably chrome. Let's be honest. So now you got lots of memory So other things it could do and yeah another reason to disable Disable swap is yeah if you're Google and your program just crashes or seg fault So you actually detect it instead of running slower. You just go out buy more memory Whatever think I said that last last lecture so There's also another thing we can do to kind of speed up things that is a different approach So the page size of four kilobytes that was decided some at some point in the 70s So now especially for newer processors Arm well, I think x86 will do this it will allow you to have multiple page sizes So remember when we had like a three level page table We had like nine bits for l2 nine bits for l1 nine bits for l0 and then the 12 bits offsets so what some Memory systems will allow you to do is essentially get rid of the lowest level and then make your page size bigger So instead of having a 2 to the 12 page size it will let you have a 12 plus 9 So 2 to the 21 or 2 megabyte page size So in your l1 page table instead pointing to an l0 entry it would point to a 2 megabyte page instead and that way your pages are a lot bigger and Essentially you're trading fragmentation for more TLB coverage. So imagine your TLB can only hold four entries or 10 entries, let's say for a nice number if my page sizes are only four kilobytes then Essentially that TLB can only have essentially 40 kilobytes of memory space cached If I have two megabyte pages and I can still only fit 10 entries guess what now I have 20 megabytes that I cover in those 10 entries So it's more likely that my TLB entries will still be in the cache Things or well TLB is a cache So it's more likely my entries will still be in the TLB things will go nice and fast So any questions about that? Yep Yeah, oh so the out-of-memory killer only runs like the kernel that's part of the kernels job It will only run when it has run out of physical memory and swap space So there's literally it can't get a new page anywhere. So if it can't get a new page anywhere It's got to make space somewhere probably kills Chrome or Whatever you want to pick off one algorithm for the out-of-memory killer Sometimes it's not even that smart just picks a random process and kills it and if it picked wrong Just kills something else until there's enough memory So you don't you don't have to be it's like what's the saying? Shoot them ask questions later that kind of thing so curl can just shoot the processes and see if it fills it up later All right, the other option is In some extreme cases if you just go and remove two levels of page tables gets your page size up to one gigabyte But then well, that's where we can really see that the fragmentation or internal fragmentation becomes a problem So if your pages are a gigabyte in size and your program is only using you know Ten kilobytes well guess what most of that page is actually wasted and you can't do anything about it so Probably you don't want to go up to this so these are called giga pages and the two megabytes if you've seen in your operating system at all They are called huge pages. I even think windows on the activity monitor will tell you how many huge pages It's using huge pages just mean the two megabyte pages instead of four kilobyte All right, any other questions for today? All right, we sped run that one I don't know how some people take two lectures to go over clock algorithm I have no idea. All right. So key data structures for a clock algorithm circular list of pages in memory Uses a reference bit for each page We put in light gray do whatever you want Has a hand which is basically an iterator that points to the last element that's examined and we only update it when we're actually doing the Replacement if we're doing the replacement what we do is we check whatever the hand is currently pointing at if it's reference bit is zero We replace it Add in that new page that new pages reference bit is one since we just used it advance the hand Otherwise, if we're currently pointing at something with a reference bit of zero while we change that Reference bit of one. Sorry, we change that one to a zero advance the hand And we just keep going through until we find a victim and then the whole idea behind this is for page accesses We do something really really fast. We just set the reference bit to one doesn't matter about the iterator or anything like that so Anything else before we end All right. Yep. So the reference bit can be stored in a giant Bitmap which might not make sense to you because we'll see that term next lecture and we'll see it also in lab six but you can just essentially just Allocate some memory and just have all the bits right next to each other or It could be it could be part of the MMU system. So for each page. It just keeps track of a bit Could be you could also use it in the page table entry itself. Most of them will one the if we go back to the page table entry one of the bits is actually a reference bit and The risk five when we saw so your MMU will just update that for you And so it'll be stored on the page table entry itself All right, all right informal poll so our Mentor or final date is some ridiculous day where you get no time at so ever so the last lecture I was playing on doing review. Do we want the last two lectures to be review? Yeah, sure. Okay done What so the original plan was I just make up something that I wouldn't put on the exam anyways and just talk about it or We just have to Reviews All right two two reviews it is that gives you also like the week before off Maybe not for you, but it gives you the Friday. You don't have to worry about for the week You essentially get a study week for this course Which is the best you could ever hope for considering you're the first day at the first time So take advantage of that and yeah, I guess we'll do two reviews lectures. So just remember phone for you We're on this together