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: 178253 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp7982393dys; Wed, 13 Dec 2023 10:28:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEoMJf9EOrTp9Eqx/NfdaRpNemE9dU3x2VUagrQklsiA1LIdQ9pnFKcp0fD110zT+uo96G4 X-Received: by 2002:a05:6a00:170a:b0:6c4:d12c:adf0 with SMTP id h10-20020a056a00170a00b006c4d12cadf0mr5248487pfc.33.1702492107949; Wed, 13 Dec 2023 10:28:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702492107; cv=none; d=google.com; s=arc-20160816; b=CtXdMkMeqjjswpcU5XsftgyhFVTbQjwv6EzhJvI9c9H91bOep6I0PqUa3U+LX6Rnuh TPz3d3N93dhbcJSIUwJCQP9yj/3Gk8bjlm/dGDu4Xltq1NlepT3CnR04EJ9C7ONd6kTd b01czOmfzfz4FS4GwHfH6pwGjegmIbCKHgXTLnx0t2nV3odpRJ4Q5Uik10jNGiuQKmOC r0lyfbR0e6aAKMCobFeVecuBsL+ETnWFJp77+sVjc0X7KPq4DojpTkNW8ve/WVeIf8of guPuRBNQjmmAWGFy4zrO15KPkgW6iLm7y3ARmSLX9AWlcS6wJgc94rDMmecGcVFFdvL7 6e0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=nuAPNO46xDrSflQHVXVYR/HY55cJaxKaqSxB7tFgKKQ=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=hUlHu0Onei6W+FPeMQWX09PXVuGG/XqQiF0oDkH2UQ8bNRC/ZuRTBvoKpoVpnM32vC X6noqznbUU1l0BqIQYns2GqgucZYWqUiorDR8Qzbcmyh9Nm8mYVEnpm9sx33ntAw7e5d 9NwqHkVoTsw6ujp2KMcaCDvU9pTIsmWXThV+zW9lFJduvcEdzcuv55HGj8IfrEmpp7OM CIZLTnXz6GENQOV2kSDOYTcktHnrU5KOkuWqkMqGSwtgSvUT4baHhr9Y2cn9sxN3Z595 1P5lzhk6Nvjxa8jYTFL0ulSIw/juqdZXNbNTTj55IvuzRwSxJqSmVywbe3WJE0kNhi3V XjPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id g5-20020a056a00078500b006d0810d9bf3si6342745pfu.129.2023.12.13.10.28.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 10:28:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id EA8208030A5F; Wed, 13 Dec 2023 10:28:24 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235598AbjLMS1z (ORCPT + 99 others); Wed, 13 Dec 2023 13:27:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235555AbjLMS1l (ORCPT ); Wed, 13 Dec 2023 13:27:41 -0500 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09988107 for ; Wed, 13 Dec 2023 10:27:46 -0800 (PST) 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 13 Dec 2023 10:28:25 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785192364934057993 X-GMAIL-MSGID: 1785192364934057993 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: 178251 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp7982207dys; Wed, 13 Dec 2023 10:28:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IFX7nTXQYmsveloHFXXGuA6iGlwDouia7jopLQHHpiomKFooRmrEM1MuoibHa8zrKBD9OFg X-Received: by 2002:a05:6a20:938e:b0:190:9d87:f9f7 with SMTP id x14-20020a056a20938e00b001909d87f9f7mr3723047pzh.43.1702492088992; Wed, 13 Dec 2023 10:28:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702492088; cv=none; d=google.com; s=arc-20160816; b=0W4BbougGrJN3l0dQLx5W+F8KJYuRg5roBGqoaNI7k3qpVHDmayY3GKtbfxBI5iqE3 NP7XarjC7McuRVBHwDpKmtD9nTWn3wLmXK5Vjazr/vllV83euKml/++GVEmzvVALW6v0 BY3HkGPzYM/jiMMY3TtL1tb/Ncocb0WgEHQ6xN6Z2doxN2nI85U6TzsdgpeA1Yd+TXN/ HXIPLNZ7rrnGiu5nDkVPN3PIW1HSEANgw9lNDZl/GCYKqHWYnYp1rKP8i3IoOGvfyFrT PG5BK+aM4IpZFFF+jydPnRmI7KmvGorQv+XciYd58rWyFl2kU4c8GsJPstaEE5xAbrAd ZqXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=hjv8NkWfXoQ+7hZi7rLJ9o88UnzNicsuLM05tnOh3cI=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=HtPS7H8cn6ksJBPRQ9WSa9uTPsuLP3iKJbTH4SAmww0nlyMVbPybKbhAi8D4QV9yEn dThlzWlkvEUVTOrMV0SvMtaxW/NG0hBNXroWP+q9lymrVqR8WsiDlnD7gV2KRMcAQu78 BfQqIxfDtxy5KRVQ8fBSnDdjL5nMEGuNstuqfXD3O7jypytEdpJN8nGUPigH5y2UTG/S d2TQ7iKfns2XjYiRDDosx8SOuHSSU6ob9/U59A8VhzaDz6m45jqUzEfRf3TjQ9yNGe7L wXpaI7x6zLit6dojMigczIXHVicRBYf4/Szkd1ZIccA1aUE92Mp9sJYbo1w91mVm9Gvg 05+g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id ks4-20020a056a004b8400b006cef66406f7si7240398pfb.276.2023.12.13.10.28.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 10:28:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 000D7826EB7B; Wed, 13 Dec 2023 10:28:03 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442530AbjLMS1u (ORCPT + 99 others); Wed, 13 Dec 2023 13:27:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233764AbjLMS1k (ORCPT ); Wed, 13 Dec 2023 13:27:40 -0500 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DBD710C for ; Wed, 13 Dec 2023 10:27:46 -0800 (PST) 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 13 Dec 2023 10:28:04 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785192344894941385 X-GMAIL-MSGID: 1785192344894941385 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: 178250 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp7982178dys; Wed, 13 Dec 2023 10:28:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IGy5ZeDi6cnqukVRPBSvcjz/TX5F+0pZTCQWyneXaXclDdqMiLVaK+QHkBUjoHOhPVONVOH X-Received: by 2002:a17:903:230e:b0:1d0:a7b7:74d6 with SMTP id d14-20020a170903230e00b001d0a7b774d6mr4909823plh.110.1702492085965; Wed, 13 Dec 2023 10:28:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702492085; cv=none; d=google.com; s=arc-20160816; b=OSziU8Zgbf2rFw6qEBggs7/67Gc+jYRckL8HpMmc2RlrYg6U21+n9x0tBix02yXzKP 18jQKLeal+k0kFNE8BA2J0L1Kgeard9tFe7Kxs3del9xJXEMI6qMm4ajkFJyuKBq9qgD WS6tw/j97Ls1VBLADWugx7C4AsRWXNAk8GlH6KVTyZHP/VlpNAFOQss2RGr6JZSFD7Za Q7DGN2qqZEujEOZtfmHrEUnsMPTC03RnjEckl69pZ6VO0kaph9mjUGDpqO/WuigY62WM 904cI7nAXeRcy2yNnlXtEwe5aACY8j/z7r1o9SbYZY94MestZiyC6QynWv2b63mCjTXy 4sUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Ju2E2+1OfMe8Vr4nHnwCyl1I0bs/30hKeMFq9hcWi4w=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=IrSTMBx/0ORUrfQ69Ww1PMdpaQaUl+TslMW+ypK2H29Qd8vKI91HPDQEBF5oPHBdV/ QROWYP18E+Enp44kxGTD5qMqRPvbshGjY2T663a35NaCeU6YHc8uh2k8m5iaisEcGbGE 0l6bXVLwiz/rV9edGyi/2hu1xSRVeRIWvnloJTV5Rb2Fasx/02krSV9bEvryotXKi4AS qbZOXAk8rVt3C8xWooWvLAFVjJaM3jMvbk3tjTpSiubZEmDbuDY/TuhvNmtrZZ9psT05 xQZacE/JRjEwngXe9CoxVIbN6uGZCcUHSpkXzOTQ7o3/yw95Q47W4EpPd0qBthcPg46f htYw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id jw5-20020a170903278500b001d09c96ba09si9796269plb.424.2023.12.13.10.28.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 10:28:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id BA63A8030A60; Wed, 13 Dec 2023 10:28:00 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235563AbjLMS1r (ORCPT + 99 others); Wed, 13 Dec 2023 13:27:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235592AbjLMS1j (ORCPT ); Wed, 13 Dec 2023 13:27:39 -0500 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42C33116 for ; Wed, 13 Dec 2023 10:27:46 -0800 (PST) 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 13 Dec 2023 10:28:00 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785192341822630135 X-GMAIL-MSGID: 1785192341822630135 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: 178252 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp7982303dys; Wed, 13 Dec 2023 10:28:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IFzx5LHZtbzjarous98XExwzEt+pmTqkyCF+os/jAcWCdkBsFM6slzha+lN/69HXvA7vqfu X-Received: by 2002:a05:6358:7e42:b0:170:c36a:a2bb with SMTP id p2-20020a0563587e4200b00170c36aa2bbmr5060816rwm.17.1702492097631; Wed, 13 Dec 2023 10:28:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702492097; cv=none; d=google.com; s=arc-20160816; b=ijCxnZ5sMbuXUlB+ilPqY8EbM9Qm1xBkjnDgYyDpkINsacinzFFs31wZHKlUN8XocE 5PhKUQdqD/PpJaN2sxnkuKoqQuChtguXrXXGSCjZthgLB3S44pgmRgI7sHMHQy27657R Fo01Y2dgfQGXrqsHFQMfgvUl79bF1pKp7hZgb3l+/N1ExEChIlv+6CtUb5F8Ai0N54rR IHuHdVJt/e93Xsuh/pxKUkYDTqSQxpXeVEen/bGJJXKqMI2KbEncf6pNKQdOR/CgcAif nDi9zHwbJNeG1uqj62FrewaWxb+kXRZg7UD6tfIq3Ii7oQlT6l4Y1xu6zmF0KMApFGN2 z8Ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=oFkMIsv/KO1omoMMrIxz+uBzORyGY8S6CnCh+B+0uQ0=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=oX5BxiKX/OZ8Tkilc4k+c0D77JwM2jaYSblTwbuOESYGZHUL30NlNEYPklb7WpV8Bx pYJU9XHfqqaS89AxLdxI9nMAedPSJm0sWJu1tPiespL4bjGBnqNMnWYi0V2iPrGwGYfG 8DmH3YtIAnQ0goRTVhzPKD3tQZONWnutZAWa0k46YUqrAEHaO4wd4BN91n8m1ZpmJjYV MOQ2KUJVJZ1Rx9QadEqqn9W739kur/EgJe4+83HioQjbI33Bkf99ZMORg+OhkBvIhZnT hitpllleyrulcpqXklfdPn/fn+zF7WyFMqbSxCQ55+CJ30PIvp9zL1e+T/bIWbkOWev6 jEcw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id k1-20020a632401000000b005bd6897634dsi9986783pgk.249.2023.12.13.10.28.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 10:28:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id B8D64826EB77; Wed, 13 Dec 2023 10:28:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442613AbjLMS1y (ORCPT + 99 others); Wed, 13 Dec 2023 13:27:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235536AbjLMS1k (ORCPT ); Wed, 13 Dec 2023 13:27:40 -0500 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6FDB11B for ; Wed, 13 Dec 2023 10:27:46 -0800 (PST) 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 13 Dec 2023 10:28:14 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785192354067472268 X-GMAIL-MSGID: 1785192354067472268 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