From: Yafang Shao <laoar.shao@gmail.com>
To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com,
andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com,
haoluo@google.com, jolsa@kernel.org, tj@kernel.org,
lizefan.x@bytedance.com, hannes@cmpxchg.org,
yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com
Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org,
Yafang Shao <laoar.shao@gmail.com>
Subject: [RFC PATCH bpf-next 7/8] selftests/bpf: Add a new cgroup helper get_cgroup_hierarchy_id()
Date: Sat, 7 Oct 2023 14:03:03 +0000 [thread overview]
Message-ID: <20231007140304.4390-8-laoar.shao@gmail.com> (raw)
In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com>
A new cgroup helper function, get_cgroup1_hierarchy_id(), has been
introduced to obtain the ID of a cgroup1 hierarchy based on the provided
cgroup name. This cgroup name can be obtained from the /proc/self/cgroup
file.
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
tools/testing/selftests/bpf/cgroup_helpers.c | 49 ++++++++++++++++++++
tools/testing/selftests/bpf/cgroup_helpers.h | 1 +
2 files changed, 50 insertions(+)
diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c
index 7cb2c9597b8f..5cb66fb3d4fe 100644
--- a/tools/testing/selftests/bpf/cgroup_helpers.c
+++ b/tools/testing/selftests/bpf/cgroup_helpers.c
@@ -633,3 +633,52 @@ unsigned long long get_classid_cgroup_id(void)
format_classid_path(cgroup_workdir);
return get_cgroup_id_from_path(cgroup_workdir);
}
+
+/**
+ * get_cgroup1_hierarchy_id - Retrieves the ID of a cgroup1 hierarchy from the cgroup1 name
+ * @cgrp_name: The cgroup1 name, which can be retrieved from /proc/self/cgroup.
+ */
+int get_cgroup1_hierarchy_id(const char *cgrp_name)
+{
+ char *c, *c2, *c3, *c4;
+ bool found = false;
+ char line[1024];
+ FILE *file;
+ int i, id;
+
+ if (!cgrp_name)
+ return -1;
+
+ file = fopen("/proc/self/cgroup", "r");
+ if (!file) {
+ log_err("fopen /proc/self/cgroup");
+ return -1;
+ }
+
+ while (fgets(line, 1024, file)) {
+ i = 0;
+ for (c = strtok_r(line, ":", &c2); c && i < 2; c = strtok_r(NULL, ":", &c2)) {
+ if (i == 0) {
+ id = strtol(c, NULL, 10);
+ } else if (i == 1) {
+ if (!strcmp(c, cgrp_name)) {
+ found = true;
+ break;
+ }
+
+ /* Multiple subsystems may share one single mount point */
+ for (c3 = strtok_r(c, ",", &c4); c3;
+ c3 = strtok_r(NULL, ",", &c4)) {
+ if (!strcmp(c, cgrp_name)) {
+ found = true;
+ break;
+ }
+ }
+ }
+ i++;
+ }
+ if (found)
+ break;
+ }
+ return found ? id : -1;
+}
diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h
index e71da4ef031b..a80c41734a26 100644
--- a/tools/testing/selftests/bpf/cgroup_helpers.h
+++ b/tools/testing/selftests/bpf/cgroup_helpers.h
@@ -20,6 +20,7 @@ int get_root_cgroup(void);
int create_and_get_cgroup(const char *relative_path);
void remove_cgroup(const char *relative_path);
unsigned long long get_cgroup_id(const char *relative_path);
+int get_cgroup1_hierarchy_id(const char *cgrp_name);
int join_cgroup(const char *relative_path);
int join_root_cgroup(void);
--
2.30.1 (Apple Git-130)
next prev parent reply other threads:[~2023-10-07 14:03 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-07 14:02 [RFC PATCH bpf-next 0/8] bpf, cgroup: Add BPF support for cgroup1 hierarchy Yafang Shao
2023-10-07 14:02 ` [RFC PATCH bpf-next 1/8] cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root() Yafang Shao
2023-10-07 15:50 ` Tejun Heo
2023-10-08 2:32 ` Yafang Shao
2023-10-09 14:45 ` Michal Koutný
2023-10-10 3:58 ` Yafang Shao
2023-10-10 8:29 ` Michal Koutný
2023-10-10 11:58 ` Yafang Shao
2023-10-07 14:02 ` [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy Yafang Shao
2023-10-07 15:55 ` Tejun Heo
2023-10-08 2:36 ` Yafang Shao
2023-10-09 11:32 ` Michal Koutný
2023-10-09 13:10 ` Yafang Shao
2023-10-09 14:48 ` Michal Koutný
2023-10-10 3:59 ` Yafang Shao
2023-10-07 14:02 ` [RFC PATCH bpf-next 3/8] bpf: Add kfuncs " Yafang Shao
2023-10-07 15:57 ` Tejun Heo
2023-10-08 2:37 ` Yafang Shao
2023-10-07 14:03 ` [RFC PATCH bpf-next 4/8] selftests/bpf: Fix issues in setup_classid_environment() Yafang Shao
2023-10-07 14:03 ` [RFC PATCH bpf-next 5/8] selftests/bpf: Add parallel support for classid Yafang Shao
2023-10-07 14:03 ` [RFC PATCH bpf-next 6/8] selftests/bpf: Add a new cgroup helper get_classid_cgroup_id() Yafang Shao
2023-10-07 14:03 ` Yafang Shao [this message]
2023-10-07 14:03 ` [RFC PATCH bpf-next 8/8] selftests/bpf: Add selftests for cgroup1 hierarchy Yafang Shao
2023-10-09 11:46 ` [RFC PATCH bpf-next 0/8] bpf, cgroup: Add BPF support " Michal Koutný
2023-10-09 13:11 ` Yafang Shao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231007140304.4390-8-laoar.shao@gmail.com \
--to=laoar.shao@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=hannes@cmpxchg.org \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=lizefan.x@bytedance.com \
--cc=martin.lau@linux.dev \
--cc=mkoutny@suse.com \
--cc=sdf@google.com \
--cc=sinquersw@gmail.com \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=yonghong.song@linux.dev \
--cc=yosryahmed@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).