Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tools lib api fs: Move cgroupsfs_find_mountpoint()

Move it from tools/perf/util/cgroup.c as it can be used by other places.
Note that cgroup filesystem is different from others since it's usually
mounted separately (in v1) for each subsystem.

I just copied the code with a little modification to pass a name of
subsystem.

Suggested-by: Jiri Olsa <jolsa@redhat.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lore.kernel.org/lkml/20200127100031.1368732-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
7982a898 d46eec8e

+72 -61
+1
tools/lib/api/fs/Build
··· 1 1 libapi-y += fs.o 2 2 libapi-y += tracing_path.o 3 + libapi-y += cgroup.o
+67
tools/lib/api/fs/cgroup.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <linux/stringify.h> 3 + #include <sys/types.h> 4 + #include <sys/stat.h> 5 + #include <fcntl.h> 6 + #include <stdio.h> 7 + #include <stdlib.h> 8 + #include <string.h> 9 + #include "fs.h" 10 + 11 + int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys) 12 + { 13 + FILE *fp; 14 + char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1]; 15 + char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path; 16 + char *token, *saved_ptr = NULL; 17 + 18 + fp = fopen("/proc/mounts", "r"); 19 + if (!fp) 20 + return -1; 21 + 22 + /* 23 + * in order to handle split hierarchy, we need to scan /proc/mounts 24 + * and inspect every cgroupfs mount point to find one that has 25 + * perf_event subsystem 26 + */ 27 + path_v1[0] = '\0'; 28 + path_v2[0] = '\0'; 29 + 30 + while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %" 31 + __stringify(PATH_MAX)"s %*d %*d\n", 32 + mountpoint, type, tokens) == 3) { 33 + 34 + if (!path_v1[0] && !strcmp(type, "cgroup")) { 35 + 36 + token = strtok_r(tokens, ",", &saved_ptr); 37 + 38 + while (token != NULL) { 39 + if (subsys && !strcmp(token, subsys)) { 40 + strcpy(path_v1, mountpoint); 41 + break; 42 + } 43 + token = strtok_r(NULL, ",", &saved_ptr); 44 + } 45 + } 46 + 47 + if (!path_v2[0] && !strcmp(type, "cgroup2")) 48 + strcpy(path_v2, mountpoint); 49 + 50 + if (path_v1[0] && path_v2[0]) 51 + break; 52 + } 53 + fclose(fp); 54 + 55 + if (path_v1[0]) 56 + path = path_v1; 57 + else if (path_v2[0]) 58 + path = path_v2; 59 + else 60 + return -1; 61 + 62 + if (strlen(path) < maxlen) { 63 + strcpy(buf, path); 64 + return 0; 65 + } 66 + return -1; 67 + }
+2
tools/lib/api/fs/fs.h
··· 28 28 #undef FS 29 29 30 30 31 + int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys); 32 + 31 33 int filename__read_int(const char *filename, int *value); 32 34 int filename__read_ull(const char *filename, unsigned long long *value); 33 35 int filename__read_xll(const char *filename, unsigned long long *value);
+2 -61
tools/perf/util/cgroup.c
··· 3 3 #include "evsel.h" 4 4 #include "cgroup.h" 5 5 #include "evlist.h" 6 - #include <linux/stringify.h> 7 6 #include <linux/zalloc.h> 8 7 #include <sys/types.h> 9 8 #include <sys/stat.h> 10 9 #include <fcntl.h> 11 10 #include <stdlib.h> 12 11 #include <string.h> 12 + #include <api/fs/fs.h> 13 13 14 14 int nr_cgroups; 15 - 16 - static int 17 - cgroupfs_find_mountpoint(char *buf, size_t maxlen) 18 - { 19 - FILE *fp; 20 - char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1]; 21 - char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path; 22 - char *token, *saved_ptr = NULL; 23 - 24 - fp = fopen("/proc/mounts", "r"); 25 - if (!fp) 26 - return -1; 27 - 28 - /* 29 - * in order to handle split hierarchy, we need to scan /proc/mounts 30 - * and inspect every cgroupfs mount point to find one that has 31 - * perf_event subsystem 32 - */ 33 - path_v1[0] = '\0'; 34 - path_v2[0] = '\0'; 35 - 36 - while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %" 37 - __stringify(PATH_MAX)"s %*d %*d\n", 38 - mountpoint, type, tokens) == 3) { 39 - 40 - if (!path_v1[0] && !strcmp(type, "cgroup")) { 41 - 42 - token = strtok_r(tokens, ",", &saved_ptr); 43 - 44 - while (token != NULL) { 45 - if (!strcmp(token, "perf_event")) { 46 - strcpy(path_v1, mountpoint); 47 - break; 48 - } 49 - token = strtok_r(NULL, ",", &saved_ptr); 50 - } 51 - } 52 - 53 - if (!path_v2[0] && !strcmp(type, "cgroup2")) 54 - strcpy(path_v2, mountpoint); 55 - 56 - if (path_v1[0] && path_v2[0]) 57 - break; 58 - } 59 - fclose(fp); 60 - 61 - if (path_v1[0]) 62 - path = path_v1; 63 - else if (path_v2[0]) 64 - path = path_v2; 65 - else 66 - return -1; 67 - 68 - if (strlen(path) < maxlen) { 69 - strcpy(buf, path); 70 - return 0; 71 - } 72 - return -1; 73 - } 74 15 75 16 static int open_cgroup(const char *name) 76 17 { ··· 20 79 int fd; 21 80 22 81 23 - if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1)) 82 + if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event")) 24 83 return -1; 25 84 26 85 scnprintf(path, PATH_MAX, "%s/%s", mnt, name);