Skip to contentSkip to conversation

tools/lib/fs: Prefer cgroup v1 path

PATCH In review Namhyung Kim 3 participants 13 messages Dec 16, 2020 – Feb 19, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 tools/lib/fs: Prefer cgroup v1 path

The cgroupfs_find_mountpoint() looks up the /proc/mounts file to find a directory for the given cgroup subsystem. It keeps both cgroup v1 and v2 path since there's a possibility of the mixed hierarchly.

But we can simply use v1 path if it's found as it will override the v2 hierarchy.

---
 tools/lib/api/fs/cgroup.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)
tools/lib/api/fs/cgroup.c +15 -19
index 889a6eb4aaca..813236d8ca48 100644
@@ -12,7 +12,7 @@ int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 {
 	FILE *fp;
 	char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
-	char path_v1[PATH_MAX + 1], path_v2[PATH_MAX + 2], *path;
+	char path_v2[PATH_MAX + 1];
 	char *token, *saved_ptr = NULL;
 
 	fp = fopen("/proc/mounts", "r");
@@ -22,45 +22,41 @@ int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 	/*
 	 * in order to handle split hierarchy, we need to scan /proc/mounts
 	 * and inspect every cgroupfs mount point to find one that has
-	 * perf_event subsystem
+	 * the given subsystem.  If we found v1, just use it.  If not we can
+	 * use v2 path as a fallback.
 	 */
-	path_v1[0] = '\0';
 	path_v2[0] = '\0';
 
 	while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %"
 				__stringify(PATH_MAX)"s %*d %*d\n",
 				mountpoint, type, tokens) == 3) {
 
-		if (!path_v1[0] && !strcmp(type, "cgroup")) {
+		if (!strcmp(type, "cgroup")) {
 
 			token = strtok_r(tokens, ",", &saved_ptr);
 
 			while (token != NULL) {
 				if (subsys && !strcmp(token, subsys)) {
-					strcpy(path_v1, mountpoint);
-					break;
+					/* found */
+					fclose(fp);
+
+					if (strlen(mountpoint) < maxlen) {
+						strcpy(buf, mountpoint);
+						return 0;
+					}
+					return -1;
 				}
 				token = strtok_r(NULL, ",", &saved_ptr);
 			}
 		}
 
-		if (!path_v2[0] && !strcmp(type, "cgroup2"))
+		if (!strcmp(type, "cgroup2"))
 			strcpy(path_v2, mountpoint);
-
-		if (path_v1[0] && path_v2[0])
-			break;
 	}
 	fclose(fp);
 
-	if (path_v1[0])
-		path = path_v1;
-	else if (path_v2[0])
-		path = path_v2;
-	else
-		return -1;
-
-	if (strlen(path) < maxlen) {
-		strcpy(buf, path);
+	if (path_v2[0] && strlen(path_v2) < maxlen) {
+		strcpy(buf, path_v2);
 		return 0;
 	}
 	return -1;
Signature · 1 line
2.29.2.684.gfbc64c5ab5-goog
#1 lore raw

PATCH 2/3 tools/lib/fs: Diet cgroupfs_find_mountpoint()

Reduce the number of buffers and hopefully make it more efficient. :)

---
 tools/lib/api/fs/cgroup.c | 70 +++++++++++++++++++++++++--------------
 1 file changed, 45 insertions(+), 25 deletions(-)
tools/lib/api/fs/cgroup.c +45 -25
index 813236d8ca48..262a4229e293 100644
@@ -11,9 +11,10 @@
 int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 {
 	FILE *fp;
-	char mountpoint[PATH_MAX + 1], tokens[PATH_MAX + 1], type[PATH_MAX + 1];
-	char path_v2[PATH_MAX + 1];
-	char *token, *saved_ptr = NULL;
+	char *line = NULL;
+	size_t len = 0;
+	char *p, *path;
+	char mountpoint[PATH_MAX];
 
 	fp = fopen("/proc/mounts", "r");
 	if (!fp)
@@ -25,38 +26,57 @@ int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 	 * the given subsystem.  If we found v1, just use it.  If not we can
 	 * use v2 path as a fallback.
 	 */
-	path_v2[0] = '\0';
+	mountpoint[0] = '\0';
 
-	while (fscanf(fp, "%*s %"__stringify(PATH_MAX)"s %"__stringify(PATH_MAX)"s %"
-				__stringify(PATH_MAX)"s %*d %*d\n",
-				mountpoint, type, tokens) == 3) {
+	/*
+	 * The /proc/mounts has the follow format:
+	 *
+	 *   <devname> <mount point> <fs type> <options> ...
+	 *
+	 */
+	while (getline(&line, &len, fp) != -1) {
+		/* skip devname */
+		p = strchr(line, ' ');
+		if (p == NULL)
+			continue;
 
-		if (!strcmp(type, "cgroup")) {
+		/* save the mount point */
+		path = ++p;
+		p = strchr(p, ' ');
+		if (p == NULL)
+			continue;
 
-			token = strtok_r(tokens, ",", &saved_ptr);
+		*p++ = '\0';
 
-			while (token != NULL) {
-				if (subsys && !strcmp(token, subsys)) {
-					/* found */
-					fclose(fp);
+		/* check filesystem type */
+		if (strncmp(p, "cgroup", 6))
+			continue;
 
-					if (strlen(mountpoint) < maxlen) {
-						strcpy(buf, mountpoint);
-						return 0;
-					}
-					return -1;
-				}
-				token = strtok_r(NULL, ",", &saved_ptr);
-			}
+		if (p[6] == '2') {
+			/* save cgroup v2 path */
+			strcpy(mountpoint, path);
+			continue;
 		}
 
-		if (!strcmp(type, "cgroup2"))
-			strcpy(path_v2, mountpoint);
+		/* now we have cgroup v1, check the options for subsystem */
+		p += 7;
+
+		p = strstr(p, subsys);
+		if (p == NULL)
+			continue;
+
+		/* sanity check: it should be separated by a space or a comma */
+		if (!strchr(" ,", p[-1]) || !strchr(" ,", p[strlen(subsys)]))
+			continue;
+
+		strcpy(mountpoint, path);
+		break;
 	}
+	free(line);
 	fclose(fp);
 
-	if (path_v2[0] && strlen(path_v2) < maxlen) {
-		strcpy(buf, path_v2);
+	if (mountpoint[0] && strlen(mountpoint) < maxlen) {
+		strcpy(buf, mountpoint);
 		return 0;
 	}
 	return -1;
Signature · 1 line
2.29.2.684.gfbc64c5ab5-goog
#2 lore raw

Re: [PATCH 2/3] tools/lib/fs: Diet cgroupfs_find_mountpoint()

On Wed, Dec 16, 2020 at 06:05:55PM +0900, Namhyung Kim wrote:

SNIP

Quoted 20 lines
tools/lib/api/fs/cgroup.c +6 -12
+		*p++ = '\0';
 
-			while (token != NULL) {
-				if (subsys && !strcmp(token, subsys)) {
-					/* found */
-					fclose(fp);
+		/* check filesystem type */
+		if (strncmp(p, "cgroup", 6))
+			continue;
 
-					if (strlen(mountpoint) < maxlen) {
-						strcpy(buf, mountpoint);
-						return 0;
-					}
-					return -1;
-				}
-				token = strtok_r(NULL, ",", &saved_ptr);
-			}
+		if (p[6] == '2') {
+			/* save cgroup v2 path */

tools/lib/api/fs/cgroup.c int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys) · line 64

Quoted 10 lines
tools/lib/api/fs/cgroup.c +6 -2
+			strcpy(mountpoint, path);
+			continue;
 		}
 
-		if (!strcmp(type, "cgroup2"))
-			strcpy(path_v2, mountpoint);
+		/* now we have cgroup v1, check the options for subsystem */
+		p += 7;
+
+		p = strstr(p, subsys);

not sure this is a real problem, but this would mixe up for cpu/cpuacct/cpuset no? we are using the function for perf_event subsys only, but it's globaly availble

jirka

Quoted 23 lines
tools/lib/api/fs/cgroup.c +12 -2
+		if (p == NULL)
+			continue;
+
+		/* sanity check: it should be separated by a space or a comma */
+		if (!strchr(" ,", p[-1]) || !strchr(" ,", p[strlen(subsys)]))
+			continue;
+
+		strcpy(mountpoint, path);
+		break;
 	}
+	free(line);
 	fclose(fp);
 
-	if (path_v2[0] && strlen(path_v2) < maxlen) {
-		strcpy(buf, path_v2);
+	if (mountpoint[0] && strlen(mountpoint) < maxlen) {
+		strcpy(buf, mountpoint);
 		return 0;
 	}
 	return -1;
Signature · 1 line
2.29.2.684.gfbc64c5ab5-goog
#3 lore raw

Re: [PATCH 2/3] tools/lib/fs: Diet cgroupfs_find_mountpoint()

Hi Jiri,

Quoted 36 lines · Jiri Olsa

On Wed, Dec 16, 2020 at 06:05:55PM +0900, Namhyung Kim wrote:

SNIP

Quoted 20 lines
tools/lib/api/fs/cgroup.c +6 -12
+             *p++ = '\0';
 
-                     while (token != NULL) {
-                             if (subsys && !strcmp(token, subsys)) {
-                                     /* found */
-                                     fclose(fp);
+             /* check filesystem type */
+             if (strncmp(p, "cgroup", 6))
+                     continue;
 
-                                     if (strlen(mountpoint) < maxlen) {
-                                             strcpy(buf, mountpoint);
-                                             return 0;
-                                     }
-                                     return -1;
-                             }
-                             token = strtok_r(NULL, ",", &saved_ptr);
-                     }
+             if (p[6] == '2') {
+                     /* save cgroup v2 path */

tools/lib/api/fs/cgroup.c int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys) · line 64

Quoted 10 lines
tools/lib/api/fs/cgroup.c +6 -2
+                     strcpy(mountpoint, path);
+                     continue;
              }
 
-             if (!strcmp(type, "cgroup2"))
-                     strcpy(path_v2, mountpoint);
+             /* now we have cgroup v1, check the options for subsystem */
+             p += 7;
+
+             p = strstr(p, subsys);
Quoted 3 lines

not sure this is a real problem, but this would mixe up for cpu/cpuacct/cpuset no? we are using the function for perf_event subsys only, but it's globaly availble

Yeah, that's why I added the sanity check below. :)

Quoted 7 lines

tools/lib/api/fs/cgroup.c int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys) · line 70

Quoted 7 lines
tools/lib/api/fs/cgroup.c +6 -0
+             if (p == NULL)
+                     continue;
+
+             /* sanity check: it should be separated by a space or a comma */
+             if (!strchr(" ,", p[-1]) || !strchr(" ,", p[strlen(subsys)]))
+                     continue;

Here.

Thanks,
Namhyung

Quoted 18 lines
Quoted 17 lines
tools/lib/api/fs/cgroup.c +6 -3
+
+             strcpy(mountpoint, path);
+             break;
      }
+     free(line);
      fclose(fp);
 
-     if (path_v2[0] && strlen(path_v2) < maxlen) {
-             strcpy(buf, path_v2);
+     if (mountpoint[0] && strlen(mountpoint) < maxlen) {
+             strcpy(buf, mountpoint);
              return 0;
      }
      return -1;
--

2.29.2.684.gfbc64c5ab5-goog

#4 lore raw

Re: [PATCH 2/3] tools/lib/fs: Diet cgroupfs_find_mountpoint()

Quoted 46 lines · Namhyung Kim

Hi Jiri,

Quoted 36 lines · Jiri Olsa

On Wed, Dec 16, 2020 at 06:05:55PM +0900, Namhyung Kim wrote:

SNIP

Quoted 20 lines
tools/lib/api/fs/cgroup.c +6 -12
+             *p++ = '\0';
 
-                     while (token != NULL) {
-                             if (subsys && !strcmp(token, subsys)) {
-                                     /* found */
-                                     fclose(fp);
+             /* check filesystem type */
+             if (strncmp(p, "cgroup", 6))
+                     continue;
 
-                                     if (strlen(mountpoint) < maxlen) {
-                                             strcpy(buf, mountpoint);
-                                             return 0;
-                                     }
-                                     return -1;
-                             }
-                             token = strtok_r(NULL, ",", &saved_ptr);
-                     }
+             if (p[6] == '2') {
+                     /* save cgroup v2 path */

tools/lib/api/fs/cgroup.c int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys) · line 64

Quoted 10 lines
tools/lib/api/fs/cgroup.c +6 -2
+                     strcpy(mountpoint, path);
+                     continue;
              }
 
-             if (!strcmp(type, "cgroup2"))
-                     strcpy(path_v2, mountpoint);
+             /* now we have cgroup v1, check the options for subsystem */
+             p += 7;
+
+             p = strstr(p, subsys);
Quoted 3 lines

not sure this is a real problem, but this would mixe up for cpu/cpuacct/cpuset no? we are using the function for perf_event subsys only, but it's globaly availble

Yeah, that's why I added the sanity check below. :)

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

tools/lib/api/fs/cgroup.c int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys) · line 70

Quoted 6 lines
tools/lib/api/fs/cgroup.c +6 -0
+             if (p == NULL)
+                     continue;
+
+             /* sanity check: it should be separated by a space or a comma */
+             if (!strchr(" ,", p[-1]) || !strchr(" ,", p[strlen(subsys)]))
+                     continue;

Here.

aah right, sry ;-) looks ok

jirka

#5 lore raw

PATCH 3/3 tools/lib/fs: Cache cgroupfs mount point

In reply to Namhyung Kim

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).

---
 tools/lib/api/fs/cgroup.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tools/lib/api/fs/cgroup.c +19 -0
index 262a4229e293..1573dae4259d 100644
@@ -8,6 +8,14 @@
 #include <string.h>
 #include "fs.h"
 
+struct cgroupfs_cache_entry {
+	char	subsys[32];
+	char	mountpoint[PATH_MAX];
+};
+
+/* just cache last used one */
+static struct cgroupfs_cache_entry cached;
+
 int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 {
 	FILE *fp;
@@ -16,6 +24,14 @@ int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 	char *p, *path;
 	char mountpoint[PATH_MAX];
 
+	if (!strcmp(cached.subsys, subsys)) {
+		if (strlen(cached.mountpoint) < maxlen) {
+			strcpy(buf, cached.mountpoint);
+			return 0;
+		}
+		return -1;
+	}
+
 	fp = fopen("/proc/mounts", "r");
 	if (!fp)
 		return -1;
@@ -75,6 +91,9 @@ int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 	free(line);
 	fclose(fp);
 
+	strncpy(cached.subsys, subsys, sizeof(cached.subsys) - 1);
+	strcpy(cached.mountpoint, mountpoint);
+
 	if (mountpoint[0] && strlen(mountpoint) < maxlen) {
 		strcpy(buf, mountpoint);
 		return 0;
Signature · 1 line
2.29.2.684.gfbc64c5ab5-goog
#6 lore raw

Re: [PATCH 3/3] tools/lib/fs: Cache cgroupfs mount point

Em Wed, Dec 16, 2020 at 06:05:56PM +0900, Namhyung Kim escreveu:

Quoted 3 lines

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).

Which is the most likely case, but can't we use something like inotify to detect that and bail out or warn the user?

- Arnaldo

Quoted 52 lines
---
 tools/lib/api/fs/cgroup.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tools/lib/api/fs/cgroup.c +19 -0
index 262a4229e293..1573dae4259d 100644
@@ -8,6 +8,14 @@
 #include <string.h>
 #include "fs.h"
 
+struct cgroupfs_cache_entry {
+	char	subsys[32];
+	char	mountpoint[PATH_MAX];
+};
+
+/* just cache last used one */
+static struct cgroupfs_cache_entry cached;
+
 int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 {
 	FILE *fp;
@@ -16,6 +24,14 @@ int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 	char *p, *path;
 	char mountpoint[PATH_MAX];
 
+	if (!strcmp(cached.subsys, subsys)) {
+		if (strlen(cached.mountpoint) < maxlen) {
+			strcpy(buf, cached.mountpoint);
+			return 0;
+		}
+		return -1;
+	}
+
 	fp = fopen("/proc/mounts", "r");
 	if (!fp)
 		return -1;
@@ -75,6 +91,9 @@ int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
 	free(line);
 	fclose(fp);
 
+	strncpy(cached.subsys, subsys, sizeof(cached.subsys) - 1);
+	strcpy(cached.mountpoint, mountpoint);
+
 	if (mountpoint[0] && strlen(mountpoint) < maxlen) {
 		strcpy(buf, mountpoint);
 		return 0;
Signature · 1 line
2.29.2.684.gfbc64c5ab5-goog
Signature · 1 line
- Arnaldo
#7 lore raw

Re: [PATCH 3/3] tools/lib/fs: Cache cgroupfs mount point

Hi Arnaldo,

Quoted 8 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 16, 2020 at 06:05:56PM +0900, Namhyung Kim escreveu:

Quoted 3 lines

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).

Which is the most likely case, but can't we use something like inotify to detect that and bail out or warn the user?

Hmm.. looks doable. Will check.

Thanks,
Namhyung

#8 lore raw

Re: [PATCH 3/3] tools/lib/fs: Cache cgroupfs mount point

Quoted 7 lines · Namhyung Kim

Hi Arnaldo,

Quoted 2 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 16, 2020 at 06:05:56PM +0900, Namhyung Kim escreveu:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

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).

Which is the most likely case, but can't we use something like inotify to detect that and bail out or warn the user?

Hmm.. looks doable. Will check.

So I've played with inotify a little bit, and it seems it needs to monitor changes on the file or the directory. I didn't get any notification from the /proc/mounts file even if I did some mount/umount.

Instead, I could get IN_UNMOUNT when the cgroup filesystem was unmounted. But for the monitoring, we need to do one of a) select-like syscall to wait for the events, b) signal-driven IO notification or c) read the inotify file with non-block mode everytime.

In a library code, I don't think we can do a) or b) since it can affect user program behaviors. Then we should go with c) but I think it's opposite to the purpose of this patch. :)

As you said, I think mostly we don't care as the accesses will happen in a short period of time. But if you really care, maybe for the upcoming perf daemon changes, I think we can add an API to invalidate the cache or internal time-based invalidation logic (like remove it after 10 sec.).

Thoughts?

Thanks,
Namhyung

#9 lore raw

Re: [PATCH 3/3] tools/lib/fs: Cache cgroupfs mount point

Hi Arnaldo,

Can you share your thoughts on this?

Quoted 40 lines · Namhyung Kim
Quoted 7 lines · Namhyung Kim

Hi Arnaldo,

Quoted 2 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 16, 2020 at 06:05:56PM +0900, Namhyung Kim escreveu:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

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).

Which is the most likely case, but can't we use something like inotify to detect that and bail out or warn the user?

Hmm.. looks doable. Will check.

So I've played with inotify a little bit, and it seems it needs to monitor changes on the file or the directory. I didn't get any notification from the /proc/mounts file even if I did some mount/umount.

Instead, I could get IN_UNMOUNT when the cgroup filesystem was unmounted. But for the monitoring, we need to do one of a) select-like syscall to wait for the events, b) signal-driven IO notification or c) read the inotify file with non-block mode everytime.

In a library code, I don't think we can do a) or b) since it can affect user program behaviors. Then we should go with c) but I think it's opposite to the purpose of this patch. :)

As you said, I think mostly we don't care as the accesses will happen in a short period of time. But if you really care, maybe for the upcoming perf daemon changes, I think we can add an API to invalidate the cache or internal time-based invalidation logic (like remove it after 10 sec.).

Thoughts?

Thanks,
Namhyung

#10 lore raw

Re: [PATCH 3/3] tools/lib/fs: Cache cgroupfs mount point

In reply to Namhyung Kim

Em Fri, Jan 08, 2021 at 02:51:44PM +0900, Namhyung Kim escreveu:

Quoted 26 lines · Namhyung Kim
Quoted 7 lines

Hi Arnaldo,

Quoted 2 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 16, 2020 at 06:05:56PM +0900, Namhyung Kim escreveu:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

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).

Which is the most likely case, but can't we use something like inotify to detect that and bail out or warn the user?

Hmm.. looks doable. Will check.

So I've played with inotify a little bit, and it seems it needs to monitor changes on the file or the directory. I didn't get any notification from the /proc/mounts file even if I did some mount/umount.

Instead, I could get IN_UNMOUNT when the cgroup filesystem was unmounted. But for the monitoring, we need to do one of a) select-like syscall to wait for the events, b) signal-driven IO notification or c) read the inotify file with non-block mode everytime.

Quoted 8 lines

In a library code, I don't think we can do a) or b) since it can affect user program behaviors. Then we should go with c) but I think it's opposite to the purpose of this patch. :)

As you said, I think mostly we don't care as the accesses will happen in a short period of time. But if you really care, maybe for the upcoming perf daemon changes, I think we can add an API to invalidate the cache or internal time-based invalidation logic (like remove it after 10 sec.).

Ok, we can have something in 'perf daemon' to periodically invalidate this, maybe do a poor man inotify and when asking for the cgroup mountpoint, check some characteristic of that file that changes when it is modified, or plain use a timestamp and have some threshold.

- Arnaldo

Quoted 4 lines

Thoughts?

Thanks,
Namhyung

Signature · 1 line
- Arnaldo
#11 lore raw

Re: [PATCH 3/3] tools/lib/fs: Cache cgroupfs mount point

Hi Arnaldo,

Quoted 37 lines · Arnaldo Carvalho de Melo

Em Fri, Jan 08, 2021 at 02:51:44PM +0900, Namhyung Kim escreveu:

Quoted 26 lines · Namhyung Kim
Quoted 7 lines

Hi Arnaldo,

Quoted 2 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 16, 2020 at 06:05:56PM +0900, Namhyung Kim escreveu:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

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).

Which is the most likely case, but can't we use something like inotify to detect that and bail out or warn the user?

Hmm.. looks doable. Will check.

So I've played with inotify a little bit, and it seems it needs to monitor changes on the file or the directory. I didn't get any notification from the /proc/mounts file even if I did some mount/umount.

Instead, I could get IN_UNMOUNT when the cgroup filesystem was unmounted. But for the monitoring, we need to do one of a) select-like syscall to wait for the events, b) signal-driven IO notification or c) read the inotify file with non-block mode everytime.

Quoted 8 lines

In a library code, I don't think we can do a) or b) since it can affect user program behaviors. Then we should go with c) but I think it's opposite to the purpose of this patch. :)

As you said, I think mostly we don't care as the accesses will happen in a short period of time. But if you really care, maybe for the upcoming perf daemon changes, I think we can add an API to invalidate the cache or internal time-based invalidation logic (like remove it after 10 sec.).

Quoted 4 lines

Ok, we can have something in 'perf daemon' to periodically invalidate this, maybe do a poor man inotify and when asking for the cgroup mountpoint, check some characteristic of that file that changes when it is modified, or plain use a timestamp and have some threshold.

I thought about this again.

We don't directly access the cgroups in the perf daemon. It just creates new record processes so they'll see a new mountpoint whenever they started since this cache is shared within the process only.

That means we don't need to care about the invalidate in the daemon but each perf record and perf stat should do it when they are required to do the work repeatedly.

But looking at the code, the cgroup is set during event parsing (-G option) or early in the command (--for-each-cgroup option). So cgroup info would not be changed even if the command runs repeatedly.

So I think you can take the patch as is.

Thanks,
Namhyung

#12 lore raw

Re: [PATCH 3/3] tools/lib/fs: Cache cgroupfs mount point

Em Fri, Feb 19, 2021 at 07:05:59PM +0900, Namhyung Kim escreveu:

Quoted 24 lines · Arnaldo Carvalho de Melo
Quoted 8 lines

Em Fri, Jan 08, 2021 at 02:51:44PM +0900, Namhyung Kim escreveu:

Quoted 6 lines · Namhyung Kim

As you said, I think mostly we don't care as the accesses will happen in a short period of time. But if you really care, maybe for the upcoming perf daemon changes, I think we can add an API to invalidate the cache or internal time-based invalidation logic (like remove it after 10 sec.).

Quoted 4 lines

Ok, we can have something in 'perf daemon' to periodically invalidate this, maybe do a poor man inotify and when asking for the cgroup mountpoint, check some characteristic of that file that changes when it is modified, or plain use a timestamp and have some threshold.

I thought about this again.

We don't directly access the cgroups in the perf daemon. It just creates new record processes so they'll see a new mountpoint whenever they started since this cache is shared within the process only.

That means we don't need to care about the invalidate in the daemon but each perf record and perf stat should do it when they are required to do the work repeatedly.

Quoted 5 lines

But looking at the code, the cgroup is set during event parsing (-G option) or early in the command (--for-each-cgroup option). So cgroup info would not be changed even if the command runs repeatedly.

So I think you can take the patch as is.

Its in perf/core branch on its way to Linus soon :-)

Thanks for checking it.

- Arnaldo