From patchwork Wed Dec 13 18:27:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13491745 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 F324CC4332F for ; Wed, 13 Dec 2023 18:27:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 597C56B04FF; Wed, 13 Dec 2023 13:27:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 546516B0500; Wed, 13 Dec 2023 13:27:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E9BC6B0501; Wed, 13 Dec 2023 13:27:48 -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 2AC9F6B04FF for ; Wed, 13 Dec 2023 13:27:48 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id F3FC3A02A7 for ; Wed, 13 Dec 2023 18:27:47 +0000 (UTC) X-FDA: 81562628574.02.86ACB6B Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf18.hostedemail.com (Postfix) with ESMTP id 415451C0016 for ; Wed, 13 Dec 2023 18:27:46 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; spf=neutral (imf18.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=1702492066; 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=nuAPNO46xDrSflQHVXVYR/HY55cJaxKaqSxB7tFgKKQ=; b=ginmIDWr0PnkE63BZeIQK23M1epWvQci7G94q/VqbVH0K4oKqN6dQqzYgJjlP835kCafJh btcvVxDaZttDi7ULaz50bQL8cqh+UXeJa2VjD0wKkDau2BuCeZBis8iuRgVS9JDs1Lnj/e p+Qonf7NDjmBNvFYpkvvF8W+WaxiKbU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702492066; a=rsa-sha256; cv=none; b=7xkYNMoG+908PWNaYIOgqzAY02a1v79Yr13+ZEZxs3Xx9AKqu9kirJfEOXDzhzRIrX0kfE PORzp53aD9YhVAxxNIxYfdQU1LOHOsxDhBycrFOogxqTPpbA2m7pyAt7lUDNk48ZTMtAi0 fxXMUXicNdsuRZfuMafH6vTUDMFN0C8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=neutral (imf18.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 A03C810CFA723; Wed, 13 Dec 2023 10:27:33 -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 v4 1/4] mm/ksm: add ksm advisor Date: Wed, 13 Dec 2023 10:27:26 -0800 Message-Id: <20231213182729.587081-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231213182729.587081-1-shr@devkernel.io> References: <20231213182729.587081-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 415451C0016 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 8q1xqbu5rg8fbamc6se9h3qqkseqzqd9 X-HE-Tag: 1702492066-584187 X-HE-Meta: U2FsdGVkX18v1E/VVGhBmOjuf1ewld48WrOqhjGcwWFoQwdJ81tqBw8rTEzBuaqB62XzhGlgkJhu/XEQ89t932tOd9sMqu3TLmKfxXrFoIapm7z6Cl/lH8QuelWPn0Kf5MR9Q0FLt8SP+oj+i9FID9wCJzluURdI5enqeP1FTGxrLDmDbA4oYTov7BouIqmK02/3QXLxpev5adhQRxWhcLYliX/NNaMc47umgWyi9hYkgZWnwRkBsErKO+PsotrJK0FJ3b0NReaXS8vcTNPqhKk+FjxppE+GB5xoygxVX99q3mXEOMOEK8swz/XTZxPE2TjxcY67OUyB3KvQp4SroiPdFWyVeYJ3fOfmzykpM3Kqa7PFBMrGvtSWNcEJsvXfLj9DQPBh903wwM2eHzSzOK5ZO4Y1r1G/GBusvKyf9hDULfWYSSmeWo22oLURG8eRCLQc3MD6dh1JemYTSO0Ht70ZoAAA86wxj+LIkRFXSY8hoWhSrieHq8soiRv3kQPc45ccCSAXf30r8LlDAOhP8G6V8mOlOMvTLRuh3i4G9l2apVTwuKvTokhI7mMh91Z0vosTopXvN7vj3CRcpiq74DjiNvWV4HGX7We7/EZRp617kz89Gz5Dt6zTWufLs8fRK/uewDBRxP5UvE6sh8uRSPadU4yjHo5mt1SsfJrbQXjpWI/Xh87VHKdMFpJBZUie6/3rRq6eVOkQK9nO2eda/4AwyBzpImsusSrqv7rFI/9gZzjLRJn4YztJKNjbTuDbsmAVL3NFnxY3xx+ouPhLtMI5/MP+oMFR0IOqtXXXLMSAWNbg8Yk4AQZ84v/ZiOCezqG16K9yh+QfvZOG1KhzRrNjDZaHHpjJI/+Rh2S9jqwkBXnAvicy68AcT7Jsyo0ycvE3g/IuAYRxre6Nvas12ecLCywnxoUbL93Qq19tCmrwmhkYCGqLanzPjrxKe/uCxc+X23L3i10VvZmUFPT W491KI1f tyM2SRrUqbcfXZ40= 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 | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 7efcc68ccc6ea..4f7b71a1f3112 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,155 @@ 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 = 30000; + +/* At least scan this many pages per batch. */ +static unsigned long ksm_advisor_min_pages = 500; + +/* Min CPU for scanning pages per scan */ +static unsigned int 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); + + /* 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 +2554,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 +2712,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; @@ -3244,6 +3400,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 Wed Dec 13 18:27:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13491749 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 9F3E2C4332F for ; Wed, 13 Dec 2023 18:27:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60FFD6B0504; Wed, 13 Dec 2023 13:27:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D97E8D0049; Wed, 13 Dec 2023 13:27:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D77C6B0504; Wed, 13 Dec 2023 13:27:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0BBC66B0504 for ; Wed, 13 Dec 2023 13:27:50 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C6F7E40269 for ; Wed, 13 Dec 2023 18:27:49 +0000 (UTC) X-FDA: 81562628658.10.4DA3F42 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf07.hostedemail.com (Postfix) with ESMTP id 19DDC4002B for ; Wed, 13 Dec 2023 18:27:47 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf07.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=1702492068; 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=hjv8NkWfXoQ+7hZi7rLJ9o88UnzNicsuLM05tnOh3cI=; b=Y9JgvDtVDQStho//kJu6y6ne4Rn8jzH34DgF+bj3sRT8x3v7N5MXfnzLmmcZFbcuqoYQFe DTuRbUYXvH+k4N/rGmjvJmXuskuw6B62kul3AGCZgSL/yPAk3XcRgKcaLsd6KOT/8o/ALd +JS8v5nMJSeQxuE6GQxe3td/9BQYPmc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf07.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=1702492068; a=rsa-sha256; cv=none; b=1ZvHBNBJVZ24MR95AAx8aYyu06gih0pMmq9jBDYZYrhDQ6Ls+EIR6m5x9Pf7fWHw3EIMys r8LAOKoEOtZ6PUzd1qR9L/nwTvxd47tzOnVkUPNemmpPaX/m+LaNIlF+ZALGBpC6la/ryv lijTGv8RuaJWmgfl0j41L8asHPHEOmo= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id AC6E210CFA728; Wed, 13 Dec 2023 10:27:33 -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 v4 2/4] mm/ksm: add sysfs knobs for advisor Date: Wed, 13 Dec 2023 10:27:27 -0800 Message-Id: <20231213182729.587081-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231213182729.587081-1-shr@devkernel.io> References: <20231213182729.587081-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 19DDC4002B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 15mx3zjxr3o5xtt5i46kdsh84czdbiym X-HE-Tag: 1702492067-418010 X-HE-Meta: U2FsdGVkX1/IxkhwjrRVWM+VXxrCoKRTPeFwGfFdVJWOlIzMAPUSS5aTdp8pGoDDoVXhqWxYSpRi+qI4tvII+BZ5D7qjX4zcbsS+lJGjYDl2uGk9nMlAfUDvSktbD4UxyUeyTwqQ2KvYv2PV1doX9mWjfEM7TZbLSInncfzH0AmDWC04OQgiguSMJLFEsY1aFNzE6d7BP665xDvYAbyZNBY0HJF//yR3OTUidkpqa9vZExtSG8h1ccvQs8wt5LzT1ZLpU/wlRga9qdQp8PoioFdwuNzXlvjWN8dodXKvneBq9BuagEMOIYnHgt2nTHfBGojmE+YCwHtlaSKgfGJem3r+s7kufx4JtS/CcWPeIGvDmZfKYE7R8Tu/qjkplaLKDLdPCpM/QxDdky2qhts6ifsa1IiYm6TYQ6KMZLXR5kGW6sijvHKlhQY/T6wibHByRMELG8tZ7ER0SvUjFhaJkxDMqFWU+beNV64HyoTSCKXZnKMegYG9C+A7w/Bs1WgBf9Ng/4vl7r2tCdMf7uaKdlIRoDKZw3J3AhA1DqiE4aYHp/8lzsuDCuxsTwK3YnCglFefevuf6RTs+9tRcqL39tlGwNU+6pPRgBtgqnchbIAcNgaeA5gV6seTN1KVNDt6rriIZfB6b/qb/k4KGZ9EHUvufdvwuLeLpXiWaKMcsD+EnP8tBSFtfBadztIEU3wGEOQkFF+RBsYQt5jJSkboVtnvo6gSPBdXMGfZPZVaNEr930mFKjRu6vK5EIKOYMaBrhicatGdpPRVzwcfUOUxVQfYnFBeHatxFx+6G7zwkJAUbPw334CpxXMndiHgd81pwZSWMxkQYzGtOcSNv4WGtRKc3AdFPZwBXOIBckZrNxFNE+LeR/Jin0dsGjkCQl17tupasYdNbL0bwUXqMocjAT71PNsxlP0Rli8QN9j6kVQi1YqEUM8SsgZb1DR0sLHALxr8NkWVi1Pgc2eZCd2 mpjap+6u raHsJUWrLgC0Hgxfrk3S2PsGrFUC2LhDK0dB2 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 --- mm/ksm.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 139 insertions(+), 3 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 4f7b71a1f3112..f7387a6d02050 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -302,10 +302,10 @@ unsigned long ksm_zero_pages; static unsigned long ksm_pages_skipped; /* Don't scan more than max pages per batch. */ -static unsigned long ksm_advisor_max_pages = 30000; +static unsigned long ksm_advisor_max_pages_to_scan = 30000; /* At least scan this many pages per batch. */ -static unsigned long ksm_advisor_min_pages = 500; +static unsigned long ksm_advisor_min_pages_to_scan = 500; /* Min CPU for scanning pages per scan */ static unsigned int ksm_advisor_min_cpu = 10; @@ -341,6 +341,16 @@ enum ksm_advisor_type { }; static enum ksm_advisor_type ksm_advisor; +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; + } +} + static inline void advisor_start_scan(void) { if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) @@ -434,7 +444,7 @@ static void scan_time_advisor(void) 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); + pages = min(pages, ksm_advisor_max_pages_to_scan); /* Update advisor context */ advisor_ctx.change = change; @@ -3722,6 +3732,127 @@ 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) +{ + 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 */ + 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, @@ -3744,6 +3875,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 Wed Dec 13 18:27:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13491747 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 C4DBAC4332F for ; Wed, 13 Dec 2023 18:27:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A79D16B0502; Wed, 13 Dec 2023 13:27:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A04186B0504; Wed, 13 Dec 2023 13:27:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C9D96B0506; Wed, 13 Dec 2023 13:27:49 -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 759406B0504 for ; Wed, 13 Dec 2023 13:27:49 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4F09780289 for ; Wed, 13 Dec 2023 18:27:49 +0000 (UTC) X-FDA: 81562628658.18.FDD1254 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf02.hostedemail.com (Postfix) with ESMTP id AEF5680008 for ; Wed, 13 Dec 2023 18:27:47 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf02.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=1702492067; 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=Ju2E2+1OfMe8Vr4nHnwCyl1I0bs/30hKeMFq9hcWi4w=; b=GexTl3IRvuT6BXyvb0dmz+pPMfVITGDwCAV7LY5/+628bFNnXzdSKmt3XE+VpACFzfUUap aOwUvlTdwWzzC8VNwUikh2WgtDRXcjhEMzMLdhwc0d9hAHhfpX2dx3W8Yl3zclGO/naIH0 oXh8qLWFxPf+3nPffxCH1bxu4yFZ4Zw= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf02.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=1702492067; a=rsa-sha256; cv=none; b=UKwztn7ASoixx3OgJjKs2VrV46YF1OP73+RCDBXyUnhTWInECcz48PJpJipkVEEBVY9wpp aDQHTwUTJCp5QazsMEte5NBBjB8HnqfjgZTs3FEJQ6f1NlV8kVFeeH8JkY3Ksct/zEEXfG NdUqs/hK51Vb0IHM17JhQ5wDQMN1Zj8= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id BCBAB10CFA72C; Wed, 13 Dec 2023 10:27:33 -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 v4 3/4] mm/ksm: add tracepoint for ksm advisor Date: Wed, 13 Dec 2023 10:27:28 -0800 Message-Id: <20231213182729.587081-4-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231213182729.587081-1-shr@devkernel.io> References: <20231213182729.587081-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: AEF5680008 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: qmpkew44tp9wz84db1bfkcewae98dgo5 X-HE-Tag: 1702492067-127830 X-HE-Meta: U2FsdGVkX198YEkd75bO2Qc0b+C1JIo6X04PBA+hFiZ/vIfAAnS73KoWFncwXRHT+rfJWFagwu04DkI23q+rLDDXH8x49k0FzyHA2rKK5tS7xVbhsaxYzSAr5hv2azuULXjGLyuEXArcs+wBsu4YXJd8teRSZA5GwQeJzIAC5ty/shXyS/eFNjCiwFjyNwlZwElXWHt00++8V5ow0k9LvuXrjbQSrkqc9dYfO3y8JbfL18ZyU6iJz+FP21QchughKJyneU1JGT3RYhEKHZaw2EOUgJIH0g2huS+/AQwIkpSZ3s8VXsbuVAEqczeeHYo968Kz0FDArdfxYVaGYcJefZgrsRhwTA8J+h+L4b9Gf3DGI9skMjy+M74VgCiTYI/lGP+ESeudMz+AL9j8GaW9K4n5WHm2ppZQwkbBI83wj2v08i+P5W9e2F5uiUDdFbeyU5O3X7/fDJ1OUlsAgzYzSxgQQPwZ5hFoZxtWWib+TrREY+HoaLKWC9kRIVJLz5Tp/DJIAikQydWbEC2JpjxW3xjfWEc+QLhRkwvRObyHo8zU39w4GyGWnYaPqS20CTxCCTsDwa1wd6Gvql59RSmf0fNTpztnrbdggfx5jmvu1LsaTmf1Mvp8wvCXTQOdXvR3wZ+eoFHk+9o0s/GmxojwNA4Otle87Hl0uUGIZ5OK1QuzDSvBYPEeD9ANi3M2AtKVyjkGrvfwm3zZKDy5HYNXDJAeSkQt7LphysP8vCjAJDxjtNT36FFLyjqsoaTdjwfjp0ZeCCche8FkwytCA/0bOotRddOLfMGD48U+91uf2Lo4PqSpmc3Z4GbpFqi/tVc02vZe/MKT75zQhq01iPceWf8q3Tt+NEfA+Zb1IP71IshjGot2TQq7l9/e5gpgB/hnw4HXIAvOlviNZxLgNwWMUiz3KipgCY4Vi4s2G/Td2OxzDiXWYlWIQziM9/NKGD7yopEQSajwCkrb6M4JRU3 lvprvVJf ByrRGwnaYr+x0xTCLq0v7QEc2xgz38G1uvGuE 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 f7387a6d02050..4962e44131e68 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -452,6 +452,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 Wed Dec 13 18:27:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13491748 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 71477C4332F for ; Wed, 13 Dec 2023 18:27:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DD7E6B0507; Wed, 13 Dec 2023 13:27:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 288746B050A; Wed, 13 Dec 2023 13:27:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 03D8C6B0508; Wed, 13 Dec 2023 13:27:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E45C96B0504 for ; Wed, 13 Dec 2023 13:27:49 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C6D091602A1 for ; Wed, 13 Dec 2023 18:27:49 +0000 (UTC) X-FDA: 81562628658.15.B81F772 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf26.hostedemail.com (Postfix) with ESMTP id 18497140018 for ; Wed, 13 Dec 2023 18:27:47 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf26.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=1702492068; 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=Kl+MBDmfa55lcVOhyEFHrcKHJXrP9nK0HhkWVodoatEmt6Dwl/i+H9qrsDRPypBg8x+zI0 2/+gpemSlGHFZQKMruxOlFs9gr76vOG3g3n4SI2dJX3N6a93hK+NSMmjhde86zW95voUPa 7u4dQXz5KLLr5iloP5q0Nb/yPMkzdMs= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf26.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=1702492068; a=rsa-sha256; cv=none; b=A62PRobPJhzbTMP+kiKo8mosPvAEH9JX0nEhMyPgadDIAE3coGKQIOAueDEGB9yB2vD9us 5UxhW2Ft0YySxC4EFKOsZzLoqb60Tt3gLmdghvrgrp5tkW125i5rxk4G6dKBfDivk1cpAw eqfz2yZix/64rPQnTvKe4tk3chbp8U4= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id C6E0610CFA72F; Wed, 13 Dec 2023 10:27:33 -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 v4 4/4] mm/ksm: document ksm advisor and its sysfs knobs Date: Wed, 13 Dec 2023 10:27:29 -0800 Message-Id: <20231213182729.587081-5-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231213182729.587081-1-shr@devkernel.io> References: <20231213182729.587081-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 18497140018 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: jsyajr5nu11j1618nyukmfn9t8kj56cd X-HE-Tag: 1702492067-343777 X-HE-Meta: U2FsdGVkX1/qC4piLtgzxvLwGnMVCofKMB2jf5bsgHBw0BmYtZMlx6RNQxqujYex3Bd98eCfUCTGQPG4m9ShhIqfetMihnwgKEBzqgBLqU+a1Tu/vBEeEJuX+2sb8iDzjtoxWQYy+32KULwT4W2MuiHqIsEYgwVN/0qO6T4/3VGngJkOua0CHzaOWuAYS6js+dn4uLxG6sNkpeDHQUssB7KUwcTNctQT2h+QCiGOrmSs22+v7UioPpNWVuxjuwC7vEdIM5J49libaF26jOzPL77nYp9RuKm/xPi/oqoMBQOzagsV9qG/iIWoVvVKA+kEG39/K5HGtpS25UJoD0i2xIp5N9FDun6v0t+nDYjFr1uxj/sh8GRS6nYiPw8QZnTdAkzXxpsAz9/4rAzhNZYOXvHE6AJ5DetKsqj4MrTZYLhzpjslc+7n2y6SQTVViYFW9WBLVZg9HuRe80LciVMPNijlh1rgc/RdHmvddMuZEk2xqGmjZlNxGcszmZXxz8F5FU4FxFuM9D4XeUTKGbY2AUoblyIB1NGN/12KuHdM9UE77HTP3Hn7YVWm75bqIIpOoRWjBYgQx1s64IdCvpV9FeQjPgfPQB/6BvTsxDyMMMJ0aUpfT4kzQwN8xZiFv4G+2SPb1DaseWSbUW4F5FgGpeq4HJ+ShpHXPtSOjlHeJTeEdshuSt51yDEjTYMYLyzJrEFm5p+y47/q7ddK6BiUDcP5CSknlQ5HpxuqZ3tKClRmJeyRJO1UtypXln9qCtE37mbm8XsYl/3mPHSX1o6q5HpuTdcW2vjhFBWbrD+Bw4KcQAIESiXcN2+pTTNgDqRQ9QcWBlBi8a3+g/jpW5BQZo3gx+ZRPBMecpD4aTxcx7fT2azGAg59mmjNsV+LPvCLIvM+hVFJ/NfcHv944+dfUzYNWRlbMJDqIk9bBFDzSjZf3xee0BBs65dYrSBoTDZkrqfqj16TBdWH4w4i/oG Fk7/olEH hM0WGZkhE3m/gb+MirtoYRpMbHV807/4wszvk+ugkspKIlg0gQXui/Lvg+w== 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