Received: by 2002:a05:7412:a9a3:b0:f9:327e:43ab with SMTP id o35csp29343rdh; Mon, 18 Dec 2023 03:29:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IExF3ETkJeE4P/V5Rp41FWch6qFVvw0hqVz5OVvqm0MGgpYhw9kJbejoPzbjqldHZgrIH6G X-Received: by 2002:aa7:988d:0:b0:6d5:3912:d5ca with SMTP id r13-20020aa7988d000000b006d53912d5camr644250pfl.50.1702898996285; Mon, 18 Dec 2023 03:29:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702898996; cv=none; d=google.com; s=arc-20160816; b=V8cFtjwY91tUegHt4JLlP8eo5SVaMwyQ21PHNjTaux+ZQw84Q9SjlWSWjqde2u5qHx RIYanl9iBg9giyDitMu4mYVZzIAhU8exzEQD3HhMF06njz8ek4jLuURyt/7bTJBlP0sL rac9uw8KSEyA6EVLpNQxARCleNjw5ZRHUodpt57WOt2HlPh3u7ypHG3peJ8bqBgNfJpi 7idFnOTN7ayn7AMnWSp/vnuJiIt3D+5p75Dov9psS7QcMZGoN4730Ou7Iq/y8xmQ+myE K/GbyS/IVGLho0XI50DnavSlKWU1Htl8I3OMXk8nzMRnxYs8KhV2ChFVUQ8OtK9iVhMt Fyww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=GSd7La5h0M9e2qVPUNvunxZbxK4L5T2M7NX5H+PBXVk=; fh=rj364kmaeZMVM5HdKawlrVtJQHBt+LI407FoTGJZ9pc=; b=LYa1bQOR41Q2K2eegRfEdIK4KxZVIb/x7rbmts4tkPJOoqdH/MDkujoLQa+95tbAa9 dFoxTGUAyKCe+J1TbnYtGZyMyVCGPJVCTo0hDXWI/54PQXPlrMDHxeTCKvCXzPH/CrP4 wUsPbZD0is3bv1MccP5Wp2L0TwCRqBfLvSSneXfCoXumPk4X78rxokJs9suHM4lMZNAL Wc+KUuOEno1Ykjmp5qP3uP749bZow3z4VnDUUtFFvyI38VTDFTtb28VY5fcMO1TNnC1U 44jtKyyKDt2uDlVHn65q91EGsaWZpPVRhr91E2pVL8ddRuNjAfQq6u+vgBX4YZNNvTPw xvBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YPrwcFgy; spf=pass (google.com: domain of linux-kernel+bounces-3432-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3432-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id kp8-20020a056a00464800b006cddd0d9820si17633093pfb.72.2023.12.18.03.29.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 03:29:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3432-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YPrwcFgy; spf=pass (google.com: domain of linux-kernel+bounces-3432-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3432-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E0FE02842F7 for ; Mon, 18 Dec 2023 11:29:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 67BAA199C1; Mon, 18 Dec 2023 11:29:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YPrwcFgy" X-Original-To: linux-kernel@vger.kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA56418E1B for ; Mon, 18 Dec 2023 11:29:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702898985; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=GSd7La5h0M9e2qVPUNvunxZbxK4L5T2M7NX5H+PBXVk=; b=YPrwcFgy+ibVOtXmOaNpyEJ+FwMGoUN734HNZ+I+2/0AkWdHGKjae7oL25phVVncNdcB+0 m0QXjCJEQgju+eyPE75c8xt1inwz+k+GhQ0rllWkIOT31jUykpZ4dwfmaify2gXo4QygS3 9JrlQMfnaqTv8aXM5sqyRkqq8GT4hPU= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-414-m6HGHiyKOGWXIePzZvPZZw-1; Mon, 18 Dec 2023 06:29:44 -0500 X-MC-Unique: m6HGHiyKOGWXIePzZvPZZw-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-33652ced20fso2122665f8f.1 for ; Mon, 18 Dec 2023 03:29:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702898983; x=1703503783; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GSd7La5h0M9e2qVPUNvunxZbxK4L5T2M7NX5H+PBXVk=; b=GP0F1vHRWdYjMCldJnQWczs0D2h1lF0h8gD2gmYEa7CyyD38I0MenCr2aVmvxnKNYI MGIG7+SHE60wHGd9On2FBa+PEd0LJJ+vm5Zo2medZKNHhwAFfEMfO9wFPEczpjV4pjRc axgOXkf9bvesdHq0XF3vNuYNOPHpVSfy27bRgj8UU+X4LUHuOqJtm/jw1jMvTP0/nJdP uV2k75x1YgYch3qlMqJu8+W5UXVR7uD0OnjNwD3NbxHP8Sn2h0oVKDyG5FCdn617Tkib 4mXgm2Wgv++UMOoqzcboXLw305DC2lUGleU11RFX9n9L5PorMiyQSjHxJIFoscj6LVca NezQ== X-Gm-Message-State: AOJu0YwXjTsnCdKIWpKyH/DJ4W3djidsMndjvZR2G4gY7RIDVdsOTbI1 ZoBgjsBKdRGantPN+YAIlJ7xSeUV7QhRlNl79TY9/eL4s9pIMpwyAAjOPNLARqipwaHcVwVAW12 +FheuZcJq2xSvJsHHZJEc8cCg X-Received: by 2002:adf:ef04:0:b0:336:6907:cbb4 with SMTP id e4-20020adfef04000000b003366907cbb4mr472476wro.53.1702898983030; Mon, 18 Dec 2023 03:29:43 -0800 (PST) X-Received: by 2002:adf:ef04:0:b0:336:6907:cbb4 with SMTP id e4-20020adfef04000000b003366907cbb4mr472462wro.53.1702898982551; Mon, 18 Dec 2023 03:29:42 -0800 (PST) Received: from ?IPV6:2003:cb:c72b:b500:b53e:6e32:1408:27ac? (p200300cbc72bb500b53e6e32140827ac.dip0.t-ipconnect.de. [2003:cb:c72b:b500:b53e:6e32:1408:27ac]) by smtp.gmail.com with ESMTPSA id r8-20020a056000014800b003364360839esm13204369wrx.81.2023.12.18.03.29.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Dec 2023 03:29:42 -0800 (PST) Message-ID: <07c3d204-8285-46d2-b7fa-c63800bd7073@redhat.com> Date: Mon, 18 Dec 2023 12:29:40 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 1/4] mm/ksm: add ksm advisor Content-Language: en-US To: Stefan Roesch , kernel-team@fb.com Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20231213182729.587081-1-shr@devkernel.io> <20231213182729.587081-2-shr@devkernel.io> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5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JOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <20231213182729.587081-2-shr@devkernel.io> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 13.12.23 19:27, Stefan Roesch wrote: > 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 > --- > 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; That will never be modified, right? Either mark it const or just turn it into a define. [...] > +/* > + * 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); I'd simply inline prev_scan_time() here and get rid of it. Whatever you think is best. Acked-by: David Hildenbrand -- Cheers, David / dhildenb