From patchwork Mon Dec 18 23:10:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13497671 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 C5EF0C35274 for ; Mon, 18 Dec 2023 23:13:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6747B8D0006; Mon, 18 Dec 2023 18:13:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5D6688D0002; Mon, 18 Dec 2023 18:13:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 401368D0006; Mon, 18 Dec 2023 18:13:40 -0500 (EST) 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 292278D0002 for ; Mon, 18 Dec 2023 18:13:40 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 07D241201AD for ; Mon, 18 Dec 2023 23:13:40 +0000 (UTC) X-FDA: 81581493000.20.9BDD33E Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf01.hostedemail.com (Postfix) with ESMTP id 5C61540017 for ; Mon, 18 Dec 2023 23:13:38 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=neutral (imf01.hostedemail.com: 66.220.144.179 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=1702941218; 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=xvQTh5z+hSn3wXnyXbOfJiynjbSPdq/w8wmlRxmqDVM=; b=S3UU3QxOA6Ar3aIl7BUA5Gc3MzW3xyRmaeJuM7kb+F3a82UOg/DeSQbC7czYea4x+xWLmj POzB3AvL6xIHpe8InBbiUcbqlGDdPKaPfQd7DKdPD/NhhzpfWwCWBcT0p4gBMnoo2+FayF zQMC5/WwDSXnUNDL44+jHs/GkmToUdY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702941218; a=rsa-sha256; cv=none; b=YEpyhxwKfiDpkSZ4bbhWR4TOoyd7WtYurWZjYkyHT4C7qRyRp4FSgTOiY/z8wgskzMZXJu a3pz8jqrNfJv2AuitTSvs+IQLR5N3bZFRBbjp7j4h4tL5/8Z3NDFmeg84mDiLJF5Xy/UNk O79QcVoae5BmKn54moJ5m0AfTD/PmQA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=neutral (imf01.hostedemail.com: 66.220.144.179 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 0523F111427AC; Mon, 18 Dec 2023 15:10:56 -0800 (PST) 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 v5 1/4] mm/ksm: add ksm advisor Date: Mon, 18 Dec 2023 15:10:51 -0800 Message-Id: <20231218231054.1625219-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231218231054.1625219-1-shr@devkernel.io> References: <20231218231054.1625219-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5C61540017 X-Rspam-User: X-Stat-Signature: 94n9gahgsk5g67kyo8cncuhsmdjrwqn3 X-Rspamd-Server: rspam03 X-HE-Tag: 1702941218-450311 X-HE-Meta: U2FsdGVkX195W8hjRdybDedmerisW09tTbcNPGiaZEENcVIsqYweAb/xJxIqllJ/jOZrwRYYuPi2W6Pv5CcmqlXuYzyuyJDDe/rweYr89NUgt2rRjbel2CZv9NOpE546d8FAUdqsmGu+7UnttRywyjmM/4j0ZE6xC496DP1twZWnT7QtsdPknep66ppkbFhB2v2s/pKI2nI7mFBthd2Z/L7gFO1sYB90OwaY0arHCaGnjf93KrKhYb4mBDbiDeRLlhrJa9nyEKmx4X0kUWdjMhDSa90pXUuCwpic3g2LPRxiD6HQ5xlExpeWXx/6mqmXr57ogNLP/Jfj00GCK0277pXoQt67UTwb6OZ24WszoJZAm9wY+QPwE8WHdUYazMMA7KV8rq2I5BxJU/ICJ/T5d4uw8r7tBTAWlReeUuBg0XWu0jhrhCEmL1jTkgNvUtFvYpchvlaN6N+XBA4YGJdpu4hvKar5sbkbtTLh0nII5E7A++emLVpmglJkCD2uRkE9IQ8Ap2+F5hW7tbr284E/URtmSPRovXhy+ZNArzlODAzRhb/3+eiMGJ7OQse4B8af6Zqqur3G24vJeeVj55pzT2Yz3aQKcTMHw3ZnYvrKuQAGquNzbL2OADfx9zoimFx56vdOlObubcqGJBf+vw9YaxIeJnSc3/1eM3cq+6tSTxGAhs5/eZAS1hNl5sWoTcTsXnXJ6T526JWcRavZQQprIkTB/9cQ0rWqdCwytjf+5KfLZzle/WmLHCcMBr6n2lvIdx/BjJx8bwjd5UxkAo+Sz08a+tCvi+1GtSIoJvPfCDiyX4gEb6Uj8CLdBLs6jXIzMRMhFYThk92itIM7TnJRLRIk8DMAaW3rnXswSCLETTd0l8dw1BsG6qnFwDnAMjK6FCtOchIw9g2bCbKx62bLI/fg3zv380qYxEd3PuNHTu9vD9Q1+uZ1S1Wlgveku4EIUlnjUfNPxwuOdpmIoXl d8U2bqib l2aElkxIvrfEdDc7t2qv/Fn+s4oL6GUGTcjqXeVoKZGf9zD8= 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: List-Subscribe: List-Unsubscribe: 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 - limit CPU resource consumption The respective parameters are: - ksm_advisor_target_scan_time (how many seconds a scan should take) - ksm_advisor_max_cpu (maximum value for cpu percent usage) - ksm_advisor_min_pages (minimum value for pages_to_scan per batch) - ksm_advisor_max_pages (maximum value 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. The advisor is managed by two main parameters: target scan time, cpu max time for the ksmd background thread. These parameters determine how aggresive ksmd scans. In addition there are min and max values for the pages_to_scan parameter to make sure that its initial and max values are not set too low or too high. This ensures that it is able to react to changes quickly enough. The default values are: - target scan time: 200 secs - max cpu: 70% - min pages: 500 - max pages: 30000 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 Acked-by: David Hildenbrand --- mm/ksm.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 157 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 16532fa85a467..59911700695f7 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -248,6 +249,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 +280,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 +301,152 @@ unsigned long ksm_zero_pages; /* The number of pages that have been skipped due to "smart scanning" */ static unsigned long ksm_pages_skipped; +/* Don't scan more than max pages per batch. */ +static unsigned long ksm_advisor_max_pages_to_scan = 30000; + +/* Min CPU for scanning pages per scan */ +#define KSM_ADVISOR_MIN_CPU 10 + +/* Max CPU for scanning pages per scan */ +static unsigned int ksm_advisor_max_cpu = 70; + +/* 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 30 + +/** + * 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 + * @cpu_time: cpu time consumed by the ksmd thread in the previous scan + */ +struct advisor_ctx { + ktime_t start_scan; + unsigned long scan_time; + unsigned long change; + unsigned long long cpu_time; +}; +static struct advisor_ctx advisor_ctx; + +/* Define different advisor's */ +enum ksm_advisor_type { + KSM_ADVISOR_NONE, + KSM_ADVISOR_SCAN_TIME, +}; +static enum ksm_advisor_type ksm_advisor; + +static inline void advisor_start_scan(void) +{ + if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) + advisor_ctx.start_scan = ktime_get(); +} + +/* + * Use previous scan time if available, otherwise use current scan time as an + * approximation for the previous scan time. + */ +static inline unsigned long prev_scan_time(struct advisor_ctx *ctx, + unsigned long scan_time) +{ + return ctx->scan_time ? ctx->scan_time : 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 perturbations 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 + * + * The new_pages_to_scan value is limited by the cpu min and max values. It + * calculates the cpu percent for the last scan and calculates the new + * estimated cpu percent cost for the next scan. That value is capped by the + * cpu min and max setting. + * + * In addition the new pages_to_scan value is capped by the max and min + * limits. + */ +static void scan_time_advisor(void) +{ + unsigned int cpu_percent; + unsigned long cpu_time; + unsigned long cpu_time_diff; + unsigned long cpu_time_diff_ms; + unsigned long pages; + unsigned long per_page_cost; + unsigned long factor; + unsigned long change; + unsigned long last_scan_time; + unsigned long scan_time; + + /* Convert scan time to seconds */ + scan_time = div_s64(ktime_ms_delta(ktime_get(), advisor_ctx.start_scan), + MSEC_PER_SEC); + scan_time = scan_time ? scan_time : 1; + + /* Calculate CPU consumption of ksmd background thread */ + cpu_time = task_sched_runtime(current); + cpu_time_diff = cpu_time - advisor_ctx.cpu_time; + cpu_time_diff_ms = cpu_time_diff / 1000 / 1000; + + cpu_percent = (cpu_time_diff_ms * 100) / (scan_time * 1000); + cpu_percent = cpu_percent ? cpu_percent : 1; + 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 = ksm_thread_pages_to_scan * 100 / factor; + /* Update pages_to_scan by weighted change percentage. */ + pages = pages * change / 100; + + /* Cap new pages_to_scan value */ + per_page_cost = ksm_thread_pages_to_scan / cpu_percent; + per_page_cost = per_page_cost ? per_page_cost : 1; + + pages = min(pages, per_page_cost * ksm_advisor_max_cpu); + pages = max(pages, per_page_cost * KSM_ADVISOR_MIN_CPU); + pages = min(pages, ksm_advisor_max_pages_to_scan); + + /* Update advisor context */ + advisor_ctx.change = change; + advisor_ctx.scan_time = scan_time; + advisor_ctx.cpu_time = cpu_time; + + ksm_thread_pages_to_scan = pages; +} + +static void advisor_stop_scan(void) +{ + if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) + scan_time_advisor(); +} + #ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes = 1; @@ -2401,6 +2551,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_start_scan(); trace_ksm_start_scan(ksm_scan.seqnr, ksm_rmap_items); /* @@ -2558,6 +2709,8 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) if (mm_slot != &ksm_mm_head) goto next_mm; + advisor_stop_scan(); + trace_ksm_stop_scan(ksm_scan.seqnr, ksm_rmap_items); ksm_scan.seqnr++; return NULL; @@ -3248,6 +3401,9 @@ static ssize_t pages_to_scan_store(struct kobject *kobj, unsigned int nr_pages; int err; + if (ksm_advisor != KSM_ADVISOR_NONE) + return -EINVAL; + err = kstrtouint(buf, 10, &nr_pages); if (err) return -EINVAL; From patchwork Mon Dec 18 23:10:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13497668 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 5F94DC46CCD for ; Mon, 18 Dec 2023 23:11:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D812F6B007B; Mon, 18 Dec 2023 18:11:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C645F6B0081; Mon, 18 Dec 2023 18:11:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB5286B0083; Mon, 18 Dec 2023 18:11:13 -0500 (EST) 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 92C926B0080 for ; Mon, 18 Dec 2023 18:11:13 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5F15E140319 for ; Mon, 18 Dec 2023 23:11:13 +0000 (UTC) X-FDA: 81581486826.04.264CD89 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf23.hostedemail.com (Postfix) with ESMTP id B3864140012 for ; Mon, 18 Dec 2023 23:11:11 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=neutral (imf23.hostedemail.com: 66.220.144.179 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=1702941071; 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=L3q75dl+BZQIu+nWeHzNCDMG1g+e331+bfkTG7uGuu4=; b=0SOjrQSzJv6w7Cy2hp4h1kcW0pHdgLYjirYh2mL775CUsygaI+T+HWaMarQrmmHoGZuWpa WtWG+XI9G3wQdUU3qB4ik6UY31HQydl8ixe6xwo6jxtHSrV2tWQQVUrWgwzMKaPHwYz5dS rc4gd6zw39S6UzvbqumvMZFJsM1VaeE= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=neutral (imf23.hostedemail.com: 66.220.144.179 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=1702941071; a=rsa-sha256; cv=none; b=6J3uk3k+/1EDSGYGMgEPnwuvVoJ8QACQfjJL8pKJpHxfkkYWynIhBa9n8R9+pj197dUxpi tY+N5URX2G4YyYnb1dy1q48v+4G8jO+4eSsuRKYo48zQgk4VtTrHui/EJyYBYPrqhE7kxZ IgXBUHa1h4UhxOpnyXiR8h5AnXiMu18= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 1568F111427AE; Mon, 18 Dec 2023 15:10:56 -0800 (PST) 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 v5 2/4] mm/ksm: add sysfs knobs for advisor Date: Mon, 18 Dec 2023 15:10:52 -0800 Message-Id: <20231218231054.1625219-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231218231054.1625219-1-shr@devkernel.io> References: <20231218231054.1625219-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: B3864140012 X-Rspam-User: X-Stat-Signature: m64fufbfz9qq48b154z9ceo11ctzmz1w X-Rspamd-Server: rspam01 X-HE-Tag: 1702941071-356402 X-HE-Meta: U2FsdGVkX18XJT4K/fSMXHQOlrp0zVEFqxNrRDUsprudPUsj2G6wdeTJeq7HmC8OtyKUyAIXUWMFmcn6yMh3yqeqM535hQAJDF4FhdW+cEaySFcYHxVOKM3mY3Z9gzaLLEgwAlVi/kFExZaCoAnaTY21mLs7jC1pwYkHJQt9cHgeY1cN2+U6lsltv7qOUIFqOyRKAqJLZECvPfrIZsKU5z9OQ7kEAOdbb84Brz0ZbUCdq9PICAw+H8BLvKDeyTBhweDkPrnWzOT+OnAXmA9lzIyTMe1BfvGUK7+Mi+g1acIY0fNlnvEU7mrJfcKRZfa8bO83uNkt6zA52luvl1d7ymoTaqcB1Meku/vc7N9dRb0KqRxD/YPrq9QS+LzHW55Av/L5NEyMWguAcaPnO15vTN465XwDkmU9AM8nLR0hFLrQtvGaYUffF4Sjd1QVy5ZzlVFNZX1KHwHLsHRklscYPflEkisF/z2VPxR1YJtvFs2YvC7CsCcCd0+jtiPKyM0aYOOHrpvwqrK2dJsU21Cfiam/mPBOj5LJHNe6yNmy2VETLAKREO1fwcCFeMHz3uetMB+365vtixaGPktYa4mdxTNP6TzlkDYRh6gEzoEcaX5sQYboacPHnkx5F7tad7PsscBVNDjHZSfRFSoxoagC7hCxXcNtIBIpOjQrQ07Vjleu7dDNWbXTYyVmmkMQOjziRYnG4VtpLtW3+mmGzJlNWVLwWLw+HlkXEvcrxqSmk7AJAOoZ5tN+YZc5lysSOknPUYn0mC4DEkfSvgubwgxyMOG/kQQUIxOOiBPxIyqxcyCDPp5nRl7w5RilWfYQ6HwkDmooZ5BncVAPr1o/Cy9hyOEJLsQ1FvAG/WA7VVSNQ/WvX1Q/MbIw5wxUGvHF6YeD+bo7PCM7YrgvRHK1gGkS0hyM74La2vlL2XAy2kvCLCc2Y700HT1/wsdqc2wENJJr4m+BqQkD4c7KbCzkzT0 r14cX6ia 5e1Tgw9ATpF1J5wrKElo5K/3s5LuZZw/78FJQ 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: List-Subscribe: List-Unsubscribe: This adds four new knobs for the KSM advisor to influence its behaviour. The knobs are: - advisor_mode: none: no advisor (default) scan-time: scan time advisor - advisor_max_cpu: 70 (default, cpu usage percent) - advisor_min_pages_to_scan: 500 (default) - advisor_max_pages_to_scan: 30000 (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 Acked-by: David Hildenbrand --- mm/ksm.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index 59911700695f7..10e2696907250 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -338,6 +338,25 @@ enum ksm_advisor_type { }; static enum ksm_advisor_type ksm_advisor; +#ifdef CONFIG_SYSFS +/* + * Only called through the sysfs control interface: + */ + +/* At least scan this many pages per batch. */ +static unsigned long ksm_advisor_min_pages_to_scan = 500; + +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) { + advisor_ctx = (const struct advisor_ctx){ 0 }; + ksm_thread_pages_to_scan = ksm_advisor_min_pages_to_scan; + } +} +#endif /* CONFIG_SYSFS */ + static inline void advisor_start_scan(void) { if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) @@ -3723,6 +3742,130 @@ 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) +{ + const char *output; + + if (ksm_advisor == KSM_ADVISOR_NONE) + output = "[none] scan-time"; + else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) + output = "none [scan-time]"; + + return sysfs_emit(buf, "%s\n", output); +} + +static ssize_t advisor_mode_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, + size_t count) +{ + enum ksm_advisor_type curr_advisor = ksm_advisor; + + if (sysfs_streq("scan-time", buf)) + ksm_advisor = KSM_ADVISOR_SCAN_TIME; + else if (sysfs_streq("none", buf)) + ksm_advisor = KSM_ADVISOR_NONE; + else + return -EINVAL; + + /* Set advisor default values */ + if (curr_advisor != ksm_advisor) + set_advisor_defaults(); + + return count; +} +KSM_ATTR(advisor_mode); + +static ssize_t advisor_max_cpu_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%u\n", ksm_advisor_max_cpu); +} + +static ssize_t advisor_max_cpu_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_cpu = value; + return count; +} +KSM_ATTR(advisor_max_cpu); + +static ssize_t advisor_min_pages_to_scan_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_min_pages_to_scan); +} + +static ssize_t advisor_min_pages_to_scan_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_to_scan = value; + return count; +} +KSM_ATTR(advisor_min_pages_to_scan); + +static ssize_t advisor_max_pages_to_scan_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_max_pages_to_scan); +} + +static ssize_t advisor_max_pages_to_scan_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_to_scan = value; + return count; +} +KSM_ATTR(advisor_max_pages_to_scan); + +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, @@ -3745,6 +3888,11 @@ static struct attribute *ksm_attrs[] = { &use_zero_pages_attr.attr, &general_profit_attr.attr, &smart_scan_attr.attr, + &advisor_mode_attr.attr, + &advisor_max_cpu_attr.attr, + &advisor_min_pages_to_scan_attr.attr, + &advisor_max_pages_to_scan_attr.attr, + &advisor_target_scan_time_attr.attr, NULL, }; From patchwork Mon Dec 18 23:10:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13497669 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 AAFA1C35274 for ; Mon, 18 Dec 2023 23:11:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 22BC26B0080; Mon, 18 Dec 2023 18:11:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1DBA66B0081; Mon, 18 Dec 2023 18:11:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C9566B0085; Mon, 18 Dec 2023 18:11:14 -0500 (EST) 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 EA2FA6B0080 for ; Mon, 18 Dec 2023 18:11:13 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A85AE1C12CF for ; Mon, 18 Dec 2023 23:11:13 +0000 (UTC) X-FDA: 81581486826.02.F0C29DB Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf28.hostedemail.com (Postfix) with ESMTP id 1B31BC0019 for ; Mon, 18 Dec 2023 23:11:10 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf28.hostedemail.com: 66.220.144.179 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=1702941071; 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=1Fz3XLdrz2+bpgvBdPbZhoJrVUUleR20qZ12O9sFG0I=; b=qlZRRpll3DBemwmZZ1uFcNXgbfI09CDJfCetWLgXAVzTWXkEN+DnneKCPPwq3HpPhB8G5x AtL30kMJ9yCL8jaGId9Ywi3wu6WrpODhhbD+itWl3IxDm0d8b1wLHXyNz+G1eseKvbSN69 rjAzOs+E3KWHiA6YZsd7PveZN8tldcM= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf28.hostedemail.com: 66.220.144.179 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=1702941071; a=rsa-sha256; cv=none; b=fyrM+oxXqR7LIKpxkgLPUVnT2qUyIWUvgXBflmDjcB4KbH15gOb+CJo3Py4kJ6mrnHfTBj D7/SluCFm37u6pps6D69U13rp6quM3D6EJZ7SnKCLa5+XvgjpKUmCZfsR9Z8/D6PmNe9U8 MvRaw1mpjnH9C0n5PBYZKpNqiTYc4GI= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 7028E111427B3; Mon, 18 Dec 2023 15:10:57 -0800 (PST) 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 v5 3/4] mm/ksm: add tracepoint for ksm advisor Date: Mon, 18 Dec 2023 15:10:53 -0800 Message-Id: <20231218231054.1625219-4-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231218231054.1625219-1-shr@devkernel.io> References: <20231218231054.1625219-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 1B31BC0019 X-Stat-Signature: zd9jfogd15y4y1eutqpwhmrf3ki7y18x X-Rspam-User: X-HE-Tag: 1702941070-790973 X-HE-Meta: U2FsdGVkX1+DcNzPyPd4TIIPKc+vMyGtqQX2aRT/JwJCEduGoPrWYJWmVf9ScrlP8LnpKkQtRQtChnpPEDkcxDgKcTQwddvUaZv3Wpx0oL94g2Q7tbbccnXRBRCYsJLmOvH6n9TSpgQQoJU3VzdEPxkfjE87ZMsebQTCcmKeWrOLNrK98ql2jIHeWxhW3lvdPrMjaANuUicYyvPBihTgB3qVuAjyE8JrUXVbHu4AQvikgqBpUYKDAExkyB2l28fRCTt3KamL3qjRJ8TT4ZciM0GydMAjc3UVoeuTYGCYE/vWxHaAMAYijtpkd7C6UrdxNRcIoWAWZzOTGpoSoFH4F/pJZ4ex5t9Sz6P3IOuMq60gIdr5GQqDxj3CDIP7aSpSGcW4zYblY9ORY5NluBwlj6UTYcmxZ2NqBQE1p+ivnrUvEVPUfBur29OtYJqxbpsgdSTkfrxJr9QGhFlSDgTaSg+RuJYAuHkv+zEtTB1x15n28gXYQfGHkL9hXWOW1ziM/jv/cA0iTr9E48hG2BtFrmGv5oES5WPolJPzh41A920xvcg1JBdnNiOYbEBcXuKSVuVE2xXze+lqBqysPx63tsODxqutWxQ5oaoLS2d6djKRPm7MWE7R2djrYYA+goavB0DE+m/WQr0RAgknDN4V1dOt/YhIA+w1YsO5kVqTCcj3G6U6y5L0ae8tyIHyc9nipXiwhO9qBxbcCIe74wzR9mIh4CH0QRnCLEn1hUPnUcFerCC+R3+wjCUbmV1tH3AXslosE1CnGfLB/kONfPI4pNk1vb9Okkk0VP0Qkl4/Db2MHq5huwGyOZnL9uPKhtS8pFFFh6POqHhnaZ82nNnHCYrlWAgj3nPdPVQFvHlnM3QlRgyySpgB9SyfC8QBmXpHKoG8k+M/+JHmHb39EF21lhdBqNT9gIv99xTrZ0PXM6bZ0vgs0t5Axh6ned6gpx7zkI3R9uROMHh7eqnMBp7 Ct9hnVkT 4CmIrmvGbbirCjQ2zye/+LCEkbuqLRcZJ64eA 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: List-Subscribe: List-Unsubscribe: This adds a new tracepoint for the ksm advisor. It reports the last scan time, the new setting of the pages_to_scan parameter and the average cpu percent usage of the ksmd background thread for the last scan. Signed-off-by: Stefan Roesch Acked-by: David Hildenbrand --- include/trace/events/ksm.h | 33 +++++++++++++++++++++++++++++++++ mm/ksm.c | 1 + 2 files changed, 34 insertions(+) diff --git a/include/trace/events/ksm.h b/include/trace/events/ksm.h index b5ac35c1d0e88..e728647b5d268 100644 --- a/include/trace/events/ksm.h +++ b/include/trace/events/ksm.h @@ -245,6 +245,39 @@ 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 + * @cpu_percent: cpu usage in percent + * + * Allows to trace the ksm advisor. + */ +TRACE_EVENT(ksm_advisor, + + TP_PROTO(s64 scan_time, unsigned long pages_to_scan, + unsigned int cpu_percent), + + TP_ARGS(scan_time, pages_to_scan, cpu_percent), + + TP_STRUCT__entry( + __field(s64, scan_time) + __field(unsigned long, pages_to_scan) + __field(unsigned int, cpu_percent) + ), + + TP_fast_assign( + __entry->scan_time = scan_time; + __entry->pages_to_scan = pages_to_scan; + __entry->cpu_percent = cpu_percent; + ), + + TP_printk("ksm scan time %lld pages_to_scan %lu cpu percent %u", + __entry->scan_time, __entry->pages_to_scan, + __entry->cpu_percent) +); + #endif /* _TRACE_KSM_H */ /* This part must be outside protection */ diff --git a/mm/ksm.c b/mm/ksm.c index 10e2696907250..82688fc6f119f 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -458,6 +458,7 @@ static void scan_time_advisor(void) advisor_ctx.cpu_time = cpu_time; ksm_thread_pages_to_scan = pages; + trace_ksm_advisor(scan_time, pages, cpu_percent); } static void advisor_stop_scan(void) From patchwork Mon Dec 18 23:10:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13497670 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 A9BBAC35274 for ; Mon, 18 Dec 2023 23:13:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 402E88D0005; Mon, 18 Dec 2023 18:13:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 38B008D0002; Mon, 18 Dec 2023 18:13:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 203A98D0005; Mon, 18 Dec 2023 18:13:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 06C188D0001 for ; Mon, 18 Dec 2023 18:13:39 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C3C891C1367 for ; Mon, 18 Dec 2023 23:13:38 +0000 (UTC) X-FDA: 81581492916.01.9BA1163 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf05.hostedemail.com (Postfix) with ESMTP id 34B4A100010 for ; Mon, 18 Dec 2023 23:13:37 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; spf=neutral (imf05.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=1702941217; 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=oFkMIsv/KO1omoMMrIxz+uBzORyGY8S6CnCh+B+0uQ0=; b=BGTbrFzzBq6M0ewRmvP1y5oGmFUYIumIZLwLWstL3YlaOGw/muLLJCknJ3+rPd34XQUzdL OM9kmiHkhNTdAktbTP0EfqEQktGojuOyfbR1QUoJCkeEUAEF00au5RY9CXcBrJGI0Jo/+E cJIDbmZCdL+VYGrS1/4pVLcjQWHQhDU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702941217; a=rsa-sha256; cv=none; b=qzCWJuoYqv60/b/kgMthz9ozVMF7dsIvQtFd2dSAXkpMEe0jsiZWfzwDRC1FBj6zhcZh6D oF0GK8uQcBzTA3EHeIqm+mz7xep6w04tAeEO0bIkrc13PGoI+g1Ns7wBbevCvNHVuawR8b PXAXv1f57FKSpt4xltMN3wsWwUzjcDk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; spf=neutral (imf05.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id E0DD6111427B7; Mon, 18 Dec 2023 15:10:57 -0800 (PST) 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 v5 4/4] mm/ksm: document ksm advisor and its sysfs knobs Date: Mon, 18 Dec 2023 15:10:54 -0800 Message-Id: <20231218231054.1625219-5-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231218231054.1625219-1-shr@devkernel.io> References: <20231218231054.1625219-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 34B4A100010 X-Rspam-User: X-Stat-Signature: dbjqe47bf3z4o9x7bipsza3mbitgwqaf X-Rspamd-Server: rspam03 X-HE-Tag: 1702941217-239714 X-HE-Meta: U2FsdGVkX190sG8fVEePhxVHua+hKl/Om6cbFFIDyMRK5OcxAEASo/xcxxVdi954J8nbmNpPz45OPgkaAZ+jX5wb0zG69sl8l7IV5m4gbZFafREBy1gFukqy0JuyE/QXo1L1Vaw3Li2lIACodmvtqKB5REB6NIcMvpoifd10fW0fUVG45FrF+fOUYAibFTIDE1wqWbuiuZStoqLoKxZV8w29NRYAuk5jU1eKh7ZLPMA6xI/mouCn/aBtSaqkZQ4wL6Mo8lNqaBs9sFvV4lp5g/1KF3+qJcqL9wikfaKrVRiRCPPvASYv5agxlBC9LA1/adbC2Oe709Uj/J/UCpac/QmofZ85H/4rvLgybt2Blby+6jN8QIHZ/qCVBBOJb5U5W5xZ6ldA0GenK/hLhcoBj7GQKxeeMMSIzc39v4sAO5vZpokggqkr3GvoncDBVrMR2CV7jJInlv0CmoQEzOfiKZ1cdUO/Zu94h/tfWLhPNGJBM/nSALXiMkwhz+PaBMcVysXF29Rudf6BSYK68VDJXaowwPY3hPFqxKGrv5k+7QJUD2DM9sMYd/Sb7kW+o/ATcr4C2csW6q+uDBP0SV5fEqF+hVvyBAzdID0DqD24c4Jf8UGIzxOfWMLaMDGM3VrbQAIesNUVWAEUFa4FImhE+bff42fZiZBM00/ic49f5+Ks2qktvUxDArV+D9a9he2Ikud7VTpCEybhx9V1GrVqsHUuvlnZ0s68VrlpTZ1yixfQEtHPLVh5uV4C206KI30UFNLcfbsiEYLyZ8cYAJE+5ELtEvGO4vhssWkXO9KhhEpfkDe/Cn8SZSxdMAysw/0DrIb92iHkgIxKCGj/cZJrYQfQG8jmfYcVXWCRTaZF5tC4xBe8QSxYhLdHic0E9TzGZqn9XbbML0gzxwPVL7EEeAN8w2X+20rn4tTwDVbBwiQbUqewfnmkbf1dqUNUVD/NyEFhcy/sUbbMyLe75n8 sX+TvCsp BI8CEBCSZwazOiLFN0cDYFpRWBpxM1GfMlAHR9R3tkWmUS70Pj1jB8Rw9QQ== 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: List-Subscribe: List-Unsubscribe: This documents the KSM advisor and its new knobs in /sys/fs/kernel/mm. Signed-off-by: Stefan Roesch Acked-by: David Hildenbrand --- Documentation/admin-guide/mm/ksm.rst | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index e59231ac6bb71..a639cac124777 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -80,6 +80,9 @@ pages_to_scan how many pages to scan before ksmd goes to sleep e.g. ``echo 100 > /sys/kernel/mm/ksm/pages_to_scan``. + The pages_to_scan value cannot be changed if ``advisor_mode`` has + been set to scan-time. + Default: 100 (chosen for demonstration purposes) sleep_millisecs @@ -164,6 +167,29 @@ 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: none and scan-time. The default is none. By setting + ``advisor_mode`` to scan-time, the scan time advisor is enabled. + The section about ``advisor`` explains in detail how the scan time + advisor works. + +adivsor_max_cpu + specifies the upper limit of the cpu percent usage of the ksmd + background thread. The default is 70. + +advisor_target_scan_time + specifies the target scan time in seconds to scan all the candidate + pages. The default value is 200 seconds. + +advisor_min_pages_to_scan + specifies the lower limit of the ``pages_to_scan`` parameter of the + scan time advisor. The default is 500. + +adivsor_max_pages_to_scan + specifies the upper limit of the ``pages_to_scan`` parameter of the + scan time advisor. The default is 30000. + The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``: general_profit @@ -263,6 +289,35 @@ 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: none and +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_max_cpu`` parameter. 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 initial value and the maximum value can be changed with +``advisor_min_pages_to_scan`` and ``advisor_max_pages_to_scan``. The default +values are sufficient for most workloads and use cases. + +The ``pages_to_scan`` parameter is re-calculated after a scan has been completed. + + -- Izik Eidus, Hugh Dickins, 17 Nov 2009