From patchwork Fri Aug 26 01:15:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Jialin X-Patchwork-Id: 12955421 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 A0C05ECAAD1 for ; Fri, 26 Aug 2022 01:18:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4417694000A; Thu, 25 Aug 2022 21:18:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C87F940009; Thu, 25 Aug 2022 21:18:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2910F94000A; Thu, 25 Aug 2022 21:18:44 -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 1B1EC940009 for ; Thu, 25 Aug 2022 21:18:44 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EE54C1A0331 for ; Fri, 26 Aug 2022 01:18:43 +0000 (UTC) X-FDA: 79839984126.26.4FBCC0A Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf30.hostedemail.com (Postfix) with ESMTP id E10D480019 for ; Fri, 26 Aug 2022 01:18:42 +0000 (UTC) Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MDMMb1t1gzlWPM; Fri, 26 Aug 2022 09:15:23 +0800 (CST) Received: from kwepemm600003.china.huawei.com (7.193.23.202) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 26 Aug 2022 09:18:39 +0800 Received: from ubuntu1804.huawei.com (10.67.174.175) by kwepemm600003.china.huawei.com (7.193.23.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 26 Aug 2022 09:18:39 +0800 From: Lu Jialin To: Zefan Li , Tejun Heo , "Johannes Weiner" , Andrew Morton , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song CC: Lu Jialin , Xiu Jianfeng , , , Subject: [RFC 2/2] memcg: Adapt cgroup.top into per-memcg Date: Fri, 26 Aug 2022 09:15:03 +0800 Message-ID: <20220826011503.103894-3-lujialin4@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220826011503.103894-1-lujialin4@huawei.com> References: <20220826011503.103894-1-lujialin4@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.174.175] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemm600003.china.huawei.com (7.193.23.202) X-CFilter-Loop: Reflected ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf30.hostedemail.com: domain of lujialin4@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=lujialin4@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661476723; a=rsa-sha256; cv=none; b=zsAYd/9WsGY+hJv0eCarTy/x7EPzZ2P5RhVMKMBJ0yaKiXb9Pj5WaXcLlPbQ+kWjMfDdEi AMAVC6yXu5prvZPCNiWwZrR5mWJzTHm2gt1z/kFJZSPFvSqC+b1sJ9jFZkjYHEfGsc1563 RfMxv2lAM6s9IIl0KtxQb17wumIGeko= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661476723; 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; bh=gY49G14HAMHUaXCZgiTY0xgXUcCYmv73OOzRh15HLf4=; b=ue+jBgumNLRm47TPNsLNUfQSAXagEnKrlFfvy49xeAn+jJIwY15Az0Wg4iDrMTXwOgOYSQ 2C4GXDrX3qZ3+3BuIfowE4IQEVQ7gaKVUP0/GZCopGIBaRyoEZR58L8i9w8YiGu1QEv1XT pigFaS6pFQB0WgTfWQtudwNc5TD7xAE= X-Stat-Signature: zriniktb1gierdr4upge9ashirwijpjr Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf30.hostedemail.com: domain of lujialin4@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=lujialin4@huawei.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: E10D480019 X-Rspam-User: X-HE-Tag: 1661476722-735446 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: cgroup.top is able to show resource usage information for each cgroups. Currently only memory usage is monitored ,including usage,anon,file, kmem(Bytes), stats for other resource types would be added as well. Show case: / # mount -t cgroup2 none /sys/fs/cgroup / # cd /sys/fs/cgroup/ /sys/fs/cgroup # echo "+memory" > cgroup.subtree_control /sys/fs/cgroup # mkdir test1 /sys/fs/cgroup # mkdir test2 /sys/fs/cgroup # mkdir test3 /sys/fs/cgroup # echo $$ > test2/cgroup.procs /sys/fs/cgroup # cd /test /test # ./memcg_malloc 512000 & /test # ./memcg_malloc 512000 & /test # ./memcg_malloc 512000 & /test # cd /sys/fs/cgroup /sys/fs/cgroup # echo $$ > test1/cgroup.procs /sys/fs/cgroup # cd /test /test # ./memcg_malloc 512000 & /test # cd /sys/fs/cgroup /sys/fs/cgroup # echo $$ > test3/cgroup.procs /sys/fs/cgroup # cat cgroup.top memory top: name usage anon file kernel test2 1974272 1671168 0 270336 test1 700416 569344 0 94208 test3 196608 86016 0 86016 Signed-off-by: Lu Jialin Co-developed-by: Xiu Jianfeng Signed-off-by: Xiu Jianfeng --- mm/memcontrol.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b69979c9ced5..e4d4afefe5a6 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5480,6 +5480,92 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) } } +static int cmp_usage(const void *a, const void *b) +{ + struct mem_cgroup *memcg_a = *(struct mem_cgroup **)a; + struct mem_cgroup *memcg_b = *(struct mem_cgroup **)b; + + return page_counter_read(&memcg_b->memory) - + page_counter_read(&memcg_a->memory); +} + +static int child_memcg_css_count(struct cgroup_subsys_state *css) +{ + struct cgroup_subsys_state *child; + int count = 0; + + css_for_each_child(child, css) + count++; + + return count; +} + +struct memory_top_info { + const char *name; + unsigned long idx; +}; + +static const struct memory_top_info memory_info[] = { + {"anon", NR_ANON_MAPPED}, + {"file", NR_FILE_PAGES}, + {"kernel", MEMCG_KMEM}, +}; + +static void mem_cgroup_css_top(struct cgroup_subsys_state *css, + struct seq_file *seq) +{ + struct mem_cgroup **array; + struct cgroup_subsys_state *child; + int memcg_number = child_memcg_css_count(css); + int i, j; + int count = 0; + + mem_cgroup_flush_stats(); + + array = kvmalloc_array(memcg_number, sizeof(struct mem_cgroup *), + GFP_KERNEL); + if (!array) + return; + + css_for_each_child(child, css) { + struct mem_cgroup *memcg = mem_cgroup_from_css(child); + + if (count == memcg_number) + break; + array[count++] = memcg; + } + + sort(array, memcg_number, sizeof(struct mem_cgroup *), cmp_usage, NULL); + + seq_printf(seq, "%s top:\n", css->ss->name); + + seq_puts(seq, "name\t\tusage\t\t"); + for (j = 0; j < ARRAY_SIZE(memory_info); j++) + seq_printf(seq, "%s\t\t", memory_info[j].name); + seq_puts(seq, "\n"); + + for (i = 0; i < memcg_number; i++) { + struct mem_cgroup *memcg = array[i]; + unsigned long usage = page_counter_read(&memcg->memory); + struct cgroup *cgroup = memcg->css.cgroup; + const char *name = cgroup->kn->name; + + seq_printf(seq, "%s\t\t%lu\t\t", name, usage * PAGE_SIZE); + for (j = 0; j < ARRAY_SIZE(memory_info); j++) { + u64 size; + + size = memcg_page_state_output(memcg, + memory_info[j].idx); + seq_printf(seq, "%llu\t\t", size); + } + seq_puts(seq, "\n"); + } + + kvfree(array); + +} + + #ifdef CONFIG_MMU /* Handlers for move charge at task migration. */ static int mem_cgroup_do_precharge(unsigned long count) @@ -6600,6 +6686,7 @@ struct cgroup_subsys memory_cgrp_subsys = { .css_free = mem_cgroup_css_free, .css_reset = mem_cgroup_css_reset, .css_rstat_flush = mem_cgroup_css_rstat_flush, + .css_top = mem_cgroup_css_top, .can_attach = mem_cgroup_can_attach, .cancel_attach = mem_cgroup_cancel_attach, .post_attach = mem_cgroup_move_task,