From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C192C35274 for ; Mon, 18 Dec 2023 23:11:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B51376B007D; Mon, 18 Dec 2023 18:11:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B035D6B007B; Mon, 18 Dec 2023 18:11:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 960E86B0081; Mon, 18 Dec 2023 18:11:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 83DC16B007B for ; Mon, 18 Dec 2023 18:11:13 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 55C631C12CD for ; Mon, 18 Dec 2023 23:11:13 +0000 (UTC) X-FDA: 81581486826.02.B1A3D41 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf24.hostedemail.com (Postfix) with ESMTP id B8A7018001D for ; Mon, 18 Dec 2023 23:11:11 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; spf=neutral (imf24.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702941071; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=53VlO8kcaR4GVn/384+KtcjXdSyibBadkFsIOrFPpnE=; b=48S92VXYMWiLc3JEQu0S8gbttCL+qpx231EtLkrQMGepWXezezfrAADrx/hccZd2Tawcck XvyUkFTPuCFE4TfcG9ZpD8npUNLcHCdvc7QzI3T02Vpm0/Ji3A5wWspTgP28sPusJUc1w3 g7d5eE1wMqnnsfzeb9rb1QYlq6ddHRY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702941071; a=rsa-sha256; cv=none; b=hHVed8va//+f/pO8LkqCPh2HlxGOD1ZaH2oO6JcDEfiwCp/GH9mNMwlkk2S9Mxoeboow5F qebOraMWtK5aWgs/zupORba2o/uPtwox/sW+OgoX6zZXkgcZ9rtYSSglAQDWEoOd/hEh9H lpp9+b7/BYtH+YnoncMYocIf1sDB5WU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; spf=neutral (imf24.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id AAA42111427A7; Mon, 18 Dec 2023 15:10:56 -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 v5 0/4] mm/ksm: Add ksm advisor Date: Mon, 18 Dec 2023 15:10:50 -0800 Message-Id: <20231218231054.1625219-1-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Stat-Signature: rixpr9x8gs1aczktfkgbzme4imao8q7o X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: B8A7018001D X-Rspam-User: X-HE-Tag: 1702941071-738258 X-HE-Meta: U2FsdGVkX1/wOs4vZ+oyeTz44VGk11ww5YBvA2Y0ZPPwVePREMRPe4vdlxoqtdwxLIyOi7NU9MGS5qpFVtu0m3IIal8KKNSif2QtwlNqSD3UzeFwTou1RCEMVlSLVzPPHaZaLXftiV3aPETYsojZhanPj5H2CDQ5KqBJmEYahQIDJlODzgCraV3/kY8nZceWsio6H30rTTxVGcF477lRq2P3wEimNP8tBp1MulSahuHtBeF6O9aejP+Dz5XZ3Z13WuTKdvMee+I4Lgp+vEXj3HQc9hXqY503r/10zcNiluy/dtA7f9P8suPA3KsiKUXQSiWRNyGSu/cW1cOcBc/UCXC9BQD4dtV+IHGjM78aF2c497OF5/FVeHGkLMayjmPvhVK28hTm4M6jW//FupLp2SO7UhgoilLAYJ3SLhZ02DdPwwSHQS83352oc8mlKYjbSf8WJXBqt55E+8bM17UGuCVIl/9n2DavLrkPng9B3yYKEMF+FAivnll+l44gC19EvrY3qtb6rc4r9M66YReQI0aMS/XUrgt+ALZDjMjUC1RIF9/TC/DKGJUGFJSpP6RwYuzhSFhptc/FSg5/kTIEKlJOxyWGix1aLEgDJV543kykUKeuOAj59/JrIWdjIkSFz6lvCT7/CCso45jnoacM7A+AcqNmA3bx8/V2l8mOEm5D1oimkkKRVVI7Nv04CRDtWrXP3FAtBOF0JUTL68eKYtYNZHCK5bxOILXqAilyncK6PvK7Dkf6SJTB5fprG1FE8xp2k5zfc5IhAEwZaY5LbEL+2uV0C4FCWTOsqtKn4/oTS1WidCBYpIIWEuysT0u08Z7RfdL0o/V2SwEaWqd8HnJQPo5losRtYufllbdm13STmvSEsRDhUgXEtKyZT+ES/80fM/3vpXd+UBAp/+w7Dk44LDd+DbsY4emE7qQXNu0IHsXFMku6T0ve+sVeoEXukmPwysLFe8XIokS6pGY LRoTxByY Pldv589h9lOlVz7RVY8UsZgt/ukVEjFhED+NjRJJPSBH9VsKrjVSSW39X/dJ5j0h5oZgvr/ac8VXuV0rIajxJ1QmFLA/EMtYyv71q63fko0/HdhS4O05tSTf42g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: What is the KSM advisor? =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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? =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D The number of candidate pages for KSM is dynamic. It can often be observe= d 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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? =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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. Th= e pages_to_scan parameter is per batch. For the administrator it is difficu= lt to set the pages_to_scan parameter. Tracing =3D=3D=3D=3D=3D=3D=3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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=3Dlinux-mm&m=3D166029880214485&w=3D2) Changes: =3D=3D=3D=3D=3D=3D=3D=3D V5: - Changed ksm_advisor_min_cpu to use a define and uppercased it - Removed ksm_advisor_min_pages_to_scan from first patch, its only needed in the second patch - Only reset defaults in advisor_mode_store() when mode changes - Add #ifdef CONFIG_SYSFS for set_advisor_defaults() V4:=20 - 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=20 - Update commit messages for the above changes 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 | 307 ++++++++++++++++++++++++++- 3 files changed, 394 insertions(+), 1 deletion(-) base-commit: 4d9256c0c56bafd76b8b1517916451e168fae495 --=20 2.39.3