KernelNewbies:
   1 <Fernand0> we are very pleased to present you today Rik van Riel. 
   2 <Fernand0> He is a kernel hacker working on memory management.
   3 <Fernand0> Currently he is working at conectiva S.A. in Brazil. As all of you know,
   4 <Fernand0> it is a big Linux company from South America.
   5 <Fernand0> Appart from kernel hacking, he also runs the Linux-MM website and the
   6 <Fernand0> #kernelnewbies IRC channel on openprojects.net
   7 <Fernand0> You can find more about him at: www.surriel.com (there you can find, among
   8 <Fernand0> other things the slides of this talk at:
   9 <Fernand0> (http://www.surriel.com/lectures/mmtour.html)
  10 <Fernand0> He will talk here about memory management but other interests of him
  11 <Fernand0> are: High availability, filesystems and various other things  ...
  12 <Fernand0> The talk will be here, in #linux channel, Mr Riel suggested us to make
  13 <Fernand0> another channel (#qc -> questions channel) to write questions during the talk. 
  14 <Fernand0> Should you have any questions, comments, etc, just write them in #qc 
  15 <Fernand0> and Mr. Riel will reply.
  16 <Fernand0> Thank you to Mr. Riel for comming here and also to all of you
  17 <Fernand0> The title of his talk is:
  18 <Fernand0> Too little, too slow; memory management
  19 <Fernand0> Mr. Riel ...
  20 <riel> I guess it's time to begin .............
  21 <riel> ok, welcome everybody
  22 <riel> today I will be giving a talk about Linux memory management
  23 <riel> the slides are at http://www.surriel.com/lectures/mmtour.html
  24 <riel> we will begin with some of the slides introducing memory management and explaining why we need memory management
  25 <riel> if you have any questions about my talk, you can ask them in #qc
  26 <riel> in #qc, you can also discuss with each other the things I talk about
  27 <riel> this channel (#linux) is meant to be completely silent ... except for me of course ;)
  28 <riel> ......   (page 1)  .....
  29 <riel> let me begin by telling a little bit about what I am doing at the moment
  30 <riel> Conectiva is paying me to work on improving the Linux kernel full-time
  31 <riel> this means that I am working for Linux Torvalds and Alan Cox, but Conectiva is paying me  ;)    [thanks Conectiva :)]
  32 <riel> now I'll move on to the real talk ... (page 2)
  33 <riel> [for the new people ... http://www.surriel.com/lectures/mmtour.html for the slides]
  34 <riel> ok, I will begin by explaining about memory management
  35 <riel> most of the introduction I will skip
  36 <riel> but I will tell a few things about the memory hierarchy and about page faults and page replacement
  37 <riel> lets start with the picture on  (page 3)
  38 <riel> this picture represents the  "memory hierarchy"
  39 <riel> every computer has more kinds of memory
  40 <riel> very fast and very small memory
  41 <riel> and very big but very slow memory
  42 <riel> fast memory can be the registers or L1 cpu cache
  43 <riel> slow memory can be L2 cache or RAM
  44 <riel> and then you have hard disk, which is REALLY REALLY extremely slow  ;)
  45 <riel> when you see this picture, some people will ask themselves the question  "but why doesn't my machine have only fast memory?"
  46 <riel> or  "why don't we just run everything from fast memory?"
  47 <riel> the reason for this is that it is impossible to make very big fast memory
  48 <riel> and even if it was possible, it would simply be too expensive
  49 <riel> and you cannot run everything from fast memory because programs are simply too big
  50 <riel> now we've talked about "fast" and "slow" memory  ... (page 4)  tells us about different kinds of speeds
  51 <riel> you have "latency"  and  "throughput"
  52 <riel> ok, good to have everybody back
  53 <riel> if you look at  (page 6)  you can see how rediculously slow some memory things are
  54 <riel> ok, lets go to (page 7)
  55 <riel> "latency" ==  "if I ask for something, how long do I have to wait until I get the answer"
  56 <riel> "throughput"  ==  "how much data can I get per minute"
  57 <riel> <riel>  I think we do not have time to look at the L1 and L2 cache things
  58 <riel> <riel> so lets move on to the RAM management
  59 <riel> <riel> on (page 14)
  60 <riel> <riel> RAM is the slowest electronic memory in a computer
  61 <riel> <riel> it is often 100 times slower than the CPU core  (in latency)
  62 <riel> <riel> this is very very slow
  63 <riel> <riel> but when you see that the disk is 100000 times slower than RAM (in latency), suddenly memory looks fast again ...  ;)
  64 <riel> <riel> this enormous difference in speed makesit very important that you have the data in memory that you need
  65 <riel> -
  66 <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 continue
  67 <riel> <riel> on the other hand, everybody knows that you NEVER have enough memory  ;)
  68 <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)
  69 <riel> <
  70 <riel> ok, lets try this again  ;)
  71 <riel> the ping timeout probably lost my last 3 minutes of the talk
  72 <riel> 6<riel> so lets move on to the RAM management
  73 <riel> <riel> on (page 14)
  74 <riel> <riel> RAM is the slowest electronic memory in a computer
  75 <riel> <riel> it is often 100 times slower than the CPU core  (in latency)
  76 <riel> <riel> this is very very slow
  77 <riel> <riel> but when you see that the disk is 100000 times slower than RAM (in latency), suddenly memory looks fast again ...  ;)
  78 <riel> <-- Sadie has quit (Ping timeout for Sadie[orka.go2.pl])
  79 <riel> <riel> this enormous difference in speed makesit very important that you have the data in memory that you need
  80 <riel> -
  81 <riel> but as we all know, no computer ever has enough memory ...  ;)
  82 <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)
  83 <riel> lets move on to page 18
  84 <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 program
  85 <riel> then the Operating System (OS) gets the job of fixing this error and letting the program continue
  86 <riel> this trap is called a "PAGE FAULT"
  87 <riel> the OS fixes the job by getting a free page, putting the right data in that page and giving the page to that program
  88 <riel> after that the process continues just like nothing happened
  89 <riel> the ONLY big problem is that such a page fault easily takes 5 _million_ CPU cycles
  90 <riel> so you want to make sure you have as little page faults as possible
  91 <riel> the other problem is that you only have a "little bit" of memory in your machine
  92 <riel> and you run out of free memory very fast
  93 <riel> at that point, the OS needs to choose which data it keeps in memory and which data it swaps out
  94 <riel>  ..... lets move to (page 19)  of http://www.surriel.com/lectures/mmtour.html ....
  95 <riel> the "perfect" thing to do is to throw away (swap out) that data which will not be needed again for the longest time
  96 <riel> that way you have the longest time between page faults and the minimum number of page faults per minute  ... so the best system performance
  97 <riel> the only problem with this method is that you need to look into the future to do this
  98 <riel> and that isn't really possible ...   ;)))
  99 <riel> so we have to come up with other ideas that approximate this idea
 100 <riel> ......
 101 <riel> one idea is LRU ... we swap out the page which has not been used for the longest time
 102 <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"
 103 <riel> which really makes a lot of sense in most situation
 104 <riel> unfortunately, there are a few (very common) cases where LRU does the exact wrong thing
 105 <riel> take for example a system where somebody is burning a CD
 106 <riel> to burn a CD at 8-speed, you will be "streaming" your data at 1.2MB per second
 107 <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 program
 108 <riel> and your system will swap out the mail reader
 109 <riel> which is the exact WRONG thing to do
 110 <riel> because most likely you will use your mail reader before you will burn the same CD image again
 111 <riel> LFU would avoid this situation
 112 <riel> LFU swaps out the page which has been used least often
 113 <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)
 114 <riel> and LFU would nicely throw out the CD image data that has been used
 115 <riel> and keep the mail reader in memory
 116 <riel> in this situation LFU is almost perfect
 117 <riel> ... now we take another example  ;)
 118 <riel> if we look at GCC, you will see that it consists of 3 parts
 119 <riel> a preprocessor (cpp),  a compiler (cc1)  and an assembler (as)
 120 <riel> suppose you only have memory for one of these at a time
 121 <riel> cpp was running just fine and used its memory 400 times in the last minute
 122 <riel> now it is cc1's turn to do work
 123 <riel> but cc1 does not fit in memory at the same time as cpp
 124 <riel> and LFU will swap out parts of cc1 because cpp used its memory a lot ...
 125 <riel> and does the exact wrong thing ... cpp _stopped doing work_ a second ago
 126 <riel> and cc1 is now the important process to keep in memory
 127 <riel> ... this means that both LRU and LFU are good for some situations, but really bad for other situations
 128 <riel> I got a question if LRU or LFU is better ... the answer is none of them ;)
 129 <riel> what we really want is something that has the good parts of both LRU and LFU but not the bad parts
 130 <riel>   luckily we have such a solution ... page aging  (on page 20)
 131 <riel> page aging is really simple
 132 <riel> the system scans over all of memory, and each page has an "age"  (just points)
 133 <riel> if the page has been used since we scanned the page last, we increase the page age
 134 <riel> <Rob> riel: How do you measure when a page has last been "used"?
 135 <riel> ... umm yes ... I almost forgot about that part  ;))
 136 <riel> --- when a page is being used, the CPU sets a special bit, the "accessed bit" on the page (or the page table)
 137 <riel> --- and we only have to look at this bit to see if the page was used
 138 <riel> --- and after we look at the bit, we set it to 0 so it will change if is being used again after we scan
 139 <riel> so back to page aging now
 140 <riel> if the page was used since we last scan it, we make the page age bigger
 141 <riel> if the page was not used, we make the page age smaller
 142 <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 else
 143 <riel> now there are different ways of making the page age bigger and smaller
 144 <riel> for making it bigger, we just add a magic number to the page age  ... page->age += 3
 145 <riel> for making it smaller, we can do multiple things
 146 <riel> if we substract a magic number  (page->age -= 1), we will be close to LFU
 147 <riel> if we divide the page age by 2 (page->age /= 2), we will be close to LRU
 148 <riel> to be honest, I have absolutely no idea which of the two would work best
 149 <riel> or if we want system administrators to select this themselves, depending on what the system is doing
 150 <riel> page aging is used by Linux 2.0, FreeBSD and Linux 2.4
 151 <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 ... ;)
 152 <riel> ... so we put it back for Linux 2.4  ;))
 153 <riel> and another question:   <HoraPe> riel: what is linux using?
 154 <riel> HoraPe: Linux 2.0 uses the "page->age -= 1" strategy
 155 <riel> HoraPe: and Linux 2.4 uses the "page->age /= 2" strategy
 156 <riel> maybe the first strategy is better, maybe the second strategy is better
 157 <riel> if we have any volunteers who want to test this, talk to me after the lecture ;))
 158 <riel> I will now go on to (page 21) and talk about drop-behind
 159 <riel> most of you have probably heard about read-ahead
 160 <riel> where the system tries to read in data from a file *before* the program which uses the file needs it
 161 <riel> this sounds difficult, but if the program is just reading the file from beginning to end it is quite simple ...
 162 <riel> one problem is that this linear read will quickly fill up all memory if it is very fast
 163 <riel> and you do not want that, because you also have other things you want to do with your memory
 164 <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)
 165 <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)
 166 <riel> and in exchange for that, you remove the data the program will probably not need any more
 167 <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 perfect
 168 <riel> ... from the part about hard disks, I will skip almost everything
 169 <riel> ... only (page 23) I will discuss today
 170 <riel> as you probably know, hard disks are really strange devices
 171 <riel> they consist of a bunch of metal (or glass) plates with a magnetic coating on them which spin around at rediculously high speeds
 172 <riel> and there is a read-write arm which can seek across the disk at very low speeds
 173 <riel> the consequences of this design are that hard disks have a high throughput ... 20 MB/second is quite normal today
 174 <riel> this is fast enough to keep a modern CPU busy
 175 <riel> on the other hand, if you need some piece of data, your CPU will have to wait for 5 _million_ CPU cycles
 176 <riel> so hard disks are MUCH too slow if you're not reading the disk from beginning to end
 177 <riel> this means that so called "linear reads" are very fast
 178 <riel> while "random access" is extremely slow
 179 <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 IO
 180 <riel> because the linear IO is so fast the disk can do it in almost no time ;)
 181 <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 possible
 182 <riel> the "as possible" can not be too large, however ...
 183 <riel> if you have "only" 64 MB RAM in your machine, you probably do not want to do readahead in 2MB pieces
 184 <riel> because that way you will throw useful data out of memory, which you will need to read in again later, etc...
 185 <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 itself
 186 <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 better
 187 <riel> and of course, another way to make "disk accesses" fast is to make sure you do not access the disk
 188 <riel> you can do this if the data you need is already (or still) in memory
 189 <riel> Linux uses all "extra" memory as a disk cache in the hope that it can avoid disk reads
 190 <riel> and most other good operating systems do the same (FreeBSD for example)
 191 <riel> ... now I will go on with Linux memory management
 192 <riel> ... on (page 28) and furter
 193 <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.4
 194 <riel> and also the things that are still wrong in Linux 2.4 and need to be fixed later  ;)
 195 <riel> ok, another question:  <rcastro> riel: to keep data in memory, have you ever thought about compressed data in memory? 
 196 <riel> --- this is a good idea in some circumstances
 197 <riel> --- research has shown that compressed cache means that some systems can do with less disk IO and are faster
 198 <riel> --- on the other hand, for some other systems it makes the system slower because of the overhead of compression
 199 <riel> --- it really depends on what you do with your system if the "compressed cache" trick is worth it or not
 200 <riel> --- and it would be interesting to see as an option on Linux since it is really useful for some special systems
 201 <riel> --- for example, systems which do not have swap
 202 <riel> ... ok, lets move on to (page 31)
 203 <riel> Linux 2.2 swapout code is really simple
 204 <riel> (at least, that's the idea)
 205 <riel> the main function is do_try_to_free_pages()
 206 <riel> this function calls shrink_mmap(), swap_out() and a few other - less important - functions
 207 <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 scanned
 208 <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 it
 209 <riel> ... (page 32)
 210 <riel> this is a really simple system which works well if the system load is not too high
 211 <riel> but as soon as the load gets higher, it can completely break down for some reasons
 212 <riel> if, for example, the load on the system is very variable, we get problems
 213 <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)
 214 <riel> and then something happens in the system  (Netscape gets started)
 215 <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 last
 216 <riel> in that situation, the OS usually swaps out the 'wrong' pages
 217 <riel> and those wrong pages are needed again 5 milliseconds later
 218 <riel> which makes the OS swap out *other* wrong pages again, until everything settles down
 219 <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 ...
 220 <riel> another problem is that (in shrink_mmap) we scan and swap out pages from the same function
 221 <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 first
 222 <riel> shrink_mmap() will scan every page in memory and start disk IO for the pages that need to be written to disk
 223 <riel> after that it will start scanning at the beginning again
 224 <riel> and no page it sees has been used since the last time we scanned it, since kswapd was the only thing running
 225 <riel> at that point the system -again- starts swapping out the wrong pages
 226 <riel> a question:  <movement> is this the do_try_to_free_pages() printk we hear so much about on lkml ?
 227 <riel> --- this printk is called when do_try_to_free_pages() cannot find pages to swap out
 228 <riel> --- not when do_try_to_free_pages() swaps the wrong pages by accident
 229 <riel> --- so these things are not the same
 230 <riel> ... lets move on to (page 33) and see how we fix these problems in Linux 2.4
 231 <riel> the two big changes for Linux 2.4 are page aging and the separation of page aging and page writeback to disk
 232 <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 memory
 233 <riel> and the system will perform better when memory is getting full
 234 <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 yet
 235 <riel> ... on (page 35) I will explain about the memory queues we have in Linux 2.4
 236 <riel> we have 3 "types" of pages in Linux 2.4
 237 <riel> active pages, inactive_dirty pages and inactive_clean pages
 238 <riel> we do page aging on the active pages
 239 <riel> and the inactive_dirty and inactive_clean pages are simply sitting there waiting to be used for something else
 240 <riel> ... now we go back to (page 34)   <sorry>
 241 <riel> having MORE inactive pages means that the system is better able to deal with big allocations and spikes in system load
 242 <riel> however, moving pages from the active to the inactive list and back is a lot of overhead
 243 <riel> so having LESS inactive pages is also good ...
 244 <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 queues
 245 <riel> I am pretty certain we can do this better for Linux 2.5, but nobody has had time yet to research this ...
 246 <riel> what Linux 2.4 also does is some light background scanning
 247 <riel> every minute or so all the cache memory is scanned
 248 <riel> and when the page->age of pages in the cache reaches 0, it will be moved to the inactive list
 249 <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 memory
 250 <riel> this fixes the biggest problems we have with Linux 2.2 VM
 251 <riel> ... because we have little time left, I will now go to the Out Of Memory (OOM) killer on (page 43)
 252 <riel> which will be the last part of the lecture, after this you can ask questions ;)
 253 <riel> ok, the OOM killer
 254 <riel> when memory *and* swap are full, there is not much you can do
 255 <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 running
 256 <riel> in Linux, the system always kills a process
 257 <riel> Linux 2.2 kills the process which is currently doing an allocation, which is very bad if it happens to be syslog or init
 258 <riel> Linux 2.4 tries to be smart and select a "good" process to kill
 259 <riel> for this, it looks at the size of the process (so killing 1 process gets us all the memory back we need)
 260 <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)
 261 <riel> and at the amount of time the process has been running and the CPU time it has used
 262 <riel> because it is better to kill a 5-second old Netscape than to kill your mathematical calculation which has been running for 3 weeks
 263 <riel> even if the Netscape is smaller ...
 264 <riel> killing the big calculation will mean the computer loses a lot of work, which is bad
 265 <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 system
 266 <riel> but that are things to do in the future
 267 <riel> ... on (page 44) you can find some URLs with interesting information
 268 <riel> ... thank you for your time, if you have any questions, feel free to join the discussion on #qc
 269 <riel> ... this is the end of my talk, but I will be in #qc for a bit more time
 270 <Fernand0> clap clap clap clap clap clap clpa clpar clap
 271 <Fernand0> clap clap clap clap clap clap clpa clpar clap
 272 <Fernand0> clap clap clap clap clap clap clpa clpar clap
 273 <riel> btw, for people interested in Linux kernel hacking we have a special IRC channel
 274 <riel> on irc.openprojects.net   #kernelnewbies
 275 <riel> see http://kernelnewbies.org/  for the #kernelnewbies website
 276 <riel> most of the time you can find me (and other kernel hackers) on that channel
 277 <riel> if you have some in-depth questions or find something interesting when reading my slides, you can always go there
 278 <Fernand0> well, my friends
 279 <Fernand0> feel free to continue discussing at #qc
 280 <Fernand0> many thanks to Rik van riel and to all of you for comming here
 281 ----
 282 CategoryDocs
 283 

KernelNewbies: Documents/MemoryManagement (last edited 2017-12-30 01:30:34 by localhost)