In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is the use of swappiness parameters in linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Swappiness is a kernel parameter of Linux that controls the relative weight of memory usage of the system when swap is performed. The swappiness parameter value can be set between 0 and 100. The lower the value of this parameter, the less swap partitions and more memory are used in the Linux system; the higher the parameter value is, the more the kernel uses swap space.
one。 About memory allocation and page collection (page reclaim)
There are two main types of scenarios that occur in page reclaim, one is the activity carried out by the kswapd background thread, and the other is direct reclaim, that is, there is no free memory when the page is allocated and the page collection needs to be carried out immediately. Generally speaking, the process of memory allocation is divided into two parts, one is fast path and the other is slow path. Usually, when the use of memory is not tight, the requirements can be met in fast path. And the memory allocation under fast path will not cause IO blocking caused by page collection such as dirty writeback and swap.
The general flow of fast path is as follows: 1. If the system mount uses memory cgroup, first check to see if the cgroup limit is exceeded, then direct reclaim if it is exceeded, and do it through do_try_to_free_pages. If not, do the charge work of cgroup (charge is done through a two-phase commit, which is not expanded here). two。 To find free pages from the local prefered zone memory node, you need to determine whether the requirements of the system watermark and dirty ratio are met. If so, extract the corresponding page from the buddy system, otherwise try to collect the local prefered zone. This time, the next page collection of fast path will only reclaim clean page, that is, dirty page and mapped page will not be considered, so that no swap and writeback will be generated, that is, no IO operation of blocking will be caused. If this collection still cannot satisfy the requested number of memory pages, enter slow path.
The general flow of slow path is as follows:\ 1. First, wake up kswapd for page reclaim background operation. \ 2. Retry the local prefered zone to allocate memory. If it fails, it will decide whether to ignore the requirements of watermark, dirty ratio and local node allocation based on the requested GFP parameters and try again. In this step, if the GFP_NOFAIL flag is specified when the page is allocated, the allocation failure will wait for retry. \ 3. If there is no GFP_NOFAIL tag, you need to start page compact and page direct reclaim operations, and then enter the OOM process if there is still no available memory.
For more information, please see the logic of the kernel code _ _ alloc_pages function. In addition, no matter who initiated the page reclaim, it will eventually be imported to shrink_zone, that is, the reclaim operation will be performed independently for each zone, and the shrink_lruvec function will eventually be entered to scan and recycle the corresponding page lru linked list of each zone.
two。 Some background knowledge about page recycling
The general process of page recycling will scan the corresponding page linked list on each zone, mainly including inactive anon/active anon (anonymous page linked list) and inactive file/active file linked list (file cache/ mapped page linked list). There are a total of four linked lists. All of our used page are basically stored in one of these four linked lists before being recycled (and some of them are in the unevictable linked list, ignored). According to the number of times it is referenced, it is determined whether it is in the active or inactive linked list, and whether it is in the anon or file linked list according to its type.
Page collection will scan all the zone of each memory node, then scan active first, move the infrequently accessed pages to the inactive linked list, then scan the inactive linked list, move the frequently referenced pages back to active, and confirm that the infrequent pages will eventually be recycled. If it is a file based page, it will be released or written back according to whether it is clean (writeback,filecache is directly released), and if it is anon, swap will be carried out. Therefore, this paper is actually concerned about the impact of swappiness parameters on anon linked list scanning.
In addition, you need to understand that the four linked lists described earlier were originally placed on the zone data structure, and later introduced the mem_cgroup rule to redefine a set of mem_cgroup_per_zone/mem_cgroup_per_node data structures. These four linked lists are defined on this set of data structures at the same time. If the system opens mem cgroup, use the latter, otherwise use the former.
In addition, it is important to say that swap is only a treatment measure of page reclaim, mainly aimed at anon page. Let's finally take a look at the exact meaning of swappiness.
three。 The exact influence of swappiness on page reclaim
The logic in page reclaim logic that scans the above four linked lists is in the get_scan_count function in vmscan.c. Most of the logic comments of this function are written very clearly. Let's briefly sort it out and focus on the value of the scan_balance variable:
\ 1. First of all, if swap is disabled or there is no swap space, only the linked list of file based is scanned, that is, the anonymous page linked list scan code is as follows:
❝
If (! sc- > may_swap | | (get_nr_swap_pages ())
\ 2. If you are not doing global page recycling (page recycling caused by cgroup resource limit) and swappiness is set to 0, anonymous page list scanning will not be performed. This is non-negotiable. The swappiness value directly determines whether swap occurs, and if it is set to 0, it will certainly not happen. In addition, it should be noted that in this case, you need to set the cgroup configuration file memory.swappiness, rather than the global sysctl vm.swappiness code as follows:
❝
If (! global_reclaim (sc) & &! vmscan_swappiness (sc)) {scan_balance = SCAN_FILE;goto out;}
\ 3. If the priority set before the list scan (this value determines how many linked list elements to scan) is 0, and the swappiness is non-0, the swap code may be as follows:
❝
If (! sc- > priority & & vmscan_swappiness (sc)) {scan_balance = SCAN_EQUAL;goto out;}
\ 4. If the global page collection, and the current free memory and the sum of all file based linked list page are less than the system high watermark, then anonymous page collection must be carried out, then swap will inevitably occur, you can see how the value of swappiness is set here is completely irrelevant, which also explains why it is 0, the system will also carry out swap reasons, in addition, we will explain in detail how the system page watermark is calculated. The code is as follows:
❝
Anon = get_lru_size (lruvec, LRU_ACTIVE_ANON) + get_lru_size (lruvec, LRU_INACTIVE_ANON); file = get_lru_size (lruvec, LRU_ACTIVE_FILE) + get_lru_size (lruvec, LRU_INACTIVE_FILE)
If (global_reclaim (sc)) {free = zone_page_state (zone, NR_FREE_PAGES); if (unlikely (file + free)
\ 5. If the system inactive file linked list is sufficient, anonymous page collection is not considered, that is, the swap code is not considered as follows:
❝
If (! inactive_file_is_low (lruvec)) {scan_balance = SCAN_FILE;goto out;}
\ 6. The last case is based on the swappiness value and the previous statistics of file and anon which is more valuable to determine the proportion of file and anon linked list scanning, then if swappiness is set to 0, then the anon linked list will not be scanned, that is, no swappiness. there will be more code and no longer posted.
four。 Calculation of system memory watermark
As we saw earlier, the system memory watermark has a decisive impact on the page collection mechanism. In fact, this value is also frequently used in memory allocation. Specifically, it has three values, low,min and high, which are specified according to the page allocation time. If the system free memory is lower than the corresponding watermark, the allocation will fail, which is also the basis for entering slow path or wakeup kswapd.
The actual calculation of this value is determined by the vm.min_free_kbytes in sysctl, and the general formula is as follows:
❝
Pages_min = min_free_kbytes > > (PAGE_SHIFT-10); tmp = (U64) pages_min * zone- > managed_pages;do_div (tmp, lowmem_pages); zone- > watermark [WMark _ MIN] = tmp;zone- > watermark [WMark _ LOW] = min_wmark_pages (zone) + (tmp > 2); zone- > watermark [WMark _ HIGH] = min_wmark_pages (zone) + (tmp > > 1)
That is to say, according to the value of min_free_kbytes, the min_watermark of zone is calculated according to the proportion of each zone management page, and then add min's 1max 4 to be low, plus 1max 2 to be high.
This is the end of this article on "what is the use of swappiness parameters in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.