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 64C7DC67871 for ; Mon, 24 Oct 2022 23:14:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD9F780008; Mon, 24 Oct 2022 19:14:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A89FA80007; Mon, 24 Oct 2022 19:14:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 978A280008; Mon, 24 Oct 2022 19:14:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8991D80007 for ; Mon, 24 Oct 2022 19:14:13 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 505D6AAEFF for ; Mon, 24 Oct 2022 23:14:13 +0000 (UTC) X-FDA: 80057398386.09.AEED669 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf04.hostedemail.com (Postfix) with ESMTP id E3CAA40021 for ; Mon, 24 Oct 2022 23:14:12 +0000 (UTC) Received: by mail-pl1-f201.google.com with SMTP id u9-20020a17090341c900b0017f8514cf61so7116499ple.16 for ; Mon, 24 Oct 2022 16:14:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zUr9nCpZGwPmjaMYLimECUICch35FeuRhnm3VL93618=; b=lGxLBaCkjGJQ5m7n6pn99E7OtikpIKcRVqc+S3npSzqN/BVCnNlv0B/ccVxzryt5rY Swq/kKFdgOZcInH3Gva6ymQSMwzkDmO6Js6CE+071v6qrE0s6U0xMVup7ALCOEuaGeAx XNDmqWyzepz0QuZ5WUA+iPBFB1uK+IklgHtLOkthABx2gvTqwPQA+wDDyiaKkkmteRhq lnUxoJYxt3RJEX5lL49GQejqBgo5NzsftGHnc28NWhLW9VZpWYOQ7lVjVOb0lhhk3KLk SR9YVlH31zbLMwxACzt1zzcmaRTcUsY7+sqUXgIgXgod2N9L0kpG1gE/BFybABUxYiGk TLvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zUr9nCpZGwPmjaMYLimECUICch35FeuRhnm3VL93618=; b=sylv6nnIc2HfPV49NZjqcnCsCRLG2I2sJYCIK11bASHll6G4GMtsVAon4/a8e7SfSe dd84i3w9KVLSNWfVAjkLiX+3QT93zAQXZNAqTxI+4v2nW9EKvQJEm3hjIUBoV+wbCCHo vgfp/D++S9OL3fwaRY7nEEv6QZvDFhSpbzcKYMCgCEWbwSly2mJemlM0ZjL3NfCFinhF zkUQQ85yNcvXd+PStFv1l5ASVMe2pwwUT5EtVUmC47PvEigjpnNupJ2INigQYZfajcQ8 2+KP9nwS7b0K+Ko8q52Lm0ESUe2hj3eFF/iF8hdm3tHkEkW2SIsPHyfIni+E0jBq0tOS BD6w== X-Gm-Message-State: ACrzQf2AFrETuTK15qkNDreony2dhtKqeJkpz2T7fMICcu3RFCtmHTEo AxcnvIMHnoDWYstKHloDqCdNysrUYks08w== X-Google-Smtp-Source: AMsMyM4A+0ArO1kTkLwRNYEITVTq3K7I/kdihJ6u1j0iy9n0LOAi68ufPrrVmhXaIZTyj+hBejplMVWycOMgxg== X-Received: from shakeelb.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:262e]) (user=shakeelb job=sendgmr) by 2002:a05:6a00:1406:b0:565:dc13:bb36 with SMTP id l6-20020a056a00140600b00565dc13bb36mr36276391pfu.46.1666653251624; Mon, 24 Oct 2022 16:14:11 -0700 (PDT) Date: Mon, 24 Oct 2022 23:14:08 +0000 In-Reply-To: <20221024153022.1b81a182eac44797b40bfda1@linux-foundation.org> Mime-Version: 1.0 References: <20221024052841.3291983-1-shakeelb@google.com> <20221024153022.1b81a182eac44797b40bfda1@linux-foundation.org> Message-ID: <20221024231408.j3moql6q5qhct3jx@google.com> Subject: Re: [PATCH] mm: convert mm's rss stats into percpu_counter From: Shakeel Butt To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666653253; a=rsa-sha256; cv=none; b=vM70ELHNEtj/U26g8Xzmmlil57g7+7tnbdnM5zAgEwzAtfUgwyxpFlsQMcCeFHjvU/ROO9 Ymax414trl5D+AjAFH0oAbtLoxTdbHFxjAtiYXN/2WSxt6yodg80Wgm85xY+pWpYRgwW73 MzT+AETH4OmnKAaCR6bGdTIdbJHI5cU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lGxLBaCk; spf=pass (imf04.hostedemail.com: domain of 3QxxXYwgKCPgxmfpjjqglttlqj.htrqnsz2-rrp0fhp.twl@flex--shakeelb.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3QxxXYwgKCPgxmfpjjqglttlqj.htrqnsz2-rrp0fhp.twl@flex--shakeelb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666653253; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zUr9nCpZGwPmjaMYLimECUICch35FeuRhnm3VL93618=; b=geodgzJwrDYJiDkMVOg0qNkcuxYJBtwCOOL3XrY5cbs8Q+/CD8jJ1Di7+B0crIqxhkPmJM qsbSOETPiO0nIqgAe12Jh0SSuhwg1TOaoMpJRVSeHS6NJPspiyNKHVhCwDjTq5HrR1v8uB 8IXDzBd4HGVeqGHSYmRJv9RStfLRbnk= X-Rspam-User: X-Rspamd-Queue-Id: E3CAA40021 Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lGxLBaCk; spf=pass (imf04.hostedemail.com: domain of 3QxxXYwgKCPgxmfpjjqglttlqj.htrqnsz2-rrp0fhp.twl@flex--shakeelb.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3QxxXYwgKCPgxmfpjjqglttlqj.htrqnsz2-rrp0fhp.twl@flex--shakeelb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: 4q8o9gikj68eqooq37wogg7r4oyiqg14 X-Rspamd-Server: rspam03 X-HE-Tag: 1666653252-167189 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: On Mon, Oct 24, 2022 at 03:30:22PM -0700, Andrew Morton wrote: > On Mon, 24 Oct 2022 05:28:41 +0000 Shakeel Butt wrote: > > > Currently mm_struct maintains rss_stats which are updated on page fault > > and the unmapping codepaths. For page fault codepath the updates are > > cached per thread with the batch of TASK_RSS_EVENTS_THRESH which is 64. > > The reason for caching is performance for multithreaded applications > > otherwise the rss_stats updates may become hotspot for such > > applications. > > > > However this optimization comes with the cost of error margin in the rss > > stats. The rss_stats for applications with large number of threads can > > be very skewed. At worst the error margin is (nr_threads * 64) and we > > have a lot of applications with 100s of threads, so the error margin can > > be very high. Internally we had to reduce TASK_RSS_EVENTS_THRESH to 32. > > > > Recently we started seeing the unbounded errors for rss_stats for > > specific applications which use TCP rx0cp. It seems like > > vm_insert_pages() codepath does not sync rss_stats at all. > > > > This patch converts the rss_stats into percpu_counter to convert the > > error margin from (nr_threads * 64) to approximately (nr_cpus ^ 2). > > Confused. The max error should be O(nr_cpus)? > So, percpu_counter code sets the percpu batch in the following way: static int compute_batch_value(unsigned int cpu) { int nr = num_online_cpus(); percpu_counter_batch = max(32, nr*2); return 0; } This means each cpu can cache (nr_cpus*2) updates. Practically the number of cpus do not change and are usually much less than the number of threads of large applications, so error margin is lower. > > However this conversion enable us to get the accurate stats for > > situations where accuracy is more important than the cpu cost. Though > > this patch does not make such tradeoffs. > > Curiousity. Can you expand on the final sentence here? > Basically we can just use percpu_counter_add_local() for the updates and percpu_counter_sum() (or percpu_counter_sync() + percpu_counter_read) for the readers. At the moment the readers are either procfs interface, oom_killer and memory reclaim which I think are not performance critical and should be ok with slow read. However I think we can make that change in a separate patch. thanks, Shakeel