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: 159201 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp958193vqb; Fri, 27 Oct 2023 17:10:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF08cgwElHzPkYBcIl4MfOhODeR1qL68ZS55EHWKK9edwmWi1hoxEyUzYD8+OAycf5e6fri X-Received: by 2002:a05:6a00:1809:b0:6b4:6b8:e945 with SMTP id y9-20020a056a00180900b006b406b8e945mr4551917pfa.15.1698451835443; Fri, 27 Oct 2023 17:10:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698451835; cv=none; d=google.com; s=arc-20160816; b=QTYB/QpHZehYxA/inPKLCAc1vyO9v076zyjUhxwnhiI0y7iYdlhg1INhGW0SogKlwR t09VhBEeLsTeSxk7RyVa2myl445NflqsEMIVfvgqRK5KeraCiFL0FSVqteAitPOlt3XQ LbXHzYtO5Um8QPyQ/EAqmVRWRNlCAGQIAMKS/DlGJBs14K1CP7bmJ6SjES9TxxE+ZOGX a1Nz1C9Uvt1XHJug37vT4mAer+YGILf0A/dDGoOzPrBKg6/L3+ZaCEa8nzMKkMaSVRGu kOXmFznAfuEpNsPclii5/hmAu5xOjcajlOX0l8iOV6VhWqDuaFvmAee1PGBbT6K5crsG 351g== 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=z1CDgPBGhGslcSULKXQTDWeInFAmX74zdDIRHDgnakc=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=spcwlRO4P+3JYXztpXCi3eWbxYiY0g0MPOGJjeas+xYvyceksCzRISNqCMvFEvYq+W /ZUDjNTeuwRdOvXeVVTe/G7a9ZU59m7NXc1D4IBGybrochkIzNQm6iOE5VQ2/nw+ISid UvhRxc6Z73AN78/PM6BjWDJEMX/TaaPyUnjhSa5m+UCm3CK3RVCveuW+taZ4J9P8umaW H1qFsZ1InJ+pmslqVPpBWzmRPmyCkZfjPTBnFFRTS2NI9zRwHjSssyMTbcTfVvuGjPpn R2vJLl5NKrCBlmtZD+LqeNE9M68a7cIO1uLUgO5jKIFt+gxILADv3ypDtZeyPJuqiKhC YH2A== 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 a26-20020a65641a000000b005859c255ce9si1758251pgv.861.2023.10.27.17.10.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 17:10:35 -0700 (PDT) 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 BEC91829B8A1; Fri, 27 Oct 2023 17:10:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231569AbjJ1AK0 (ORCPT + 27 others); Fri, 27 Oct 2023 20:10:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232896AbjJ1AKW (ORCPT ); Fri, 27 Oct 2023 20:10:22 -0400 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 894ED1B8 for ; Fri, 27 Oct 2023 17:10:16 -0700 (PDT) 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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]); Fri, 27 Oct 2023 17:10:32 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780955831897017241 X-GMAIL-MSGID: 1780955831897017241 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: 159203 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp959190vqb; Fri, 27 Oct 2023 17:13:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGAUny0m2Wk/TAS+znZhdu+PEbACGAtQBrKfRWufP8IobVlg7xEhPHi2TAEaOhqJ7r7G2lK X-Received: by 2002:a17:902:d4cd:b0:1cc:1811:2e57 with SMTP id o13-20020a170902d4cd00b001cc18112e57mr5374924plg.49.1698451992637; Fri, 27 Oct 2023 17:13:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698451992; cv=none; d=google.com; s=arc-20160816; b=uHeGIqGwohCPqtARgongLUEvvikTLsZ9bEBg9eR/SuNxl2HiLPkjApDqJvNOZLQNcI DaNDOelrZN5lqH5wJLLhRBxkK5J5ebeIY0sCe5Hw28l8Zp8iQa/MHf72jaRiEm/R1Qkm 9UChkix6zbT2+/cLs6h/Rmvkt9+hYNvkWoVdj0+vzB+GC1g04oNzFL+Hea4T/v87rsiR fU2k89rzgpqiaZ6pH+Uk7wZmuXBDNN2tXzersOPvw5E2DrPnhRep16DpdSIgQPLb4lmc evuBSpbB9QxbYI5T6+HXIsoqnlUVgoWEm3lQ6vGPMY16NDL/Tm1Oex39fVe6R9pCH49O YeqA== 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=fIYG39v7H9yeRxBf6f67UPkqml33y0Gunt5ynWSumOg=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=RCXMpPpa4fHjz1xUCCU7AO0ZuJk2e7g3b7EfE0Vx1v/92AfMSqQJ9JFKCc8gm4GCsx Zebf24m922l7dyPLg1RBSZMNAP32uxlyFMNlI708FVlKpLHpTm5wDqD9ObvyuJT7MVOZ I7SMWZbcX3U/NIYa/i71IZfLvVaTExiH0Xs4O6/ZEiDuzJCL8/K6YOgxp+rTETo4iXvr 7fSgyUn4Q4/7haDPmk2FdDHF9oasbmKZP5e4deTz5PVhb/TRIQsO7nTsNn2keiXrnD7L /2R1HvNDKPuTDag39xenR6WmqqgMAa7X+5kHmLnrbeboPM+0bQjuzc7w7pghCA4o7yPW VWVQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id kc14-20020a17090333ce00b001ca7af18a99si1755507plb.564.2023.10.27.17.13.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 17:13:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id BDF3E80699CA; Fri, 27 Oct 2023 17:13:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346749AbjJ1ANF (ORCPT + 27 others); Fri, 27 Oct 2023 20:13:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346735AbjJ1ANB (ORCPT ); Fri, 27 Oct 2023 20:13:01 -0400 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 D6CB21B8 for ; Fri, 27 Oct 2023 17:12:59 -0700 (PDT) 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-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,RDNS_DYNAMIC,SPF_HELO_PASS,SPF_NEUTRAL, TVD_RCVD_IP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Fri, 27 Oct 2023 17:13:11 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780955996662499470 X-GMAIL-MSGID: 1780955996662499470 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: 159202 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp959162vqb; Fri, 27 Oct 2023 17:13:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGbzXwa/7l7wa6Wz3G1GG3lJ7Da4lwOg7Xt5o9lgbsY1CyPBBDiZMA+BMpJftT9/j2NiG+J X-Received: by 2002:a05:6870:ac87:b0:1ea:bb5:a531 with SMTP id ns7-20020a056870ac8700b001ea0bb5a531mr5279581oab.13.1698451989300; Fri, 27 Oct 2023 17:13:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698451989; cv=none; d=google.com; s=arc-20160816; b=tBYg8y0/n5OaX0yKPFy9k1FFzA5W3Q8MPydhqzgP0sJAm5Ya1u2DaYTxoGpIe7GL70 ZWw679u0cdKzk07J7Lj+W3Ns/P7V+thyOzZZBLO5VYNZpGLFejC49GdhbLyF3AWG1bpD RJj10VjPV5g1EouJDceRtCzMIGJze61OGjoSuSu2rOUjKX0HvvAPur/3nisU4XpEMjW7 4nEXqMCZKKEQ0B2hkTGHUnVrwlTezK57OcQw5gNqDTNo23HqkKT1tfiho1+3ao9T4HpO g5od3YFOA23HZQLI6WmkrGkyW0GB+PETKSXhU9pIq/FgTgP3n8rZKzb3S3XgoLuWinzN pfnA== 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=31VaYngcTYzlNRfnB5nKk/Q/RitTaJ38QTejLRm4eBI=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=hhy3GYKdwAMQnKE4wDkdkGKsoT1v6z891MuPrYEri2Ow8kjkvRnQnDThMdJ+BkHWd3 8d5CfnkMDYCEYcwhVLyRgvgA3Ml0b4xCpMe2spMuJX8jgOMtu/IMh1oIZbcZq1c/+ADC HIznMj3ZZcprmAsyfGatcYgzlgrvZECuwCC0vOjJodC5KGppZn0ppJZbVAxVOiF5YBym sFocApIdI2+ztpiejmA/hIrXHZwRqnyGp9Rgpd9GQDZbQaKEQPTelemcG24RCjopDSl2 e8rL7PmwuLTxQOZDYs7h1KtJ9fOpJZTdzrtUov1HyzZjdIrzyxNS4Ia6Awf2NRGtcvc3 47fw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id y29-20020a056a00181d00b006b54b82dac0si1692201pfa.298.2023.10.27.17.13.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 17:13:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 1A3E680699C7; Fri, 27 Oct 2023 17:13:08 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346743AbjJ1ANC (ORCPT + 27 others); Fri, 27 Oct 2023 20:13:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231444AbjJ1AM7 (ORCPT ); Fri, 27 Oct 2023 20:12:59 -0400 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 B5F511B8 for ; Fri, 27 Oct 2023 17:12:57 -0700 (PDT) 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-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,RDNS_DYNAMIC,SPF_HELO_PASS,SPF_NEUTRAL, TVD_RCVD_IP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Fri, 27 Oct 2023 17:13:08 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780955993268588104 X-GMAIL-MSGID: 1780955993268588104 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: 159204 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp959274vqb; Fri, 27 Oct 2023 17:13:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEQMbX9ymN+TQJ+CpuEgTUI4cdxdo7/dLjZcW7ySrN0F/WdazyBijoWXPjZ9ftEz2jXadkB X-Received: by 2002:a05:6a00:2303:b0:690:d4fa:d43d with SMTP id h3-20020a056a00230300b00690d4fad43dmr4397335pfh.6.1698452004971; Fri, 27 Oct 2023 17:13:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698452004; cv=none; d=google.com; s=arc-20160816; b=YHyA5cCXZ1C1rKMs4+cszN/KXHX23nncFRXfQGtWRqnnFFj/2zNcFlTf2plpoUtL+M cnBjmIiJmmAFS/WSVxU+HQ1NCGE/TMxh67hR6KGDhYTiEkGUV0CQviMjukQbCCqPDZWQ Ql+3JvRkcUGkzRefPvYpGX+hiFyXx7DR6vKWLjS7EB3G8ogbislUiyUNteVXwBvDxxjz 1qSmiZNoWYrwrrkzbk+4u9mCPO5749hyzF9kMiY7N44mud01Jn2JJq3mMWYZZnW7tvtX oeNukFi9eh55wWI+Xluoe4RLoTmb+zE9PGBAcGTsT+3sM1yiMW09AHXioxULK+2QKPwp iMuw== 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=h32iBnhLVg6ZmUoj8PAweva7s48LAK0ljHaKrtYPXVM=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=oQNnq0d/5nxfU8bfncs14nhD20Lwz1mYJtDOZNBOHUShEDDlS3GJJOR38wHsZ/MzLW 8HGQm61JIpyEcFZBx3Qr92AFiLm0qsPkLi0lYB9HaF7NPR9+0ZEhOL9h6qTxkAuv1IIw cEuD6PbRE6YIfHTEVvs54BFZJVWCoPGsH7XaPj6RU5LJIMnqPAj2phMpBa7DiQ8Sbr/f XLYNzkhrRoWCUEpT3UDFtjKROjkf5+ZE/YW23iwNoZ2lA5u+jGuQ0ekY++zmDXO0qrL+ HPr/NIaxQNg8qTr2WNvrwNWxUE0XRLzIGnGf6XZQKKt7xncvm/fgYu9u9wnwH16aE1nA lY+w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id e4-20020a656884000000b005b11e5a69fdsi1703920pgt.508.2023.10.27.17.13.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 17:13:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 286A683F02BC; Fri, 27 Oct 2023 17:13:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232648AbjJ1ANA (ORCPT + 27 others); Fri, 27 Oct 2023 20:13:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230104AbjJ1AM7 (ORCPT ); Fri, 27 Oct 2023 20:12:59 -0400 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 29BEE1B6 for ; Fri, 27 Oct 2023 17:12:57 -0700 (PDT) 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.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 (howler.vger.email [0.0.0.0]); Fri, 27 Oct 2023 17:13:17 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780956009513056762 X-GMAIL-MSGID: 1780956009513056762 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