From patchwork Fri Sep 20 22:11:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kaiyang Zhao X-Patchwork-Id: 13808711 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 0A161CF9C6B for ; Fri, 20 Sep 2024 22:12:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F26E6B0082; Fri, 20 Sep 2024 18:12:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 87BFC6B0085; Fri, 20 Sep 2024 18:12:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 71C436B0088; Fri, 20 Sep 2024 18:12:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 569956B0082 for ; Fri, 20 Sep 2024 18:12:49 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 017EC1406BC for ; Fri, 20 Sep 2024 22:12:48 +0000 (UTC) X-FDA: 82586517258.21.248FD3F Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) by imf12.hostedemail.com (Postfix) with ESMTP id 3AE4D4000C for ; Fri, 20 Sep 2024 22:12:47 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=cs.cmu.edu header.s=google-2021 header.b=XyHY4HRR; dmarc=pass (policy=none) header.from=cs.cmu.edu; spf=pass (imf12.hostedemail.com: domain of kaiyang2@andrew.cmu.edu designates 209.85.219.51 as permitted sender) smtp.mailfrom=kaiyang2@andrew.cmu.edu ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726870282; a=rsa-sha256; cv=none; b=AdJO/PLUl5oI7uz9suR50tJ8TDlga4zomj2QC8hgDJqeVeVEvZBKHg2Gc8TFY2wIUu1JMF 982f2eubvBsg67OzxCJ2WlHdLLsTg5BePJ5ai9k7xkQoeemXueuAGcTOUK624ytHLFZv9u 9UOsiun6nkIz41j+L3mu1Mn9iNUklFY= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=cs.cmu.edu header.s=google-2021 header.b=XyHY4HRR; dmarc=pass (policy=none) header.from=cs.cmu.edu; spf=pass (imf12.hostedemail.com: domain of kaiyang2@andrew.cmu.edu designates 209.85.219.51 as permitted sender) smtp.mailfrom=kaiyang2@andrew.cmu.edu ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726870282; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mjQIYISgv8bXMzl4SQq6QWJhA5lMgQlc/Hdxb7kdcNw=; b=z2owilUMkoF+nYEOwB5QYl7AE8DC3qzoX/OVYr3ZkGyLI+x67DNFI+fFq1Or7AYOQP/QZQ O2pR8Kf8LllOUuLICeIoGLebaCmgFYKevoCQqTSQsMfpoGOGrDBQLy6hZfleSOt5DKjqHl 15yZdchUumBx85JcPHTF5vDbutqQS9M= Received: by mail-qv1-f51.google.com with SMTP id 6a1803df08f44-6c35357cdacso17239326d6.0 for ; Fri, 20 Sep 2024 15:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.cmu.edu; s=google-2021; t=1726870366; x=1727475166; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mjQIYISgv8bXMzl4SQq6QWJhA5lMgQlc/Hdxb7kdcNw=; b=XyHY4HRRpbrLrAl+aNc8S1kO6UEep46/f6DZxqoGpCkt84M+pOWKNbYTqdirDd/ZCe BHthymhF3Jl/3HUb3rlOh+X3VyT+CiFTXeUdO+STLoShigLP4M4RJkp+jtVNBG9xokuv I8gbm73svG9zLBINO91Nz96PwLJepEqgaatUJryh6Brep+YXOkzKy1njcqKyTKQ+p1IX gueo9TrBnOktZE1aR0ufFcGZ3+zTSxsKtQJbt+82YnRlyzXNQ0u5ICKsRhFw/b1xfjb/ H+wvZLuQfFG/ckoyGdN5OkTZBmm6KqBc9ekPI31H3w2OSo0AL1+ePshsEOSC6i8AnL4L aueQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726870366; x=1727475166; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mjQIYISgv8bXMzl4SQq6QWJhA5lMgQlc/Hdxb7kdcNw=; b=DntuObMtJyIY5fCdVXcVvAHcN+L7bnr74mUqvqL6C0aU0CFGyIWYwCtiJO7k2rPG7B YyPC9bBOOPwo7q/fYkZbq9xn88vqts/mR1IklOrgiodnLLWZAxlj5sJ2jkEsXSKwQDYX TwtUTzmjnkirVBDjSIE6562NUxE8z5c8NLLS8vl/PCmhMt/6N8/RtA61dtUwxFbrSp0F eteGF41HcKikFBhnCd26ooZxE9vgmvAHWvOGd35iRAO8MlW5at6VheSoavMYbZ5BrF/0 dn2fdAU2et1CrIh3p+/WB6bNnEswyEaCuaRnXpP7v+2Gu/eK8cyBJG4M+di7ew/TKUT+ Z6tg== X-Gm-Message-State: AOJu0Yyvf3ZEQx2nhfSWBMolHkgGzIzbVzHIACITaRsg0lmcjbhtxH3m JytJEmmGDXCcS3N46bnRMuUZAnAYiPDhQ0oWDriv76Us11S8hAMTb98zw6TCr92lfXra/W2hknm rdEvQOb6nHKcG5KUEmQImkueGaALn2w/PSccYPNKwwx2uXuPSrGQ+4E+B6wV6W4xkN9JxIS8pPb 6DDXDWmXgWYLawLyPz691CmuEMkGBMjvd0LLX30w== X-Google-Smtp-Source: AGHT+IES2HVe0X98tZeGiUhTQNrbSdJWRIaPaFJBiEj5ef4ucxT4/xboRx0SNgqoj2W0gV0qAMe+fw== X-Received: by 2002:a0c:fac3:0:b0:6c7:c658:5f40 with SMTP id 6a1803df08f44-6c7c658610bmr26601826d6.14.1726870365846; Fri, 20 Sep 2024 15:12:45 -0700 (PDT) Received: from localhost (pool-74-98-231-160.pitbpa.fios.verizon.net. [74.98.231.160]) by smtp.gmail.com with UTF8SMTPSA id 6a1803df08f44-6c75e557c01sm22904026d6.101.2024.09.20.15.12.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 20 Sep 2024 15:12:45 -0700 (PDT) From: kaiyang2@cs.cmu.edu To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, akpm@linux-foundation.org, mhocko@kernel.org, nehagholkar@meta.com, abhishekd@meta.com, hannes@cmpxchg.org, weixugc@google.com, rientjes@google.com, Kaiyang Zhao Subject: [RFC PATCH 1/4] Add get_cgroup_local_usage for estimating the top-tier memory usage Date: Fri, 20 Sep 2024 22:11:48 +0000 Message-ID: <20240920221202.1734227-2-kaiyang2@cs.cmu.edu> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240920221202.1734227-1-kaiyang2@cs.cmu.edu> References: <20240920221202.1734227-1-kaiyang2@cs.cmu.edu> MIME-Version: 1.0 X-Rspamd-Queue-Id: 3AE4D4000C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 4scesxsnzyanogqwfgasdjixs45tdk6i X-HE-Tag: 1726870367-282275 X-HE-Meta: U2FsdGVkX18+D1kv2FEOgR1/GOlcZt4nw9JC26oyxC/hU08tMQOB64U7EnrePlHKD0/hng34Pv1Oq3g1o2M+GU6rLgv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X53I0cUi1KXAiYGw/kME24n9Tww24uylI7Hjz+8cLeENlkcdep/HmD5sQMVPTCzrGlDlVT+MlkQl9BLxcartzbP4SCASpLvQG8giuaw/WUJswYfpRrAxqMk4VK0pGjAVt2ZEYw3XK8R5iFJq2Uz1SvpS5Tc6nKycDtgJCgJgX0T7gK8xd9P5/ho21I0vnEIoh9oR1FWgW+NCxr5s8c4a2DR43rhoQJtTqbjRpCKDD5hCajTXI7edCowxlQ4pdPkvhgOOqPirbLw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.002013, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Kaiyang Zhao Approximate the usage of top-tier memory of a cgroup by its anon, file, shmem and slab sizes in the top-tier. Signed-off-by: Kaiyang Zhao --- include/linux/memcontrol.h | 2 ++ mm/memcontrol.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 34d2da05f2f1..94aba4498fca 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -648,6 +648,8 @@ static inline bool mem_cgroup_unprotected(struct mem_cgroup *target, memcg == target; } +unsigned long get_cgroup_local_usage(struct mem_cgroup *memcg, bool flush); + static inline bool mem_cgroup_below_low(struct mem_cgroup *target, struct mem_cgroup *memcg) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index f19a58c252f0..20b715441332 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -855,6 +855,30 @@ unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) return READ_ONCE(memcg->vmstats->events_local[i]); } +/* Usage is in pages. */ +unsigned long get_cgroup_local_usage(struct mem_cgroup *memcg, bool flush) +{ + struct lruvec *lruvec; + const int local_nid = 0; + + if (!memcg) + return 0; + + if (flush) + mem_cgroup_flush_stats_ratelimited(memcg); + + lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(local_nid)); + unsigned long anon = lruvec_page_state(lruvec, NR_ANON_MAPPED); + unsigned long file = lruvec_page_state(lruvec, NR_FILE_PAGES); + unsigned long shmem = lruvec_page_state(lruvec, NR_SHMEM); + /* Slab size are in bytes */ + unsigned long slab = + lruvec_page_state(lruvec, NR_SLAB_RECLAIMABLE_B) / PAGE_SIZE + + lruvec_page_state(lruvec, NR_SLAB_UNRECLAIMABLE_B) / PAGE_SIZE; + + return anon + file + shmem + slab; +} + struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) { /*