From patchwork Mon Dec 4 23:49:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13479203 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 1B7D3C4167B for ; Mon, 4 Dec 2023 23:49:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A57796B0082; Mon, 4 Dec 2023 18:49:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A06B86B0083; Mon, 4 Dec 2023 18:49:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CE826B0085; Mon, 4 Dec 2023 18:49:24 -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 7C9696B0082 for ; Mon, 4 Dec 2023 18:49:24 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 292311403FB for ; Mon, 4 Dec 2023 23:49:24 +0000 (UTC) X-FDA: 81530779848.07.C4F4CA3 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf02.hostedemail.com (Postfix) with ESMTP id 7F9548000C for ; Mon, 4 Dec 2023 23:49:22 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=neutral (imf02.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=1701733762; 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=/VTrMN/BrcioFf/k17fIgK+kv8rzuRZxzS2IvYcj0/E=; b=ECr9zX5QT+N5zs1ZKJLIlIf0aW3kVkzdNk45boVJOZwz5zIzLDmHust6xIgHNg6Dlu4oS8 IlG+ROvGS8V8CYCRHiVDVhI26J/OP1c3ylLx5IiugY9doI8t3Zr6/PUXTJ0RZ2NlrG4tAx bwBp/xRvtxxTA+OTUCWO5EE6LPQOhOc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701733762; a=rsa-sha256; cv=none; b=bBue1qd5V7FKOkwgRvjKUYK2f2CSekkAi57s6M3hNk1lm0pHYZfh2c9hNFX73GmzzZYcBp A2T+aTqbcx8mWq+/MqdvVmx33a4dsNjSfKBdAxgc48GkTWJzHl4aqdxzzoiNmnfZ+9Nb5n yrBE92q/sAnypCAWVL+Tz2KZMd5YhD0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=neutral (imf02.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 41A8F105D3A7B; Mon, 4 Dec 2023 15:49:08 -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 v3 1/4] mm/ksm: add ksm advisor Date: Mon, 4 Dec 2023 15:49:03 -0800 Message-Id: <20231204234906.1237478-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231204234906.1237478-1-shr@devkernel.io> References: <20231204234906.1237478-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7F9548000C X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: icte3z1t1bpowujo68u4g3engauhj3cw X-HE-Tag: 1701733762-995748 X-HE-Meta: U2FsdGVkX1+54IW9wPKNa+UscDt9aPeHjjfhMH2ljDA//Ne0Q4t6zH51wd0C1Mb+xwDNLmbGmQ/lc5qcphFlJdMSsAJ0iowKhjwo/OWSEHXz5Liu8dWnKYxR8kANUVf4njj1oJdiKcdMBzhC8VtzlXwmJ7pCPN18GB6bqUJuZ3Od3xuNx9C7BktHSGAsayEQReksDWefY/txy9IQ5ZBIrxj9UN2htDdDk4HgQIbqh7gINadixVIJ0zqv0CMxrC5L1+6wB89Sq3XLykmWwSiOAiVKAeg3BdoMqqtpeMugYRTrvKCbufAgk0zz2VihNAtGl05d6gOtPstoTTA7vS9PnAdP6jixvT5jwAIezpEiZGpAQLtJLXQqMzEv50Jhe/+CxcboygEdcIJ5lpUcedbwY4bPib9dNLB6CdYnMf7WTH9PUyqF0mSjDMoMZfvX4cpDrU7KkFm7d/AlN3vANZjMkZtruFAVqTE0dFYJOLLihN8DYWC+Wz6fmg7uxr1CnD2gM4oTdQKbaXYP/ZNNWuMp7PC+D40D7XJDP8DLbLb3qd2emf1Pnjp/UfrRHDM3pFLkiRN2fmWwdjiSSWxRG+cKe3zPdmTaGRRmzyDALkk0lZK9gb5Rzd+0DkqDg5lCbP0e01yorM4gskBC2LRHqjG2HwX6SSabqlVnwT0nMXMtcQT43sC0HHiVO95m2mceeMJ16Nv+ABRbeAi5tGBWWCCjG8C96ydi6u6TU+bbjcs5he3itQ8uY1P6x/iKtISXQ+O70s/buPnY73uBMqacrZYe6WtGe6oQlkSCf6XmwjHzoL7A7qzfpXfDbzUT6Os6yTw+YyRfwMXlqi62Ow8uuQxIHME66kni14QO81tYhAVWwlZeXvfw2Tzz2FRHP1U2Kyr9hGMpGjzyeUSoz/hDD4O6sg6F1eiwMuoRvDgHMrrKFIPuW+t5rRL7SUch9ELy9Hl07pRFf0nPRAYctTO49hW LIQOmPrP UH1rQBmwRHR89QkHcexjYcOfZ7g== 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 | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 174 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 7efcc68ccc6ea..b27010fa2e946 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,168 @@ 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 void init_advisor(void) +{ + advisor_ctx = (const struct advisor_ctx){ 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; +} + +static inline void advisor_start_scan(void) +{ + if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) + advisor_ctx.start_scan = ktime_get(); +} + +static inline s64 advisor_stop_scan(void) +{ + return ktime_ms_delta(ktime_get(), advisor_ctx.start_scan); +} + +/* + * 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 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(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; + s64 scan_time; + + /* Convert scan time to seconds */ + scan_time = advisor_stop_scan(); + scan_time = div_s64(scan_time, 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 run_advisor(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 +2567,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 +2725,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; @@ -3244,6 +3413,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; @@ -3603,6 +3775,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 Mon Dec 4 23:49:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13479207 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 CAE69C10DCE for ; Mon, 4 Dec 2023 23:49:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 28E4D6B0088; Mon, 4 Dec 2023 18:49:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 23E1C6B0089; Mon, 4 Dec 2023 18:49:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 091276B008A; Mon, 4 Dec 2023 18:49:26 -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 CE4966B0089 for ; Mon, 4 Dec 2023 18:49:25 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id ADBC21C069C for ; Mon, 4 Dec 2023 23:49:25 +0000 (UTC) X-FDA: 81530779890.27.EA56574 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf05.hostedemail.com (Postfix) with ESMTP id 105B0100009 for ; Mon, 4 Dec 2023 23:49:23 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; spf=neutral (imf05.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=1701733764; 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=lSOH/I2hSe0+XvfrQBNIjBmF6L0o2lUHd9hlQ8s2gkY=; b=S+JdyIQb3r7F1aw1fFTun7qWEhVYq6GO37kvuS/2Wh+XT2YujV7aACtN79bc7KerYx5gqF KpS6lwADFSpExbRWsPg2fUi9xthFzDezUpYWz7BwahTQ823L3jDu4eV4GKXRYsZPlbB0zP UwYwUFWi1vzzMMbtTPvssas5o0g/pIs= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; spf=neutral (imf05.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=1701733764; a=rsa-sha256; cv=none; b=Zky2NJvAIkRpGt8q6Iw9Zf7SPzthjRx7pneKypIsFInpUXecFiYJg/RbDsR4VoaHTVHViA LWNtVUyhdo8qGMJFA0O/SFt7j4659J4ioUC+t+7UALx7AdMU9mkZQ8FOdwZCl9jakCMib4 wtTP8DplV2gSWQi6IgukKoH99QItkRQ= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id E345C105D3A87; Mon, 4 Dec 2023 15:49:09 -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 v3 2/4] mm/ksm: add sysfs knobs for advisor Date: Mon, 4 Dec 2023 15:49:04 -0800 Message-Id: <20231204234906.1237478-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231204234906.1237478-1-shr@devkernel.io> References: <20231204234906.1237478-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 105B0100009 X-Rspam-User: X-Stat-Signature: tf1t67n4hbhrynt4srmdjdjqw1comt3w X-Rspamd-Server: rspam01 X-HE-Tag: 1701733763-449779 X-HE-Meta: U2FsdGVkX18vMBafwcYTqg+JqD+HoEOVkp25sExIN7pjtGP0yJHNxC16tcLOhggqT2lXJ7sNWxMrxL4TZC2uzOlz5jMyQD2Wlu4KA8u+/YAJZyhYf+1jRqui2+MXF5mqaeaI4uVZeuSBbnGJ08fW0+MnXVxgqKktZft7NFSuibOQQe/nCPyGqHrhNnLBinhqq6OQ+O6+dNb3TJSMnn98LJ+jXlEWuR9K4jmR2wTkglt21dJVetPHz1PUPxt7ERhTgiGed1vtwdnyK9bXgtYrKd7HcHfFpAniEeIv+Lf/2d5Y1Tvyp+DS/3PvZfkmdcDCAlFrb8S9XyO+HiP3pYX/xHhlM+b7rNOk8iJr1msl7geXf8EhCGIsufBuo4Cz6Tq6s/+6CpYKyPVRpN/xOkJh8Mpi5fc7To7kYXEFLKkSj2cksUoyZ9TlF6gZTLwxwiGG2e4tXmMAEWrZITiDznsO0NsQYz0D49dL/pSPTfk49L0L/yc7Z5npu3ZKMKCefWsIQxgLa8KAxuigGEN/R6FW4q1nV2A3wBDTQWKj+fM62hZzsPjtEZ2wTohihoeq3RzwKwysv7U/IJNPGEvnMs7qxrTZqiJTvkCrlwU8JysyPUcmz/EPfBIY5WXIkKd7Q3JmlS+DZTEBUOWvJERxxKrCO/sw7KUY+ZJuDnLLCGjhhppWZzlNyl6vwH5TPcm1WtfkH5dmAJr9wGawgZVdOcyAaCOOTfjDiggD69BugAriK11YlwkRwRfmEMas7VFFeHr8lqlsa9i0gcNtDeV3BScnbZyn2jm/qhQOC9AyXl9SXYS8CZChYbmoSXU6gpXzba8QLb0Khbx0yKuKRXc3I9pmxgoL0O43071/ZIu42+biPHP7crEn1o7c5VIutwoaDqgW5rmJ9jm8WJo0VoF2L1FfzxRK/6wO/Ve1YmjrKNRIc/eB6L3d+IxY6NeY19Ut4YqcYhW1pfIZRtyhgiv6UP5 xFfwopg/ oHtIbMPU+f2KUCIan9MJWTN7K4Q== 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: 500 (default) - advisor_max_pages: 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 | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index b27010fa2e946..18b7185bbc65b 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3735,6 +3735,128 @@ 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 */ + init_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_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, @@ -3757,6 +3879,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_attr.attr, + &advisor_max_pages_attr.attr, + &advisor_target_scan_time_attr.attr, NULL, }; From patchwork Mon Dec 4 23:49:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13479204 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 637F4C10DC1 for ; Mon, 4 Dec 2023 23:49:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5EAC46B0083; Mon, 4 Dec 2023 18:49:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 572CD6B0085; Mon, 4 Dec 2023 18:49:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 413F26B0088; Mon, 4 Dec 2023 18:49:25 -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 319AE6B0083 for ; Mon, 4 Dec 2023 18:49:25 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0C0D21602F2 for ; Mon, 4 Dec 2023 23:49:25 +0000 (UTC) X-FDA: 81530779890.30.8E61456 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf27.hostedemail.com (Postfix) with ESMTP id 6FCAC4001F for ; Mon, 4 Dec 2023 23:49:22 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=neutral (imf27.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=1701733762; 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=KrJCHAvMqaPD2ExlYvE6d8JvnMrC8e7u3sjXW8vUDYc=; b=YUxzR+ONK94ld6jTefaRsy21HWDbYAeH1vUuX8j9BcqtR+MesO0fMPXeX9BoU9Y1qSkP6u kyuuAiaPR0vVGPkwfDoz2cBFz0SjzMIe6KW8Hf9rx1poXGT8JHsn0bvLKRF4aQao31eEgk 9aDxRH8bpiLc5aooX7WwBKSON1CvavA= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=neutral (imf27.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=1701733762; a=rsa-sha256; cv=none; b=8KgFlpj4zbHWZqycWb37Ickh5iFA7xOhn7KgYnVb/GGQjvq+TDuQz0SiIm9XAEbbXzA5Z6 5cp8txfpv0oALYdjCA4rY+sKHJOg8JRmORA/42IVrju+wUEgqwPrDHvC+9tEKgOMefipFv 27Pcqxxp9XhDF0bOxjnVHG0Zt3udsyo= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id EF772105D3A8A; Mon, 4 Dec 2023 15:49:09 -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 v3 3/4] mm/ksm: add tracepoint for ksm advisor Date: Mon, 4 Dec 2023 15:49:05 -0800 Message-Id: <20231204234906.1237478-4-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231204234906.1237478-1-shr@devkernel.io> References: <20231204234906.1237478-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6FCAC4001F X-Rspam-User: X-Stat-Signature: tpfxx69rumryrqxhki99kfcdhdscok78 X-Rspamd-Server: rspam01 X-HE-Tag: 1701733762-707583 X-HE-Meta: U2FsdGVkX1+INNaTk/i7XFHt2XvUgEZnrt66Z5qhl383vspwIpUxAyFX6K2W6Vp6uNIhW6kbUDWdkvILHYTKVqJ4u9n2Pt8k8Ei709rpMmDqZzDsqTdwsvF3q6Ly5Tdx/+9/LYjeEU93yxi7yZYiRJllhwR11gF0sTl6WomqgM27Ht6mC5+eXPcqpXLik4vfHR+IndsNXIshopH0qgudQeFBaudxnX5AvNQCTLsR4QeCkguWVYYf5ZDInghkQENUOABZR3N285Mi2DWwehKy0xW855/eDYSr0nF8jklTYcno81FgJhS+DXcj20/mfTwCpcFxAlmxjdboX+/Jcm840A72UmmWiEs73aY0BoB1ASHpyaieFBvf7bUwADXepqDKif5IT2NhYF/4JyPRydtIHAH7D/6FoobfeyOYBMy5gN1PP7Uq4s/SR2GsYwnhT6/SqyARSvW8GBR9/tYBYKWoR1nXJr2qnEOfJT65C0i/Z1nqjJLaTnSVAD02AyEv5cw0njmAiSpXJunjCiNR1jhXrJRkkcC1mhN5Gp0OhMDLzTZvIY0g8fQIiW3IdutZp6/v5AEnQNOPdSFy0Pe43sXn4+MWsd2oEbmJIIVufYPo46Uq2Qfx0ZSr/4SOCU6uXLfewzCQ1BLF54o0nwnPR6hTx12+Rya2whaHEf8TjeLv444H8ufAERWX5jcMTdvHqlDEvCO+/TwcnQYhfzB3QlLcedZPPO2GDytiEnful71g8lzDgwrqjBmJdy4LyJ3mv5W1Zv9RMSBiZ36s1g6aluQDbmM25FJfSXh0QMWT0P7xwG0ujZo7k3kuWbERVld9JZjlWXMJvBfa527lxAyJ8o/ECFjRuQcA4QcvCrauyVQwhTJBUn+56kjcowv1OckXMX7xs9neJWX/gyFPUsU1ZcOUZaNgX+pyv1bonouow/k7z96P8fgnnO8M40UVuNpnFCLd7yLtaQryldD61EGI7TS KJXeWylA qOPDcFOdeg5SzZNM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000008, 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 18b7185bbc65b..b8065c5d5f833 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -455,6 +455,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 run_advisor(void) From patchwork Mon Dec 4 23:49:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13479205 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 00721C46CA0 for ; Mon, 4 Dec 2023 23:49:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 941A46B0085; Mon, 4 Dec 2023 18:49:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AE6D6B0089; Mon, 4 Dec 2023 18:49:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B1F56B0087; Mon, 4 Dec 2023 18:49:25 -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 32F816B0085 for ; Mon, 4 Dec 2023 18:49:25 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 10C22A0D2E for ; Mon, 4 Dec 2023 23:49:25 +0000 (UTC) X-FDA: 81530779890.29.E11F24D Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf28.hostedemail.com (Postfix) with ESMTP id 72389C001A for ; Mon, 4 Dec 2023 23:49:22 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; spf=neutral (imf28.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=1701733762; 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=LZorcygG4jZkuFwNCG5i1dAxxLHjOAqJDWOY87GS9rg=; b=YA2GF9uE5GuMIC9g6KeIcxykEzsBPrjTa3q3WjZAfhZWvVXS5yZOpAQZmGXg5X8jjn0AoR rDylWchx8F1LQCnv4R0qv4T+6/NpmmtXZrBlrOWtD7DaBD445BioH/bUagli7EKeUWJX6q GuCpwqYrgIjy3mjj96QdeED4riFXuA8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; spf=neutral (imf28.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=1701733762; a=rsa-sha256; cv=none; b=dc9ok/sbYhOpiBXZXYzaWujZo9E1BrTEOAryzBX++yHUr16esSGP/X/Oh12ldpPGZHRo+4 OyQmp3A+l6olqqHneb9jT8wLNjbXA0CNqoNSA2x/m9mmijPDxAMg2GZK+29bpvJQskI0ov MITVNR9BQXAzsz2JlanXifyXYXbS5qA= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 057BB105D3A8C; Mon, 4 Dec 2023 15:49:09 -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 v3 4/4] mm/ksm: document ksm advisor and its sysfs knobs Date: Mon, 4 Dec 2023 15:49:06 -0800 Message-Id: <20231204234906.1237478-5-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231204234906.1237478-1-shr@devkernel.io> References: <20231204234906.1237478-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 72389C001A X-Rspam-User: X-Stat-Signature: 6mxi4y68z7hkith1yg5q79318oj1o1hn X-Rspamd-Server: rspam01 X-HE-Tag: 1701733762-534088 X-HE-Meta: U2FsdGVkX1/yOz3664ss681zxygD5msZYYN0qJk/Vrug0zLQL637fXLERQAGH13KIFp90RldwtZy007MzOjhsXhgXZ0t/Ha5fF2LukS2P/P0d1tuVV1V90vMnwT/ZbIQl0f98e1SeVn6iwyQw3J7wrkJUKmEPtrky5Yx6owlTgTNmE23UXfmCe7y9F1RrG2tIHFcU24DpZt1nUC4DW//JBzkzNeeXZXHKO6Wu++ZqKkAxnQlDTQwuq/GIdogWU2RZDfLVfTV4GoitI5gwBUcLrpbzE1+aeax0hsxxh9D2rLUeiJNBAOfs9y3r7GSpPNoxgQ8rvapKDjhDqrH1H+JkVweNw/ovz6rCMksd5kw6tak6/shkUFFn/FEL3fvwTwuNWAx5M+zj7IPrPPZsHPWs0OQu9FjjpNLd4oMthOJRKGHX9pWZ0SApXFHMyF7UxacFmCLLKP8kZD8hB0tV02iUsIXPRyPmtxde97QlepYrD5UvGrJRFQUVIlWj4MMJ19lgBv1njQk0XTWI3zWCEg9KwBuSAAric36ex6nNWd6lzNkvvEDm6RdCI0febsu9enPBKAg9JAJOKnb2nsshTkT82NeXvV9mg6hpUVL7bq57l0OVKkbuCXsT7uMBij66IG+kn61c4aaCrUgwSI+DRGjM+W36X0jAFEkHEAV88zL2najBpqVTSqP1YbQ2X3zhhHP21MZ68pZiR34u0j2DwjpzrUVyi37UX2hGdoRs+NeaRE40rblfEl4fwFX93ts/p6jvyYBjEZUHkOPQOZyBUoy/Q6LfFDq2OJXTRzjnFqq2m6AeDOXZr79GL1XmtWgl8sBS9e1m10tuzXaYxwrDsQEbFnB0UwoeB4tv6xeyQrlzmveZj/Czpou586QgXvfNa2HdlQ/RF7Cqzpd4vSKY1XH1FGg7Qyrar9FAflXzGdImofOmOll2lZDnXK8pa5VG/1cDeTfsYtJxQg1PfxmD5d D+j175mv WqNRN/HIII5z2xQlZ3JG5Dh8H3Fa0YTbFyafg 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 | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index e59231ac6bb71..297c4a0d04dee 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 + 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 30000. + The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``: general_profit @@ -263,6 +289,34 @@ 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`` +and ``advisor_max_pages``. The default values are sufficient for most workloads. + +The ``pages_to_scan`` parameter is re-calculated after a scan has been completed. + + -- Izik Eidus, Hugh Dickins, 17 Nov 2009