From patchwork Wed Dec 13 18:27:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 18007 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp7982139dys; Wed, 13 Dec 2023 10:28:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IH0BanTKwGllhdOi0B4h4GkYSedB0DN2KVE+brLN/ETKXSrtVT22CUWPsMxIKGho9QXsyUn X-Received: by 2002:a17:90a:604c:b0:286:cc6f:ef78 with SMTP id h12-20020a17090a604c00b00286cc6fef78mr6025779pjm.96.1702492082704; Wed, 13 Dec 2023 10:28:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702492082; cv=none; d=google.com; s=arc-20160816; b=bQ1uISarx6qCHJzTt2jiaISdEH4sPphPVb1UjJ4JyHaEkN7aktNT2l/CTLkzWJnHPX /ZJL1xK7c6VdPkgtPGve7J437URfxyhqpb8Bh6b6ILMHgauGG9PvS7pIrin2IwQz1gCO L3V7GwViz82fZ5I7YgIhoW82URozDdNEXNeWY9efiU9CPjVuS05UbIeEgHX0+4XpILD6 jUQczlCb8BquTvms2xPn48g+MjpjBVvP4ZARDR9k5HRvnRvuxchg75LJm61dJF8LEqdH RswV42TUiQ/tlFsXtbwoku/XPZiTgnK5gELJvfXCVzHwdAoKdhJf+ACazt47FdhBpEfp JFCw== 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 :message-id:date:subject:cc:to:from; bh=XiESXIOKxmbcHfM9lo7L8GEdPq4HkaF4j61dsOXpT8c=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=LC/jJfQoVMQl5h3xtORDEYPOncYeXkhvj+SLmSZgH9FpHlhZOE6maLVHyvWun8FMzA PeiKOFJYk19xNCCqDpIlMFA4HaIK+qRskZJ818yKbU6zWl3LsnignumwvMRNUAIlNSh9 5IzfMP3DpvamG/GWvw9KHxNIN/Z0Utoe1BON6NMnjY089oLJf2YJ+A4xaIVtHNCzFgF7 H5vpRSuvODTzFyfUbDYCpkNexFz385pJuL4wC5ldXazQXbvDs7+G8afE7fQ5g8DCjfhl lQc10AXdvvBUVzVoYsaNhuIzhsxAEsXm+p+DjagF0ZLt+0u+WxbXVXemEZzGeqirZ2eW spbA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id y23-20020a17090ad71700b0028881bd2486si9891376pju.182.2023.12.13.10.28.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 10:28:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id E09AA80C3AA8; Wed, 13 Dec 2023 10:27:53 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233776AbjLMS1o (ORCPT + 99 others); Wed, 13 Dec 2023 13:27:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235590AbjLMS1j (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 4A37C111 for ; Wed, 13 Dec 2023 10:27:45 -0800 (PST) Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 9918D10CFA721; 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 0/4] mm/ksm: Add ksm advisor Date: Wed, 13 Dec 2023 10:27:25 -0800 Message-Id: <20231213182729.587081-1-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 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 fry.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 (fry.vger.email [0.0.0.0]); Wed, 13 Dec 2023 10:27:54 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785192338086191409 X-GMAIL-MSGID: 1785192338086191409 What is 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. Why do we need a KSM advisor? ============================== The number of candidate pages for KSM is dynamic. It can often be 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. With the scan time advisor the pages_to_scan parameter based can be changed based on demand. Algorithm ========== 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 algorithm has a max and min value to: - guarantee responsiveness to changes - to limit CPU resource consumption Parameters to influence the KSM scan advisor ============================================= The respective parameters are: - ksm_advisor_mode 0: None (default), 1: scan time advisor - ksm_advisor_target_scan_time how many seconds a scan should of all candidate pages take - ksm_advisor_max_cpu upper limit for the cpu usage in percent of the ksmd background thread The initial value and the max value for the pages_to_scan parameter can be limited with: - ksm_advisor_min_pages_to_scan minimum value for pages_to_scan per batch - ksm_advisor_max_pages_to_scan maximum value for pages_to_scan per batch The default settings for the above two parameters should be suitable for most workloads. The parameters are exposed as knobs in /sys/kernel/mm/ksm. By default the scan time advisor is disabled. Currently there are two advisors: - none and - scan-time. Resource savings ================= 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. The new advisor works especially well if the smart scan feature is also enabled. How is defining a target scan time better? =========================================== For an administrator it is more logical to set a target scan time.. The administrator can determine how many pages are scanned on each scan. Therefore setting a target scan time makes more sense. In addition the administrator might have a good idea about the memory sizing of its respective workloads. Setting cpu limits is easier than setting The pages_to_scan parameter. The pages_to_scan parameter is per batch. For the administrator it is difficult to set the pages_to_scan parameter. Tracing ======= A new tracing event has been added for the scan time advisor. The new trace event is called ksm_advisor. It reports the scan time, the new pages_to_scan setting and the cpu usage of the ksmd background thread. Other approaches ================= Approach 1: Adapt pages_to_scan after processing each batch. If KSM merges pages, increase the scan rate, if less KSM pages, reduce the the pages_to_scan rate. This doesn't work too well. While it increases the pages_to_scan for a short period, but generally it ends up with a too low pages_to_scan rate. Approach 2: Adapt pages_to_scan after each scan. The problem with that approach is that the calculated scan rate tends to be high. The more aggressive KSM scans, the more pages it can de-duplicate. There have been earlier attempts at an advisor: propose auto-run mode of ksm and its tests (https://marc.info/?l=linux-mm&m=166029880214485&w=2) Changes: ======== V4: - rename ksm_advisor_min_pages to ksm_advisor_min_pages_to_scan - rename ksm_advisor_max_pages to ksm_advisor_max_pages_to_scan - folded init_advisor() into set_advisor_defaults() - moved set_advisor_defaults() to second patch - Fixed long division for 32 bit platforms in scan_time_advisor() - folded stop_advisor_scan() into scan_time_advisor() - renamed run_advisor() to stop_advisor_scan() - Fixed typo - Added documentation for min and max cpu and how they are used in the scan time advisor calculation V3: - Use string parameters for advisor mode - Removed min cpu load sysfs knob - dropped unused enums in ksm_advisor_type - renamed KSM_ADVISOR_LAST to KSM_ADVISOR_COUNT - init_advisor() is needed but changed how it is initialized - don't allow to change pages_to_scan parameter when scan-time advisor is enabled - add ksm_advisor_start_scan() and ksm_advisor_stop_scan() functions to calculate scan time - removed scan time parameter to scan_time_advisor() function V2: - Use functions for long long calculations to support 32 bit platforms - Use cpu min and cpu max settings for the advisor instead of the pages min and max parameters. - pages min and max values are now used for the initial and max values. Generally they are not required to be changed. - Add cpu percent usage value to tracepoint definition - Update documentation for cpu min and cpu max values - Update commit messages for the above changes *** BLURB HERE *** Stefan Roesch (4): mm/ksm: add ksm advisor mm/ksm: add sysfs knobs for advisor mm/ksm: add tracepoint for ksm advisor mm/ksm: document ksm advisor and its sysfs knobs Documentation/admin-guide/mm/ksm.rst | 55 +++++ include/trace/events/ksm.h | 33 +++ mm/ksm.c | 298 ++++++++++++++++++++++++++- 3 files changed, 385 insertions(+), 1 deletion(-) base-commit: 12d04a7bf0da67321229d2bc8b1a7074d65415a9