mm, swap: VMA based swap readahead
Subsystems (3)
RFC 1/1 mm, swap: VMA based swap readahead
From: Huang Ying <[email protected]>
The swap readahead is a important mechanism to reduce the swap in latency. But the original swap readahead algorithm has some issues.
a) The original swap readahead algorithm does readahead based on the consecutive blocks in swap device. But the consecutive blocks in swap device just reflect the order of page reclaiming, but don't necessarily reflect the access sequence in RAM. Although for some workloads (such as whole system pure sequential workload), it works, but in general, it doesn't work well for more complex access pattern, such as combined workloads (sequential and random workload runs together) or changing accessing patterns in workload.
b) The original swap readahead algorithm just turns random read into sequential read, but doesna??t parallel CPU and disk operations. This can reduce the average latency and the lowest latency, but doesna??t help much for high percentile latency (like 90%, 95%, 99%).
In this patch, a VMA based swap readahead algorithm is implemented. When the page fault occurs, the pages along the access direction will be checked and the swapped out pages will be readahead if they fall inside the readahead window. There is one readahead window state for each VMA, to reflect the different access patterns for different VMAs. The readahead window is scaled based on whether the accessed page is consecutive in virtual address with the previous accessed page. If so, the readahead window will be extended, otherwise, the readahead window will be shrunk rapidly.
The test and comparison result is as below.
Common test condition
=====================
Test Machine: Xeon E5 v3 (2 sockets, 72 threads, 32G RAM) Swap device: NVMe disk
Pure sequential access pattern workload
=======================================
Test case
---------
vm-scalability, sequential swap test case, 4 processes to eat 50G virtual memory space, repeat the sequential memory write until 300 seconds, for each page accessed, 24us is delayed to emulate CPU operations on the page. The first round writing will trigger swap out, the following rounds will trigger sequential swap in and out.
Result
------
Base kernel:
samples: 100952, average: 40.08us, min: 0us, max: 16.2ms
50th: 1us, 60th: 1us, 70th: 1us, 80th: 3us, 90th: 158us, 95th: 181us, 99th: 254us
Optimized kernel:
samples: 118509, average: 12.04us, min: 0us, max: 17.42ms
50th: 1us, 60th: 2us, 70th: 2us, 80th: 2us, 90th: 8us, 95th: 11us, 99th: 42us
The VMA based swap readahead has much lower latency. Mainly because VMA based swap readahead algorithm parallels CPU operations and disk IO, while the original swap readahead algorithm doesna??t do that.
Pure random access pattern workload
===================================
Test Case
---------
vm-scalability, random swap test case, 4 processes to eat 50G virtual memory space, repeat the memory random write until 300 seconds, for each page accessed, 24us is delayed to emulate CPU operations on the page. The first round writing will trigger swap out, the following rounds will trigger random swap in.
Result
------
Base kernel:
samples: 70757, average: 70.71us, min: 0us, max: 15.19ms
50th: 78us, 60th: 83us, 70th: 91us, 80th: 93us, 90th: 97us, 95th: 103us, 99th: 120us
Optimized kernel:
samples: 70842, average: 72.55us, min: 0us, max: 15.47ms
50th: 79us, 60th: 83us, 70th: 92us, 80th: 94us, 90th: 97us, 95th: 104us, 99th: 122us
Almost exact same for all results. Both are good at avoiding readahead.
Combined access pattern workload
================================
Test Case
---------
vm-scalability, sequential swap test case, 4 processes to eat 50G virtual memory space, repeat the sequential memory write until 300 seconds, for each page accessed, 24us is delayed to emulate CPU operations on the page. The first round writing will trigger swap out, the following round will trigger sequential swap in and out.
As background noise, vm-scalability, random swap test case, 8 processes to eat 30G virtual memory space, repeat the random memory write until 300 seconds, for each page accessed, 24us is delayed to emulate CPU operations on the page. It will trigger random swap in in the background.
All in all, after reaching relative stable state, there are sequential and random swap in.
Result (sequential workload)
----------------------------
Base kernel:
samples: 228658, average: 68.52us, min: 0us, max: 23.67ms
50th: 2us, 60th: 42us, 70th: 74us, 80th: 106us, 90th: 150us, 95th: 181us, 99th: 263us
Optimized kernel:
samples: 271851, average: 26.8us, min: 0us, max: 21.24ms
50th: 1us, 60th: 1us, 70th: 2us, 80th: 3us, 90th: 58us, 95th: 115us, 99th: 225us
The original swap readahead algorithm is confused by the background random access workload, so readahead is not enough for the sequential access workload. The VMA-base readahead algorithm works much better.
--- include/linux/mm_types.h | 1 + include/linux/swap.h | 42 +++++++ mm/memory.c | 14 ++- mm/swap_state.c | 281 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 329 insertions(+), 9 deletions(-)
include/linux/mm_types.h +1 -0
@@ -351,6 +351,7 @@ struct vm_area_struct {struct file * vm_file;/* File we map to (can be NULL). */ void * vm_private_data;/* was vm_pte (shared mem) */ +atomic_long_t swap_readahead_info;#ifndef CONFIG_MMU struct vm_region *vm_region;/* NOMMU mapping region */ #endif
include/linux/swap.h +42 -0
72 lines not on this page · raw message
mm/memory.c +11 -3
@@ -2558,12 +2558,15 @@ int do_swap_page(struct vm_fault *vmf)struct vm_area_struct *vma = vmf->vma;struct page *page, *swapcache;struct mem_cgroup *memcg; +struct vma_swap_readahead swap_ra;swp_entry_t entry;pte_t pte;int locked;int exclusive =0 ;int ret =0 ; +if (swap_vma_readahead) +swap_readahead_detect (vmf, &swap_ra);if (!pte_unmap_same (vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) goto out;
@@ -2583,8 +2586,12 @@ int do_swap_page(struct vm_fault *vmf)delayacct_set_flag (DELAYACCT_PF_SWAPIN ); page =lookup_swap_cache (entry);if (!page) { - page =swapin_readahead (entry,GFP_HIGHUSER_MOVABLE , vma, - vmf->address); +if (swap_vma_readahead) + page =do_swap_page_readahead (vmf, &swap_ra, + entry,NULL ); +else + page =swapin_readahead (entry, +GFP_HIGHUSER_MOVABLE , vma, vmf->address);if (!page) {/* * Back out if somebody else faulted in this pte
@@ -2611,7 +2618,8 @@ int do_swap_page(struct vm_fault *vmf)delayacct_clear_flag (DELAYACCT_PF_SWAPIN ); swapcache = page; goto out_release; - } + }else if (swap_vma_readahead) +do_swap_page_readahead (vmf, &swap_ra, entry, page); swapcache = page; locked =swap_lock_page_or_retry (page, vma->vm_mm, vmf->flags);
mm/swap_state.c +275 -6
297 lines not on this page · raw message
Signature · 7 lines
2.11.0 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>