we are very pleased to present you today Rik van Riel. [[BR]] He is a kernel hacker working on memory management.[[BR]] Currently he is working at conectiva S.A. in Brazil. As all of you know,[[BR]] it is a big Linux company from South America.[[BR]] Appart from kernel hacking, he also runs the Linux-MM website and the[[BR]] #kernelnewbies IRC channel on openprojects.net[[BR]] You can find more about him at: www.surriel.com (there you can find, among[[BR]] other things the slides of this talk at:[[BR]] ([http://www.surriel.com/lectures/mmtour.html])[[BR]] He will talk here about memory management but other interests of him[[BR]] are: High availability, filesystems and various other things ...[[BR]] The talk will be here, in #linux channel, Mr Riel suggested us to make[[BR]] another channel (#qc -> questions channel) to write questions during the talk. [[BR]] Should you have any questions, comments, etc, just write them in #qc [[BR]] and Mr. Riel will reply.[[BR]] Thank you to Mr. Riel for comming here and also to all of you[[BR]] The title of his talk is:[[BR]] Too little, too slow; memory management[[BR]] Mr. Riel ...[[BR]] I guess it's time to begin .............[[BR]] ok, welcome everybody[[BR]] today I will be giving a talk about Linux memory management[[BR]] the slides are at [http://www.surriel.com/lectures/mmtour.html][[BR]] we will begin with some of the slides introducing memory management and explaining why we need memory management[[BR]] if you have any questions about my talk, you can ask them in #qc[[BR]] in #qc, you can also discuss with each other the things I talk about[[BR]] this channel (#linux) is meant to be completely silent ... except for me of course ;)[[BR]] ...... (page 1) .....[[BR]] let me begin by telling a little bit about what I am doing at the moment[[BR]] Conectiva is paying me to work on improving the Linux kernel full-time[[BR]] this means that I am working for Linux Torvalds and Alan Cox, but Conectiva is paying me ;) [thanks Conectiva :)][[BR]] now I'll move on to the real talk ... (page 2)[[BR]] [for the new people ... [http://www.surriel.com/lectures/mmtour.html] for the slides][[BR]] ok, I will begin by explaining about memory management[[BR]] most of the introduction I will skip[[BR]] but I will tell a few things about the memory hierarchy and about page faults and page replacement[[BR]] lets start with the picture on (page 3)[[BR]] this picture represents the "memory hierarchy"[[BR]] every computer has more kinds of memory[[BR]] very fast and very small memory[[BR]] and very big but very slow memory[[BR]] fast memory can be the registers or L1 cpu cache[[BR]] slow memory can be L2 cache or RAM[[BR]] and then you have hard disk, which is REALLY REALLY extremely slow ;)[[BR]] when you see this picture, some people will ask themselves the question "but why doesn't my machine have only fast memory?"[[BR]] or "why don't we just run everything from fast memory?"[[BR]] the reason for this is that it is impossible to make very big fast memory[[BR]] and even if it was possible, it would simply be too expensive[[BR]] and you cannot run everything from fast memory because programs are simply too big[[BR]] now we've talked about "fast" and "slow" memory ... (page 4) tells us about different kinds of speeds[[BR]] you have "latency" and "throughput"[[BR]] ok, good to have everybody back[[BR]] if you look at (page 6) you can see how rediculously slow some memory things are[[BR]] ok, lets go to (page 7)[[BR]] "latency" == "if I ask for something, how long do I have to wait until I get the answer"[[BR]] "throughput" == "how much data can I get per minute"[[BR]] I think we do not have time to look at the L1 and L2 cache things[[BR]] so lets move on to the RAM management[[BR]] on (page 14)[[BR]] RAM is the slowest electronic memory in a computer[[BR]] it is often 100 times slower than the CPU core (in latency)[[BR]] this is very very slow[[BR]] but when you see that the disk is 100000 times slower than RAM (in latency), suddenly memory looks fast again ... ;)[[BR]] this enormous difference in speed makesit very important that you have the data in memory that you need[[BR]] -[[BR]] 6 if you do not have the data you need in RAM, you need to wait VERY LONG (often more than 5 million CPU cycles) before your data is there and your program can continue[[BR]] on the other hand, everybody knows that you NEVER have enough memory ;)[[BR]] so the system has to chose which pages to keep in memory (or which pages to read from disk) and which pages to throw away (swap out)[[BR]] <[[BR]] ok, lets try this again ;)[[BR]] the ping timeout probably lost my last 3 minutes of the talk[[BR]] 6 so lets move on to the RAM management[[BR]] on (page 14)[[BR]] RAM is the slowest electronic memory in a computer[[BR]] it is often 100 times slower than the CPU core (in latency)[[BR]] this is very very slow[[BR]] but when you see that the disk is 100000 times slower than RAM (in latency), suddenly memory looks fast again ... ;)[[BR]] <-- Sadie has quit (Ping timeout for Sadie[orka.go2.pl])[[BR]] this enormous difference in speed makesit very important that you have the data in memory that you need[[BR]] -[[BR]] but as we all know, no computer ever has enough memory ... ;)[[BR]] and the speed difference is REALLY big ... this means that the system has to choose very carefully what data it keeps in RAM and what data it throws away (swaps out)[[BR]] lets move on to page 18[[BR]] ok, if a page of a process is NOT in memory (but the process wants it) then the CPU will give an error and abort the program[[BR]] then the Operating System (OS) gets the job of fixing this error and letting the program continue[[BR]] this trap is called a "PAGE FAULT"[[BR]] the OS fixes the job by getting a free page, putting the right data in that page and giving the page to that program[[BR]] after that the process continues just like nothing happened[[BR]] the ONLY big problem is that such a page fault easily takes 5 _million_ CPU cycles[[BR]] so you want to make sure you have as little page faults as possible[[BR]] the other problem is that you only have a "little bit" of memory in your machine[[BR]] and you run out of free memory very fast[[BR]] at that point, the OS needs to choose which data it keeps in memory and which data it swaps out[[BR]] ..... lets move to (page 19) of [http://www.surriel.com/lectures/mmtour.html] ....[[BR]] the "perfect" thing to do is to throw away (swap out) that data which will not be needed again for the longest time[[BR]] that way you have the longest time between page faults and the minimum number of page faults per minute ... so the best system performance[[BR]] the only problem with this method is that you need to look into the future to do this[[BR]] and that isn't really possible ... ;)))[[BR]] so we have to come up with other ideas that approximate this idea[[BR]] ......[[BR]] one idea is LRU ... we swap out the page which has not been used for the longest time[[BR]] the idea is: "if a page has not been used for 30 minutes, I can be pretty sure I will not use it again in the next 5 seconds"[[BR]] which really makes a lot of sense in most situation[[BR]] unfortunately, there are a few (very common) cases where LRU does the exact wrong thing[[BR]] take for example a system where somebody is burning a CD[[BR]] to burn a CD at 8-speed, you will be "streaming" your data at 1.2MB per second[[BR]] at that speed, it will take just 30 seconds on your 64MB workstation before your mail reader is "older" than the old data from the CD write program[[BR]] and your system will swap out the mail reader[[BR]] which is the exact WRONG thing to do[[BR]] because most likely you will use your mail reader before you will burn the same CD image again[[BR]] LFU would avoid this situation[[BR]] LFU swaps out the page which has been used least often[[BR]] so it would see that the mail reader is being used all the time (pages used 400 times in the last 2 minutes) while the CD image has only been used one time (read from disk, burn to CD and forget about it)[[BR]] and LFU would nicely throw out the CD image data that has been used[[BR]] and keep the mail reader in memory[[BR]] in this situation LFU is almost perfect[[BR]] ... now we take another example ;)[[BR]] if we look at GCC, you will see that it consists of 3 parts[[BR]] a preprocessor (cpp), a compiler (cc1) and an assembler (as)[[BR]] suppose you only have memory for one of these at a time[[BR]] cpp was running just fine and used its memory 400 times in the last minute[[BR]] now it is cc1's turn to do work[[BR]] but cc1 does not fit in memory at the same time as cpp[[BR]] and LFU will swap out parts of cc1 because cpp used its memory a lot ...[[BR]] and does the exact wrong thing ... cpp _stopped doing work_ a second ago[[BR]] and cc1 is now the important process to keep in memory[[BR]] ... this means that both LRU and LFU are good for some situations, but really bad for other situations[[BR]] I got a question if LRU or LFU is better ... the answer is none of them ;)[[BR]] what we really want is something that has the good parts of both LRU and LFU but not the bad parts[[BR]] luckily we have such a solution ... page aging (on page 20)[[BR]] page aging is really simple[[BR]] the system scans over all of memory, and each page has an "age" (just points)[[BR]] if the page has been used since we scanned the page last, we increase the page age[[BR]] riel: How do you measure when a page has last been "used"?[[BR]] ... umm yes ... I almost forgot about that part ;))[[BR]] --- when a page is being used, the CPU sets a special bit, the "accessed bit" on the page (or the page table)[[BR]] --- and we only have to look at this bit to see if the page was used[[BR]] --- and after we look at the bit, we set it to 0 so it will change if is being used again after we scan[[BR]] so back to page aging now[[BR]] if the page was used since we last scan it, we make the page age bigger[[BR]] if the page was not used, we make the page age smaller[[BR]] and when the page age reaches 0, the page is a candidate for swapout ... we remove the data and use the memory for something else[[BR]] now there are different ways of making the page age bigger and smaller[[BR]] for making it bigger, we just add a magic number to the page age ... page->age += 3[[BR]] for making it smaller, we can do multiple things[[BR]] if we substract a magic number (page->age -= 1), we will be close to LFU[[BR]] if we divide the page age by 2 (page->age /= 2), we will be close to LRU[[BR]] to be honest, I have absolutely no idea which of the two would work best[[BR]] or if we want system administrators to select this themselves, depending on what the system is doing[[BR]] page aging is used by Linux 2.0, FreeBSD and Linux 2.4[[BR]] somebody thought it would be a good idea to remove page aging in Linux 2.2, but it turned out not to work very well ... ;)[[BR]] ... so we put it back for Linux 2.4 ;))[[BR]] and another question: riel: what is linux using?[[BR]] HoraPe: Linux 2.0 uses the "page->age -= 1" strategy[[BR]] HoraPe: and Linux 2.4 uses the "page->age /= 2" strategy[[BR]] maybe the first strategy is better, maybe the second strategy is better[[BR]] if we have any volunteers who want to test this, talk to me after the lecture ;))[[BR]] I will now go on to (page 21) and talk about drop-behind[[BR]] most of you have probably heard about read-ahead[[BR]] where the system tries to read in data from a file *before* the program which uses the file needs it[[BR]] this sounds difficult, but if the program is just reading the file from beginning to end it is quite simple ...[[BR]] one problem is that this linear read will quickly fill up all memory if it is very fast[[BR]] and you do not want that, because you also have other things you want to do with your memory[[BR]] the solution is to put all the pages _behind_ where the program has been reading on the list of pages we will swap out next (the inactive list)[[BR]] so in front of where the program is now, you read in all the data the program needs next (very friendly for the program)[[BR]] and in exchange for that, you remove the data the program will probably not need any more[[BR]] of course you can make mistakes here, but if you get it right 90% of the time it is still good for performance ... you do not need to be perfect[[BR]] ... from the part about hard disks, I will skip almost everything[[BR]] ... only (page 23) I will discuss today[[BR]] as you probably know, hard disks are really strange devices[[BR]] they consist of a bunch of metal (or glass) plates with a magnetic coating on them which spin around at rediculously high speeds[[BR]] and there is a read-write arm which can seek across the disk at very low speeds[[BR]] the consequences of this design are that hard disks have a high throughput ... 20 MB/second is quite normal today[[BR]] this is fast enough to keep a modern CPU busy[[BR]] on the other hand, if you need some piece of data, your CPU will have to wait for 5 _million_ CPU cycles[[BR]] so hard disks are MUCH too slow if you're not reading the disk from beginning to end[[BR]] this means that so called "linear reads" are very fast[[BR]] while "random access" is extremely slow[[BR]] you should not be surprised if 90% of the data is in linear reads, but hard disks spend 95% of their time doing random disk IO[[BR]] because the linear IO is so fast the disk can do it in almost no time ;)[[BR]] the normal optimisation for this is "IO clustering", where the OS reads (or writes) as much data in one place of the disk as possible[[BR]] the "as possible" can not be too large, however ...[[BR]] if you have "only" 64 MB RAM in your machine, you probably do not want to do readahead in 2MB pieces[[BR]] because that way you will throw useful data out of memory, which you will need to read in again later, etc...[[BR]] so it is good to read in a small part of data, but it is also good to read in very big parts of data ... and the OS will have to decide on some good value all by itself[[BR]] Linux has some auto-tuning readahead code for this situation (in mm/filemap.c::generic_file_readahead(), for the interested) but that code still needs some work to make it better[[BR]] and of course, another way to make "disk accesses" fast is to make sure you do not access the disk[[BR]] you can do this if the data you need is already (or still) in memory[[BR]] Linux uses all "extra" memory as a disk cache in the hope that it can avoid disk reads[[BR]] and most other good operating systems do the same (FreeBSD for example)[[BR]] ... now I will go on with Linux memory management[[BR]] ... on (page 28) and furter[[BR]] I will explain how memory management in Linux 2.2 chooses which pages to swap out, what is wrong with that and how we fix the situation in Linux 2.4[[BR]] and also the things that are still wrong in Linux 2.4 and need to be fixed later ;)[[BR]] ok, another question: riel: to keep data in memory, have you ever thought about compressed data in memory? [[BR]] --- this is a good idea in some circumstances[[BR]] --- research has shown that compressed cache means that some systems can do with less disk IO and are faster[[BR]] --- on the other hand, for some other systems it makes the system slower because of the overhead of compression[[BR]] --- it really depends on what you do with your system if the "compressed cache" trick is worth it or not[[BR]] --- and it would be interesting to see as an option on Linux since it is really useful for some special systems[[BR]] --- for example, systems which do not have swap[[BR]] ... ok, lets move on to (page 31)[[BR]] Linux 2.2 swapout code is really simple[[BR]] (at least, that's the idea)[[BR]] the main function is do_try_to_free_pages()[[BR]] this function calls shrink_mmap(), swap_out() and a few other - less important - functions[[BR]] shrink_mmap() simply scans all of memory and will throw away (swap out) all cache pages which were not used since the last time we scanned[[BR]] and swap_out() scans the memory of all programs and swaps out every program page which was not used since the last time we scanned it[[BR]] ... (page 32)[[BR]] this is a really simple system which works well if the system load is not too high[[BR]] but as soon as the load gets higher, it can completely break down for some reasons[[BR]] if, for example, the load on the system is very variable, we get problems[[BR]] if you have enough memory for 30 minutes and all memory has been used in those 30 minutes, then after 30 minutes _every_ page has been used since the last time we scanned (30 minutes ago)[[BR]] and then something happens in the system (Netscape gets started)[[BR]] but the OS has no idea which page to swap out, since all pages were used in the last 30 minutes, when we scanned last[[BR]] in that situation, the OS usually swaps out the 'wrong' pages[[BR]] and those wrong pages are needed again 5 milliseconds later[[BR]] which makes the OS swap out *other* wrong pages again, until everything settles down[[BR]] so every time the system load increases, you have a period where the system is really slow and has to adjust to the load ...[[BR]] another problem is that (in shrink_mmap) we scan and swap out pages from the same function[[BR]] this breaks down when we have a very high load on the system and a lot of the pages we want to swap out need to be written to disk first[[BR]] shrink_mmap() will scan every page in memory and start disk IO for the pages that need to be written to disk[[BR]] after that it will start scanning at the beginning again[[BR]] and no page it sees has been used since the last time we scanned it, since kswapd was the only thing running[[BR]] at that point the system -again- starts swapping out the wrong pages[[BR]] a question: is this the do_try_to_free_pages() printk we hear so much about on lkml ?[[BR]] --- this printk is called when do_try_to_free_pages() cannot find pages to swap out[[BR]] --- not when do_try_to_free_pages() swaps the wrong pages by accident[[BR]] --- so these things are not the same[[BR]] ... lets move on to (page 33) and see how we fix these problems in Linux 2.4[[BR]] the two big changes for Linux 2.4 are page aging and the separation of page aging and page writeback to disk[[BR]] page aging means we are more precise in chosing which page we swap out, so we will have a better chance of having the pages we need in memory[[BR]] and the system will perform better when memory is getting full[[BR]] the separation of page aging and page flushing means that we will not swap out the wrong page just because the right page still needs to be written to disk and we cannot use it for something else yet[[BR]] ... on (page 35) I will explain about the memory queues we have in Linux 2.4[[BR]] we have 3 "types" of pages in Linux 2.4[[BR]] active pages, inactive_dirty pages and inactive_clean pages[[BR]] we do page aging on the active pages[[BR]] and the inactive_dirty and inactive_clean pages are simply sitting there waiting to be used for something else[[BR]] ... now we go back to (page 34) [[BR]] having MORE inactive pages means that the system is better able to deal with big allocations and spikes in system load[[BR]] however, moving pages from the active to the inactive list and back is a lot of overhead[[BR]] so having LESS inactive pages is also good ...[[BR]] the solution Linux 2.4 takes is to see how much memory is being reclaimed for other purposes each second (averaged over a minute) and simply keep 1 second of allocations in the inactive queues[[BR]] I am pretty certain we can do this better for Linux 2.5, but nobody has had time yet to research this ...[[BR]] what Linux 2.4 also does is some light background scanning[[BR]] every minute or so all the cache memory is scanned[[BR]] and when the page->age of pages in the cache reaches 0, it will be moved to the inactive list[[BR]] so when the system gets a burst of activity again after 30 minutes, the system knows exactly which pages to swapout and which pages to keep in memory[[BR]] this fixes the biggest problems we have with Linux 2.2 VM[[BR]] ... because we have little time left, I will now go to the Out Of Memory (OOM) killer on (page 43)[[BR]] which will be the last part of the lecture, after this you can ask questions ;)[[BR]] ok, the OOM killer[[BR]] when memory *and* swap are full, there is not much you can do[[BR]] in fact, you can either sit there and wait until a program goes away, or you can kill a program and hope the system goes on running[[BR]] in Linux, the system always kills a process[[BR]] Linux 2.2 kills the process which is currently doing an allocation, which is very bad if it happens to be syslog or init[[BR]] Linux 2.4 tries to be smart and select a "good" process to kill[[BR]] for this, it looks at the size of the process (so killing 1 process gets us all the memory back we need)[[BR]] but also at if it is a root process or if the process has direct hardware access (it is very bad to kill these programs)[[BR]] and at the amount of time the process has been running and the CPU time it has used[[BR]] because it is better to kill a 5-second old Netscape than to kill your mathematical calculation which has been running for 3 weeks[[BR]] even if the Netscape is smaller ...[[BR]] killing the big calculation will mean the computer loses a lot of work, which is bad[[BR]] for Linux 2.5, I guess some people will also want to have the OOM killer look at which _user_ is doing bad things to the system[[BR]] but that are things to do in the future[[BR]] ... on (page 44) you can find some URLs with interesting information[[BR]] ... thank you for your time, if you have any questions, feel free to join the discussion on #qc[[BR]] ... this is the end of my talk, but I will be in #qc for a bit more time[[BR]] clap clap clap clap clap clap clpa clpar clap[[BR]] clap clap clap clap clap clap clpa clpar clap[[BR]] clap clap clap clap clap clap clpa clpar clap[[BR]] btw, for people interested in Linux kernel hacking we have a special IRC channel[[BR]] on irc.openprojects.net #kernelnewbies[[BR]] see [http://kernelnewbies.org/] for the #kernelnewbies website[[BR]] most of the time you can find me (and other kernel hackers) on that channel[[BR]] if you have some in-depth questions or find something interesting when reading my slides, you can always go there[[BR]] well, my friends[[BR]] feel free to continue discussing at #qc[[BR]] many thanks to Rik van riel and to all of you for comming here[[BR]] ---- CategoryDocs