Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the water level of the Linux partition page frame allocator

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to understand the water level of the Linux partition page frame allocator", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the water level of the Linux partition page frame allocator.

Let's take a look at this first:

Static struct page *

Get_page_from_freelist (gfp_t gfp_mask, unsigned int order, int alloc_flags

Const struct alloc_context * ac)

{

For_next_zone_zonelist_nodemask (zone, z, ac- > zonelist, ac- > high_zoneidx, ac- > nodemask)

{

If (! zone_watermark_fast (zone, order, mark, ac_classzone_idx (ac), alloc_flags))

{

Ret = node_reclaim (zone- > zone_pgdat, gfp_mask, order)

Switch (ret) {

Case NODE_RECLAIM_NOSCAN:

Continue

Case NODE_RECLAIM_FULL:

Continue

Default:

If (zone_watermark_ok (zone, order, mark, ac_classzone_idx (ac), alloc_flags))

Goto try_this_zone

Continue

}

}

Try_this_zone: / / normal water level of this zone

Page = rmqueue (ac- > preferred_zoneref- > zone, zone, order, gfp_mask, alloc_flags, ac- > migratetype)

}

Return NULL

}

We can see that there is a judgment about the water level before the partner algorithm allocation. today we will look at the concept of the water level.

To put it simply, when using the partition page allocator, you will compare the available free pages with the watermark in the zone.

Water level initialization int _ _ meminit init_per_zone_wmark_min (void)

{

Unsigned long lowmem_kbytes

Int new_min_free_kbytes

/ / nr_free_buffer_pages is the total number of pages above the high water level in ZONE_DMA and ZONE_NORMAL areas nr_free_buffer_pages = managed_pages-high_pages

Lowmem_kbytes = nr_free_buffer_pages () * (PAGE_SIZE > > 10)

New_min_free_kbytes = int_sqrt (lowmem_kbytes * 16)

If (new_min_free_kbytes > user_min_free_kbytes) {

Min_free_kbytes = new_min_free_kbytes

/ / the min value must be between 128kib-65536kib

If (min_free_kbytes

< 128) min_free_kbytes = 128; if (min_free_kbytes >

65536)

Min_free_kbytes = 65536

} else {

Pr_warn ("min_free_kbytes is not updated to% d because user defined value% d is preferred\ n"

New_min_free_kbytes, user_min_free_kbytes)

}

/ / after you get the total min, you can calculate their respective min values through do_div according to the percentage of each zone in the total memory.

Setup_per_zone_wmarks ()

Refresh_zone_stat_thresholds ()

Setup_per_zone_lowmem_reserve ()

Return 0

}

Nr_free_buffer_pages is the total number of pages above the high water level in ZONE_DMA and ZONE_NORMAL areas nr_free_buffer_pages = managed_pages-high_pages

Min_free_kbytes is the total min size, min_free_kbytes = 4 * sqrt (lowmem_kbytes)

Setup_per_zone_wmarks according to the total min value, plus the proportion of each zone in the total memory, and then calculate their respective min value through do_div, and then calculate the water level of each zone. The relationship of min,low,high is as follows: low = min * 125%

High = min * 150%

Min:low:high = 4:5:6

When setup_per_zone_lowmem_reserve fails from Normal, it attempts to apply for allocation from DMA and restricts allocation requests from Normal through lowmem_reserve [DMA]. Its value can be modified through / proc/sys/vm/lowmem_reserve_ratio.

As can be seen from this picture:

If the number of free pages min value, the zone is very short of pages, page recycling pressure is very high, the application write memory operation will be blocked, directly in the application's process context for recycling, that is, direct reclaim. If the number of free pages is less than the low value, the kswapd thread will wake up and start releasing recycled pages. If the value of the idle page is greater than the high value, the state of the zone is perfect and the kswapd thread will sleep again. Regulation of water level in Android system

To avoid direct reclaim, we need to keep the amount of free memory above the min value all the time. However, Android, a system in which a large number of users operate the network, will inevitably encounter a sudden increase in the amount of data and need to apply for a large amount of memory temporarily. At this time, it is possible that the speed of memory recovery by kswapd is less than the speed of memory allocation, that is, direct reclaim occurs, thus blocking applications seriously affect performance.

We know that when memory is allocated, only the area between low and min is the active area of kswapd. The default value between low and min in linux is relatively small, so it is easy to cause the direct reclaim situation.

"extra_free_kbytes":

For this reason, Android adds the variable extra_free_kbytes to the water level of linux. This extra is added between low and min, which increases the low value when the min is constant.

The source code is as follows

Static void _ setup_per_zone_wmarks (void)

{

Unsigned long pages_min = min_free_kbytes > > (PAGE_SHIFT-10)

Unsigned long pages_low = extra_free_kbytes > > (PAGE_SHIFT-10)

.

For_each_zone (zone) {

.

Do_div (min, lowmem_pages)

.

}

Calculate_totalreserve_pages ()

}

To see if the introduction of extra_free_kbytes works, you can see through the pageoutrun and allocstall in / proc/vmstat, which represent the number of times kswapd and direct reclaim are started, respectively.

"watermark_scale_factor":

The kernel is always making progress. In the linux kernel version 4.6, a new way to adjust the water level has been born, that is, the watermark_scale_factor coefficient, whose default value is 10, and the corresponding memory ratio is 10000cm 0.1%. It can be set through / proc/sys/vm/watermark_scale_factor, and the maximum value is 1000. For example, when it is set to 1000, it means the difference between min and low, and the difference between low and high will be 10% of the memory size.

The extra_free_kbytes approach mentioned earlier only increases the difference between min and low, while watermark_scale_factor increases the difference between min and low,low and high at the same time.

At this point, I believe that everyone on the "Linux partition page frame allocator how to understand the water level" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report