From patchwork Wed Oct 4 19:02:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13409345 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 969B3E7C4ED for ; Wed, 4 Oct 2023 19:03:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85ABC8D00AE; Wed, 4 Oct 2023 15:03:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 66ED98D00B3; Wed, 4 Oct 2023 15:03:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B6698D00AE; Wed, 4 Oct 2023 15:03:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id EF3708D00B1 for ; Wed, 4 Oct 2023 15:03:20 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B36E51CA480 for ; Wed, 4 Oct 2023 19:03:20 +0000 (UTC) X-FDA: 81308702160.07.A27E5A2 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf04.hostedemail.com (Postfix) with ESMTP id 093014002B for ; Wed, 4 Oct 2023 19:03:18 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf04.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696446199; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EeBQ4CD9wlWlM9GOiS7w5/83c9YEl/Dj/IX4sBPGk/c=; b=buABa4F4Dft7PezFVHGtr5wbuGO/kQRFc0pidKyPgZXxWsqhoP1I+rcsprMfcEs/3smnKH B4HRfmelH5UP63SOJ7wj16PRw1nYFPIIpG2+8IoxF68Fc9vMObWWVaJN4wp2zZtdfrsiDh hDUo8DRQotYpEY0xEgF+lcUg+7BFQao= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf04.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696446199; a=rsa-sha256; cv=none; b=vHESH3pYZnjseXz6/DnIGkoPgxW6pI3Jyvsx4y0N1GM2h9fy6dd2wgs0czldl7c2kuEVZP ya1+KKDa1zMYQ/g57Yxovm5byfGzmEZ7VBF1f2JBeZS638rsU6p2X+0YVmTZw7RnJJeP8P msf1171KwGnYREf12EtyrBv/2jetvw0= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 70FD2D08BFFB; Wed, 4 Oct 2023 12:03:02 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 1/4] mm/ksm: add ksm advisor Date: Wed, 4 Oct 2023 12:02:46 -0700 Message-Id: <20231004190249.829015-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231004190249.829015-1-shr@devkernel.io> References: <20231004190249.829015-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 093014002B X-Stat-Signature: 4sft45783mtmphxgb8ijeaxde19bmuqa X-Rspam-User: X-HE-Tag: 1696446198-72375 X-HE-Meta: U2FsdGVkX1+o/i/xAV5CRiX5xTkxZJ0mWBkzLyjoitaTOP+wCtiF1PKlqxv1c5qzUClwPIh+7ilelSiof3LHCciUZ02Zb0kGFFJH4t1U32WkCG3A1s6npeOsMW6FyVg45RCW2/fl4kbMaPZp/epxiVwSqBe4o254zpHri5ao1AgIi6tKRT6wqXJR56F0izWlqifQl4a88MaHhSK54kSUQvFS9daZqbu6l7zUEJp2FSx+iEeXru+8O0RI4O7I/dBwITdaUyEfv1TeHK/OcWVVuyFGWB9pAWLtFFUpXT4WKKKdeUe9iQGl06NByxucPl1U5PuFx4YrVK0X47DhwFasCJwfA5pyFXLCnw4yBbvaA3hl3S0w5k5Fq/SGZdW1DLmzIsxzmie3Fr1Xe8spZj+E+DtJpW1NEDUurJcFjln9GSWYHKf5BbMYP2aT99Lyn/Gfw3rAMMB2aZG9nmD38Yy4BXoik0E5IV6QEdSeGJBWJotMzammSCKFl5iM3/bHzQF1Y+opGMlzMvJDzsiuqmpKO5OlW3CQ1oZSXsPf7vYKRLOSz8nMAHk+swiG64HkjYTuGde5Hxkf2ahS+F/1vo6+MYEw/TjkeDiIGvi1PBIi6qxIFSTaAVLxmHB5IyPn4hQyq27GCK2XoFgE47QtZqL2EH/LwFYYAkDnGnKf8Jsc+4N8NSVveVo4nz5emPxfwgphtGvrAVrErbPGpMxhmxZRNTr/NR2qeqY8WDnBvzDwzv1jQa9N/m3FPsq/ic/2cuyUjXhqvL+jb2cuSfNrfmkRWGgucRbNFyRN8ObYq6S1kuETNTznV70vR78f51dE2f/E4C6B3RepNTgZ2vRE53dgl/GBWTrL2hMEENAKcksUIQjZXFH23uYm7vgWN1lwBSlN+G1oKk5JZoKJm2uEaeCN7m8rEWeYMuKuajpAY9ueCFy2nA62n6YwsLMDkMwHjyPd0iHW5CZF4LIWdt7gcXI wxFHdLpV hocmN5kJh+OLqDAAhDCT8DuF5ZFeNHe2IC6+b X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This adds the ksm advisor. The ksm advisor automatically manages the pages_to_scan setting to achieve a target scan time. The target scan time defines how many seconds it should take to scan all the candidate KSM pages. In other words the pages_to_scan rate is changed by the advisor to achieve the target scan time. The algorithm has a max and min value to: - guarantee responsiveness to changes - to avoid to spend too much CPU The respective parameters are: - ksm_advisor_target_scan_time (how many seconds a scan should take) - ksm_advisor_min_pages (minimum value for pages_to_scan per batch) - ksm_advisor_max_pages (maximum valoe for pages_to_scan per batch) The algorithm calculates the change value based on the target scan time and the previous scan time. To avoid pertubations an exponentially weighted moving average is applied. By default the advisor is disabled. Currently there are two advisors: none and scan_time. Tests with various workloads have shown considerable CPU savings. Most of the workloads I have investigated have more candidate pages during startup, once the workload is stable in terms of memory, the number of candidate pages is reduced. Without the advisor, the pages_to_scan needs to be sized for the maximum number of candidate pages. So having this advisor definitely helps in reducing CPU consumption. For the instagram workload, the advisor achieves a 25% CPU reduction. Once the memory is stable, the pages_to_scan parameter gets reduced to about 40% of its max value. Signed-off-by: Stefan Roesch --- mm/ksm.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 7efcc68ccc6e..c9edfb293024 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -248,6 +248,9 @@ static struct kmem_cache *rmap_item_cache; static struct kmem_cache *stable_node_cache; static struct kmem_cache *mm_slot_cache; +/* Default number of pages to scan per batch */ +#define DEFAULT_PAGES_TO_SCAN 100 + /* The number of pages scanned */ static unsigned long ksm_pages_scanned; @@ -276,7 +279,7 @@ static unsigned int ksm_stable_node_chains_prune_millisecs = 2000; static int ksm_max_page_sharing = 256; /* Number of pages ksmd should scan in one batch */ -static unsigned int ksm_thread_pages_to_scan = 100; +static unsigned int ksm_thread_pages_to_scan = DEFAULT_PAGES_TO_SCAN; /* Milliseconds ksmd should sleep between batches */ static unsigned int ksm_thread_sleep_millisecs = 20; @@ -297,6 +300,129 @@ unsigned long ksm_zero_pages; /* The number of pages that have been skipped due to "smart scanning" */ static unsigned long ksm_pages_skipped; +/* At least scan this many pages per batch. */ +static unsigned long ksm_advisor_min_pages = 500; + +/* Don't scan more than max pages per batch. */ +static unsigned long ksm_advisor_max_pages = 5000; + +/* Target scan time in seconds to analyze all KSM candidate pages. */ +static unsigned long ksm_advisor_target_scan_time = 200; + +/* Exponentially weighted moving average. */ +#define EWMA_WEIGHT 50 + +/** + * struct advisor_ctx - metadata for KSM advisor + * @start_scan: start time of the current scan + * @scan_time: scan time of previous scan + * @change: change in percent to pages_to_scan parameter + */ +struct advisor_ctx { + ktime_t start_scan; + s64 scan_time; + unsigned long change; +}; +static struct advisor_ctx advisor_ctx; + +/* Define different advisor's */ +enum ksm_advisor_type { + KSM_ADVISOR_NONE, + KSM_ADVISOR_FIRST = KSM_ADVISOR_NONE, + KSM_ADVISOR_SCAN_TIME, + KSM_ADVISOR_LAST = KSM_ADVISOR_SCAN_TIME +}; +static enum ksm_advisor_type ksm_advisor; + +static void init_advisor(void) +{ + advisor_ctx.start_scan = 0; + advisor_ctx.scan_time = 0; + advisor_ctx.change = 0; +} + +/* + * Use previous scan time if available, otherwise use current scan time as an + * approximation for the previous scan time. + */ +static inline s64 prev_scan_time(struct advisor_ctx *ctx, s64 new_scan_time) +{ + return ctx->scan_time ? ctx->scan_time : new_scan_time; +} + +/* Calculate exponential weighted moving average */ +static unsigned long ewma(unsigned long prev, unsigned long curr) +{ + return ((100 - EWMA_WEIGHT) * prev + EWMA_WEIGHT * curr) / 100; +} + +/* + * The scan time advisor is based on the current scan rate and the target + * scan rate. + * + * new_pages_to_scan = pages_to_scan * (scan_time / target_scan_time) + * + * To avoid pertubations it calculates a change factor of previous changes. + * A new change factor is calculated for each iteration and it uses an + * exponentially weighted moving average. The new pages_to_scan value is + * multiplied with that change factor: + * + * new_pages_to_scan *= change facor + * + * In addition the new pages_to_scan value is capped by the max and min + * limits. + */ +static void scan_time_advisor(s64 scan_time) +{ + unsigned long pages; + unsigned long factor; + unsigned long change; + unsigned long last_scan_time; + + pages = ksm_thread_pages_to_scan; + last_scan_time = prev_scan_time(&advisor_ctx, scan_time); + + /* Calculate scan time as percentage of target scan time */ + factor = ksm_advisor_target_scan_time * 100 / scan_time; + factor = factor ? factor : 1; + + /* + * Calculate scan time as percentage of last scan time and use + * exponentially weighted average to smooth it + */ + change = scan_time * 100 / last_scan_time; + change = change ? change : 1; + change = ewma(advisor_ctx.change, change); + + /* Calculate new scan rate based on target scan rate. */ + pages = pages * 100 / factor; + /* Update pages_to_scan by weighted change percentage. */ + pages = pages * change / 100; + + /* Cap new pages_to_scan value */ + pages = max(pages, ksm_advisor_min_pages); + pages = min(pages, ksm_advisor_max_pages); + + /* Update advisor context */ + advisor_ctx.change = change; + advisor_ctx.scan_time = scan_time; + ksm_thread_pages_to_scan = pages; +} + +static void run_advisor(void) +{ + if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) { + s64 scan_time; + + /* Convert scan time to seconds */ + scan_time = ktime_ms_delta(ktime_get(), advisor_ctx.start_scan); + scan_time /= MSEC_PER_SEC; + scan_time = scan_time ? scan_time : 1; + + scan_time_advisor(scan_time); + } +} + #ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes = 1; @@ -2401,6 +2527,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) mm_slot = ksm_scan.mm_slot; if (mm_slot == &ksm_mm_head) { + advisor_ctx.start_scan = ktime_get(); trace_ksm_start_scan(ksm_scan.seqnr, ksm_rmap_items); /* @@ -2558,6 +2685,8 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) if (mm_slot != &ksm_mm_head) goto next_mm; + run_advisor(); + trace_ksm_stop_scan(ksm_scan.seqnr, ksm_rmap_items); ksm_scan.seqnr++; return NULL; @@ -3603,6 +3732,7 @@ static int __init ksm_init(void) zero_checksum = calc_checksum(ZERO_PAGE(0)); /* Default to false for backwards compatibility */ ksm_use_zero_pages = false; + init_advisor(); err = ksm_slab_init(); if (err) From patchwork Wed Oct 4 19:02:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13409341 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FBBFE7C4EC for ; Wed, 4 Oct 2023 19:03:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA76E8D00AF; Wed, 4 Oct 2023 15:03:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C55888D0002; Wed, 4 Oct 2023 15:03:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B017F8D00AF; Wed, 4 Oct 2023 15:03:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9B54F8D0002 for ; Wed, 4 Oct 2023 15:03:19 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6110880410 for ; Wed, 4 Oct 2023 19:03:19 +0000 (UTC) X-FDA: 81308702118.27.118F792 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf13.hostedemail.com (Postfix) with ESMTP id C2E4320032 for ; Wed, 4 Oct 2023 19:03:17 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=neutral (imf13.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696446197; a=rsa-sha256; cv=none; b=Hewhc1NcHWIJA2UbypmQPojsLJD+JNDuxU1nPds7Q7sjx4SzLleRpSuWQeMdG0LvbjHV9s ZlF0DjE9h0C+TT751hk49cUuqv5wFuL2Brrb1Kl/ieW8q6N6AhnTnMIc2H0oWrhGG36lE4 VyJteBBwlyqdL8ZAwZTXTMFdR5wbbfU= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=neutral (imf13.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696446197; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OwQWDABFLnnr40odHJRGCCaIkSZd/6hq/j8i4+KYZoE=; b=CO2QqR2z307BcqfJphDtrYlEW3zOgxvPbhQ1DPXZDaeS2OCjgAZYppf5vh/luC6QrActBq 4ggZU1HqKY4H2G+yQQgNvZX52kUVNRbWXHi1D8+GW4/5NPUkAWaEqvngC//HV1VMpOHsV4 O6i9AWA8MFPJZoeETOb2yGn/mTCPU14= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id B20EFD08BFFD; Wed, 4 Oct 2023 12:03:02 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 2/4] mm/ksm: add sysfs knobs for advisor Date: Wed, 4 Oct 2023 12:02:47 -0700 Message-Id: <20231004190249.829015-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231004190249.829015-1-shr@devkernel.io> References: <20231004190249.829015-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C2E4320032 X-Stat-Signature: ademnmbxbbtperw94uoqwm8axb7wzqm6 X-HE-Tag: 1696446197-723872 X-HE-Meta: U2FsdGVkX1825YDIsRceWETqI+hgl/bjW90+5f2s+dXjghA+4GEeyLGP27nVb+WAeFCCA8xruVKKfTazKpO8ZpVFMFY664C/H7jQtkWX1GNBbyAatBCFzdUtzYRzGaGa7hQaGyXadpOmZKR8G3ZacQZpJ9bm8kLxcZe1sD17mqOcagXJaILUORq2Z3dvSIfSAJQOrrDGZBZQBUMdAAwK54xJQRe3br/rpNvS/lzQ+kilF2S0qQUZyyaAIPBDvu0aJgaERuv8eO0st636eG7xqGu/HaDQJBVF4UDbaXxfH3LAj5rlgwTM5W4fiZdVNTPVyGdOWYtYXt2zH9bTX3d5pzCmfkcJrUfIA3YN9E0hBtjtawM/An5BYocilkz4X2QCeT6nshEPplrGrTWm/uJSoCg8aWWUeWeb4MqzMR/lgaHryIwdstpum60h7hvY5xqRt/36LVijKeyHK4g2VxtBA0fVZ9nGhh6ENZYpRbC1hUtJNKEyUTTzmUHDv86SdpLN8k6O8bT1/bjxRN5X7HH2hRyh2dvuNHaObAKjFHVnmRiAVcaNc81BcKA95yxVBRA2QIrVO4qQwi7hAEXKhiMvEDNTslwv5fxs3xC819F6TjIqQYh4cd7l+T4E+/+8jUPZw6bOzFD62yWaEgQXGo2PSq56l7CtuaBjq/yDjFPdZIs4Wz8NylbvfbETGdOoJ/WfVufinP5dEbKa2qVUtrCIxByIbr+blmKxG8aijfBhRUlZ40+Rgkuzhscd0CaPZU8w3IY3ziYy7V63lnxsyPPYVxxsrWJPjvKSb2ofd9Rc4HcDHwqRhGdRZMqFx/pZxQLoTjY4WSRV//UiiPgYoPXTRUVTHB/wUSLPJlbutFWpCjy6vZeWyid6wusftd0IP+0jPJ6Agbk4b5YRO5XiDbjEdMfMkT7h6VDlwRpfqVnFlYcjnYALMu7iU6db6BrxArvFix3j5UKdpqLSNdNGTx2 m3X0+piT mZyrETkQBcyEELysV3v84BFw5KAQxiJahGuDD X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This adds four new knobs for the KSM advisor to influence its behaviour. The knobs are: - advisor_mode: 0: no advisor (default) 1: scan time advisor - advisor_min_pages: 500 (default) - advisor_max_pages: 5000 (default) - advisor_target_scan_time: 200 (default in seconds) The new values will take effect on the next scan round. Signed-off-by: Stefan Roesch --- mm/ksm.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index c9edfb293024..12e70f806b2b 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -341,6 +341,14 @@ static void init_advisor(void) advisor_ctx.change = 0; } +static void set_advisor_defaults(void) +{ + if (ksm_advisor == KSM_ADVISOR_NONE) + ksm_thread_pages_to_scan = DEFAULT_PAGES_TO_SCAN; + else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) + ksm_thread_pages_to_scan = ksm_advisor_min_pages; +} + /* * Use previous scan time if available, otherwise use current scan time as an * approximation for the previous scan time. @@ -3692,6 +3700,102 @@ static ssize_t smart_scan_store(struct kobject *kobj, } KSM_ATTR(smart_scan); +static ssize_t advisor_mode_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%u\n", ksm_advisor); +} + +static ssize_t advisor_mode_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, + size_t count) +{ + unsigned int mode; + int err; + + err = kstrtouint(buf, 10, &mode); + if (err) + return -EINVAL; + if (mode > KSM_ADVISOR_LAST) + return -EINVAL; + + /* Set advisor default values */ + ksm_advisor = mode; + init_advisor(); + set_advisor_defaults(); + + return count; +} +KSM_ATTR(advisor_mode); + +static ssize_t advisor_min_pages_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_min_pages); +} + +static ssize_t advisor_min_pages_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int err; + unsigned long value; + + err = kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_min_pages = value; + return count; +} +KSM_ATTR(advisor_min_pages); + +static ssize_t advisor_max_pages_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_max_pages); +} + +static ssize_t advisor_max_pages_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int err; + unsigned long value; + + err = kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_max_pages = value; + return count; +} +KSM_ATTR(advisor_max_pages); + +static ssize_t advisor_target_scan_time_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_target_scan_time); +} + +static ssize_t advisor_target_scan_time_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int err; + unsigned long value; + + err = kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + if (value < 1) + return -EINVAL; + + ksm_advisor_target_scan_time = value; + return count; +} +KSM_ATTR(advisor_target_scan_time); + static struct attribute *ksm_attrs[] = { &sleep_millisecs_attr.attr, &pages_to_scan_attr.attr, @@ -3714,6 +3818,10 @@ static struct attribute *ksm_attrs[] = { &use_zero_pages_attr.attr, &general_profit_attr.attr, &smart_scan_attr.attr, + &advisor_mode_attr.attr, + &advisor_min_pages_attr.attr, + &advisor_max_pages_attr.attr, + &advisor_target_scan_time_attr.attr, NULL, }; From patchwork Wed Oct 4 19:02:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13409344 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 563ABE7C4E9 for ; Wed, 4 Oct 2023 19:03:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59D9A8D00B1; Wed, 4 Oct 2023 15:03:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 52B418D00B0; Wed, 4 Oct 2023 15:03:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A0888D00B3; Wed, 4 Oct 2023 15:03:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id F22188D00B0 for ; Wed, 4 Oct 2023 15:03:20 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C2A1B120415 for ; Wed, 4 Oct 2023 19:03:20 +0000 (UTC) X-FDA: 81308702160.03.0FF5F07 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf01.hostedemail.com (Postfix) with ESMTP id 1480040024 for ; Wed, 4 Oct 2023 19:03:18 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf01.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696446199; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nGJLAZBPfk0bEDQ5MQJUdfphtdeAgk92zsf1B92wAEU=; b=LpPRwphSwPIPNW6M6kP1mxIF8sidIhr5OMDxTULvCtvk2E92gFEDOPDyvTyL/p7De2Ze6G YH2iSHrhKe0kPdDXbgw5+7tvr2dHY9n17O2e/089KSteRPnYqj3K1nqPPJHyAbuNFqwqG9 NDYBNimCUQXTY/N5C0Op3vRal4b+l2I= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf01.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696446199; a=rsa-sha256; cv=none; b=CpUAjs04hPjddRcWMvi8d46WFtUf4YyCNxqPAwUUtD/Cz8ZFkeB0pumNcVb6gF5K3T2QYW MZbwHl3cVDSoUHimtaBhwZYLcEgCMKZrXm5hnLcGCfKqwER78sjug0+nYOEn0QRqGz8axd xnq01Gd2nJuoVsO3Ui17gnEBNlO9Z+k= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 0521ED08C001; Wed, 4 Oct 2023 12:03:02 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 3/4] mm/ksm: add tracepoint for ksm advisor Date: Wed, 4 Oct 2023 12:02:48 -0700 Message-Id: <20231004190249.829015-4-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231004190249.829015-1-shr@devkernel.io> References: <20231004190249.829015-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 7ihw45nb9kzjno6din7fqgtgd68dm94o X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 1480040024 X-HE-Tag: 1696446198-207927 X-HE-Meta: U2FsdGVkX1+nxEcC6AUMKXHgayN5IDGFKg30pWdik+TSPkXVzouWJibatc1b56N5eINhxTS8nkMG5PnRi3hTDLBINmKlPTTQw6KG4eQiw8/4Fb9CabpuoZuiKjjCTpVcelB5WctTPmvTCwtSzX20yODD3SHeBYobHyVTvj4OZtkLYjqkERj+s6SNPx71EbslR9ftysmt/kMwAkSaHJ4ikAAAcgZ2lBv26S7J85sFCQcrLByi8l6Lu1iHExKrPbvb+Xc+mxBs3tzCVA9rd4sSMnd42CEk64t8COOX3+dv3pPsTSxIhLRPj+bAYn+iNk71pnUFhx13jpXlurMfsLA9x2e9psATaNSSE1uSDHKBzvTN8YohRToBzUHxQd2zM4DD0R34baQ+LIs7+LpdKuL0iai+kJ1/+JOKS3fRYWe0i535pfavS8ZwLsbFtC4P5nVouGkdgx5IzA58niMA0Do3vZpZQc9QfWC2s+fKKIo56u99HiiGtsOtyeppVp/pedHIotx2mV/bk/jiaEJkie6qf+ILrH7C8BEMUH3QAovOhzICCH1DvD1QuTfDbW0s+XXDojJWqSArm8ap+4zzC6meEG4Qy0PExj332M5OKGNH1O25taZcCVl7GPgkTEOA5IWMD0ImAQKEWvk+IvrvCZi5fQynwAUR0oD/g9CP989W7WqyFWZ+sKjJ6sy+jtjChfwolBTmIvur+UBcCSKZ+LQmGpmuOjfhjVFOioqjLH/5b1TEc63MdY4UrW2olJ/GC+xITWIXUuWULCaEfaa8cPRArZg50MVm9pPPTvWB3OfLNlD5JJ8Ykb2vnjL6u5qK158tQxdI93Gtu/JfSq3uH2LV6oo4waryY1AWxK3JUN3S+shGX9o/H60uqkuHki5MYxoxbHqLOLKVwW6zIccPZ/ps5WQYXVndW0hce4t8pzOXQQajHImoSmJ0RHR53Am3uW2Asl+nt/bu2VQUCE1gC+P LLV6R6SW 1R8boKdB3Zyn+QzjJ89nJ+Oz/wZMWjgVfmP5d X-Bogosity: Ham, tests=bogofilter, spamicity=0.000131, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This adds a new tracepoint for the ksm advisor. It reports the last scan time and the new setting of the pages_to_scan parameter. Signed-off-by: Stefan Roesch --- include/trace/events/ksm.h | 28 ++++++++++++++++++++++++++++ mm/ksm.c | 2 ++ 2 files changed, 30 insertions(+) diff --git a/include/trace/events/ksm.h b/include/trace/events/ksm.h index b5ac35c1d0e8..164133014922 100644 --- a/include/trace/events/ksm.h +++ b/include/trace/events/ksm.h @@ -245,6 +245,34 @@ TRACE_EVENT(ksm_remove_rmap_item, __entry->pfn, __entry->rmap_item, __entry->mm) ); +/** + * ksm_advisor - called after the advisor has run + * + * @scan_time: scan time in seconds + * @pages_to_scan: new pages_to_scan value + * + * Allows to trace the ksm advisor. + */ +TRACE_EVENT(ksm_advisor, + + TP_PROTO(s64 scan_time, unsigned long pages_to_scan), + + TP_ARGS(scan_time, pages_to_scan), + + TP_STRUCT__entry( + __field(s64, scan_time) + __field(unsigned long, pages_to_scan) + ), + + TP_fast_assign( + __entry->scan_time = scan_time; + __entry->pages_to_scan = pages_to_scan; + ), + + TP_printk("ksm scan time %lld pages_to_scan %lu", + __entry->scan_time, __entry->pages_to_scan) +); + #endif /* _TRACE_KSM_H */ /* This part must be outside protection */ diff --git a/mm/ksm.c b/mm/ksm.c index 12e70f806b2b..93dff974f6ea 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -415,6 +415,8 @@ static void scan_time_advisor(s64 scan_time) advisor_ctx.change = change; advisor_ctx.scan_time = scan_time; ksm_thread_pages_to_scan = pages; + + trace_ksm_advisor(scan_time, pages); } static void run_advisor(void) From patchwork Wed Oct 4 19:02:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13409343 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12844E7C4ED for ; Wed, 4 Oct 2023 19:03:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 111A88D00B2; Wed, 4 Oct 2023 15:03:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEF5F8D00AE; Wed, 4 Oct 2023 15:03:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF2C68D00B0; Wed, 4 Oct 2023 15:03:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BF5F38D00AE for ; Wed, 4 Oct 2023 15:03:20 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7B458120404 for ; Wed, 4 Oct 2023 19:03:20 +0000 (UTC) X-FDA: 81308702160.10.3D9684E Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf27.hostedemail.com (Postfix) with ESMTP id C41364002D for ; Wed, 4 Oct 2023 19:03:18 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf27.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696446198; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IrnoYKEsaMdrdPk/QW6pcNkJHjkVEFDhjGBdxi2+XMU=; b=7kYFGtYoihGPRGW0MqibQ0qQZVHdIqRrQjKo/ArrkiL/zcYlPQVj2s81sfxKJpO4offA+D hpu1Xdh+dfc8KZhXez2qJTzOsmjQ/vqve0kL0oWFUwBSbA8XDhgdk+f7OxVdKDq88xlNcK hSNPI8YmD86oeFLEvNmXam5f/MIY47E= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf27.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696446198; a=rsa-sha256; cv=none; b=zmNJiEy5baxlzVsV81Fk2l/Gwlqp6G4Mx9lm4QhWkja+YsTya+UasJe0hPSl9qbVnSbq2d tErFD/N7gvdlvdKf4+91ansRrNpl2k1eHUxrkoOFnm6+/vdaqfKXkIXkeal5wcx9jRM4WA wblMAzD2epnz8i12qYZO+cjn6gG+8qw= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 0F49CD08C004; Wed, 4 Oct 2023 12:03:03 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 4/4] mm/ksm: document ksm advisor and its sysfs knobs Date: Wed, 4 Oct 2023 12:02:49 -0700 Message-Id: <20231004190249.829015-5-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231004190249.829015-1-shr@devkernel.io> References: <20231004190249.829015-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: C41364002D X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: hdguo8f68eadx1351zcfpo8yoamj4ra7 X-HE-Tag: 1696446198-645527 X-HE-Meta: U2FsdGVkX191JHZIihmanRV9dIRU7VovWjDiT+aUufffRy7tfRIT0vU6SpJ9Ub5NQsYjzRPDkjaVq3nUA3LPHbFEs3I+SCwdJjpCr9L0xFbNdwbINT+vGEnCgWNLuQ74PGw0VzElyy//dasFBNn56ZCKYJD60gta2/Xc2uBYyZMx5QvZ+3AbUpY/psnRW69fGLdLr4D9BzLMINk4UUOVfBhebdCfGyBqhom/EFDF04OJUCYbb5zAHdriXhJumxkcCdtdNfOdVAIQlOjfnRq0ea/owgtG3tIArDDM1UlbxOgjGzmzPnppcKiyZeBqDTYyCSldC/vIRyCQ4lcdzdG8WBWCDfCeCExrKabt+fuXnoZjleE/za9SmUtj9nyKdfraZUKz++ZumqbrtaEWPObe+4yY0eKtgnX5OibbqX9KbZ8zXBRA5vVu58jZCbwu3eJRsRP1wmPKkm9XMpPsXdhebAWAdOq4s8I5TG+Ajjy59Wod7HFqu1dYWr540KQg1mUtW9qaF8gLJsJinDEPevplHhDLXypFzVotVZ3GIVdX3R62dEJVS/jiN33Yb1PSVMJgNG2r9KKKiaGAWAldTjk0N4em/8apyXelCh5NitAE7cxwb0aN6uuz+IK24c6TlviyryUSiRZq/1jjJTILnx27OCPkfWWFEyu/gvMiCMmdbA1tAN4iVlqaIKXFUgs+42hWRXeh7/DPpHyNXPJ6TkLZEwoPJIcFP5Onn+7QPlefU/rCTQR9LyeDdf1r8H/4WUgoNUuZ5PwUuAyYK4j7h9zVwsLbDgjqoPpHbhuD172ttCOfS20vk+06Gjw6ISs10B5LLzv9ZA17su8GTD6Vqru0IFWasYG1U6H+PvR1QSz6IokkHDv6WFJTCtXhX4kJtbUpczzD+foUfeekADatDr4FKEDO9H978TSf8GvgT0Gtv2FYBDF4sMO/+V5rXjB8ks7ecH0OYX0TpEeBZH+dZbG Ii+2OoCw +Y7/I6Nz+98sr6fVdpyHFbiIEpxtUvInIx+sA X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This documents the KSM advisor and its new knobs in /sys/fs/kernel/mm. Signed-off-by: Stefan Roesch --- Documentation/admin-guide/mm/ksm.rst | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index e59231ac6bb7..8de93650d3b0 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -164,6 +164,25 @@ smart_scan optimization is enabled. The ``pages_skipped`` metric shows how effective the setting is. +advisor_mode + The ``advisor_mode`` selects the current advisor. Two modes are + supported: 0 (None) and 1 (Scan time). The default is None. By + setting ``advisor_mode`` to 1, the scan time advisor is enabled. + The section about ``advisor`` explains in detail how the scan time + advisor works. + +advisor_min_pages + specifies the lower limit of the ``pages_to_scan`` parameter of the + scan time advisor. The default is 500. + +adivsor_max_pages + specifies the upper limit of the ``pages_to_scan`` parameter of the + scan time advisor. The default is 5000. + +advisor_target_scan_time + specifies the target scan time in seconds to scan all the candidate + pages. The default value is 200 seconds. + The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``: general_profit @@ -263,6 +282,32 @@ ksm_swpin_copy note that KSM page might be copied when swapping in because do_swap_page() cannot do all the locking needed to reconstitute a cross-anon_vma KSM page. +Advisor +======= + +The number of candidate pages for KSM is dynamic. It can be often observed +that during the startup of an application more candidate pages need to be +processed. Without an advisor the ``pages_to_scan`` parameter needs to be +sized for the maximum number of candidate pages. The scan time advisor can +changes the ``pages_to_scan`` parameter based on demand. + +The advisor can be enabled so KSM can automatically adapt to changes in the +number of candidate pages to scan. Two advisors are implemented: 0 (None) and +1 (Scan time). With None no advisor is enabled. The default is None. + +The Scan time advisor changes the ``pages_to_scan`` parameter based on the +observed scan times. The possible values for the ``pages_to_scan`` parameter is +limited by the ``advisor_min_pages`` and ``advisor_max_pages`` parameters. In +addition there is also the ``advisor_target_scan_time`` parameter. This +parameter sets the target time to scan all the KSM candidate pages. The +parameter ``advisor_target_scan_time`` decides how aggressive the scan time +advisor scans candidate pages. Lower values make the scan time advisor to scan +more aggresively. This is the most important parameter for the configuration of +the scan time advisor. + +The ``pages_to_scan`` parameter is re-calculated after a scan has been completed. + + -- Izik Eidus, Hugh Dickins, 17 Nov 2009