Skip to contentSkip to conversation

perf_events: add support for per-cpu per-cgroup monitoring (v4)

RFC PATCH In review Stephane Eranian 3 participants 9 messages Oct 6, 2010 – Oct 20, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 perf_events: add support for per-cpu per-cgroup monitoring (v4)

This kernel patch adds the ability to filter monitoring based on container groups (cgroups). This is for use in per-cpu mode only.

The cgroup to monitor is passed as a file descriptor in the pid argument to the syscall. The file descriptor must be opened to the cgroup name in the cgroup filesystem. For instance, if the cgroup name is foo and cgroupfs is mounted in /cgroup, then the file descriptor is opened to /cgroup/foo. Cgroup mode is activated by passing PERF_FLAG_PID_CGROUP into the flags argument to the syscall.

---

include/linux/cgroup.h +2 -0
index 709dfb9..67cf276 100644
@@ -623,6 +623,8 @@ bool css_is_ancestor(struct cgroup_subsys_state *cg,
 unsigned short css_id(struct cgroup_subsys_state *css);
 unsigned short css_depth(struct cgroup_subsys_state *css);
 
+struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
+
 #else /* !CONFIG_CGROUPS */
 
 static inline int cgroup_init_early(void) { return 0; }
include/linux/cgroup_subsys.h +4 -0
index ccefff0..93f86b7 100644
@@ -65,4 +65,8 @@ SUBSYS(net_cls)
 SUBSYS(blkio)
 #endif
 
+#ifdef CONFIG_PERF_EVENTS
+SUBSYS(perf)
+#endif
+
 /* */
include/linux/perf_event.h +20 -0
index 61b1e2d..ad79f0a 100644
@@ -454,6 +454,7 @@ enum perf_callchain_context {
 
 #define PERF_FLAG_FD_NO_GROUP	(1U << 0)
 #define PERF_FLAG_FD_OUTPUT	(1U << 1)
+#define PERF_FLAG_PID_CGROUP	(1U << 2) /* pid=cgroup id, per-cpu mode */
 
 #ifdef __KERNEL__
 /*
@@ -461,6 +462,7 @@ enum perf_callchain_context {
  */
 
 #ifdef CONFIG_PERF_EVENTS
+# include <linux/cgroup.h>
 # include <asm/perf_event.h>
 # include <asm/local64.h>
 #endif
@@ -698,6 +700,18 @@ struct swevent_hlist {
 #define PERF_ATTACH_CONTEXT	0x01
 #define PERF_ATTACH_GROUP	0x02
 
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+	u64 time;
+	u64 timestamp;
+};
+
+struct perf_cgroup {
+	struct cgroup_subsys_state css;
+	struct perf_cgroup_time *time;
+};
+#endif
+
 /**
  * struct perf_event - performance event kernel representation:
  */
@@ -801,6 +815,10 @@ struct perf_event {
 	struct event_filter		*filter;
 #endif
 
+#ifdef CONFIG_CGROUPS
+	struct perf_cgroup		*css;
+#endif
+
 #endif /* CONFIG_PERF_EVENTS */
 };
 
@@ -854,6 +872,8 @@ struct perf_event_context {
 	u64				generation;
 	int				pin_count;
 	struct rcu_head			rcu_head;
+
+	int				nr_cgroups;
 };
 
 /*
kernel/cgroup.c +22 -0
index 291ba3d..2de239a 100644
@@ -4723,6 +4723,28 @@ css_get_next(struct cgroup_subsys *ss, int id,
 	return ret;
 }
 
+/*
+ * get corresponding css from file open on cgroupfs directory
+ */
+struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
+{
+	struct cgroup *cgrp;
+	struct inode *inode;
+
+	inode = f->f_dentry->d_inode;
+	/* check in cgroup filesystem dir */
+	if (inode->i_op != &cgroup_dir_inode_operations)
+		return ERR_PTR(-EBADF);
+
+	if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
+		return ERR_PTR(-EINVAL);
+
+	/* get cgroup */
+	cgrp = __d_cgrp(f->f_dentry);
+
+	return cgrp->subsys[id];
+}
+
 #ifdef CONFIG_CGROUP_DEBUG
 static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
 						   struct cgroup *cont)
kernel/perf_event.c +473 -66

875 lines not on this page · raw message

#1 lore raw
Quoted 65 lines · Stephane Eranian

This kernel patch adds the ability to filter monitoring based on container groups (cgroups). This is for use in per-cpu mode only.

The cgroup to monitor is passed as a file descriptor in the pid argument to the syscall. The file descriptor must be opened to the cgroup name in the cgroup filesystem. For instance, if the cgroup name is foo and cgroupfs is mounted in /cgroup, then the file descriptor is opened to /cgroup/foo. Cgroup mode is activated by passing PERF_FLAG_PID_CGROUP into the flags argument to the syscall.

---

include/linux/cgroup.h +2 -0
index 709dfb9..67cf276 100644
@@ -623,6 +623,8 @@ bool css_is_ancestor(struct cgroup_subsys_state *cg,
 unsigned short css_id(struct cgroup_subsys_state *css);
 unsigned short css_depth(struct cgroup_subsys_state *css);
 
+struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
+
 #else /* !CONFIG_CGROUPS */
 
 static inline int cgroup_init_early(void) { return 0; }
include/linux/cgroup_subsys.h +4 -0
index ccefff0..93f86b7 100644
@@ -65,4 +65,8 @@ SUBSYS(net_cls)
 SUBSYS(blkio)
 #endif
 
+#ifdef CONFIG_PERF_EVENTS
+SUBSYS(perf)
+#endif
+
 /* */
include/linux/perf_event.h +2 -0
index 61b1e2d..ad79f0a 100644
@@ -454,6 +454,7 @@ enum perf_callchain_context {
 
 #define PERF_FLAG_FD_NO_GROUP	(1U << 0)
 #define PERF_FLAG_FD_OUTPUT	(1U << 1)
+#define PERF_FLAG_PID_CGROUP	(1U << 2) /* pid=cgroup id, per-cpu mode */
 
 #ifdef __KERNEL__
 /*
@@ -461,6 +462,7 @@ enum perf_callchain_context {
  */
 
 #ifdef CONFIG_PERF_EVENTS
+# include <linux/cgroup.h>
 # include <asm/perf_event.h>
 # include <asm/local64.h>
 #endif
@@ -698,6 +700,18 @@ struct swevent_hlist {
 #define PERF_ATTACH_CONTEXT	0x01
 #define PERF_ATTACH_GROUP	0x02
 

include/linux/perf_event.h struct swevent_hlist { · line 712

Quoted 10 lines
include/linux/perf_event.h +10 -0
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+	u64 time;
+	u64 timestamp;
+};
+
+struct perf_cgroup {
+	struct cgroup_subsys_state css;
+	struct perf_cgroup_time *time;
+};

Can we avoid adding this perf cgroup subsystem? It has 2 disavantages:

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.
- If there are several different cgroup mount points, only one can be monitored.

To choose which cgroup hierarchy to monitor, hierarchy id can be passed from userspace, which is the 2nd column below:

$ cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   0       1       1
net_cls 0       1       1
Quoted 1 line
include/linux/perf_event.h +1 -0
+#endif
#2 lore raw
Quoted 66 lines · Li Zefan
Quoted 65 lines · Stephane Eranian

This kernel patch adds the ability to filter monitoring based on container groups (cgroups). This is for use in per-cpu mode only.

The cgroup to monitor is passed as a file descriptor in the pid argument to the syscall. The file descriptor must be opened to the cgroup name in the cgroup filesystem. For instance, if the cgroup name is foo and cgroupfs is mounted in /cgroup, then the file descriptor is opened to /cgroup/foo. Cgroup mode is activated by passing PERF_FLAG_PID_CGROUP into the flags argument to the syscall.

---

include/linux/cgroup.h +2 -0
index 709dfb9..67cf276 100644
@@ -623,6 +623,8 @@ bool css_is_ancestor(struct cgroup_subsys_state *cg,
  unsigned short css_id(struct cgroup_subsys_state *css);
  unsigned short css_depth(struct cgroup_subsys_state *css);
 
+struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
+
  #else /* !CONFIG_CGROUPS */
 
  static inline int cgroup_init_early(void) { return 0; }
include/linux/cgroup_subsys.h +4 -0
index ccefff0..93f86b7 100644
@@ -65,4 +65,8 @@ SUBSYS(net_cls)
  SUBSYS(blkio)
  #endif
 
+#ifdef CONFIG_PERF_EVENTS
+SUBSYS(perf)
+#endif
+
  /* */
include/linux/perf_event.h +2 -0
index 61b1e2d..ad79f0a 100644
@@ -454,6 +454,7 @@ enum perf_callchain_context {
 
  #define PERF_FLAG_FD_NO_GROUP        (1U << 0)
  #define PERF_FLAG_FD_OUTPUT  (1U << 1)
+#define PERF_FLAG_PID_CGROUP (1U << 2) /* pid=cgroup id, per-cpu mode */
 
  #ifdef __KERNEL__
  /*
@@ -461,6 +462,7 @@ enum perf_callchain_context {
   */
 
  #ifdef CONFIG_PERF_EVENTS
+# include <linux/cgroup.h>
  # include <asm/perf_event.h>
  # include <asm/local64.h>
  #endif
@@ -698,6 +700,18 @@ struct swevent_hlist {
  #define PERF_ATTACH_CONTEXT  0x01
  #define PERF_ATTACH_GROUP    0x02
 
Quoted 13 lines

include/linux/perf_event.h struct swevent_hlist { · line 712

Quoted 10 lines
include/linux/perf_event.h +10 -0
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+     u64 time;
+     u64 timestamp;
+};
+
+struct perf_cgroup {
+     struct cgroup_subsys_state css;
+     struct perf_cgroup_time *time;
+};

Can we avoid adding this perf cgroup subsystem? It has 2 disavantages:

Well, I need to maintain some timing information for each cgroup. This has to be stored somewhere.

Quoted 1 line

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.

That's unfortunately true ;-)

Quoted 6 lines

- If there are several different cgroup mount points, only one can be monitored.

To choose which cgroup hierarchy to monitor, hierarchy id can be passed from userspace, which is the 2nd column below:

Ok, I will investigate this. As long as the hierarchy id is unique AND it can be searched, then we can use it. Using /proc is fine with me.

Quoted 7 lines
$ cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   0       1       1
net_cls 0       1       1
Quoted 1 line
include/linux/perf_event.h +1 -0
+#endif
#3 lore raw
Quoted 87 lines · stephane eranian
Quoted 66 lines · Li Zefan
Quoted 65 lines · Stephane Eranian

This kernel patch adds the ability to filter monitoring based on container groups (cgroups). This is for use in per-cpu mode only.

The cgroup to monitor is passed as a file descriptor in the pid argument to the syscall. The file descriptor must be opened to the cgroup name in the cgroup filesystem. For instance, if the cgroup name is foo and cgroupfs is mounted in /cgroup, then the file descriptor is opened to /cgroup/foo. Cgroup mode is activated by passing PERF_FLAG_PID_CGROUP into the flags argument to the syscall.

---

include/linux/cgroup.h +2 -0
index 709dfb9..67cf276 100644
@@ -623,6 +623,8 @@ bool css_is_ancestor(struct cgroup_subsys_state *cg,
  unsigned short css_id(struct cgroup_subsys_state *css);
  unsigned short css_depth(struct cgroup_subsys_state *css);
 
+struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
+
  #else /* !CONFIG_CGROUPS */
 
  static inline int cgroup_init_early(void) { return 0; }
include/linux/cgroup_subsys.h +4 -0
index ccefff0..93f86b7 100644
@@ -65,4 +65,8 @@ SUBSYS(net_cls)
  SUBSYS(blkio)
  #endif
 
+#ifdef CONFIG_PERF_EVENTS
+SUBSYS(perf)
+#endif
+
  /* */
include/linux/perf_event.h +2 -0
index 61b1e2d..ad79f0a 100644
@@ -454,6 +454,7 @@ enum perf_callchain_context {
 
  #define PERF_FLAG_FD_NO_GROUP        (1U << 0)
  #define PERF_FLAG_FD_OUTPUT  (1U << 1)
+#define PERF_FLAG_PID_CGROUP (1U << 2) /* pid=cgroup id, per-cpu mode */
 
  #ifdef __KERNEL__
  /*
@@ -461,6 +462,7 @@ enum perf_callchain_context {
   */
 
  #ifdef CONFIG_PERF_EVENTS
+# include <linux/cgroup.h>
  # include <asm/perf_event.h>
  # include <asm/local64.h>
  #endif
@@ -698,6 +700,18 @@ struct swevent_hlist {
  #define PERF_ATTACH_CONTEXT  0x01
  #define PERF_ATTACH_GROUP    0x02
 
Quoted 13 lines

include/linux/perf_event.h struct swevent_hlist { · line 712

Quoted 10 lines
include/linux/perf_event.h +10 -0
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+     u64 time;
+     u64 timestamp;
+};
+
+struct perf_cgroup {
+     struct cgroup_subsys_state css;
+     struct perf_cgroup_time *time;
+};

Can we avoid adding this perf cgroup subsystem? It has 2 disavantages:

Well, I need to maintain some timing information for each cgroup. This has to be stored somewhere.

Quoted 1 line

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.

That's unfortunately true ;-)

Quoted 14 lines
Quoted 6 lines

- If there are several different cgroup mount points, only one can be monitored.

To choose which cgroup hierarchy to monitor, hierarchy id can be passed from userspace, which is the 2nd column below:

Ok, I will investigate this. As long as the hierarchy id is unique AND it can be searched, then we can use it. Using /proc is fine with me.

Quoted 5 lines
$ cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   0       1       1
net_cls 0       1       1

If I mount all subsystems:
mount -t cgroup none /dev/cgroup
Then, I get:
#subsys_name hierarchy num_cgroups enabled
cpuset 1 1 1
cpu 1 1 1
perf_event 1 1 1

In other words, the hierarchy id is not unique. If the perf_event is not mounted, then hierarchy id = 0.

When I compare with my approach, if perf_event is not mounted, then the file descriptor won't lead to the css, and therefore you will fail and that is fine because it means the perf_event subsystem is not instantiated therefore it cannot be used.

In my patch, there was a missing check for a NULL css. I fixed that now, and it works fine.

As for multiple mount points, it seems like the first mount determines the restrictions for all mounts. In other words, if you mount only cpuset, then no other mount can provide more than cpuset, and vice-versa.

I have tried mounting cgroupfs in multiple places at the same time. Whatever directory I used, I got to the right css.

Am I missing your point here?

#4 lore raw
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

include/linux/perf_event.h struct swevent_hlist { · line 712

Quoted 10 lines
include/linux/perf_event.h +10 -0
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+     u64 time;
+     u64 timestamp;
+};
+
+struct perf_cgroup {
+     struct cgroup_subsys_state css;
+     struct perf_cgroup_time *time;
+};

Can we avoid adding this perf cgroup subsystem? It has 2 disavantages:

Quoted 3 lines
Quoted 3 lines

Well, I need to maintain some timing information for each cgroup. This has to be stored somewhere.

Seems you can simply store it in struct perf_event?

Quoted 26 lines
Quoted 17 lines
Quoted 1 line

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.

That's unfortunately true ;-)

Quoted 6 lines

- If there are several different cgroup mount points, only one can be monitored.

To choose which cgroup hierarchy to monitor, hierarchy id can be passed from userspace, which is the 2nd column below:

Ok, I will investigate this. As long as the hierarchy id is unique AND it can be searched, then we can use it. Using /proc is fine with me.

Quoted 5 lines
$ cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   0       1       1
net_cls 0       1       1

If I mount all subsystems:
mount -t cgroup none /dev/cgroup
Then, I get:
#subsys_name hierarchy num_cgroups enabled
cpuset 1 1 1
cpu 1 1 1
perf_event 1 1 1

Quoted 3 lines

In other words, the hierarchy id is not unique. If the perf_event is not mounted, then hierarchy id = 0.

Yes, it's unique. ;)

You mounted them together, and that's a cgroup hierarchy, so they have the same hierarchy id.

If you mount them seperately:

# mount -t cgroup -o debug xxx /cgroup1
# mount -t cgroup -o net_cls xxx /cgroup2/
# cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   1       1       1
net_cls 2       1       1

They now have different hierarchy id, because they belong to different cgroup hierarchy.

So pid + hierarchy_id locates the cgroup.

Quoted 14 lines

When I compare with my approach, if perf_event is not mounted, then the file descriptor won't lead to the css, and therefore you will fail and that is fine because it means the perf_event subsystem is not instantiated therefore it cannot be used.

In my patch, there was a missing check for a NULL css. I fixed that now, and it works fine.

As for multiple mount points, it seems like the first mount determines the restrictions for all mounts. In other words, if you mount only cpuset, then no other mount can provide more than cpuset, and vice-versa.

Quoted 5 lines

I have tried mounting cgroupfs in multiple places at the same time. Whatever directory I used, I got to the right css.

Am I missing your point here?

I should use the words "cgroup hierarchies" instead of mount points..

#5 lore raw
Quoted 12 lines · Li Zefan
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

include/linux/perf_event.h struct swevent_hlist { · line 712

Quoted 10 lines
include/linux/perf_event.h +10 -0
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+     u64 time;
+     u64 timestamp;
+};
+
+struct perf_cgroup {
+     struct cgroup_subsys_state css;
+     struct perf_cgroup_time *time;
+};

Can we avoid adding this perf cgroup subsystem? It has 2 disavantages:

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Well, I need to maintain some timing information for each cgroup. This has to be stored somewhere.

Seems you can simply store it in struct perf_event?

No, timing has to be shared by events monitoring the same cgroup at the same time.
Works like a timestamp. It needs to be centralized for all events attached to the same
cgroup.

Quoted 35 lines
Quoted 26 lines
Quoted 17 lines
Quoted 1 line

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.

That's unfortunately true ;-)

Quoted 6 lines

- If there are several different cgroup mount points, only one can be monitored.

To choose which cgroup hierarchy to monitor, hierarchy id can be passed from userspace, which is the 2nd column below:

Ok, I will investigate this. As long as the hierarchy id is unique AND it can be searched, then we can use it. Using /proc is fine with me.

Quoted 5 lines
$ cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   0       1       1
net_cls 0       1       1

If I mount all subsystems:
mount -t cgroup none /dev/cgroup
Then, I get:
#subsys_name  hierarchy       num_cgroups     enabled cpuset        1       1       1
cpu           1       1       1
perf_event    1       1       1

Quoted 3 lines

In other words, the hierarchy id is not unique. If the perf_event is not mounted, then hierarchy id = 0.

Yes, it's unique. ;)

You mounted them together, and that's a cgroup hierarchy, so they have the same hierarchy id.

Quoted 9 lines

If you mount them seperately:

# mount -t cgroup -o debug xxx /cgroup1
# mount -t cgroup -o net_cls xxx /cgroup2/
# cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   1       1       1
net_cls 2       1       1

Ok, but if you mount perf_event twice, you get the same hierarchy id for it:

# mount -t cgroup -operf_event none /cgroup
# cat /proc/cgroups
#subsys_name	hierarchy	num_cgroups	enabled
cpuset	0	1	1
cpu	        0	1	1
perf_event	1	1	1
# mount -t cgroup -operf_event none /cgroup2
# cat /proc/cgroups
#subsys_name	hierarchy	num_cgroups	enabled
cpuset	0	1	1
cpu	        0	1	1
perf_event	1	1	1

It does not seem like I can mount the same subsystem twice with difference hierarchies:

# umount /cgroup2
# mount -t cgroup -operf_event,cpuset none /cgroup2
mount: none already mounted or /cgroup2 busy
# mount -t cgroup  none /cgroup2
mount: none already mounted or /cgroup2 busy
Quoted 5 lines

They now have different hierarchy id, because they belong to different cgroup hierarchy.

So pid + hierarchy_id locates the cgroup.

I cannot do task's pid + cgroup hierarchy_id. It's one or the other.

Quoted 8 lines
Quoted 5 lines

I have tried mounting cgroupfs in multiple places at the same time. Whatever directory I used, I got to the right css.

Am I missing your point here?

I should use the words "cgroup hierarchies" instead of mount points..

Can you mount the same subsystem multiple times with DIFFERENT hierarchies?

#6 lore raw

(Sorry for the late reply. I've been keeping busy..)

Quoted 16 lines · Stephane Eranian
Quoted 15 lines · Li Zefan
Quoted 15 lines
Quoted 15 lines
Quoted 12 lines

include/linux/perf_event.h struct swevent_hlist { · line 712

Quoted 10 lines
include/linux/perf_event.h +10 -0
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+     u64 time;
+     u64 timestamp;
+};
+
+struct perf_cgroup {
+     struct cgroup_subsys_state css;
+     struct perf_cgroup_time *time;
+};

Can we avoid adding this perf cgroup subsystem? It has 2 disavantages:

Well, I need to maintain some timing information for each cgroup. This has to be stored somewhere.

Quoted 7 lines
Quoted 2 lines

Seems you can simply store it in struct perf_event?

No, timing has to be shared by events monitoring the same cgroup at the same time.
Works like a timestamp. It needs to be centralized for all events attached to the same cgroup.

I no little about internel perf code, so I don't know if we can store this somewhere in perf. The last resort could be store it in struct cgroup.

Quoted 68 lines
Quoted 33 lines
Quoted 20 lines
Quoted 17 lines
Quoted 1 line

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.

That's unfortunately true ;-)

Quoted 6 lines

- If there are several different cgroup mount points, only one can be monitored.

To choose which cgroup hierarchy to monitor, hierarchy id can be passed from userspace, which is the 2nd column below:

Ok, I will investigate this. As long as the hierarchy id is unique AND it can be searched, then we can use it. Using /proc is fine with me.

Quoted 5 lines
$ cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   0       1       1
net_cls 0       1       1

If I mount all subsystems:
mount -t cgroup none /dev/cgroup
Then, I get:

Quoted 8 lines
#subsys_name  hierarchy       num_cgroups     enabled
cpuset        1       1       1
cpu           1       1       1
perf_event    1       1       1

In other words, the hierarchy id is not unique. If the perf_event is not mounted, then hierarchy id = 0.

Yes, it's unique. ;)

You mounted them together, and that's a cgroup hierarchy, so they have the same hierarchy id.

Quoted 9 lines

If you mount them seperately:

# mount -t cgroup -o debug xxx /cgroup1
# mount -t cgroup -o net_cls xxx /cgroup2/
# cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   1       1       1
net_cls 2       1       1

Ok, but if you mount perf_event twice, you get the same hierarchy id for it:

# mount -t cgroup -operf_event none /cgroup
# cat /proc/cgroups
#subsys_name	hierarchy	num_cgroups	enabled
cpuset	0	1	1
cpu	        0	1	1
perf_event	1	1	1
# mount -t cgroup -operf_event none /cgroup2
# cat /proc/cgroups
#subsys_name	hierarchy	num_cgroups	enabled
cpuset	0	1	1
cpu	        0	1	1
perf_event	1	1	1

It does not seem like I can mount the same subsystem twice with difference hierarchies:

# umount /cgroup2
# mount -t cgroup -operf_event,cpuset none /cgroup2
mount: none already mounted or /cgroup2 busy
# mount -t cgroup  none /cgroup2
mount: none already mounted or /cgroup2 busy
Quoted 9 lines
Quoted 5 lines

They now have different hierarchy id, because they belong to different cgroup hierarchy.

So pid + hierarchy_id locates the cgroup.

I cannot do task's pid + cgroup hierarchy_id. It's one or the other.

I've looked into the patch again, and I see you pass the fd from userspace, so you don't need hierarchy_id.

And to get rid of perf_cgroup subsys, seems you just need to find another place to store the time info, somewhere inside perf code or in struct cgroup.

#7 lore raw
Quoted 104 lines · Li Zefan

(Sorry for the late reply. I've been keeping busy..)

Quoted 16 lines · Stephane Eranian
Quoted 15 lines · Li Zefan
Quoted 15 lines
Quoted 15 lines
Quoted 12 lines

Review

Quoted 10 lines
(quoted diff) +10 -0
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+     u64 time;
+     u64 timestamp;
+};
+
+struct perf_cgroup {
+     struct cgroup_subsys_state css;
+     struct perf_cgroup_time *time;
+};

Can we avoid adding this perf cgroup subsystem? It has 2 disavantages:

Well, I need to maintain some timing information for each cgroup. This has to be stored somewhere.

Quoted 7 lines
Quoted 2 lines

Seems you can simply store it in struct perf_event?

No, timing has to be shared by events monitoring the same cgroup at the same time.
Works like a timestamp. It needs to be centralized for all events attached to the same cgroup.

I no little about internel perf code, so I don't know if we can store this somewhere in perf. The last resort could be store it in struct cgroup.

Quoted 74 lines
Quoted 33 lines
Quoted 25 lines
Quoted 17 lines
Quoted 1 line

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.

That's unfortunately true ;-)

Quoted 6 lines

- If there are several different cgroup mount points, only one can be monitored.

To choose which cgroup hierarchy to monitor, hierarchy id can be passed from userspace, which is the 2nd column below:

Ok, I will investigate this. As long as the hierarchy id is unique AND it can be searched, then we can use it. Using /proc is fine with me.

Quoted 5 lines
$ cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   0       1       1
net_cls 0       1       1

If I mount all subsystems:
mount -t cgroup none /dev/cgroup
Then, I get:
#subsys_name  hierarchy       num_cgroups     enabled cpuset        1       1       1
cpu           1       1       1
perf_event    1       1       1

Quoted 3 lines

In other words, the hierarchy id is not unique. If the perf_event is not mounted, then hierarchy id = 0.

Yes, it's unique. ;)

You mounted them together, and that's a cgroup hierarchy, so they have the same hierarchy id.

Quoted 9 lines

If you mount them seperately:

# mount -t cgroup -o debug xxx /cgroup1
# mount -t cgroup -o net_cls xxx /cgroup2/
# cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   1       1       1
net_cls 2       1       1

Ok, but if you mount perf_event twice, you get the same hierarchy id for it:

# mount -t cgroup -operf_event none /cgroup
# cat /proc/cgroups
#subsys_name  hierarchy       num_cgroups     enabled
cpuset        0       1       1
cpu           0       1       1
perf_event    1       1       1
# mount -t cgroup -operf_event none /cgroup2
# cat /proc/cgroups
#subsys_name  hierarchy       num_cgroups     enabled
cpuset        0       1       1
cpu           0       1       1
perf_event    1       1       1

It does not seem like I can mount the same subsystem twice with difference hierarchies:

# umount /cgroup2
# mount -t cgroup -operf_event,cpuset none /cgroup2
mount: none already mounted or /cgroup2 busy
# mount -t cgroup  none /cgroup2
mount: none already mounted or /cgroup2 busy
Quoted 5 lines

They now have different hierarchy id, because they belong to different cgroup hierarchy.

So pid + hierarchy_id locates the cgroup.

Quoted 7 lines
Quoted 3 lines

I cannot do task's pid + cgroup hierarchy_id. It's one or the other.

I've looked into the patch again, and I see you pass the fd from userspace, so you don't need hierarchy_id.

True.

Quoted 4 lines

And to get rid of perf_cgroup subsys, seems you just need to find another place to store the time info, somewhere inside perf code or in struct cgroup.

Something I may have missed since the beginning of our conversation is why do you think definition perf_cgroup subsys is wrong or useless. What kind of problem does it introduce. I think it is fine to reject cgroup mode if the perf cgroup is not mounted.

The other key point is that from a task (on context switch), I need to identify the perf_cgroup subsys that it corresponds to. How would I have such link if I don't leverage the existing cgroup infrastructure?

#8 lore raw
Quoted 111 lines · stephane eranian
Quoted 100 lines · Li Zefan

(Sorry for the late reply. I've been keeping busy..)

Quoted 16 lines · Stephane Eranian
Quoted 15 lines · Li Zefan
Quoted 15 lines
Quoted 15 lines
Quoted 12 lines

Review

Quoted 10 lines
(quoted diff) +10 -0
+#ifdef CONFIG_CGROUPS
+struct perf_cgroup_time {
+     u64 time;
+     u64 timestamp;
+};
+
+struct perf_cgroup {
+     struct cgroup_subsys_state css;
+     struct perf_cgroup_time *time;
+};

Can we avoid adding this perf cgroup subsystem? It has 2 disavantages:

Well, I need to maintain some timing information for each cgroup. This has to be stored somewhere.

Quoted 7 lines
Quoted 2 lines

Seems you can simply store it in struct perf_event?

No, timing has to be shared by events monitoring the same cgroup at the same time.
Works like a timestamp. It needs to be centralized for all events attached to the same cgroup.

I no little about internel perf code, so I don't know if we can store this somewhere in perf. The last resort could be store it in struct cgroup.

Quoted 71 lines
Quoted 33 lines
Quoted 20 lines
Quoted 17 lines
Quoted 1 line

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.

That's unfortunately true ;-)

Quoted 6 lines

- If there are several different cgroup mount points, only one can be monitored.

To choose which cgroup hierarchy to monitor, hierarchy id can be passed from userspace, which is the 2nd column below:

Ok, I will investigate this. As long as the hierarchy id is unique AND it can be searched, then we can use it. Using /proc is fine with me.

Quoted 5 lines
$ cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   0       1       1
net_cls 0       1       1

If I mount all subsystems:
mount -t cgroup none /dev/cgroup
Then, I get:

Quoted 8 lines
#subsys_name  hierarchy       num_cgroups     enabled
cpuset        1       1       1
cpu           1       1       1
perf_event    1       1       1

In other words, the hierarchy id is not unique. If the perf_event is not mounted, then hierarchy id = 0.

Yes, it's unique. ;)

You mounted them together, and that's a cgroup hierarchy, so they have the same hierarchy id.

Quoted 9 lines

If you mount them seperately:

# mount -t cgroup -o debug xxx /cgroup1
# mount -t cgroup -o net_cls xxx /cgroup2/
# cat /proc/cgroups
#subsys_name    hierarchy       num_cgroups     enabled
debug   1       1       1
net_cls 2       1       1

Ok, but if you mount perf_event twice, you get the same hierarchy id for it:

# mount -t cgroup -operf_event none /cgroup
# cat /proc/cgroups
#subsys_name  hierarchy       num_cgroups     enabled
cpuset        0       1       1
cpu           0       1       1
perf_event    1       1       1
# mount -t cgroup -operf_event none /cgroup2
# cat /proc/cgroups
#subsys_name  hierarchy       num_cgroups     enabled
cpuset        0       1       1
cpu           0       1       1
perf_event    1       1       1

It does not seem like I can mount the same subsystem twice with difference hierarchies:

# umount /cgroup2
# mount -t cgroup -operf_event,cpuset none /cgroup2
mount: none already mounted or /cgroup2 busy
# mount -t cgroup  none /cgroup2
mount: none already mounted or /cgroup2 busy
Quoted 3 lines

They now have different hierarchy id, because they belong to different cgroup hierarchy.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

So pid + hierarchy_id locates the cgroup.

I cannot do task's pid + cgroup hierarchy_id. It's one or the other.

I've looked into the patch again, and I see you pass the fd from userspace, so you don't need hierarchy_id.

True.

Quoted 9 lines
Quoted 4 lines

And to get rid of perf_cgroup subsys, seems you just need to find another place to store the time info, somewhere inside perf code or in struct cgroup.

Something I may have missed since the beginning of our conversation is why do you think definition perf_cgroup subsys is wrong or useless. What kind of problem does it introduce. I think it is fine to reject cgroup mode if the perf cgroup is not mounted.

Actually I don't have strong option over this perf_cgroup subsys. Anyway, I already have cpuacct subsys.

For the disavantage I mentioned before:

- If one mounted cgroup fs without perf cgroup subsys, he can't monitor it.

This is not a problem if we can bind a subsys to a cgroup hierarchy via remount. Currently we can do this only when the cgroupfs has root cgroup only.

For the case that the cgroupfs has child cgroups, adding a subsys to it should not be difficult, but seems removing is another story..