Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ The difference between free command displays the buffers and cache     - Linux and SELinux Exploration Program Manager (Linux)

- Oracle 11g creates virtual private directory RMAN-06004 ORA-00942 error handling (Database)

- Ubuntu 15.04 installed JDK and configured as the default JDK (Linux)

- Build the first ASP.NET 5 Web project in Mac OS X Yosemite 10.10.3 (Server)

- Linux, security encryption to transfer files between machines (Linux)

- Use Linux firewall camouflage defense hacked (Linux)

- Linux mention the right notes (Linux)

- Usage logs Python library (Programming)

- Linux kernel RCU (Read Copy Update) lock Brief (Linux)

- Use the DBMS_SCHEDULER package to manage scheduled tasks (Database)

- CentOS6.5 setup Oracle 11gR2 startup (Database)

- Hadoop1.2.1 plug compilation (Server)

- How to run in support UDp Docker container JBoss data virtualization GA (Server)

- shellinabox: one uses AJAX Web-based terminal emulator (Linux)

- How to achieve the MySQL Oracle table associated with the two operations update (Database)

- RabbitMQ tutorial examples: RabbitMQ installation under Windows (Linux)

- Installation Android IDE development tools, Android Studio 1.5 under Ubuntu (Linux)

- Linux System Getting Started Learning: modify environment variables in Linux PATH (Linux)

- Flask deploy applications using Nginx on Ubuntu (Server)

- grep command usage (Linux)

 
         
  The difference between free command displays the buffers and cache
     
  Add Date : 2018-11-21      
         
         
         
  It is said that very few people can say clearly distinguish free command displays the "buffers" and "cached" between:

# Free
            total used free shared buffers cached
Mem: 3848656 2983016 865640 5312 324432 2024904
- / + Buffers / cache: 633680 3214976
Swap: 2031612 0 2031612

We listed first conclusion, if you are interested in the research process can continue reading the following paragraphs:
"Buffers", a block device (block device) occupied by cached pages, including: direct block device to read and write, as well as file system metadata (metadata) such as SuperBlock used cached pages;
"Cached" represents ordinary file data occupied by cached pages.

Here is the analysis: start start with strace command track free, see if I can find out how it is to calculate the "buffers" and "cached" in:

# Strace free
...
open ( "/ proc / meminfo", O_RDONLY) = 3
lseek (3,0, SEEK_SET) = 0
read (3, "MemTotal: 3848656 kB \ nMemF" ..., 2047) = 1170
...

Obviously free command is to read information from / proc / meminfo in, with the results of our direct read as:

# Cat / proc / meminfo
MemTotal: 3848656kB
MemFree: 865640kB
Buffers: 324432kB
Cached: 2024904kB
...
SwapTotal: 2031612kB
SwapFree: 2031612kB
...
Shmem: 5312kB
...

So / proc / meminfo The "Buffers" and "Cached" is how come it? This time can not be lazy, only to see the source code. Source code files are: fs / proc / meminfo.c, the function we are interested in: meminfo_proc_show (), that read:

"Buffers" from nr_blockdev_pages () return value.
"Cached" derived from the following formula:
global_page_state (NR_FILE_PAGES) - total_swapcache_pages - i.bufferram
In the above calculation formula cached, global_page_state (NR_FILE_PAGES) from vmstat [NR_FILE_PAGES], represents the sum of all the cached pages (page cache), which comprises:

Cached
buffers
Swap cache (swap cache)
Here briefly explain swap cache:

Those anonymous memory pages, such as user processes by malloc () allocated memory pages are not associated with any file (as opposed to backing storage disk-based file memory pages), if the paging swapping occurs, this type of memory page is written exchange zone. From an anonymous memory page is determined to be feed start, it was included in the swap cache, but will not necessarily be immediately written to the physical exchange zone, because Linux is the principle unless absolutely necessary, to avoid I / O. So swap cache included are sure you want to change page swapping, but not yet written anonymous physical memory page swap area.

vmstat [NR_FILE_PAGES] can be viewed through the / proc / vmstat, it represents the total number of all cached pages:

# Cat / proc / vmstat
...
nr_file_pages587334
...

Note that the above is nr_file_pages page as a unit, rather than the free command is kilobytes (KB), a page is equal to 4KB.

Nr_file_pages directly modify the kernel functions are:
__inc_zone_page_state (page, NR_FILE_PAGES) and
__dec_zone_page_state (page, NR_FILE_PAGES),
One for increasing and one for reducing.

Look at the "cached":
"Cached" is to remove the number of "buffers" and "swap cache" outside the cache page:
global_page_state (NR_FILE_PAGES) - total_swapcache_pages - i.bufferram
So the key is to understand the "buffers" what is the meaning.

Look at the "buffers":
We see from the source code, the return value "buffers" from nr_blockdev_pages (), we look at this function is doing:

longnr_blockdev_pages (void)
{
        structblock_device * bdev;
        longret = 0;
        spin_lock (& ​​bdev_lock);
        list_for_each_entry (bdev, & all_bdevs, bd_list) {
                ret + = bdev-> bd_inode-> i_mapping-> nrpages;
        }
        spin_unlock (& ​​bdev_lock);
        returnret;
}

This code is very simple, which means through all block devices (block device), the cumulative number of pages in each block i_mapping device inode, statistical get is buffers. So obviously, buffers and block devices is directly related.

So who will update the number of cached pages block device (nrpages) it? We continue to look down.

Search kernel source code is found, the final update mapping-> function is add_to_page_cache and __remove_from_page_cache nrpages fields:

staticinline intadd_to_page_cache (structpage * page,
                structaddress_space * mapping, pgoff_t offset, gfp_t gfp_mask)
{
        interror;
 
        __set_page_locked (page);
        error = add_to_page_cache_locked (page, mapping, offset, gfp_mask);
        if (unlikely (error))
                __clear_page_locked (page);
        returnerror;
}
 
voidremove_from_page_cache (structpage * page)
{
        structaddress_space * mapping = page-> mapping;
        void (* freepage) (structpage *) = NULL;
        structinode * inode = mapping-> host;
 
        BUG_ON (! PageLocked (page));
 
        if (IS_AOP_EXT (inode))
                freepage = EXT_AOPS (mapping-> a_ops) -> freepage;
 
        spin_lock_irq (& mapping-> tree_lock);
        __remove_from_page_cache (page);
        spin_unlock_irq (& mapping-> tree_lock);
        mem_cgroup_uncharge_cache_page (page);
 
        if (freepage)
                freepage (page);
}

These two functions are generic, block device and file inode can be called, as the update is a block device (buffers) or file (cached), depending on the call parameters variable mapping: If the corresponding mapping file inode, natural It will not affect the "buffers"; if the mapping corresponds to the block device, then the corresponding statistics will be reflected in the "buffers" in. Here we take a look at where the kernel will pass in a block device mapping.

Search the kernel source code found parameters ext4_readdir function call is passed page_cache_sync_readahead sb-> s_bdev-> bd_inode-> i_mapping, which s_bdev is a block device, that read directories (ext4_readdir) may increase when "buffers" values :

staticintext4_readdir (structfile * filp,
                        void * dirent, filldir_t filldir)
{
 
...
        structsuper_block * sb = inode-> i_sb;
...
                        if (! ra_has_index (& filp-> f_ra, index))
                                page_cache_sync_readahead (
                                        sb-> s_bdev-> bd_inode-> i_mapping,
                                        & Filp-> f_ra, filp,
                                        index, 1);
...
}

Continue pondering the above code, sb represents SuperBlock, belonging to the file system metadata (metadata), and suddenly everything suddenly: because metadata is not part of the file, there is no corresponding inode, therefore, cached page metadata involved in the operation can only use block device mapping, counted into the statistics of buffers.

Make a fork: ext4_readdir call () in page_cache_sync_readahead () apparently during the pre-read (read-ahead), read-ahead Why not use ordinary file inode of mapping, but the use of the underlying block device do? From the description recorded in the patch, this is a stopgap measure, look here, so do not get to the bottom.

By analogy, if the file contains indirect blocks (indirect blocks), because indirect block belonging to metadata, so mapping is walking a block device. View the source code, is really the case:

ext4_get_blocks
-> Ext4_ind_get_blocks
    -> Ext4_get_branch
        -> Sb_getblk
 
staticinline structbuffer_head *
sb_getblk (structsuper_block * sb, sector_t block)
{
        return__getblk (sb-> s_bdev, block, sb-> s_blocksize);
}

In this way, we will know, "buffers" is a block device (block device) occupied by cached pages, divided into two situations:

For block devices directly read and write operations;
File system metadata (metadata), such as SuperBlock.
verification:
Now let's do a test, to test the above conclusion. Now read the directory EXT4 file system will be used to "buffers", we use the find command scans the file system, observe the "buffers" to increase the case:

# Free
            total used free shared buffers cached
Mem: 3848656 2889508 959148 5316 263896 2023340
- / + Buffers / cache: 602272 3246384
Swap: 2031612 0 2031612
 
# Find / -name abc.def
 
# Free
            total used free shared buffers cached
Mem: 3848656 2984052 864604 5320 319612 2023348
- / + Buffers / cache: 641092 3207564
Swap: 2031612 0 2031612

Re-test directly read block device, observe the "buffers" to increase the phenomenon:

# Free
            total used free shared buffers cached
Mem: 3848656 3006944 841712 5316 331020 2028648
- / + Buffers / cache: 647276 3201380
Swap: 2031612 0 2031612
 
# Dd if = / dev / sda1 of = / dev / null count = 2000
2000 + 0records in
2000 + 0records out
1024000bytes (1.0MB) copied, 0.026413s, 38.8MB / s
 
# Free
            total used free shared buffers cached
Mem: 3848656 3007704 840952 5316 331872 2028692
- / + Buffers / cache: 647140 3201516
Swap: 2031612 0 2031612

in conclusion:
free command displays the "buffers", a block device (block device) occupied by cached pages, including direct block device to read and write, as well as file system metadata (metadata) as SuperBlock used cached pages;
And "cached" indicates a regular file occupied the cache page.
     
         
         
         
  More:      
 
- After you change the GRUB boot disk partition repair (Linux)
- Android LayoutInflater source parsing (Programming)
- The Linux disguised as windows to make the system more secure (Linux)
- Server Security Analysis attack on Linux (Linux)
- GitLab installation configuration notes (Linux)
- Object-oriented language Java some of the basic features (Programming)
- Thunderbird 24.4.2 has been released for use with PPA updates (Linux)
- OpenJDK7 source compiler installation on CentOS 6.5 (Linux)
- Necessity in Java packages (Programming)
- Ubuntu 14.04 can be used to create a WIFI hotspot for Android (Linux)
- Function Getting the Linux shell (Programming)
- PLSQL often lose resolution process automatically disconnect the connection (Database)
- Xmanager Remote Desktop login CentOS 6.5 (Linux)
- The Zabbix2.4.5 source compiler installation under Ubuntu 14.04 (Server)
- Linux redirection and piping (Linux)
- ARM platform compiler installation Golang (Linux)
- To help you easily protect the Linux System (Linux)
- Oracle table space create large files (Database)
- To execute the cp command prompt type skip folder under CentOS (Linux)
- To modify the existing user ID and comments GPG key (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.