Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2001524rdb; Thu, 7 Dec 2023 15:26:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnd9K/StNXjZ+RJ7r0MHzIh3HpAibnXMXqlHIl8qY2JHaE0Tgqhgcllh3EbB51RyTLn0Lf X-Received: by 2002:a05:6a20:b794:b0:18c:19d1:5e91 with SMTP id fh20-20020a056a20b79400b0018c19d15e91mr2101233pzb.16.1701991597501; Thu, 07 Dec 2023 15:26:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701991597; cv=none; d=google.com; s=arc-20160816; b=j9EcorbnsNevF1RflK/yghGXrMqY0W6dYI7Jl2W48LdKLzeysVzF8q9UYLIbHeIpwL M5EcnLSCShsrnd/83I639yBhHUZt0s08FDUiJqzFFOV4VsTdEqWOr1x3VH32vOznD4Oq 7e4vYbOL6IXSnA9GANtmICFcmBAmmr1n48z7e98jjafRoWGzsDPk1uD7AlrlvSdjkBsc 31NGrGO3aQ7NDPUTQFlyE3idKkxw3PBHyyvrPEDAuGNOtq9zdoeR7L2+gCBnBkyQrEma o4Xszd/hw2OuiteUZN7o56UlPzdKdswvs4ilyuYqwBtRhPPsvLt9ivos4vraWaEPSXMg m2Ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:in-reply-to:date:subject :cc:to:from:user-agent:references:feedback-id:dkim-signature :dkim-signature; bh=BtllAeRuCcPTl4gOdGM/QH1NvWPxw+8wNvSb0SE/0x4=; fh=4yab9sgk6bnYvZDbVqikwkrODM5Y2lzG3PnlNfT3s6A=; b=NHUyqk5B+5XubkHR9MPZWmav8SbIajuY550fRV1X8woYJdDqkxNpghqKwFsS5kdt6w gHrSX4TuXoKP5wrdbGqAqxqqi2ysR2SrxQVRak8xdF0L7hvXzKUBbvVFHMscGAKKB0XI 9gm2gTkbGjNnAyEVp3JUiI8HRRMFCYaA4syOLRx68PpzntCWtanPhVhxFU9o7DYFVnAw kOAtma78mRaXM3Sb298OJ9d/6zsJ3DrEkUu5TpzFmUX+F84AtiMnnvXjDVsOrv9csXDQ CdIRboDVZOxs/4/fZU3EVAVGpOA5BcAD5iz8qBq5CoYZrV5Nk8R78YcO/P2Hnoz0FcP0 hTIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@devkernel.io header.s=fm2 header.b=EaAnBEEk; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=SPimJHge; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id b12-20020a17090a800c00b002859d83de02si566664pjn.139.2023.12.07.15.26.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 15:26:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@devkernel.io header.s=fm2 header.b=EaAnBEEk; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=SPimJHge; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 5465280FC185; Thu, 7 Dec 2023 15:26:34 -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 S1444110AbjLGX0O (ORCPT + 99 others); Thu, 7 Dec 2023 18:26:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232505AbjLGX0L (ORCPT ); Thu, 7 Dec 2023 18:26:11 -0500 Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D9C41719 for ; Thu, 7 Dec 2023 15:26:17 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 37EDE3200B8F; Thu, 7 Dec 2023 18:26:16 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Thu, 07 Dec 2023 18:26:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devkernel.io; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm2; t=1701991575; x=1702077975; bh=Bt llAeRuCcPTl4gOdGM/QH1NvWPxw+8wNvSb0SE/0x4=; b=EaAnBEEkCePGS88Kzt KeGILp5fDctSLpMJrATWla6rcjKJROlBbb/Jp9oa9HRF22ijq7D70YpIAPyX5Rn8 ZqRjT14uc0F9df6Yk5Dk0kL7jaZT8OGuHt5LnC+kIIGQK1cu92FvaO9CfyC37let I0kkOoAEmaFHnxMtcejllHRadVW8jB59t0JfzOTte0jTjKxt8TqXjVtMNrWadT6N j/N9ZcwplQPVg83+JSsJQNq9P66OW4AzasD2Oh180NdMMfbOdO63QWhLivToRNOu 55wO3jqKPxa6vIlP6bjvSGCm9MM4aU/HPadA4fTpxyJGX17vuZ+dLJQiwHF1yI3t R6oQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1701991575; x=1702077975; bh=BtllAeRuCcPTl 4gOdGM/QH1NvWPxw+8wNvSb0SE/0x4=; b=SPimJHgega8xzRp7sPUJLqkbYjFmw CbHOvU5wtYJXA08x9q8+sTR7P1/oR7ZKDzGY5Vj2n6GeyDms/Ont5aOfybAyz5pN 9cNNzw1WJk1xazjratCZ5NvtRoWP99b9278BWi7pZoQPHuk8MBifUuPHHZ/HLxnR DdDN4/yVb0AbQj7zf23utL/jd7i8DCpRML3ebSQIWAKdQ71PxXabsK4guInpooha r5btUwawqJ+/ZXhh1YFKdhlPKDgIAnxiC3IWSgNfkT/MFO79Pf0qt4tIXHEmCxdI MvVnyxwnLhXuxZTChA6hV3D+DXflHbs01BKcB3vbekQ7ypsJUDzIqPXBg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudekgedgtddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpehffgfhvfevufffjgfkgggtsehttd ertddtredtnecuhfhrohhmpefuthgvfhgrnhcutfhovghstghhuceoshhhrhesuggvvhhk vghrnhgvlhdrihhoqeenucggtffrrghtthgvrhhnpeduteejkefhueffkeekudffueeihe dukeeuteejvedvgeekhfehheektddtuefghfenucffohhmrghinhepmhgrrhgtrdhinhhf ohenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehshh hrseguvghvkhgvrhhnvghlrdhioh X-ME-Proxy: Feedback-ID: i84614614:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 7 Dec 2023 18:26:13 -0500 (EST) References: <20231204234906.1237478-1-shr@devkernel.io> <20231205124610.ca70c47e880daa81a0f21f07@linux-foundation.org> User-agent: mu4e 1.10.3; emacs 29.1 From: Stefan Roesch To: Andrew Morton Cc: kernel-team@fb.com, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v3 0/4] mm/ksm: Add ksm advisor Date: Thu, 07 Dec 2023 15:18:27 -0800 In-reply-to: <20231205124610.ca70c47e880daa81a0f21f07@linux-foundation.org> Message-ID: <87il59vbl9.fsf@devkernel.io> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,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]); Thu, 07 Dec 2023 15:26:34 -0800 (PST) Andrew Morton writes: > On Mon, 4 Dec 2023 15:49:02 -0800 Stefan Roesch wrote: > >> 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. > > Dumb question time. Can this be done in userspace? Presumably this > will require exposing some additional kernel state to userspace. > Userspace doesn't look like a good fit - We need access to how much cpu the ksmd kernel thread spent during the last scan - We want to recompute this after each scan completes, so the new parameter is in effect for the next scan and to avoid we have to partially deal with new values in the middle - When the advisor is active we want to disable that other users can manually set the scan rate. The scan advisor might be augmented with per page cost in the future. >> 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 >> minimum value for pages_to_scan per batch >> - ksm_advisor_max_pages >> maximum value for pages_to_scan per batch > > Should these be called ksm_advisor_min_pages_to_scan? > That's a good recommendation. I'll make that change and the corresponding change for max. >> 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. > > Disabling it will reduce the effectiveness of testing. What are the > risks of defaulting to "on"? > Some users might have already tuned the values for pages_to_scan. So generally turning it on might not be good. However we could turn it on, it the default value of 100 for pages_to_scan hasn't been changed. Any thoughts? >> 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. > > 25% of what? What is the overall affect on machine resource > consumption? > 25% of the cpu consumption of the ksmd background thread. On a 32 cpu machine this translates to 1 to 2% cpu savings alltogether. However this is highly workload dependent. >> 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)