KernelNewbies:

<Fernand0> we are very pleased to present you today Rik van Riel. BR <Fernand0> He is a kernel hacker working on memory management.BR <Fernand0> Currently he is working at conectiva S.A. in Brazil. As all of you know,BR <Fernand0> it is a big Linux company from South America.BR <Fernand0> Appart from kernel hacking, he also runs the Linux-MM website and theBR <Fernand0> #kernelnewbies IRC channel on openprojects.netBR <Fernand0> You can find more about him at: www.surriel.com (there you can find, amongBR <Fernand0> other things the slides of this talk at:BR <Fernand0> ([http://www.surriel.com/lectures/mmtour.html])[[BR]] <Fernand0> He will talk here about memory management but other interests of himBR <Fernand0> are: High availability, filesystems and various other things ...BR <Fernand0> The talk will be here, in #linux channel, Mr Riel suggested us to makeBR <Fernand0> another channel (#qc -> questions channel) to write questions during the talk. BR <Fernand0> Should you have any questions, comments, etc, just write them in #qc BR <Fernand0> and Mr. Riel will reply.BR <Fernand0> Thank you to Mr. Riel for comming here and also to all of youBR <Fernand0> The title of his talk is:BR <Fernand0> Too little, too slow; memory managementBR <Fernand0> Mr. Riel ...BR <riel> I guess it's time to begin .............BR <riel> ok, welcome everybodyBR <riel> today I will be giving a talk about Linux memory managementBR <riel> the slides are at [http://www.surriel.com/lectures/mmtour.html][[BR]] <riel> we will begin with some of the slides introducing memory management and explaining why we need memory managementBR <riel> if you have any questions about my talk, you can ask them in #qcBR <riel> in #qc, you can also discuss with each other the things I talk aboutBR <riel> this channel (#linux) is meant to be completely silent ... except for me of course ;)BR <riel> ...... (page 1) .....BR <riel> let me begin by telling a little bit about what I am doing at the momentBR <riel> Conectiva is paying me to work on improving the Linux kernel full-timeBR <riel> this means that I am working for Linux Torvalds and Alan Cox, but Conectiva is paying me ;) [thanks Conectiva :)]BR <riel> now I'll move on to the real talk ... (page 2)BR <riel> [for the new people ... [http://www.surriel.com/lectures/mmtour.html] for the slides]BR <riel> ok, I will begin by explaining about memory managementBR <riel> most of the introduction I will skipBR <riel> but I will tell a few things about the memory hierarchy and about page faults and page replacementBR <riel> lets start with the picture on (page 3)BR <riel> this picture represents the "memory hierarchy"BR <riel> every computer has more kinds of memoryBR <riel> very fast and very small memoryBR <riel> and very big but very slow memoryBR <riel> fast memory can be the registers or L1 cpu cacheBR <riel> slow memory can be L2 cache or RAMBR <riel> and then you have hard disk, which is REALLY REALLY extremely slow ;)BR <riel> when you see this picture, some people will ask themselves the question "but why doesn't my machine have only fast memory?"BR <riel> or "why don't we just run everything from fast memory?"BR <riel> the reason for this is that it is impossible to make very big fast memoryBR <riel> and even if it was possible, it would simply be too expensiveBR <riel> and you cannot run everything from fast memory because programs are simply too bigBR <riel> now we've talked about "fast" and "slow" memory ... (page 4) tells us about different kinds of speedsBR <riel> you have "latency" and "throughput"BR <riel> ok, good to have everybody backBR <riel> if you look at (page 6) you can see how rediculously slow some memory things areBR <riel> ok, lets go to (page 7)BR <riel> "latency" == "if I ask for something, how long do I have to wait until I get the answer"BR <riel> "throughput" == "how much data can I get per minute"BR <riel> <riel> I think we do not have time to look at the L1 and L2 cache thingsBR <riel> <riel> so lets move on to the RAM managementBR <riel> <riel> on (page 14)BR <riel> <riel> RAM is the slowest electronic memory in a computerBR <riel> <riel> it is often 100 times slower than the CPU core (in latency)BR <riel> <riel> this is very very slowBR <riel> <riel> but when you see that the disk is 100000 times slower than RAM (in latency), suddenly memory looks fast again ... ;)BR <riel> <riel> this enormous difference in speed makesit very important that you have the data in memory that you needBR <riel> -BR <riel> 6<riel> 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 continueBR <riel> <riel> on the other hand, everybody knows that you NEVER have enough memory ;)BR <riel> <riel> 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 <riel> <BR <riel> ok, lets try this again ;)BR <riel> the ping timeout probably lost my last 3 minutes of the talkBR <riel> 6<riel> so lets move on to the RAM managementBR <riel> <riel> on (page 14)BR <riel> <riel> RAM is the slowest electronic memory in a computerBR <riel> <riel> it is often 100 times slower than the CPU core (in latency)BR <riel> <riel> this is very very slowBR <riel> <riel> but when you see that the disk is 100000 times slower than RAM (in latency), suddenly memory looks fast again ... ;)BR <riel> <-- Sadie has quit (Ping timeout for Sadie[orka.go2.pl])BR <riel> <riel> this enormous difference in speed makesit very important that you have the data in memory that you needBR <riel> -BR <riel> but as we all know, no computer ever has enough memory ... ;)BR <riel> 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 <riel> lets move on to page 18BR <riel> 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 programBR <riel> then the Operating System (OS) gets the job of fixing this error and letting the program continueBR <riel> this trap is called a "PAGE FAULT"BR <riel> the OS fixes the job by getting a free page, putting the right data in that page and giving the page to that programBR <riel> after that the process continues just like nothing happenedBR <riel> the ONLY big problem is that such a page fault easily takes 5 _million_ CPU cyclesBR <riel> so you want to make sure you have as little page faults as possibleBR <riel> the other problem is that you only have a "little bit" of memory in your machineBR <riel> and you run out of free memory very fastBR <riel> at that point, the OS needs to choose which data it keeps in memory and which data it swaps outBR <riel> ..... lets move to (page 19) of [http://www.surriel.com/lectures/mmtour.html] ....BR <riel> the "perfect" thing to do is to throw away (swap out) that data which will not be needed again for the longest timeBR <riel> that way you have the longest time between page faults and the minimum number of page faults per minute ... so the best system performanceBR <riel> the only problem with this method is that you need to look into the future to do thisBR <riel> and that isn't really possible ... ;)))BR <riel> so we have to come up with other ideas that approximate this ideaBR <riel> ......BR <riel> one idea is LRU ... we swap out the page which has not been used for the longest timeBR <riel> 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 <riel> which really makes a lot of sense in most situationBR <riel> unfortunately, there are a few (very common) cases where LRU does the exact wrong thingBR <riel> take for example a system where somebody is burning a CDBR <riel> to burn a CD at 8-speed, you will be "streaming" your data at 1.2MB per secondBR <riel> 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 programBR <riel> and your system will swap out the mail readerBR <riel> which is the exact WRONG thing to doBR <riel> because most likely you will use your mail reader before you will burn the same CD image againBR <riel> LFU would avoid this situationBR <riel> LFU swaps out the page which has been used least oftenBR <riel> 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 <riel> and LFU would nicely throw out the CD image data that has been usedBR <riel> and keep the mail reader in memoryBR <riel> in this situation LFU is almost perfectBR <riel> ... now we take another example ;)BR <riel> if we look at GCC, you will see that it consists of 3 partsBR <riel> a preprocessor (cpp), a compiler (cc1) and an assembler (as)BR <riel> suppose you only have memory for one of these at a timeBR <riel> cpp was running just fine and used its memory 400 times in the last minuteBR <riel> now it is cc1's turn to do workBR <riel> but cc1 does not fit in memory at the same time as cppBR <riel> and LFU will swap out parts of cc1 because cpp used its memory a lot ...BR <riel> and does the exact wrong thing ... cpp _stopped doing work_ a second agoBR <riel> and cc1 is now the important process to keep in memoryBR <riel> ... this means that both LRU and LFU are good for some situations, but really bad for other situationsBR <riel> I got a question if LRU or LFU is better ... the answer is none of them ;)BR <riel> what we really want is something that has the good parts of both LRU and LFU but not the bad partsBR <riel> luckily we have such a solution ... page aging (on page 20)BR <riel> page aging is really simpleBR <riel> the system scans over all of memory, and each page has an "age" (just points)BR <riel> if the page has been used since we scanned the page last, we increase the page ageBR <riel> <Rob> riel: How do you measure when a page has last been "used"?BR <riel> ... umm yes ... I almost forgot about that part ;))BR <riel> --- when a page is being used, the CPU sets a special bit, the "accessed bit" on the page (or the page table)BR <riel> --- and we only have to look at this bit to see if the page was usedBR <riel> --- and after we look at the bit, we set it to 0 so it will change if is being used again after we scanBR <riel> so back to page aging nowBR <riel> if the page was used since we last scan it, we make the page age biggerBR <riel> if the page was not used, we make the page age smallerBR <riel> and when the page age reaches 0, the page is a candidate for swapout ... we remove the data and use the memory for something elseBR <riel> now there are different ways of making the page age bigger and smallerBR <riel> for making it bigger, we just add a magic number to the page age ... page->age += 3BR <riel> for making it smaller, we can do multiple thingsBR <riel> if we substract a magic number (page->age -= 1), we will be close to LFUBR <riel> if we divide the page age by 2 (page->age /= 2), we will be close to LRUBR <riel> to be honest, I have absolutely no idea which of the two would work bestBR <riel> or if we want system administrators to select this themselves, depending on what the system is doingBR <riel> page aging is used by Linux 2.0, FreeBSD and Linux 2.4BR <riel> 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 <riel> ... so we put it back for Linux 2.4 ;))BR <riel> and another question: <HoraPe> riel: what is linux using?BR <riel> HoraPe: Linux 2.0 uses the "page->age -= 1" strategyBR <riel> HoraPe: and Linux 2.4 uses the "page->age /= 2" strategyBR <riel> maybe the first strategy is better, maybe the second strategy is betterBR <riel> if we have any volunteers who want to test this, talk to me after the lecture ;))BR <riel> I will now go on to (page 21) and talk about drop-behindBR <riel> most of you have probably heard about read-aheadBR <riel> where the system tries to read in data from a file *before* the program which uses the file needs itBR <riel> this sounds difficult, but if the program is just reading the file from beginning to end it is quite simple ...BR <riel> one problem is that this linear read will quickly fill up all memory if it is very fastBR <riel> and you do not want that, because you also have other things you want to do with your memoryBR <riel> 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 <riel> 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 <riel> and in exchange for that, you remove the data the program will probably not need any moreBR <riel> 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 perfectBR <riel> ... from the part about hard disks, I will skip almost everythingBR <riel> ... only (page 23) I will discuss todayBR <riel> as you probably know, hard disks are really strange devicesBR <riel> they consist of a bunch of metal (or glass) plates with a magnetic coating on them which spin around at rediculously high speedsBR <riel> and there is a read-write arm which can seek across the disk at very low speedsBR <riel> the consequences of this design are that hard disks have a high throughput ... 20 MB/second is quite normal todayBR <riel> this is fast enough to keep a modern CPU busyBR <riel> on the other hand, if you need some piece of data, your CPU will have to wait for 5 _million_ CPU cyclesBR <riel> so hard disks are MUCH too slow if you're not reading the disk from beginning to endBR <riel> this means that so called "linear reads" are very fastBR <riel> while "random access" is extremely slowBR <riel> 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 IOBR <riel> because the linear IO is so fast the disk can do it in almost no time ;)BR <riel> the normal optimisation for this is "IO clustering", where the OS reads (or writes) as much data in one place of the disk as possibleBR <riel> the "as possible" can not be too large, however ...BR <riel> if you have "only" 64 MB RAM in your machine, you probably do not want to do readahead in 2MB piecesBR <riel> because that way you will throw useful data out of memory, which you will need to read in again later, etc...BR <riel> 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 itselfBR <riel> 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 betterBR <riel> and of course, another way to make "disk accesses" fast is to make sure you do not access the diskBR <riel> you can do this if the data you need is already (or still) in memoryBR <riel> Linux uses all "extra" memory as a disk cache in the hope that it can avoid disk readsBR <riel> and most other good operating systems do the same (FreeBSD for example)BR <riel> ... now I will go on with Linux memory managementBR <riel> ... on (page 28) and furterBR <riel> 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.4BR <riel> and also the things that are still wrong in Linux 2.4 and need to be fixed later ;)BR <riel> ok, another question: <rcastro> riel: to keep data in memory, have you ever thought about compressed data in memory? BR <riel> --- this is a good idea in some circumstancesBR <riel> --- research has shown that compressed cache means that some systems can do with less disk IO and are fasterBR <riel> --- on the other hand, for some other systems it makes the system slower because of the overhead of compressionBR <riel> --- it really depends on what you do with your system if the "compressed cache" trick is worth it or notBR <riel> --- and it would be interesting to see as an option on Linux since it is really useful for some special systemsBR <riel> --- for example, systems which do not have swapBR <riel> ... ok, lets move on to (page 31)BR <riel> Linux 2.2 swapout code is really simpleBR <riel> (at least, that's the idea)BR <riel> the main function is do_try_to_free_pages()BR <riel> this function calls shrink_mmap(), swap_out() and a few other - less important - functionsBR <riel> 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 scannedBR <riel> 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 itBR <riel> ... (page 32)BR <riel> this is a really simple system which works well if the system load is not too highBR <riel> but as soon as the load gets higher, it can completely break down for some reasonsBR <riel> if, for example, the load on the system is very variable, we get problemsBR <riel> 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 <riel> and then something happens in the system (Netscape gets started)BR <riel> but the OS has no idea which page to swap out, since all pages were used in the last 30 minutes, when we scanned lastBR <riel> in that situation, the OS usually swaps out the 'wrong' pagesBR <riel> and those wrong pages are needed again 5 milliseconds laterBR <riel> which makes the OS swap out *other* wrong pages again, until everything settles downBR <riel> 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 <riel> another problem is that (in shrink_mmap) we scan and swap out pages from the same functionBR <riel> 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 firstBR <riel> shrink_mmap() will scan every page in memory and start disk IO for the pages that need to be written to diskBR <riel> after that it will start scanning at the beginning againBR <riel> and no page it sees has been used since the last time we scanned it, since kswapd was the only thing runningBR <riel> at that point the system -again- starts swapping out the wrong pagesBR <riel> a question: <movement> is this the do_try_to_free_pages() printk we hear so much about on lkml ?BR <riel> --- this printk is called when do_try_to_free_pages() cannot find pages to swap outBR <riel> --- not when do_try_to_free_pages() swaps the wrong pages by accidentBR <riel> --- so these things are not the sameBR <riel> ... lets move on to (page 33) and see how we fix these problems in Linux 2.4BR <riel> the two big changes for Linux 2.4 are page aging and the separation of page aging and page writeback to diskBR <riel> 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 memoryBR <riel> and the system will perform better when memory is getting fullBR <riel> 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 yetBR <riel> ... on (page 35) I will explain about the memory queues we have in Linux 2.4BR <riel> we have 3 "types" of pages in Linux 2.4BR <riel> active pages, inactive_dirty pages and inactive_clean pagesBR <riel> we do page aging on the active pagesBR <riel> and the inactive_dirty and inactive_clean pages are simply sitting there waiting to be used for something elseBR <riel> ... now we go back to (page 34) <sorry>BR <riel> having MORE inactive pages means that the system is better able to deal with big allocations and spikes in system loadBR <riel> however, moving pages from the active to the inactive list and back is a lot of overheadBR <riel> so having LESS inactive pages is also good ...BR <riel> 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 queuesBR <riel> I am pretty certain we can do this better for Linux 2.5, but nobody has had time yet to research this ...BR <riel> what Linux 2.4 also does is some light background scanningBR <riel> every minute or so all the cache memory is scannedBR <riel> and when the page->age of pages in the cache reaches 0, it will be moved to the inactive listBR <riel> 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 memoryBR <riel> this fixes the biggest problems we have with Linux 2.2 VMBR <riel> ... because we have little time left, I will now go to the Out Of Memory (OOM) killer on (page 43)BR <riel> which will be the last part of the lecture, after this you can ask questions ;)BR <riel> ok, the OOM killerBR <riel> when memory *and* swap are full, there is not much you can doBR <riel> 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 runningBR <riel> in Linux, the system always kills a processBR <riel> Linux 2.2 kills the process which is currently doing an allocation, which is very bad if it happens to be syslog or initBR <riel> Linux 2.4 tries to be smart and select a "good" process to killBR <riel> for this, it looks at the size of the process (so killing 1 process gets us all the memory back we need)BR <riel> 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 <riel> and at the amount of time the process has been running and the CPU time it has usedBR <riel> because it is better to kill a 5-second old Netscape than to kill your mathematical calculation which has been running for 3 weeksBR <riel> even if the Netscape is smaller ...BR <riel> killing the big calculation will mean the computer loses a lot of work, which is badBR <riel> 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 systemBR <riel> but that are things to do in the futureBR <riel> ... on (page 44) you can find some URLs with interesting informationBR <riel> ... thank you for your time, if you have any questions, feel free to join the discussion on #qcBR <riel> ... this is the end of my talk, but I will be in #qc for a bit more timeBR <Fernand0> clap clap clap clap clap clap clpa clpar clapBR <Fernand0> clap clap clap clap clap clap clpa clpar clapBR <Fernand0> clap clap clap clap clap clap clpa clpar clapBR <riel> btw, for people interested in Linux kernel hacking we have a special IRC channelBR <riel> on irc.openprojects.net #kernelnewbiesBR <riel> see [http://kernelnewbies.org/] for the #kernelnewbies websiteBR <riel> most of the time you can find me (and other kernel hackers) on that channelBR <riel> if you have some in-depth questions or find something interesting when reading my slides, you can always go thereBR <Fernand0> well, my friendsBR <Fernand0> feel free to continue discussing at #qcBR <Fernand0> many thanks to Rik van riel and to all of you for comming hereBR


CategoryDocs

KernelNewbies: Documents/MemoryManagement (last edited 2006-08-15 04:43:28 by h-64-105-74-181)