From 5968d371e66eba1f4aa170ca25a7eb7a7afd4f8f Mon Sep 17 00:00:00 2001 From: Namhyung Kim Date: Fri, 25 Jun 2021 00:18:24 -0700 Subject: [PATCH] perf tools: Add cgroup_is_v2() helper The cgroup_is_v2() is to check if the given subsystem is mounted on cgroup v2 or not. It'll be used by BPF cgroup code later. Signed-off-by: Namhyung Kim Acked-by: Ian Rogers Cc: Andi Kleen Cc: Jiri Olsa Cc: Peter Zijlstra Cc: Song Liu Cc: Stephane Eranian Link: http://lore.kernel.org/lkml/20210625071826.608504-3-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/cgroup.c | 19 +++++++++++++++++++ tools/perf/util/cgroup.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/tools/perf/util/cgroup.c b/tools/perf/util/cgroup.c index ef18c988c6818..e819a4f30fc2f 100644 --- a/tools/perf/util/cgroup.c +++ b/tools/perf/util/cgroup.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -70,6 +71,24 @@ int read_cgroup_id(struct cgroup *cgrp) } #endif /* HAVE_FILE_HANDLE */ +#ifndef CGROUP2_SUPER_MAGIC +#define CGROUP2_SUPER_MAGIC 0x63677270 +#endif + +int cgroup_is_v2(const char *subsys) +{ + char mnt[PATH_MAX + 1]; + struct statfs stbuf; + + if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, subsys)) + return -1; + + if (statfs(mnt, &stbuf) < 0) + return -1; + + return (stbuf.f_type == CGROUP2_SUPER_MAGIC); +} + static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str) { struct evsel *counter; diff --git a/tools/perf/util/cgroup.h b/tools/perf/util/cgroup.h index f3c9787469999..de5b272560abc 100644 --- a/tools/perf/util/cgroup.h +++ b/tools/perf/util/cgroup.h @@ -48,4 +48,6 @@ static inline int read_cgroup_id(struct cgroup *cgrp __maybe_unused) } #endif /* HAVE_FILE_HANDLE */ +int cgroup_is_v2(const char *subsys); + #endif /* __CGROUP_H__ */ -- 2.39.5