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?
$ vmstat-a
procs -----------memory-------------swap-------io-----system--------cpu-----
r b swpd free inact active si so bi bo in cs us sy id wa st
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/meminfo
Data obtained in:
$ grep-i act /proc/meminfo
Active:1767928 kB
Inactive:1373760 kB
While/proc/meminfo
The data is generated in the following kernel functions:
fs/proc/meminfo.c:
==================
0023staticint meminfo_proc_show(struct seq_file *m,void*v)
0024{
...
0032unsignedlong pages[NR_LRU_LISTS];
...
0051for(lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
0052 pages[lru]= global_page_state(NR_LRU_BASE + lru);
...
0095"Active: %8lu kB\n"
0096"Inactive: %8lu kB\n"
0097"Active(anon): %8lu kB\n"
0098"Inactive(anon): %8lu kB\n"
0099"Active(file): %8lu kB\n"
0100"Inactive(file): %8lu kB\n"
...
0148 K(pages[LRU_ACTIVE_ANON]+ pages[LRU_ACTIVE_FILE]),
0149 K(pages[LRU_INACTIVE_ANON]+ pages[LRU_INACTIVE_FILE]),
0150 K(pages[LRU_ACTIVE_ANON]),
0151 K(pages[LRU_INACTIVE_ANON]),
0152 K(pages[LRU_ACTIVE_FILE]),
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: