 what's going welcome back how was spring break it's awesome you missed it good song classic coming to class on time the benefits all right so today we're gonna finish up the unit on VM do I sound funny I feel like I sound like I've sometimes this just happened to Carl while I was gone sometimes Carl's voice like goes up an octave and he sounds like he's been sucking helium and this like lasts for several days that's just it's hard to listen to him without giggling a little bit so I feel like I sound like that today so don't laugh so today we're gonna talk about some of the last piece of the puzzle so so we we proceeded in a similar manner when we approached memory as we didn't approach the CPU and now we're at the point where we can actually talk about some of the policies that govern how memory is used and specifically the particular policy and there's lots of memory related policies the operating system is is responsible for but the particular one we're going to talk about today is how do we decide what pages should be moved out to disk so how do we make choices about what should be memory resident okay so a couple of notes about the midterm so in the past we had this preterm exam the beginning of class and that kind of being at the term and that sort of gave us a chance to practice how we do the midterm so and that's important because the midterm is a 50-minute exam it's in this room which is a bad room to hold an exam in other words come to mind but particularly for a class this large so there'll be more people here on Friday there will be people that you haven't seen all semester that I haven't seen all semester who are you huh yeah so so that and it's important that we do this right right so so we have this down you know like my crew has been doing this for years so I think we're pretty good at it but the the variable here this year is you guys you guys haven't done this before okay so here's why we do this couple hours before the exam I'll distribute a seating chart so we're going to use the numbers on these seats and I'm pretty sure I went around one year and marked all the desks that were broken you've noticed that I there's some left-hand desks so if you want a left-hand desk please contact me and if you don't want a left-hand desk you won't get a left-hand desk don't worry so yeah I think hopefully there haven't I'm sure there's a few new broken seats because you know it's you be so it's been a few years so we will sign you a seat we will distribute that seating chart before class I'll post it on discourse it'll also be posted on the doors outside the room so objection number one is knowing your seat and we also have like a seating chart so you so know roughly where the seat is in the room so before you come in know where you're seating and know kind of roughly where that is like these rows are not very easy to maneuver down so if you spend a lot of time what and once people start sitting in the seats one of the beautiful features of these seat numbers is that they're invisible once people sit down so please try to be here on time when you come in and are silently there will be an exam waiting for you on your desk and that exam will have your ID photo looking back at you right so there's a great moment of sort of self you know identification when you sit down and you realize that your exam is is cognizant it's not going to take itself for you unfortunately but it will gaze back me things you need you need a UB ID card this sometimes has surprised students yes you in fact need a working UB ID card if you don't have one you have tomorrow to visit the ID card office and obtain a UB ID card this is like sort of a thing right like so you need that writing materials and water or if you need water and that's what you should have at your seat everything else can go up here in these little hallways along the sides in the back somewhere else right coats backpacks stuff like that smart phones very important any sort of electronic device you know it's it's very instinctive during an exam to have a phone ring in your pocket reach down and grab it at which point I see it throw you out of the room in a fairly unceremonious and embarrassing fashion and give you zero on the exam which is one fewer exam that we have to great that's not a problem for me and once you find your exam will get everybody seated and you guys get started right so maybe you think I'm being a little bit pedantic about this but the exam that I'm giving tomorrow is a 50 minute exam people typically right till the end of class so if you guys waste 10 minutes at the beginning of class figuring out you know up from down then that's just few less time you guys will have on the exam so please I'll post these things online but sort of please make sure you you come ready ready to rock all right any questions about this pretty basic okay oh yeah sorry so no makeup exams are going to be given if you're not here and you're not like in the hospital here we'll get a zero again those are really easy exams to grade and I pretty much already said these things we'll have we'll have videotaping set up and please be very careful about your devices just leave them leave them in your code turn them off right so we don't have to sit listen to your ringtone repeatedly during the exam maybe if I hear one I'll answer it and I don't know like order pizza for you or something like that embarrassing okay questions about the exam not not about content we'll come back to content later but about procedures okay sweet and last reminder if you want to left-hand desk email me tonight because I'm gonna put the exams tomorrow so okay final thing about assignment 3 just last little bit of administration if you are working alone on assignment 3 and for some reason we need to know this so please make sure that you've contacted the core staff already it doesn't hurt to give us send us another email it just reminds us of that fact okay so let's review a little bit about the material that that Carl covered on Monday so difference between a T.O.B. fault and a page fault you can remind me with the differences two memory related faults that can occur yeah okay that's a subset of page faults who can complete the set the T.O.B. fault part is correct so T.O.B. faults what it sounds like when the T.O.B. doesn't understand how to translate a page yeah page is not resident in memory right so that page might not exist it might be the kind of page that operating systems create on demand there's something called zero pages and things like this it also might have been moved to disk might have been swapped out to disk and obviously how the operating system response to these two faults is is quite different on MIPS architectures every page fault is preceded by a T.O.B. fault this is how MIPS works we'll come back in a minute a slide that sort of was left over from last class and talk about other types of architectures where this is a little bit different but on the architecture you guys are developing on for OS 161 there really is no notion of a page fault a page fault is something that the operating system realize this has happened because it tries to handle a T.O.B. fault and realizes that the page it needs is on disk or hasn't been created yet yeah now hopefully you know in the best case again we if some of the algorithms we're going to talk about later work well the pages in memory I can load a translation that told into the T.O.B. very quickly and get on with life so the T.O.B. faults are much easier for the operating system to handle a page fault involves you know in the best case having to allocate a page of memory which may not take very long but involves a little bit of work and the worst case involves actually moving things back and forth to the disk which can get really really slow so page faults are something that we try to avoid when possible okay so let's say I want to swap page from core to disk what are some of the things I need to do what's the process of doing this I have a memory resident page and I want to move it to disk to make create more memory what are the things I need to do one of them yeah so I better get it out of the T.O.B. if I wait to do this what can happen let's say I start copying the contents to disk but I've forgotten to take the T.O.B. entry out what can happen she sure yeah remember when it's in the T.O.B. if there's a process running that process could be running on another on another core in you know pair and parallel with the operating system is actually doing the swap out so it's really critical that I get it out of the T.O.B. not only of the core that I'm running out but of every other core so this is something you'll have to do in assignment three it's called shoot down right I need to notify all the other cores that are that are running that this translation for this process is no longer valid because if there's anybody modifying that memory while I'm doing the copy out the copy that ends up on disk and the copy that I eventually get rid of in memory are not the same anymore and the whole idea of moving the page to disk is sort of a rendered move so I have to get it out of the T.O.B. copy the contents to disk update my page table so I need to update the bookkeeping that the kernel keeps around so I know where to find the page later because I'm assuming that at some point the process is actually going to get back to using this page and I need to be able to find the contents all right so and you know here's my fancy diagram for doing this right okay great and I won't spend too much time on review today because I assumed that Carl did an awesome job so now swapping in now I have a page that's on disk so what triggers you know swapping out so why would I swap out a page what's what's the impetus for doing this yeah I'm trying to create memory you know I'm I'm either facing memory pressure I actually have an urgent need to create memory or in a lot of cases what process what the operating system will do is be continually trimming pages from processes so if I see pages that haven't been used for a long time I don't want to wait until I need memory desperately because usually when I need memory desperately I'm trying to do something the user is waiting for and so at that point it's too late so a lot of operating systems trying to create and maintain a relatively healthy amount of free memory at all time at all times right the amount of memory I'm using is also something that's shared with the file system buffer cache and that's something we'll talk about I don't think we've talked about yet but we'll talk about in a few weeks right so there's other things I can do with memory if I create free memory here I might be able to use it to make the file system look faster that's also good why am I swapping in a page this is a little this is a little different so swapping out is something I can kind of do whenever I want to just in cases where I think a page isn't useful anymore and I'm trying to create some memory that I might need right now and I might just anticipate need in the future why am I swapping in a page what's the real only reason to swap in yeah the process tried to use it and so suddenly I'm like oh you know I actually need to make this page pay resident anymore remember I mean the process try to do a load or store from a page that I've sort of sneakily behind its back moved out to just create more room and so now now the clock is ticking it's on some level swapping pages in is a little bit more of a time pressure operation because the process is waiting for this data there's certain cases where I can try to anticipate this and we might talk about this a little bit later in the semester but in a lot of cases I'm doing this because the page is needed now okay so I stopped so the first thing I have to do is stop this instruction this instruction that's accessing memory cannot proceed until I've sort of restored the memory address that it's trying to use now what do I do similar to the swap out process there's a couple extra steps but what's the first thing I need to do yeah I need a page so that's important and what might this require swapping something out so this is one of the reasons why I try to keep some spare memory around right if I have to swap out a page on the swap in path it's like things just got real bad because not only is this going to require an IO but this the swap out might require another IO so something that was going to be slow anyway just got really slow so again this is one of the reasons is nice to keep a memory buffer around so now I need to find a page on disk using the page table entries this is again something you guys talked about with Carl before and it's important that my page table entry data structures allow me to do this quickly because again I'm on a hot path here I'm not trying to waste time there's something waiting for this copy the contents back what else do I need to do need to update the page table right to make sure that the page table knows it's a memory and load the translation of the TLB and only at this point can I restart that instruction and of course the magic of all of this is that the process has no idea that this just happened all the process knows is that it tried to access an instruction it was just on its merry way just asking instruction that trying to use an instruction that access memory that it felt like it had every right in the world to use and suddenly there's this huge gap in time because I had to do this yeah so again you you can do it either there's two reasons that you can so one reason is I need space right either I need space for a process page that's been faulted in or the buffer cache need space or something like that but but it's it's useful to have some extra space around and so a lot of times what the what the operating system will be doing is sort of in the background walking around looking at processes and being like haven't used that page for a while I'm going to move that to disk preemptively right what's that well I mean how you decide is something we'll talk about later right but the point is however I'm making the decision I try to preemptively move some things out to disk to make sure that I have a little bit of chunk of memory because for example you walk up to the computer and you start only start up Photoshop or something like that that results in a huge amount of instant memory pressure if I'm using all of the memory on the system then that also results in a lot of extra IO right and so it's attention here I mean you bought the memory you want the system to use all the memory but to handle these sort of unexpected events particularly startup right starting big programs can produce an enormous amount of memory pressure all of a sudden right and the operating system obviously has really no way of predicting when you're about to do that part of it maybe it depends right I mean eventually if a process just sitting around now running it probably what happens is most of its contents been swapped out right I mean have you guys ever had this experience particularly like if you've run a really memory intensive computation maybe you ran like a like a garbage who would you like an indexing program or maybe a virus collector virus something and there's a program on your computer you haven't used for a while and suddenly you go to start it and it's just like really laggy all of a sudden right I mean that's what's happening right a lot of the pages that that program uses have been moved out to disk because you haven't used it in a long time and the other thing you need to experience this is that something else has had to run and something else is used up all the memory on the system so something else has pushed all those pages out when you go back to that that application of that process with that tab or whatever it can take a while to bring everything back in right all right okay and then again we have the pretty diagrams okay one last thing okay so my last thing I want to point out before before we move on is there are there are common architectures in use today including the x86 and maybe arm to I really should know this but I don't look it up later that have something that's called a hardware managed TLB so it's kind of an interesting design choice so the what we've been talking about up to this point is a software managed to be so on the software managed to be the operating system code is what's responsible for loading entries into the TLB so in the TLB doesn't know how to transit a particular page all it does is generate a software exception and force the operating system to manipulate the TLB on x86 what happens is the TLB actually knows how to walk the page tables that the operating system sets up so when the TLB doesn't know how to translate an address the first thing it'll do is it'll look through the page tables that are set up for the running process and try to find a valid translation if the page is in memory the TLB will automatically update itself and everything will go on and the operating system doesn't get involved at all if the page is on disk then it generates a page fault and then the operating system has to do some work now obviously there are some trade-offs here I mean one trade-off is the operating system has no control over what entries are in the TLB it's all up to the TLB itself so when the TLB evicts things the operating system doesn't know about it so the TLB is now setting the policy about what entries remain in the TLB the other thing that is probably obvious to use that the operating system in the TLB now have to agree on the format of the page tables so if the TLB is going to walk the operating system page tables the TLB has to understand the page table data structures and so that limits some of the design choices you can make in the operating system about how the page tables work because the TLB has to understand this is a piece of hardware right it's not something you can reprogram yeah yeah yeah again I mean you'd have to you know the thing with pages that are on the page table stuff gets more complicated right sometimes it's copy like a copy on write issues sometimes you have zero field pages that you want to create on demand right sometimes you want to share pages between processes so so yeah but you could you absolutely could yeah so so yeah so this is this is a you know again a design choice that that that people made the pro here obvious is I avoid a certain number of of operating system exceptions the hardware can do this faster and avoiding the context which is the operating system would save time and the con is some of the things we've already talked about the page table data structures are fixed and it also limits the operating systems visibility into certain types of faults yeah so that's a good question so on a hardware managed TLB how is the operating system experience different so before we talked about there's two types of faults there's TLB faults there's page faults on a software managed TLB the only faults that are generated by the TLB or what kind or the only faults that are generated by the MMD or what kind of faults they're TLB faults right so essentially the TLB says I don't have an entry and then the operating system might discover when it looks up the entry in the page table that the page is on disk on a hardware managed to be what are the only kind of faults that are generated page faults because the TLB faults are handled automatically by hardware does that make sense yeah okay yeah so I just said that right the TLB faults are never handled in hardware the only faults that the operating system actually sees are page faults so that's the point where the MME really throws up his hands and says I have no idea what to do now I tried my best I did the school thing and again I I should figure out what arm I have a suspicion that arm has a hardware managed TLB as well yeah yeah size of the disk I mean I mean normally yeah that's not normally true I mean normally you don't need a SWAT file that's anywhere close to the size of disk but on Windows I mean has anyone done this I mean Windows has like a SWAT file I think that you can reconfigure I can't remember what they call it what is it called yeah right right and has anyone ever set up a Linux machine before when you set up the partition table there's an area for swap right it's a fixed size I think on Linux now usually they suggest like the same amount of memory right so that's that's a reasonable default but you could set that to be larger so yeah so the but but you don't like at some point usually I don't need you know it's particularly with growing memory sizes I don't need a huge SWAT file on disk good questions alright any questions about this before we talk about policies at this point you guys sort of understand that the core memory management policies that the sorry mechanisms so you can run out of memory even if you configure it to use disk right so that's that's a great question did it wasn't there a slide on the Linux out of memory killer that's one of my favorite pieces of software is it's a it's a it's like a total hack right so Linux can run out of memory for a variety of reasons of the so there are certain pages that the kernel needs that have to remain in memory right and then in certain cases I can run out of swap space so I have so many pages in memory I have nowhere to put them in the swap out anymore particularly on Linux because I have a fixed-size swap file so what do I do then let's say the operating system is literally sort of out of memory right I've played all these games I can play and I have no memory left what do I do what are some options I could like reboot you know that's it like whatever I mean there there are no good options at this point so everything we're going to talk about is like a nasty hack right rebooting seems dramatic right what else could I do yeah yeah I could just start killing processes Linux doesn't do this randomly Linux has something called the out of memory killer and it has this I mean I might as well be random right I mean has some sort of weird metric for deciding what process it's going to kill some of it makes a little bit of sense because one of the things that at least considers is how much memory is the process using there's no point killing our processes that aren't consuming any memory they're like hey I'm not the problem you know look over there's like Postgres has like eight gigs right like you should talk to Postgres about its memory usage yeah so that there's this I don't know the code for this is really weird right but it's a strange calculation that that is done and then it just shuts down a process and I like I don't know if you guys have maintained Linux machines you may have seen this happen before particularly when you run things that have memory leaks and over time they start consuming more and more memory eventually you know that the system was out of memory and you get some sort of message that says the out of memory killer killed usually the most useful thing on your system is for some reason the thing that gets killed right like I shut down the web server thanks right didn't I didn't want that yeah yeah usually once you've hit the end of your swap something's something's wrong right I mean you're usually trying to do too much with the machine or something as a bug that's causing it to leak memory and yeah I mean reboot sewed out a lot of this stuff particularly when you have slow memory leaks yeah so yeah once you run once you you can't definitely get to the point where you're completely out of memory and at that point to kind of like you know sometimes restarting things helps right so I think actually what the out of memory killer does is it sends like a sick hop so it asks that the process to restart and in certain cases with things that have memory leaks if I just reboot the web server or restart you know whatever background services consuming a lot of memory the memory leak goes away and then that I start over and things are good for a while so yeah good question all right okay so let's talk about page replacement policies so this is something that when I'm under memory pressure is pretty important so I have this decision to make when I evict pages so when I'm gonna when I need memory and I can and again I this can be because I urgently need memory to bring in a page that a process actually needs right now or I can apply the same logic to try to find pages to move out to disk just to create that spare memory with that I want to keep around in case the user suddenly launches an application that uses a lot of memory so here's the cost-benefit calculation when I do swapping right what's the benefit swapping out a page what do I get what is operating system get out of moving a page to disk yeah well that's what I get from the aggregate right but what do I get from the just that one like one page I moved one page to desk what do I have now because I'm gonna do some work right yeah I have 4k more memory how long do I have that memory forever that would be awesome I'm just I'm creating more memory on the system every time I swap out a page to get 4k more memory forever right I can just like that's an awesome strategy for memory man yeah exactly so the cost is I have I have to move things to disk and this is considerable right but I mean this is pretty much fixed okay there's not much I can do about this people have talked about ways to optimize this but it doesn't really matter right I've got to move stuff to disk I've got 4k stuff or 8k or 2 megabytes if we're talking about you know large pages whatever I've got a chunk of bits who knows what they are and they've got to go to disk and they've got to get there so this is not something I can usually attack the benefit however is a little more interesting so I have again I have some amount of memory I have a page in memory how long do I have that memory forever is not the correct answer right how long do I have it till the page gets swapped back in okay so there is where I have a little bit of leverage yeah she's well I'm I'm I'm conceptually think about this particular page right I'm pushing this page onto disk right the longer that page stays on disk the longer I can pretend that I have this 4k of extra memory right so as long as the page on disk remains unused I'm good you know if this and so essentially what we try to do here is maximize the benefit again there's there's things we can try to do to minimize the cost but it's really hard to get this to go away entire right so so another way to think you know if I do a good job of maximizing the benefit here this also accomplishes minimizing the page fault rate because I'm gonna have fewer page faults overall thrashing now this is an interesting question so memory is so cheap today it does anyone's computer do this anymore I do feel like a little boy or something my voice is weird octave yeah yeah that's yeah you should yeah that will probably do it actually like open like two virtual box instances each configured to use half of your RAM right and then try to do something I mean it's it's sort of I don't know I mean a lot of computers have a lot of memory today right so it's not clear that this happens but has anyone ever experienced this happening like yeah so I mean you know this when this happens I mean this is when you you you you you really think hard about rebooting and probably do like you said that you know the machine is really unresponsive on old machines again you guys are so young we haven't had these experiences the disk is loud and it's like you know it's making all this noise the mouse starts you know arcing across the screen like suddenly showing you history about it's like I turned on mouse history I didn't want my sister here yeah so the like this is a very very distinct experience and again this is what happens when everything just goes terribly wrong and the system is making for some reason making very very bad decisions about what to move back and forth but there's no real you know there's no real explicit definition of fashion it's a sort of colloquial experience but but you do you do notice and essentially what's happening you know again think about when we talked about scheduling there's overhead to making scheduling decisions that overhead is not doing useful work swapping there's overhead to moving things back and forth that overhead is not useful work but when I get stuck doing so much of it I can't get anything else done it's very very noticeable particularly because I always so slow right I like this this causes the performance of the computer to to degrade or collapse okay so how do we maximize this benefit what's what's the goal here right so again cost I can't really do much about how do I maximize the benefit of the swap out decision yeah so okay so that's a good thing to do preemptively right I can definitely make sure that I don't get into the thrashing situation by doing this right but when I'm actually picking a page whether it's before I start thrashing or when I need memory how do I maximize the benefit yeah pick the page that's going to stay on just the longest right so the best there there is an amazing choice here right maybe this this is sort of like Dan's optimal solution right there are pages that are like the magic unicorn here that if I can find them everything is great what page is that what page is going to stay on just the longest how long is a page going to stand what's the longest a page could stay on disk like the end of time right it will never be used again so again you blundered into some strange menu and word and like it was really you know you were really terrified so you ran away and like went back to using the five commands you know how to use and that code will never come back into memory ever you know you've learned your lesson like you're not going into that some menu again so so those pages you know again can be moved to disk and they will never come back so that is that's the ideal most pages aren't like this unfortunately it's too bad I mean some are but you know so now let's go back and you know what did we do when we think about scheduling algorithms what if we had and what's the oracle solution so if there's an oracle solution the oracle solution is going to know how long it is before a page gets used so if there's something about the future here that this algorithm wants to know it's simply how long before this page it's brought back if I knew that I order pages by that and I pick the one with the maximum done like that is that is the provably optimal solution to minimizing the page fall rate so yeah anyway obviously this is this is easy okay unfortunately this is a hard schedule to actually implement right so I don't have this information about the future so what do we do we can't predict the future use the past to predict the future so the okay so so now now we're going to start talking about how we actually try to implement this and get this to work so let's say that I want to start so clearly I need to know something about how pages are used and that's actually one of the big trade-offs when designing page replacement algorithms that we didn't have when we designed to schedulers why is this such a problem so schedulers can actually track a fair amount of state about each process on the system what makes this hard to do for pages let's say I wanted to track every time a page is accessed you know if pages are you know like how many times a page has been accessed you know rolling averages of how long it's been things like that why is this so difficult there's two problems that's one of them so one of them is there's so many pages there's not that many processes on the system there might be a few thousand processes on the system there could be billions of pages on the system and so I have to think a lot more carefully remember we spent a lot of time designing these page table data structures to be really compact we were really proud because we got all the information we needed to store a page like 32 bits and now you're telling me I'm gonna store a 32-bit timestamp with every page you've just doubled the size of my page table entries probably not not ideal the other problem is actually also visibility in so think about a hardware managed tlb what information does that hardware managed tlb actually not share with me so remember hardware managed tlb is going to be walking the page tables behind my back and loading the tlb so what does the operating system not see anymore yeah and unless the hardware decides to share that information with me which maybe it does maybe the hardware keeps in statistics that I can I can use but in order for the operating system to track this information I actually have to get involved so when we're scheduling again I mean the operating system knows exactly how long things run because it's in control but with memory I had these hardware assists I have these pieces of hardware they're helping me out and they can get in the way by by sort of obstructing the operating system's visibility into things that are going on so in a lot of cases operating systems actually don't have detailed information about how often each page is tracked or each page is used or what kind of use right I might know that the page was used over some period of time I don't know how many times it could have been one time or a million times I don't know what type of use that was it could have been read only or a mixture of reads and writes this information is just not considered that you know useful enough for the hardware to store and it also makes the process of translating pages slower yeah types of processes how like how would that what would I how would I do that yeah so so there is so it's not something we're going to talk about today but a lot of operating systems have a concept of what's called I think we talked about this in the past we talked about locality a working set for a process so another here's another way to think about what and this goes back to this idea of trying to trim all the time so imagine the operating system it knows that the process needs a certain number of pages to make forward progress smoothly but it may not know exactly what pages those are or how many so one approach is I can trim and I can keep trimming and if the page fault rate of the process starting late starts to go up then or I can actually I can reduce the total number of pages that are allocated to that process and at some point the total number of the page fault rates gonna gonna increase and at that point I stop I sort of back off right so I try to give the process as few pages as possible while also maintaining a reasonable page fault right that allows a process like Photoshop which naturally needs a lot more memory to have a lot more memory right I certainly don't want to say okay bash and Photoshop both get the same memory allocation right that results in completely unnecessary allocations to bash and terrible performance for Photoshop right so there's there's actually a lot of subtlety here right I mean this one lecture is glossing over like 30 or 40 years of thought and design which to some degree has all been rendered and moved because now you just buy like 32 gigabytes of RAM and call it a day right yeah what's that I suspect that is considered yeah yeah absolutely I certainly don't want to to essentially because because the access to memory affects prior the priority of the process right it affects my ability to run so if I have a really high priority process but I've taken all of its memory moved it to disk it can't run very very much because every time it runs a few instructions and then it faults and I have to wait for things to come back in the disk so do it absolutely so these are the challenges when I start thinking about storing this information right how do I store this compactly how do I figure out how to get the hardware to give me the information I need and then how do I you know how do I store it as part of the page table entries yeah so we've talked about this right so you know the statistics can be expensive to collect I and here's another you know one of these classic trade-offs we talked about scheduling allocates but the scheduling algorithm takes so long to run that it wastes so much time that I could have used to actually do useful work it's not not very helpful similarly if the information that I'm trying to store to make quote-unquote better page replacement decisions consumes so much memory that it would have been better just to like use that memory for useful stuff remember this is overhead the kernel page table data structures are not useful memory nobody gets to store cat pictures in them or whatever right they're just wasted and so the more memory I use for this this is not good all right so simplest possible page replacement algorithms sort of goes back to our scheduling discussion about schedule as well as the simplest possible schedule random yes great leave it you've at least we've appreciated a gain in appreciation for randomness right no state no cues no whatever just pick a page at random and throw it out and again this is not a bad starting point when you when you guys are implementing assignment 3 I would suggest that you use random it's very easy to do and as usual it's a good benchmark for building other things random simple it is probably too simple when when I choose so what's the worst case scenario here like for choosing what can happen if I choose the worst page to swap out yeah yeah exactly like no sooner did I swap it out that it heads right back in right in fact the most depressing thing that can happen on a multi-course system is while I'm actually swapping it out the process runs on another core and needs that memory right so like I know like halfway through doing this long task that I'm scum you know like you know and a lot of times there's no way to I mean at least on OS 161 there's no good way to cancel the IO so it's no it's terrible anyway yeah that's the worst case right I mean imagine you're in the middle of doing some stupid tedious task and someone tells you that it's worthless and you have to undo all of it right but you have to finish it first right yeah anyway so that's bad that's what we try to avoid and from time to time random is going to pick that page right and and it's you're going to feel bad about it actually I guess the nice thing about choosing random for assignment 3 is it forces you to handle that though that terrible corner case which is great because that's that's one of the nastier little pieces of Simon 3 is figuring out how to get this right okay so so an algorithm that tries to predict the past so in general the common approach to to doing this that doesn't get into all these complexities of working sets and other things is just this idea that once a process has stopped using a page for a while it's probably done using that page this is this idea least recently used find the page that the process hasn't touched for the longest and move that page out to disk and again I'm using the recent past here to predict the future the recent past this the process has to use the page the prediction for the future is it won't use it again yeah well so so ideally yeah so this is a great question so ideally if I could store lots of state about the page I could store the timestamps that the process use the page and then I essentially say what's the page that is resident for this process that has not been touched for the longest right does that make sense right well remember I'm doing this when I need when I need pages right like not but I'm still confused by a question so if I've touched a page okay I see what you mean yeah I mean if I'm trying to try and if I'm trying to force a process to use like two pages then things are going to be bad right that's possible you can you can get that to work in fact I remember one year when we were testing Harvard assignment three kernels hopefully this will make you guys feel better about your submissions we had we had one submission that never failed but ran like seemingly endlessly right like I think David was testing this one and he could not get it to finish right he let it run for several days it finally got killed off by like one of the system administrators that I think thought it was a virus or something so he moved it to his private machine and he let it run for a week or something running parallel to him and it finished actually like it never it never stopped I think we can I think we concluded it they had accidentally like forced the process use like only two or three pages right so it was essentially running with like 14k of RAM right which was slow but it was but it was awesome it just ran and ran and ran and David was like well I think we should keep giving it a chance I was like at some point we have to turn in grades so yeah no it finally it finally finished once we found it a happy home where it could work work like one of the most futile tasks ever I mean parallel games pretty stupid anyway right and running it with three pages even double it so so anyway so yeah I mean if you're really over-constraining a process to the point then yeah there really are no good decisions right once you get out of the working set right then then you start to think about this all right so this was what we'd like to do this is this is LRU this is the goal you know this this might be as good as we can do without being able to predict the future the cons here come down to storing this state right so how do we tell how long it's been since a page was accessed and how do we store how long it's been since a page is accessed and again if this ends up doubling the size of my page table entries and therefore doubling the size of my page table related data structures this is probably not good plus in a lot of cases remember I can't even tell when the page has been accessed or not very accurately all right so if we're loading entries into the TLB we know this now here's the other other thing about even even on a software managed TLB so even when the operating system is being asked to load entries into the TLB do we know when the page was accessed why not I mean I know a time when the page was accessed right so what's a time that a page was accessed when I loaded it into the TLB so I know for a fact right that you know it was accessed at this point because I put it in the TLB but at that point what do I know nothing like absolutely nothing so for example I could have two TLB faults one for a page that gets used once once for a page that gets used continuously by the process over the next you know 10 seconds from the operating systems point of view both of them were used at the same point and and distinguishing between them is very difficult remember part of the the goal of the TLB is to have enough entries so that once the process starts to run I have to fill it up but then at some point it just runs away and I don't have to add entries as often and so I lose visibility at that point for what's going on in the TLB I was actually talking with a colleague of mine at Duke recently and he was they're doing some work on on tape tracking on Android and I guess on newer systems one of their approaches was basically as slow as forcing the kernel to get involved if with every load in store right so we talked about how slow that was in class and I was like how slow is it he's like I lost track of how many zeros like we had to add it was like a hundred thousand times slower than than the regular system so we can't do this on a normal machine so so I don't have visibility into this and recording every page access is way slow again that would be equivalent to not having a TLB at all right all right okay I have time to talk about the clock we're gonna finish the clock again you could sort of do the math on on storing this information right so if I have 32 bits that doubles the page table size maybe I can store eight bits of time but I really only have 256 ticks what's a tick I have to decide in the kernel maybe that's a second maybe it's a microsecond you know so this gets tricky you know just just the idea of sort of trying to cram time into these data structures and then you know so measuring it was bad enough so now actually how do I find the least recently used page the page table data structure that I've created is not built for this it's built for quickly taking a virtual address and finding a page table entry that has more information about the page it is not built is like a heap to allow me to quickly find the smallest entry so this would have to be another data structure that have to layer on top of this right which sounds sounds pretty bad all right so sort of classic algorithm for doing this that maintains some of the properties that we want from least recently used but is way simpler to implement is something called the clock it is simple it requires one bit of information and it achieves LRU like properties all right so here's what we do we when we need a page to evict we go through all the pages that are in memory in some order and when you guys start implementing assignment three if you guys have started and you're working on your core map you can think of a logical way to do this right so I go through pages in order pages that are in memory pages that I can't evict pages that aren't pinned for some reason and if the page has been accents I will if the if the page has the accessed bit set I clearly and move on if the page access bit is clear then I evicted sorry I said those backwards right so if the page is if the page access bit is clear I evict that page and stop if the page access bit is set I clear the bit and sorry there's one last thing here which is that when pages are accessed specifically when I load them into the into the TLB I set this bit so it makes sense so when I load the TLB or when I know the page is accessed I set this bit in this data structure when I need a page I go through them in the order clearing bits until I find one that's clear and then I evict that page yeah no I'm looking for one page I mean I could be looking for end pages right but the algorithm is the same I'm looking for one page I go till I find one one clear bit kick that out if I need more pages I start at the same point right and keep going right if I always started at the beginning I'll be very unfair to the pages that were beginning yeah whenever I know that the page was accessed in this case you can think of it as loading the TLB that's the visibility you have on this so here's the obligatory diagram let's see that these are the pages that are start out with their access bit clear I remove that page next time I keep going I'm clearing bits as I go so again in parallel what happens is as those pages are used the the those bits are being are being set again and so pages that are being used frequently essentially constantly have their bits being set and so every time the clock hand comes back to them they're still set and they don't get evicted a page that is has has not a page that has not been used long enough for the clock to clear the bit and then make an entire traversal of the rest of memory and come back to it gets evicted so again so this ends up having LRU like properties while only consuming one bit bit of data right it is not perfect LRU by any means but it consumes a lot less information so it just from a high level what what does it mean when the we're almost done what does it mean when the clock hand is turning slowly what does that mean about the system well it also can mean I don't have a lot of memory pressure right I mean this is the ideal case right pretty quickly I find a page to evict and I'm not needing pages very often if the clock hand starts yeah or we're making good decisions if the clock hand starts moving really quickly so the worst case scenario for clock is that I start and all the bits are set and so what happens if all the bits and memory are set what's the algorithm gonna do it always has to find a page so it's gonna go all the way around clearing bits until it gets back where it started where we'll find a clear bit so in that case you know usually what's happening is there's lots of memory pressure or for whatever reason the clock our LRU algorithm is evicting bad pages right this is this is the bad case essentially at this point it almost becomes random I'm basically evicting the whatever page the clock starts okay questions about this wrap up yeah you guys can start packing up it's one bit for clock it's one bit for every page in memory no remember I only the operating system sets that bit when I load the TLB the processor is no role in this right yeah or whenever the operating system knows the page was used all right good luck on the exam on Friday oh we didn't get a copyright that's okay that won't be an exam