{"id":457,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:40","modified_gmt":"2020-08-22T20:26:40","slug":"this-is-the-page-title-toplevel-290","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=457","title":{"rendered":"The Linux Page Cache"},"content":{"rendered":"\n<title>The Linux Page Cache<\/title>\n<p>\n<img decoding=\"async\" src=\"page-cache.gif\"><br \/>\n<p>\nFigure: The Linux Page Cache\n<p>\n<p>The role of the Linux page cache is to speed up access to files on disk.\nMemory mapped files are read a page at a time and these pages are stored in the page cache.\nThe figure above shows that\nthe page cache consists of the <tt>page_hash_table<\/tt>, a vector\nof pointers to <tt>mem_map_t<\/tt> data structures.\n<p>\nEach file in Linux is identified by a VFS <tt>inode<\/tt> data structure (described\npreviously). Each VFS <tt>inode<\/tt> is unique and fully describes\none and only one file.\nThe index into the page table is derived from the file&#8217;s VFS <tt>inode<\/tt> and the offset into\nthe file.\n<p>\nWhenever a page is read from a memory mapped file, for example when it needs to be brought\nback into memory during demand paging, the page is read through the page cache.\nIf the page is present in the cache, a pointer to the <tt>mem_map_t<\/tt> data structure\nrepresenting it is returned to the page fault handling code.\nOtherwise the page must be brought into memory from the file system that holds the image.\nLinux allocates a physical page and reads the page from the file on disk.\n<p>\nIf it is possible, Linux will initiate a read of the next page in the file.\nThis single page read ahead means that if the process is accessing the pages in the file\nserially, the next page will be waiting in memory for the process.\n<p>\nOver time the page cache grows as images are read and executed.\nPages will be removed from the cache as they are no longer needed, say as an image\nis no longer being used by any process.\nAs Linux uses memory it can start to run low on physical pages.\nIn this case Linux will reduce the size of the page cache.\n<p>\n<h3>Reducing the Size of the Page and Buffer Caches<\/h3>\n<p>\nThe pages held in the page and buffer caches are good candidates for being freed into the <tt>free_area<\/tt>\n vector.\nThe Page Cache, which contains pages of memory mapped files, may contain unneccessary pages that are filling\nup the system&#8217;s memory.\nLikewise the Buffer Cache, which contains buffers read from or being written to physical devices, may also contain\nunneeded buffers.\nWhen the physical pages in the system start to run out, discarding pages from these caches is relatively easy\nas it requires no writing to physical devices (unlike swapping pages out of memory).\nDiscarding these pages does not have too many harmful side effects other than making access to physical devices and\nmemory mapped files slower.\nHowever, if the discarding of pages from these caches is done fairly, all processes will suffer equally.\n<p>\nEvery time the kernel swap daemon tries to shrink these caches\nit examines a block of pages in the <tt>mem_map<\/tt> page vector to see if any\ncan be discarded from physical memory.\nThe size of the block of pages examined is higher if the kernel swap daemon is intensively swapping; that is if\nthe number of free pages in the system has fallen dangerously low.\nThe blocks of pages are examined in a cyclical manner; a different block of pages\nis examined each time an attempt is made to shrink the memory map.\nThis is known as the <em>clock<\/em> algorithm as, rather like the minute hand of a\nclock, the whole <tt>mem_map<\/tt> page vector is examined\na few pages at a time.\n<p>\nEach page being examined is checked to see if it is cached in either\nthe page cache or the buffer cache.\nYou should note that shared pages are not considered for discarding\nat this time and that a page cannot be in both caches at the\nsame time.\nIf the page is not in either cache then the next page in the\n<tt>mem_map<\/tt> page vector is examined.\n<p>\nPages are cached  in the buffer cache (or rather the buffers within the pages\nare cached) to make buffer allocation and deallocation more efficient.\nThe memory map shrinking code tries to free the buffers that are\ncontained within the page being examined.\n<p>\nIf all the buffers are freed, then the pages that contain them are\nalso be freed.\nIf the examined page is in the Linux page cache, it is removed from\nthe page cache and freed.\n<p>\nWhen enough pages have been freed on this attempt then the kernel\nswap daemon will wait until the next time it is periodically awakened.\nAs none of the freed pages were part of any process&#8217; virtual memory (they\nwere cached pages), then no page tables need updating.\nIf there were not enough cached pages discarded then the swap daemon\nwill try to swap out some shared pages.\n","protected":false},"excerpt":{"rendered":"<p>The Linux Page Cache Figure: The Linux Page Cache The role of the Linux page cache is to speed up access to files on disk. Memory mapped files are read a page at a time and these pages are stored &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=457\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-457","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=457"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/457\/revisions"}],"predecessor-version":[{"id":748,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/457\/revisions\/748"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}