Measure the test taker's knowledge about the metrics in vmstat by reading the source code.

Source: Internet
Author: User

Measure the test taker's knowledge about the metrics in vmstat by reading the source code.

The vmstat-a command can see active memory and inactive memory, but what do they mean?

  1. $ vmstat-a
  2. procs -----------memory-------------swap-------io-----system--------cpu-----
  3. r b swpd free inact active si so bi bo in cs us sy id wa st
  4. 1013809631956013724081757848002323109900

Their meanings are described in manpage, but not in detail:

inact: the amount of inactive memory. (-a option)
active: the amount of active memory. (-a option)

Here we try to understand its meaning accurately. By reading the vmstat source code (vmstat. c and proc/sysinfo. c), we know that the vmstat command is directly from/proc/meminfoData obtained in:

  1. $ grep-i act /proc/meminfo
  2. Active:1767928 kB
  3. Inactive:1373760 kB

While/proc/meminfoThe data is generated in the following kernel functions:

  1. fs/proc/meminfo.c:
  2. ==================
  3. 0023staticint meminfo_proc_show(struct seq_file *m,void*v)
  4. 0024{
  5. ...
  6. 0032unsignedlong pages[NR_LRU_LISTS];
  7. ...
  8. 0051for(lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
  9. 0052 pages[lru]= global_page_state(NR_LRU_BASE + lru);
  10. ...
  11. 0095"Active: %8lu kB\n"
  12. 0096"Inactive: %8lu kB\n"
  13. 0097"Active(anon): %8lu kB\n"
  14. 0098"Inactive(anon): %8lu kB\n"
  15. 0099"Active(file): %8lu kB\n"
  16. 0100"Inactive(file): %8lu kB\n"
  17. ...
  18. 0148 K(pages[LRU_ACTIVE_ANON]+ pages[LRU_ACTIVE_FILE]),
  19. 0149 K(pages[LRU_INACTIVE_ANON]+ pages[LRU_INACTIVE_FILE]),
  20. 0150 K(pages[LRU_ACTIVE_ANON]),
  21. 0151 K(pages[LRU_INACTIVE_ANON]),
  22. 0152 K(pages[LRU_ACTIVE_FILE]),
  23. 0153 K(pages[LRU_INACTIVE_FILE]),

This code counts all LRU lists. Active Memory equals to the sum of ACTIVE_ANON and ACTIVE_FILE, and Inactive Memory equals to the sum of INACTIVE_ANON and INACTIVE_FILE.

LRU list is the data structure Used by the Page Frame Reclaiming Algorithm in the Linux kernel. LRU is the abbreviation of Least Recently Used. The core idea of this algorithm is: Recycling pages should be the least recently used.

To achieve this goal, the ideal condition is that each page has an age item, which is used to record the last page access time. Unfortunately, x86 CPU hardware does not support this feature, the x86 CPU can only set an Access Bit when accessing the page, and the time cannot be recorded.

Therefore, the Linux kernel uses a compromise: it uses the LRU list and places the accessed page at the beginning of the column, the closer the end of a column, the longer the page has not been accessed. In this way, although the access time cannot be recorded, however, you can easily find the longest page in the LRU list.

In Linux, two types of LRU list are designed: active list and inactive list. The accessed pages are put into active list, and those that have not been accessed for a long time are put into inactive list, in this way, it is easy to recycle the page from inactive list. The kernel thread kswapd periodically moves the qualified pages in the active list to the inactive list.refill_inactive_zone()Completed. This code counts all LRU lists. Active Memory equals to the sum of ACTIVE_ANON and ACTIVE_FILE, and Inactive Memory equals to the sum of INACTIVE_ANON and INACTIVE_FILE.

LRU list

LRU_list

The active/inactive memory displayed by vmstat is the memory size in the active list and inactive list respectively. If the inactive list is large, many pages can be recycled when necessary. If the inactive list is small, there are not many pages that can be recycled.

Active/inactive memory is used for the memory occupied by user processes. The memory occupied by the kernel (including slab) is not included.

The ACTIVE_ANON and ACTIVE_FILE in the source code indicate anonymous pages and mapped pages respectively. Memory pages of user processes are divided into two types: Memory pages associated with files (such as memory pages corresponding to program files and data files) and memory irrelevant to files (such as the process stack, memory applied with malloc). The former is called file pages or mapped pages, and the latter is called anonymous pages. When a page-in or page-out occurs, file pages are read or written from the corresponding file. When a page-in or page-out occurs, read/write operations on the swap area.

This article permanently updates the link address:

Related Article

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.