Linux cache and buffer "Go"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/turkeyzhou/article/details/6426738

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Access to files and access to the device under Linux is usually increased by the cache, which is the default behavior of the system. The cache needs to consume our memory, although this memory can be released voluntarily through commands such as Echo 3>/proc/sys/vm/drop_caches. But sometimes we still need to understand who is consuming our memory.

Let's start by understanding the memory usage:

[[email protected] ~]# free       total used       free     shared    buffers     cachedmem:      24676836     626568   24050268          0      30884     508312-/+ buffers/cache:      87372   24589464Swap:      8385760
Page cache and buffer cache have always been two more confusing concepts, and there are a lot of people on the internet arguing and guessing what the difference is between the two caches, and the discussion finally has not been a uniform and correct conclusion, during my time at work,
The concept of page cache and buffer cache has bothered me once, but the two concepts are actually very clear when analyzed carefully. If you can understand the nature of these two caches, then we may be more handy when analyzing IO issues.
The page cache is actually for the file system, the file cache, and the data at the file level is cached to page cache. The logical layer of the file needs to be mapped to the actual physical disk, and this mapping is done by the file system. When the page cache data
When it needs to be refreshed, the data in the page cache is given to buffer cache, but this processing has become simple after the 2.6 kernel, with no real cache operation.
Buffer cache is a caching of disk blocks that, in the absence of a filesystem, caches data directly into the buffer cache, for example, the file system's metadata is cached in buffer cache.
In short, page cache is used to cache file data, and buffer cache is used to cache disk data. In the case of a file system, the file operation, then the data will be cached to page cache, if the direct use of DD and other tools to read and write to the disk, then the data will be
Buffer cache to buffer cache add a point, at the filesystem layer each device will be assigned a Def_blk_ops file operation method, this is the operation method of the device, there will be a radix tree under the inode of each device, this radix tree
The page of the cached data will be placed below. The page number will be displayed in the buffer column of the top program. If the device does a file system,
Then an inode is generated, and this inode allocates operations such as Ext3_ops, which are methods of the filesystem, and there is also a radix tree underneath the inode,
The page of the file is cached here, and the number of cached pages is counted in the cache column of the top program. As can be seen from the above analysis, the buffer cache and page cache in the 2.6 kernel
The processing is consistent, but there is a conceptual difference, page cache for file Cache,buffer is the cache for disk block data, that's all.

With the great Systemtap, we can use the Stap script to understand who is consuming our cache:

#这个命令行用来调查谁在加数据入page_cache [[email protected] ~]# stap-e ' probe Vfs.add_to_page_cache {printf ("dev=%d, devname=%s, ino= %d, index=%d, nrpages=%d/n ", Dev, devname, Ino, Index, nrpages)} ' ... dev=2, devname=n/a, ino=0, index=2975, Nrpages=1777de v=2, devname=n/a, ino=0, index=3399, nrpages=2594dev=2, devname=n/a, ino=0, index=3034, nrpages=1778dev=2, Devname=N/A, Ino=0, index=3618, nrpages=2595dev=2, devname=n/a, ino=0, index=1694, nrpages=106dev=2, devname=n/a, ino=0, index=1703, nrpages=107dev=2, devname=n/a, ino=0, index=1810, nrpages=210dev=2, devname=n/a, ino=0, index=1812, nrpages=211 ...

At this time we copy a large file:

[[email protected] ~]$ CP huge_foo.file bar# This time we can see the contents of the file is added to the cache: ... dev=8388614, Devname=sda6, ino= 2399271, index=39393, nrpages=39393dev=8388614, Devname=sda6, ino=2399271, index=39394, nrpages=39394dev=8388614, Devname=sda6, ino=2399271, index=39395, nrpages=39395dev=8388614, Devname=sda6, ino=2399271, index=39396, nrpages= 39396dev=8388614, Devname=sda6, ino=2399271, index=39397, nrpages=39397dev=8388614, Devname=sda6, ino=2399271, index= 39398, nrpages=39398dev=8388614, Devname=sda6, ino=2399271, index=39399, nrpages=39399dev=8388614, Devname=sda6, ino= 2399271, index=39400, nrpages=39400dev=8388614, Devname=sda6, ino=2399271, index=39401, nrpages=39401dev=8388614, Devname=sda6, ino=2399271, index=39402, nrpages=39402dev=8388614, Devname=sda6, ino=2399271, index=39403, nrpages= 39403dev=8388614, Devname=sda6, ino=2399271, index=39404, nrpages=39404dev=8388614, Devname=sda6, ino=2399271, index= 39405, nrpages=39405dev=8388614, Devname=sda6, ino=2399271, index=39406, nrpages=39406dev=8388614, Devname=sda6, ino=2399271, index=39407, nrpages=39407dev=8388614, Devname=sda6, ino=2399271, index= 39408, nrpages=39408dev=8388614, Devname=sda6, ino=2399271, index=39409, nrpages=39409dev=8388614, Devname=sda6, ino= 2399271, index=39410, nrpages=39410dev=8388614, Devname=sda6, ino=2399271, index=39411, nrpages=39411 ...

Linux cache and buffer "go"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.