From patchwork Fri Aug 26 01:15:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lu Jialin X-Patchwork-Id: 12955420 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 F05CAECAAA2 for ; Fri, 26 Aug 2022 01:18:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D5EB6B0074; Thu, 25 Aug 2022 21:18:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 88363940009; Thu, 25 Aug 2022 21:18:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74DAE6B0078; Thu, 25 Aug 2022 21:18:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 66DF66B0074 for ; Thu, 25 Aug 2022 21:18:42 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3C8CE12060C for ; Fri, 26 Aug 2022 01:18:42 +0000 (UTC) X-FDA: 79839984084.25.288AAFB Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf12.hostedemail.com (Postfix) with ESMTP id 6766240018 for ; Fri, 26 Aug 2022 01:18:41 +0000 (UTC) Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MDMMF5WJbzkWgX; Fri, 26 Aug 2022 09:15:05 +0800 (CST) Received: from kwepemm600003.china.huawei.com (7.193.23.202) by dggemv704-chm.china.huawei.com (10.3.19.47) 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:38 +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:37 +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 1/2] cgroup: Introduce per-cgroup resource top show interface Date: Fri, 26 Aug 2022 09:15:02 +0800 Message-ID: <20220826011503.103894-2-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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661476721; 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=JmRV06yxriXdEmN8w/JnuFB6P6hei2eUi2dcIUSiu50=; b=0XpLGfHGBtdmWvespl7650Vwl2Tp4RDxoNDhdnXJqm5PPGYRFuJaUS76Lrjuy61+IULo+W GbxjMbaceXu+OMN696XEgyIVApyrbpO11L83u8xR95n7hdDfEV1XC75K8acsu7wxVpvpAj 9dnTQdr7mIaRFdZvyp8aCECD4zae4n8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of lujialin4@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=lujialin4@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661476721; a=rsa-sha256; cv=none; b=KkO6OdcVN3DnwCdAMb+DtJOZVf3ZPeZ5jmj5XPthgn6mQp7MtAHq8sU6B3mOrD6gKu/qkq roJptkv8UFwUqXZOW9LPmPUzm4WQwue6MSF87x8Vk8DKc9NNm1h81SGIbiXUmPA9f50ACh 19bvHc4Zpvi7dGCgDXujoDyUGUb/DPU= Authentication-Results: imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of lujialin4@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=lujialin4@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspam-User: X-Stat-Signature: 6gcb77gy5bwhcbuzqeyqt8cckasd17e9 X-Rspamd-Queue-Id: 6766240018 X-Rspamd-Server: rspam03 X-HE-Tag: 1661476721-628227 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: From: Xiu Jianfeng This patch introduces cgroup.top interface for each cgroup. When accessed by userspace, cgroup.top is able to: 1. Sort cgroups by their usage on various resources(e.g. memory, cpu, etc.) 2. Display resource usage status on all child cgroups. Signed-off-by: Xiu Jianfeng Co-developed-by: Lu Jialin Signed-off-by: Lu Jialin --- include/linux/cgroup-defs.h | 1 + kernel/cgroup/cgroup.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index 4bcf56b3491c..ba11e09f8f03 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -651,6 +651,7 @@ struct cgroup_subsys { int (*css_extra_stat_show)(struct seq_file *seq, struct cgroup_subsys_state *css); + void (*css_top)(struct cgroup_subsys_state *css, struct seq_file *seq); int (*can_attach)(struct cgroup_taskset *tset); void (*cancel_attach)(struct cgroup_taskset *tset); void (*attach)(struct cgroup_taskset *tset); diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index ffaccd6373f1..01bd1a734a01 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -3867,6 +3867,22 @@ static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf, return ret ?: nbytes; } +static int cgroup_top_show(struct seq_file *seq, void *v) +{ + struct cgroup *cgrp = seq_css(seq)->cgroup; + struct cgroup_subsys_state *css; + int ssid; + + rcu_read_lock(); + for_each_css(css, ssid, cgrp) { + if (css->ss->css_top) + css->ss->css_top(css, seq); + } + rcu_read_unlock(); + + return 0; +} + static int cgroup_file_open(struct kernfs_open_file *of) { struct cftype *cft = of_cft(of); @@ -5125,6 +5141,10 @@ static struct cftype cgroup_base_files[] = { .release = cgroup_pressure_release, }, #endif /* CONFIG_PSI */ + { + .name = "cgroup.top", + .seq_show = cgroup_top_show, + }, { } /* terminate */ }; 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,