From patchwork Sat Oct 28 00:09:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13439317 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 71E70C27C47 for ; Sat, 28 Oct 2023 00:10:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 043186B0400; Fri, 27 Oct 2023 20:10:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F34CA6B0401; Fri, 27 Oct 2023 20:10:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DAEE36B0402; Fri, 27 Oct 2023 20:10:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CD2D46B0400 for ; Fri, 27 Oct 2023 20:10:18 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AFA7A160F7B for ; Sat, 28 Oct 2023 00:10:18 +0000 (UTC) X-FDA: 81392938116.06.C70EC4A 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 10EC64000A for ; Sat, 28 Oct 2023 00:10:16 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698451817; 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=z1CDgPBGhGslcSULKXQTDWeInFAmX74zdDIRHDgnakc=; b=7fv6IG09Nj0HC5gImIVMlpMX+f37LaPtO6GQ5Ofjq9OBjVXJhXLICJdpWPb7hcZvsUszgp spjqnTHRo3S5fJ+kfMGbDS7vgWqjsS2gm+LAetbQ55C3JrOiqL5sCUAZMz+SxTiU7XYTGZ mUAZGDjkHOWmaDW/kGOdODUxsvSjmHc= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698451817; a=rsa-sha256; cv=none; b=2p2PJO69C4kr84xggJYHW72Q57w/9/fHgWOlw5wy459ofC2SJGX5KXsjU8LVgjs1P6yP+N pdBBWSlU62ZRb2QKotkZNb0gSSWZ6393dw22pFH9BCNcOsVa6hT+UMzDRyVUU/k2LceYjQ SQDddXtsbWsom4dTTmGWYVbUzilymUU= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 7429EE5A2556; Fri, 27 Oct 2023 17:10:00 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 1/4] mm/ksm: add ksm advisor Date: Fri, 27 Oct 2023 17:09:42 -0700 Message-Id: <20231028000945.2428830-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231028000945.2428830-1-shr@devkernel.io> References: <20231028000945.2428830-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 10EC64000A X-Stat-Signature: zdn1op1tfiu4kiff5xg6oucj7phmq631 X-Rspam-User: X-HE-Tag: 1698451816-658956 X-HE-Meta: U2FsdGVkX18mL8xbUieBvl/61TeT9yXFUednJPWYdwef+aGd6TLvpaSYtw0BfhH6mghcf8DE6ipYx19kpmB8jJCkDtXChTZ4jgqSSwmlVsD10dh9Nqq0lmmKKimHGNozT/ch6QAqjYgkRyNHGNbNG6TRHYLOG7JJkUfvoW5f1H/S0TsLrmKbPkps4pRcz9yikJA7yDcv+ZNKLckabSSBOP8Lf8FmDpAwZLXpd0fiifeCimwsbtmmUTcyleISZZGVjhwhi3vogccpVQ495mruQbD7PHUYQIH72pq9uAAYeOzU3N+4RiBRc8DOZAMBjZufHi9OUuuV1D2pJJ9FugTH4ASy+jWLC252xZJW4cvoTrOw78tK95siBxbbUJRfvtvKijlX85PPPBUef8pHXq8exOPgx8ulU4bYPq5B/gOkpLJ3qR5SgRDtyQfCnfBQmZa7jBs1sY0RS/k5yt6+1vbnRAJS0KCeopiPzTNpv2wflAdvYT/qj7Yg0/jMYBl8e1SYUZPHbo6pACY/KQcUb6kkMkVy8z5POLDpwKaz1kFV4UdxZaJ4gghA7Vq0Y0xagNgu2cI1X8jTcXN/1m8bT5Xpp+i0HqI4It9/U2chVm/DEeldfGcR6QlN2k382IAUvuVoGSZhXKvdxkoJnoS/3BbV6RJiQwToTeWMKNqEaQOoXG8M4HiBu6ilClQq98tzUupynAFjhJ+xxaJcLfMP4GmdIGp3rtN2X33aKDTJk/SgBCYrN3ufn0WRCje+g5i4juIfBFFyj1On6VL4vx9actf4kkECqfXeXA6FI0z1RnQeVgVswttJ3VQq3m+K32Xr3HRdTHHbpWvTadokby07DpsuZX8WQRX9iwGi7AOkzEjxuqT8DaFPX4iAZONYrbUX2mkp58p9efYEXqmxpDeOPt2tERGIwyADmpUe+sHNgFIzCzaLG/zdPrBEODGu76YqWibMnQziamE2jXR/Tozx4CQ i78HkxYV BcOsFX6OKxgcG468= 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 - to avoid to spend too much CPU The respective parameters are: - ksm_advisor_target_scan_time (how many seconds a scan should take) - ksm_advisor_min_cpu (minimum value for cpu percent usage) - 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 three main parameters: target scan time, cpu min time and 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 - min cpu: 15% - 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 --- mm/ksm.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 7efcc68ccc6e..e18fecfb359d 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 = 15; + +/* 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_percent: average cpu percent usage of the ksmd thread for the last 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_FIRST = KSM_ADVISOR_NONE, + KSM_ADVISOR_SCAN_TIME, + KSM_ADVISOR_LAST = KSM_ADVISOR_SCAN_TIME +}; +static enum ksm_advisor_type ksm_advisor; + +static void init_advisor(void) +{ + advisor_ctx.start_scan = 0; + advisor_ctx.scan_time = 0; + advisor_ctx.change = 0; + advisor_ctx.cpu_time = 0; +} + +/* + * 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(unsigned long scan_time) +{ + 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; + + 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) { + s64 scan_time; + + /* Convert scan time to seconds */ + scan_time = ktime_ms_delta(ktime_get(), advisor_ctx.start_scan); + scan_time = div_s64(scan_time, MSEC_PER_SEC); + scan_time = scan_time ? scan_time : 1; + + scan_time_advisor((unsigned long)scan_time); + } +} + #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_ctx.start_scan = ktime_get(); 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; + run_advisor(); + trace_ksm_stop_scan(ksm_scan.seqnr, ksm_rmap_items); ksm_scan.seqnr++; return NULL; @@ -3603,6 +3759,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 Sat Oct 28 00:09:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13439320 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 6BD60C27C49 for ; Sat, 28 Oct 2023 00:13:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E76766B0400; Fri, 27 Oct 2023 20:13:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DE4486B0403; Fri, 27 Oct 2023 20:13:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9FEA6B0402; Fri, 27 Oct 2023 20:13:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B9C926B0400 for ; Fri, 27 Oct 2023 20:13:01 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 67F5540E7A for ; Sat, 28 Oct 2023 00:13:01 +0000 (UTC) X-FDA: 81392944962.16.C2B5D8E Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf15.hostedemail.com (Postfix) with ESMTP id BE9ACA0004 for ; Sat, 28 Oct 2023 00:12:59 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf15.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=1698451979; 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=fIYG39v7H9yeRxBf6f67UPkqml33y0Gunt5ynWSumOg=; b=zGO96+KAYqdBuUFj3eeilyGTXsbhcFBwdUz4+28CPObtXO7DgRP8WP5XXhRND/TlvLC3yn YBY+8lK/3+SxCIDCtM3bP5HNGXen6ZHy2nALxnK/IGid9sBdArvC13ubHTIlethXrZWYF4 2VDKof5N/IiC2iCEYs1tCbucm6+UI/s= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf15.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=1698451979; a=rsa-sha256; cv=none; b=6EB70b7RgNv+/oNYDCGWb3UCD6FYFklATWNt7YdMP5AiVxsXnpcrIDMkK+zRk6lPHDxwSd 8N6kRcOl7fjDapMYaecQG2aWjRw8xKuzxqFVAJG1e5NnvsxRT/MvYwRcoWaH4s8ul13oFx ZS9QiMEr++OnT4NXruOtfIoCBtRy5cc= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 85062E5A25DF; Fri, 27 Oct 2023 17:10:02 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 2/4] mm/ksm: add sysfs knobs for advisor Date: Fri, 27 Oct 2023 17:09:43 -0700 Message-Id: <20231028000945.2428830-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231028000945.2428830-1-shr@devkernel.io> References: <20231028000945.2428830-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: BE9ACA0004 X-Stat-Signature: 9fieeysri4ex8sygdeyyn95szuoxcrm3 X-HE-Tag: 1698451979-268332 X-HE-Meta: U2FsdGVkX1/oSnObWk7+gUj/2mIB5AHLFxxrQyn0R7T3NMND4eJHJDzoGfV16/wF277wwxnN2FnpD1T5VVTTVkUnsMBL9Z0m1CJQnjeatQaXrpazu3H9bnbO3N+CtL+K2SMuB45v/5k8RJ49YcEAZgpdHojinldsUmG4NWwYtFbwVLnno/F74CUB0OCMh+A9ukUyu+yJIIa7ai0MYMhobOtoS7wAoiz7huPI7DLp1IIe1jQSquZK7jMpXrsqalnnG3tvQjgWxQa/82sGhnacDT8Cd+D2yh2md9LXp8wwZDgNI8wNxwnyA//o0D5kIcHMSg5zMHDDyLTJ1+7tZYNG4FJPtcdz2U6/rGrs8RTX+X5TDlj+LfEfIKngPwzBw9YFsrAvi3VEnPrXzj3v9KOIRH7KPGl8Sq3ucqgdmtLRZ2Y+r0CXXzNOP30oMORRUdIbPXPc1mTXGpCNh2oMxO/8+f33vP0ptAwNrAHzgKgPMJlfQEVA3RCFf6juoTbfRB6+kXtxaPmmJXnZFaDjnd7QWYkmRXcoVOyRO4GJ88L7AwVXzlPuKo5zaUZaP92Pdt4I2CSkmdUNg0pOOf50vXCcgcqTRKNPEFBwnN6tjIk+u6w6jJp4ZLahyXPT0NDspbTizZV3G3oeouw8sd1PbmDYCohZUzq5rVz+RTTfENkHOhJmcD0DZ6aRF2W0yCCdb5maRFTS2k9kk3Re+2BSkfDrUmgm6lKX+judVYnXKNKreMVa7AceFkMsNdnQpWlIUTu1CoSN9SNrsZadGhft3AoTeMbwqgBebaG6vSglAQfc3Xt4r9DlP1jNkg0FXOGkibl2l2y8FjKyPsPy7m48u+vJ2Kep90W4+cJTgy+euqHeeBK8zqLIsaBAD1atZ6+S2AmDWzOPPVqKLuHTu5/gKgx0iawMlIXpYHKgV06EzqA/4eglJ9Lz3Pc/wV6eDS4o9Rd2Ovkk8Y316tFDJBIM3p/ ftZcEhBB 8LsXLgy0lTlwWVY4= 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: 0: no advisor (default) 1: scan time advisor - advisor_min_cpu: 15 (default, cpu usage percent) - 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 | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index e18fecfb359d..042ecaeb0beb 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -351,6 +351,14 @@ static void init_advisor(void) advisor_ctx.cpu_time = 0; } +static void set_advisor_defaults(void) +{ + if (ksm_advisor == KSM_ADVISOR_NONE) + ksm_thread_pages_to_scan = DEFAULT_PAGES_TO_SCAN; + else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) + ksm_thread_pages_to_scan = ksm_advisor_min_pages; +} + /* * Use previous scan time if available, otherwise use current scan time as an * approximation for the previous scan time. @@ -3719,6 +3727,146 @@ static ssize_t smart_scan_store(struct kobject *kobj, } KSM_ATTR(smart_scan); +static ssize_t advisor_mode_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%u\n", ksm_advisor); +} + +static ssize_t advisor_mode_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, + size_t count) +{ + unsigned int mode; + int err; + + err = kstrtouint(buf, 10, &mode); + if (err) + return -EINVAL; + if (mode > KSM_ADVISOR_LAST) + return -EINVAL; + + /* Set advisor default values */ + ksm_advisor = mode; + init_advisor(); + set_advisor_defaults(); + + return count; +} +KSM_ATTR(advisor_mode); + +static ssize_t advisor_min_cpu_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%u\n", ksm_advisor_min_cpu); +} + +static ssize_t advisor_min_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_min_cpu = value; + return count; +} +KSM_ATTR(advisor_min_cpu); + +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, @@ -3741,6 +3889,12 @@ static struct attribute *ksm_attrs[] = { &use_zero_pages_attr.attr, &general_profit_attr.attr, &smart_scan_attr.attr, + &advisor_mode_attr.attr, + &advisor_min_cpu_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 Sat Oct 28 00:09:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13439319 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 CC421C27C4A for ; Sat, 28 Oct 2023 00:13:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 242D86B0403; Fri, 27 Oct 2023 20:13:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F2776B0402; Fri, 27 Oct 2023 20:13:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 092696B0404; Fri, 27 Oct 2023 20:13:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id DD57D6B0401 for ; Fri, 27 Oct 2023 20:13:01 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BCA24B636C for ; Sat, 28 Oct 2023 00:13:01 +0000 (UTC) X-FDA: 81392944962.10.1E3A97B Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf16.hostedemail.com (Postfix) with ESMTP id 348AA180016 for ; Sat, 28 Oct 2023 00:12:59 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf16.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=1698451980; 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=31VaYngcTYzlNRfnB5nKk/Q/RitTaJ38QTejLRm4eBI=; b=74P/jiGCfsGeuccNrBvF9zpvToZ6D7QYpNyL1NjEeMEubVYY1VPDjECVzxJ85UeYJOZQHV HhVXMj4Y8hrbmSIRSoUHVImMfCQ7g28ZRk++mZhlRnXXdGSv8bcOC1x77tKXHpzA6LFZfG qaUmzPC7dHHt9oP813M3K3rmb9KLTyE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf16.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=1698451980; a=rsa-sha256; cv=none; b=8jHKcPiolz90fGjMFmEBW+vzO9LQMAYDhaPpKtWesryaHEgvtU87JHVC66aFmPRLubRTle AVAQ8MweBllF69if1+fHsQ6sgPPwHTSK0eBPZEl9mEj+/27Za3jqT+m/+ZNkoi/SmeXQLO xetZTO1e/8oMjCUTpMG5Zr76BqmeYq4= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 6C454E5A25F5; Fri, 27 Oct 2023 17:10:25 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 3/4] mm/ksm: add tracepoint for ksm advisor Date: Fri, 27 Oct 2023 17:09:44 -0700 Message-Id: <20231028000945.2428830-4-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231028000945.2428830-1-shr@devkernel.io> References: <20231028000945.2428830-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 348AA180016 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: cngq5zcgu96mr48b8pqqqzw99i85m9ju X-HE-Tag: 1698451979-915075 X-HE-Meta: U2FsdGVkX1+tatT5pV9QKHhP0+sXggyt1N8eQVi2iRyx2xAjqpG+bSroY35Mh/aVxsh9tS64x9Dd7q3sOLAzeXXNY7+M7Wgykf7DzOkx7F/oFo3bFjIV7Gi8hqZU9yo6iTCj+zHGQYYRziKI4DFCQKoDEKYqd7QO2QGn+H4aDRGNVCsRsl2MgPxf4BPDstbiCvbJQxUM1scYPvsK9xpxOviWlq9SXQUGS7WaDAXJQAg+LFuGakR3cDmaRFQW2Fo8erEY5JaGFUsqDSwBd9D2yDb1Pcc49fyD3+HA9IZpd5GmEipsb2D4UM3uSlQeqZgSDgjTvqpYYA8eZCiCiqH+52Y6styBd2N3HqtuRDL04w4z3LVzO+DXzUUsFgOkAtgXDs6ATckRpRMFqbMK3fQgsu8EKj3B4S1JLnFWt9+8I+xpyUoJTFq5E4FZh+VCM+yRehDmIJYU1sLcULAuwoFskyXXNvJn8MwspwlJgeVs7x12tl1fPPP0yavQUbFhY4wT5vznAu6byecGKVN0ga31UOuqf5HD7l9f+xtYn0K05ocV5jOjT/IHkDghFIPTGpNv0CSjqdGaBDYXRAqXNwyTNgUmJyCizh+etloL1Qk5vVcDhpTtRhb1ixcGjOAohWaevWKJQ3T3/qRhKr8XSkWqFB/BOdBC0a1pNgHNPvjZHfLMp8B5WMcxOCQH1CwLxWOrL8R+mWs03F3LYPYv/LBommAJUV1YGo8ShxK7HZW9nOt4339UUSxHLuYqw9NLLLbja3KqInSV1wHhvBKBwVyYjbcHI/QPfOgy340QLaUm1NbaTxTwaoXkPlDT0ysovVM82n1yrXHzXvJGV/Pfi+PEFD56ZITVGU1dniNxGYxUWFuDoX4qhAubduylDHtC2MGckpNKB3WiERQGiqGLRF3xehzCfajQqdUPnXN4WWixVqK3JJbd6g1EwDJtZwjwl1Tiq4d4w9C7EjTcaT6lqFw Kc2wTUkr 5gBRo0EefbJ3kjuaNH96rTSEtdj4MauJAp1+W 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 --- 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 b5ac35c1d0e8..e728647b5d26 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 042ecaeb0beb..b2a519083d66 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -442,6 +442,7 @@ static void scan_time_advisor(unsigned long scan_time) 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 Sat Oct 28 00:09:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13439318 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 E8E18C27C47 for ; Sat, 28 Oct 2023 00:13:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 462296B03FF; Fri, 27 Oct 2023 20:13:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 412CC6B0400; Fri, 27 Oct 2023 20:13:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 301076B0401; Fri, 27 Oct 2023 20:13:00 -0400 (EDT) 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 217AD6B03FF for ; Fri, 27 Oct 2023 20:13:00 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id EEA93C0ED7 for ; Sat, 28 Oct 2023 00:12:58 +0000 (UTC) X-FDA: 81392944836.29.0BB8ED0 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf13.hostedemail.com (Postfix) with ESMTP id 69B1020017 for ; Sat, 28 Oct 2023 00:12:57 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=neutral (imf13.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698451977; 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=h32iBnhLVg6ZmUoj8PAweva7s48LAK0ljHaKrtYPXVM=; b=QWzro4nhHT67YAwlbFDj8s7/grfX9FXHe0n57rwe6WQmcBJMP166VD6qj8bP9W3hv7TjWD mer8DaP65MTmfM16hSMjZjdynz0kMt9KePl+XLPIEzqC9iPX0FaKTVEZHx7/ROHMlDpN24 2o4GyNraDQmKbNsKRN2jy050pkCB9hw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698451977; a=rsa-sha256; cv=none; b=YXAR9NREz8soM/24MtffdU/V6CZw0k6MZh/hw85gJ7psfwXz1YfIMRHd3FwmaGdZRBbYO3 uT2lBboTV9KFfiCII9hSuVJMjxJyctxkuKDVHKewaclNqGxnWWwlFbYssan47cKzNiXcOR PzeR29cBQbslYcTerZm00kvB7RYN7TU= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=neutral (imf13.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 762FBE5A2603; Fri, 27 Oct 2023 17:10:28 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 4/4] mm/ksm: document ksm advisor and its sysfs knobs Date: Fri, 27 Oct 2023 17:09:45 -0700 Message-Id: <20231028000945.2428830-5-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231028000945.2428830-1-shr@devkernel.io> References: <20231028000945.2428830-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 69B1020017 X-Rspam-User: X-Stat-Signature: ooy7yjsjand7536x6cys99mzp3d6d8ki X-Rspamd-Server: rspam03 X-HE-Tag: 1698451977-155850 X-HE-Meta: U2FsdGVkX19p/CBtmxjqaml6IocUG0JtK397cXInAbz3Sr50yhpZuIb+sMO3o15UVs4XdWXJngIBuT45W2nrZp1CCw25uhVIHpZ/OdZ4Ychz+F9FrcGtqQY1cKQIiNS8FgvmTtuhqd49PdtDDhgVpWqE+9/QeGyGa6nm9T1SWNaR3yAy08RwzNvvTiscWuQZncITy9u64U94F0g6cD++PNyDRmJ8nKwDgCUbZAQx9HbaL79y3ff1wQwBvp2pHVgq4KhWdmUM15Uk7KuXAzE0j9v6SXXAGOeGHehU4Ms5MfIm0Z19mgHAkk62gxbCSOwdUwmTVrdSV9Td9GDD8Q6VRVryrVu/0fYiLtleSv0Zbjy4kRA+3F2vuBz1YIfLdDbJE2/Kn0p/+BMTZgsthb7xAWVwpz0qnWV4POtPUd4j8s0U8v7GaAqxmwkQC0ZXCAPrQ7UgRYXn9aEOo4JU3KKK/9oiZe1y5HxRNfse8rh0oKAhLp7eW/y2vl2nc67RKk9x3wTczQkjvVR29Fttf5YV1hJD4CQe++OISSHCiCXAtGFxapgD/FoJu74Mrvvr8Nno3d8XiD5lOxg761eCdkd+CuhM5tYt+TAm2NJjSvzcz6GQQXuM+tYJ3rL76Me7A7rt9Y17KtGwB819AgJa1zjDnZMPL1qF5eOIimuCmdnEMd8FHClzEPv88yuPtaEcgBaYQgC5+nHGcBN+rqE84VZ8z76VZJVG/iD1hfC+Fm4KXfLTxOpanW2lqfIROCcDwJhrGUjBjMgPDxDpPkeI+FzQQfDgssQOw4vBAz+lTHRmpViUqnA3//WseLw9dluN/wNU7S3/6fK2pZYzt5fpbforQc3EqCLFr+QGEBPpb/UGmU0U11BKqCHvMcIfBaL/Pqfmvbmsljtabc4Mf77Q5nQh7LTsUgVd26u4qgQxftucjwCZ4vbbFrrKWXznphFiQlWyRUFm1R+TkTTooG/hL5C dUp3zjRC ypiLvMLFGnqRAmB/a2gd1kTpBKg== 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 --- Documentation/admin-guide/mm/ksm.rst | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index e59231ac6bb7..7e956692b656 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -164,6 +164,33 @@ smart_scan optimization is enabled. The ``pages_skipped`` metric shows how effective the setting is. +advisor_mode + The ``advisor_mode`` selects the current advisor. Two modes are + supported: 0 (None) and 1 (Scan time). The default is None. By + setting ``advisor_mode`` to 1, the scan time advisor is enabled. + The section about ``advisor`` explains in detail how the scan time + advisor works. + +advisor_min_cpu + specifies the lower limit of the cpu percent usage of the ksmd + background thread. The default is 15. + +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 +290,45 @@ ksm_swpin_copy note that KSM page might be copied when swapping in because do_swap_page() cannot do all the locking needed to reconstitute a cross-anon_vma KSM page. +Advisor +======= + +The number of candidate pages for KSM is dynamic. It can be often observed +that during the startup of an application more candidate pages need to be +processed. Without an advisor the ``pages_to_scan`` parameter needs to be +sized for the maximum number of candidate pages. The scan time advisor can +changes the ``pages_to_scan`` parameter based on demand. + +The advisor can be enabled, so KSM can automatically adapt to changes in the +number of candidate pages to scan. Two advisors are implemented: 0 (None) and +1 (Scan time). With None no advisor is enabled. The default is None. + +The Scan time advisor changes the ``pages_to_scan`` parameter based on the +observed scan times. The possible values for the ``pages_to_scan`` parameter is +limited by the ``advisor_min_pages`` and ``advisor_max_pages`` parameters. In +addition there is also the ``advisor_target_scan_time`` parameter. This +parameter sets the target time to scan all the KSM candidate pages. The +parameter ``advisor_target_scan_time`` decides how aggressive the scan time +advisor scans candidate pages. Lower values make the scan time advisor to scan +more aggresively. This is the most important parameter for the configuration of +the scan time advisor. + +The Scan time advisor changes the ``pages_to_scan`` parameter based on the +observed scan times. The possible values for the ``pages_to_scan`` parameter is +limited by the ``advisor_min_cpu`` and ``advisor_max_cpu`` parameters. In +addition there is also the ``advisor_target_scan_time`` parameter. This +parameter sets the target time to scan all the KSM candidate pages. The +parameter ``advisor_target_scan_time`` decides how aggressive the scan time +advisor scans candidate pages. Lower values make the scan time advisor to scan +more aggresively. This is the most important parameter for the configuration of +the scan time advisor. + +The 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