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

tools api fs: Cache cgroupfs mount point

Currently it parses the /proc file everytime it opens a file in the
cgroupfs. Save the last result to avoid it (assuming it won't be
changed between the accesses).

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/20201216090556.813996-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
48859e52 6fd99b7f

+19
+19
tools/lib/api/fs/cgroup.c
··· 8 8 #include <string.h> 9 9 #include "fs.h" 10 10 11 + struct cgroupfs_cache_entry { 12 + char subsys[32]; 13 + char mountpoint[PATH_MAX]; 14 + }; 15 + 16 + /* just cache last used one */ 17 + static struct cgroupfs_cache_entry cached; 18 + 11 19 int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys) 12 20 { 13 21 FILE *fp; ··· 23 15 size_t len = 0; 24 16 char *p, *path; 25 17 char mountpoint[PATH_MAX]; 18 + 19 + if (!strcmp(cached.subsys, subsys)) { 20 + if (strlen(cached.mountpoint) < maxlen) { 21 + strcpy(buf, cached.mountpoint); 22 + return 0; 23 + } 24 + return -1; 25 + } 26 26 27 27 fp = fopen("/proc/mounts", "r"); 28 28 if (!fp) ··· 90 74 } 91 75 free(line); 92 76 fclose(fp); 77 + 78 + strncpy(cached.subsys, subsys, sizeof(cached.subsys) - 1); 79 + strcpy(cached.mountpoint, mountpoint); 93 80 94 81 if (mountpoint[0] && strlen(mountpoint) < maxlen) { 95 82 strcpy(buf, mountpoint);