{"id":434,"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:39","modified_gmt":"2020-08-22T20:26:39","slug":"this-is-the-page-title-toplevel-267","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=434","title":{"rendered":"The Buffer Cache"},"content":{"rendered":"\n<title>The Buffer Cache<\/title>\n<p>\n<img decoding=\"async\" src=\"buffer-cache.gif\">\n<p>\nFigure: The Buffer Cache<\/center>\n<a name=\"buffer-cache-figure\"><\/a>\n<p>\n<p>As the mounted file systems are used they generate a lot of requests to the block\ndevices to read and write data blocks.\nAll block data read and write requests are given to the device drivers in the form\nof <tt>buffer_head<\/tt> data structures via standard kernel routine calls.\nThese give all of the information that the block device drivers need; the device\nidentifier uniquely identifies the device and the block number tells the driver\nwhich block to read.\nAll block devices are viewed as linear collections of blocks of the same size.\nTo speed up access to the physical block devices, Linux maintains a cache of block\nbuffers.\nAll of the block buffers in the system are kept somewhere in this buffer cache, even\nthe new, unused buffers.\nThis cache is shared between all of the physical block devices; at any one time\nthere are many block buffers in the cache, belonging to any one of the system&#8217;s\nblock devices and often in many different states.\nIf valid data is available from the buffer cache this saves the system\nan access to a physical device.\nAny block buffer that has been used to read data from a block device or to write data\nto it goes into the buffer cache.\nOver time it may be removed from the cache to make way for a more deserving\nbuffer or it may remain in the cache as it is frequently accessed.\n<p>\nBlock buffers within the cache are uniquely identfied by the owning device\nidentifier and the block number of the buffer.\nThe buffer cache is composed of two functional parts.\nThe first part is the lists of free block buffers.\nThere is one list per supported buffer size and the system&#8217;s free block buffers\nare queued onto these lists when they are first created or when they have been\ndiscarded.\nThe currently supported buffer sizes are 512, 1024, 2048, 4096 and 8192 bytes.\nThe second functional part is the cache itself.\nThis is a hash table which is a vector of pointers to chains of buffers that have\nthe same hash index.\nThe hash index is generated from the owning device identifier and the block number\nof the data block.\nFigure&nbsp;<a href=\"#buffer-cache-figure\"\n> 9.7<\/a> shows the hash table together with a few entries.\nBlock buffers are either in one of the free lists or they are in the buffer cache.\nWhen they are in the buffer cache they are also queued onto Least Recently Used\n(LRU) lists.\nThere is an LRU list for each buffer type and\nthese are used by the system to perform work on buffers of a type, for example, writing\nbuffers with new data in them out to disk.\nThe buffer&#8217;s type reflects its state and Linux currently supports the following types:\n<dl compact>\n<p>\n\t<dt><b>clean<\/b><\/dt><dd> Unused, new buffers,\n\t<dt><b>locked<\/b><\/dt><dd> Buffers that are locked, waiting to be written,\n\t<dt><b>dirty<\/b><\/dt><dd> Dirty buffers.  These contain new, valid data, and will be written\n\t\tbut so far have not been scheduled to write,\n\t<dt><b>shared<\/b><\/dt><dd> Shared buffers,\n\t<dt><b>unshared<\/b><\/dt><dd> Buffers that were once shared but which are now not shared,\n<\/dl>\n<p>\nWhenever a file system needs to read a buffer from its underlying physical\ndevice, it trys to get a block from the buffer cache.\nIf it cannot get a buffer from the buffer cache, then it will get a clean one from\nthe appropriate sized free list and this new buffer will go into the buffer cache.\nIf the buffer that it needed is in the buffer cache, then it may or may not be up to\ndate.\nIf it is not up to date or if it is a new block buffer, the file system must request\nthat the device driver read the appropriate block of data from the disk.\n<p>\nLike all caches, the buffer cache must be maintained so that it runs efficiently\nand fairly allocates cache entries between the block devices using the buffer\ncache.\nLinux uses the <tt>bdflush<\/tt>\n<p>\nkernel daemon to perform a lot of housekeeping duties\non the cache but some happen automatically as a result of the cache being used.\n<p>\n","protected":false},"excerpt":{"rendered":"<p>The Buffer Cache Figure: The Buffer Cache As the mounted file systems are used they generate a lot of requests to the block devices to read and write data blocks. All block data read and write requests are given to &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=434\">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-434","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/434","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=434"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/434\/revisions"}],"predecessor-version":[{"id":735,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/434\/revisions\/735"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}