Skip to contentSkip to conversation

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

RFC PATCH In review Stephane Eranian 7 participants 14 messages Sep 9, 2010 – Sep 10, 2010

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

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

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

The patch adds perf_event_attr.cgroup, a boolean, to activate this new mode. The cgroup is designated by passing in perf_event_attr.cgroup_fd, an opened file descriptor to the <mnt>/<cgroup>/perf_event.perf file.

This is the second version of this patch. It corrects the way time_enabled is accounted for. In cgroup mode, time_enabled reflects the time the cgroup was active, i.e., threads from the cgroup executed on the monitored CPU. This is a more useful metric than just wall-clock. The meaning of time_enabled without cgroup is unaffected.

--

include/linux/cgroup.h +2 -0
index 3cb7d04..ed76357 100644
@@ -618,6 +618,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_file(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 +22 -2
index 000610c..f84b38e 100644
@@ -215,8 +215,9 @@ struct perf_event_attr {
 				 */
 				precise_ip     :  2, /* skid constraint       */
 				mmap_data      :  1, /* non-exec mmap data    */
+				cgroup         :  1, /* cgroup aggregation    */
 
-				__reserved_1   : 46;
+				__reserved_1   : 45;
 
 	union {
 		__u32		wakeup_events;	  /* wakeup every n events */
@@ -226,6 +227,8 @@ struct perf_event_attr {
 	__u32			bp_type;
 	__u64			bp_addr;
 	__u64			bp_len;
+
+	int			cgroup_fd;
 };
 
 /*
@@ -461,6 +464,7 @@ enum perf_callchain_context {
  */
 
 #ifdef CONFIG_PERF_EVENTS
+# include <linux/cgroup.h>
 # include <asm/perf_event.h>
 # include <asm/local64.h>
 #endif
@@ -657,6 +661,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:
  */
@@ -759,7 +775,9 @@ struct perf_event {
 	struct ftrace_event_call	*tp_event;
 	struct event_filter		*filter;
 #endif
-
+#ifdef CONFIG_CGROUPS
+	struct perf_cgroup		*css;
+#endif
 #endif /* CONFIG_PERF_EVENTS */
 };
 
@@ -806,6 +824,8 @@ struct perf_event_context {
 	u64				generation;
 	int				pin_count;
 	struct rcu_head			rcu_head;
+
+	int				nr_cgroups;
 };
 
 /*
kernel/cgroup.c +17 -0
index e5c5497..3e56354 100644
@@ -4722,6 +4722,23 @@ css_get_next(struct cgroup_subsys *ss, int id,
 	return ret;
 }
 
+struct cgroup_subsys_state *cgroup_css_from_file(struct file *f, int id)
+{
+	struct cgroup *cgrp;
+
+	/* check in cgroup filesystem */
+	if (f->f_op != &cgroup_seqfile_operations)
+		return ERR_PTR(-EBADF);
+
+	if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
+		return ERR_PTR(-EINVAL);
+
+	/* get cgroup */
+	cgrp = __d_cgrp(f->f_dentry->d_parent);
+
+	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 +542 -118

945 lines not on this page · raw message

#1 lore raw
Quoted 17 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 patch adds perf_event_attr.cgroup, a boolean, to activate this new mode. The cgroup is designated by passing in perf_event_attr.cgroup_fd, an opened file descriptor to the <mnt>/<cgroup>/perf_event.perf file.

This is the second version of this patch. It corrects the way time_enabled is accounted for. In cgroup mode, time_enabled reflects the time the cgroup was active, i.e., threads from the cgroup executed on the monitored CPU. This is a more useful metric than just wall-clock. The meaning of time_enabled without cgroup is unaffected.

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

Quoted 9 lines
include/linux/perf_event.h +9 -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;
	struct perf_cgroup_time __percpu *time;

Please run sparse after this "__percpu" change.

Quoted 6 lines
kernel/perf_event.c +5 -0
+};
+#endif
+
 /*
+
+	jc->time = alloc_percpu(struct perf_cgroup_time);

kernel/perf_event.c static int __init perf_event_sysfs_init(void) · line 6492

Quoted 10 lines
kernel/perf_event.c +10 -0
+	if (!jc->time) {
+		vfree(jc);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	for_each_possible_cpu(c) {
+		t = per_cpu_ptr(jc->time, c);
+		t->time = 0;
+		t->timestamp = 0;
+	}

alloc_percpu() is zalloc_percpu() in fact, memory is already cleared.

#2 lore raw

Eric,

Quoted 30 lines · Eric Dumazet
Quoted 17 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 patch adds perf_event_attr.cgroup, a boolean, to activate this new mode. The cgroup is designated by passing in perf_event_attr.cgroup_fd, an opened file descriptor to the <mnt>/<cgroup>/perf_event.perf file.

This is the second version of this patch. It corrects the way time_enabled is accounted for. In cgroup mode, time_enabled reflects the time the cgroup was active, i.e., threads from the cgroup executed on the monitored CPU.  This is a more useful metric than just wall-clock. The meaning of time_enabled without cgroup is unaffected.

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

Quoted 9 lines
include/linux/perf_event.h +9 -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;

       struct perf_cgroup_time __percpu *time;

Quoted 3 lines

Please run sparse  after this "__percpu" change.

Will do.

Quoted 12 lines
Quoted 11 lines

+     jc->time = alloc_percpu(struct perf_cgroup_time);
+     if (!jc->time) {
+             vfree(jc);
+             return ERR_PTR(-ENOMEM);
+     }
+
+     for_each_possible_cpu(c) {
+             t = per_cpu_ptr(jc->time, c);
+             t->time = 0;
+             t->timestamp = 0;
+     }

Quoted 2 lines

alloc_percpu() is zalloc_percpu() in fact, memory is already cleared.

I remember thinking about this and trying to trace to the code down to figure this out. But it is rather complicated. If alloc_percpu() always clears the memory, then I think that calling is zalloc_percpu() would be more helpful....

Quoted 2 lines
maintainer
#3 lore raw
Quoted 6 lines · Stephane Eranian
Quoted 2 lines

alloc_percpu() is zalloc_percpu() in fact, memory is already cleared.

I remember thinking about this and trying to trace to the code down to figure this out. But it is rather complicated. If alloc_percpu() always clears the memory, then I think that calling is zalloc_percpu() would be more helpful....

pcpu_populate_chunk() in mm/percpu-vm.c does indeed do that memset, the one in mm/percpu-km.c does not.

It is not obviously clear to me the -km allocator does indeed result in zero filled memory.

Tejun?

maintainer
#4 lore raw

Hello,

Quoted 7 lines · Peter Zijlstra
Quoted 6 lines · Stephane Eranian
Quoted 2 lines

alloc_percpu() is zalloc_percpu() in fact, memory is already cleared.

I remember thinking about this and trying to trace to the code down to figure this out. But it is rather complicated. If alloc_percpu() always clears the memory, then I think that calling is zalloc_percpu() would be more helpful....

Maybe but at this point it might be a bit too late. The allocator has been that way since the beginning.

Quoted 5 lines

pcpu_populate_chunk() in mm/percpu-vm.c does indeed do that memset, the one in mm/percpu-km.c does not.

It is not obviously clear to me the -km allocator does indeed result in zero filled memory.

Nice catch. Fortunately, the -km allocator isn't currently being used in upstrea although it was enabled for linux-next a couple of days ago. I'll fix it up.

Thank you.

Signature · 1 line
tejun
#5 lore raw

PATCH 1/1 percpu: clear memory allocated with the km allocator

Percpu allocator should clear memory before returning it but the km allocator forgot to do it. Fix it.

---
 mm/percpu-km.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
mm/percpu-km.c +5 -1
index 7037bc7..89633fe 100644
@@ -35,7 +35,11 @@
 
 static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
 {
-	/* noop */
+	unsigned int cpu;
+
+	for_each_possible_cpu(cpu)
+		memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
+
 	return 0;
 }
 
Signature · 1 line
1.7.1
maintainer Acked-by on v1 1/1
#6 lore raw

Re: [PATCH percpu#for-next] percpu: clear memory allocated with the km allocator

Quoted 5 lines · Tejun Heo

Percpu allocator should clear memory before returning it but the km allocator forgot to do it. Fix it.

(fwiw, -tip uses Reported-by)

Quoted 21 lines
---
 mm/percpu-km.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
mm/percpu-km.c +5 -1
index 7037bc7..89633fe 100644
@@ -35,7 +35,11 @@
 
 static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
 {
-	/* noop */
+	unsigned int cpu;
+
+	for_each_possible_cpu(cpu)
+		memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
+
 	return 0;
 }
 
#10 lore raw
In reply to Tejun Heo
Quoted 7 lines · Tejun Heo

Hello,

Quoted 3 lines · Peter Zijlstra
Quoted 2 lines · Stephane Eranian
Quoted 2 lines

alloc_percpu() is zalloc_percpu() in fact, memory is already cleared.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

I remember thinking about this and trying to trace to the code down to figure this out. But it is rather complicated. If alloc_percpu() always clears the memory, then I think that calling is zalloc_percpu() would be more helpful....

Maybe but at this point it might be a bit too late.  The allocator has been that way since the beginning.

Then add a comment in the header or at the definition of the function to make this more explicit.

Quoted 14 lines
Quoted 5 lines

pcpu_populate_chunk() in mm/percpu-vm.c does indeed do that memset, the one in mm/percpu-km.c does not.

It is not obviously clear to me the -km allocator does indeed result in zero filled memory.

Nice catch.  Fortunately, the -km allocator isn't currently being used in upstrea although it was enabled for linux-next a couple of days ago.  I'll fix it up.

Thank you.

--
tejun

maintainer
#11 lore raw

PATCH 1/1 percpu: update comments to reflect that percpu allocations are always zero-filled

---
 mm/percpu.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
mm/percpu.c +6 -5
index 0cd4bf6..12dea33 100644
@@ -826,8 +826,8 @@ fail_unlock_mutex:
  * @size: size of area to allocate in bytes
  * @align: alignment of area (max PAGE_SIZE)
  *
- * Allocate percpu area of @size bytes aligned at @align.  Might
- * sleep.  Might trigger writeouts.
+ * Allocate zero-filled percpu area of @size bytes aligned at @align.
+ * Might sleep.  Might trigger writeouts.
  *
  * CONTEXT:
  * Does GFP_KERNEL allocation.
@@ -846,9 +846,10 @@ EXPORT_SYMBOL_GPL(__alloc_percpu);
  * @size: size of area to allocate in bytes
  * @align: alignment of area (max PAGE_SIZE)
  *
- * Allocate percpu area of @size bytes aligned at @align from reserved
- * percpu area if arch has set it up; otherwise, allocation is served
- * from the same dynamic area.  Might sleep.  Might trigger writeouts.
+ * Allocate zero-filled percpu area of @size bytes aligned at @align
+ * from reserved percpu area if arch has set it up; otherwise,
+ * allocation is served from the same dynamic area.  Might sleep.
+ * Might trigger writeouts.
  *
  * CONTEXT:
  * Does GFP_KERNEL allocation.
Signature · 1 line
1.7.1
#12 lore raw

Re: [PATCH] percpu: update comments to reflect that percpu allocations are always zero-filled

Thanks

Quoted 39 lines · Tejun Heo

---
 mm/percpu.c |   11 ++++++-----

 1 files changed, 6 insertions(+), 5 deletions(-)
mm/percpu.c +6 -6
index 0cd4bf6..12dea33 100644
@@ -826,8 +826,8 @@ fail_unlock_mutex:
  * @size: size of area to allocate in bytes
  * @align: alignment of area (max PAGE_SIZE)
  *
- * Allocate percpu area of @size bytes aligned at @align.  Might
- * sleep.  Might trigger writeouts.
+ * Allocate zero-filled percpu area of @size bytes aligned at @align.
+ * Might sleep.  Might trigger writeouts.
  *
  * CONTEXT:
  * Does GFP_KERNEL allocation.
@@ -846,9 +846,10 @@ EXPORT_SYMBOL_GPL(__alloc_percpu);
  * @size: size of area to allocate in bytes
  * @align: alignment of area (max PAGE_SIZE)
  *
- * Allocate percpu area of @size bytes aligned at @align from reserved
- * percpu area if arch has set it up; otherwise, allocation is served
- * from the same dynamic area.  Might sleep.  Might trigger writeouts.
+ * Allocate zero-filled percpu area of @size bytes aligned at @align
+ * from reserved percpu area if arch has set it up; otherwise,
+ * allocation is served from the same dynamic area.  Might sleep.
+ * Might trigger writeouts.
  *
  * CONTEXT:
  * Does GFP_KERNEL allocation.
--

1.7.1

#7 lore raw

Re: [PATCH percpu#for-next] percpu: clear memory allocated with the km allocator

In reply to Peter Zijlstra
Quoted 8 lines · On Fri, 10 Sep 2010 10:55:18 +0200 Peter Zijlstra wrote:
Quoted 5 lines · Tejun Heo

Percpu allocator should clear memory before returning it but the km allocator forgot to do it. Fix it.

(fwiw, -tip uses Reported-by)

fyi, Documentation/SubmittingPatches does also.

Quoted 12 lines
Quoted 10 lines
---
 mm/percpu-km.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
mm/percpu-km.c +0 -0
index 7037bc7..89633fe 100644
@@ -35,7 +35,11 @@
 
Quoted 10 lines

mm/percpu-km.c line 44

Quoted 10 lines
mm/percpu-km.c +5 -1
 static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
 {
-	/* noop */
+	unsigned int cpu;
+
+	for_each_possible_cpu(cpu)
+		memset((void *)pcpu_chunk_addr(chunk, cpu, 0) + off, 0, size);
+
 	return 0;
 }

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#8 lore raw

Re: [PATCH percpu#for-next] percpu: clear memory allocated with the km allocator

Quoted 6 lines · Randy Dunlap
Quoted 4 lines · On Fri, 10 Sep 2010 10:55:18 +0200 Peter Zijlstra wrote:
Quoted 3 lines · Tejun Heo

Percpu allocator should clear memory before returning it but the km allocator forgot to do it. Fix it.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

(fwiw, -tip uses Reported-by)

fyi, Documentation/SubmittingPatches does also.

Yeah, and, FYI, there's observed strong correlation between the maintenance policies of -tip and Documentation/SubmittingPatches. [i'd even venture so far to claim causation]

Thanks,

Ingo

#9 lore raw

Re: [PATCH percpu#for-next] percpu: clear memory allocated with the km allocator

Quoted 12 lines · Ingo Molnar
Quoted 9 lines · Randy Dunlap
Quoted 7 lines · On Fri, 10 Sep 2010 10:55:18 +0200 Peter Zijlstra wrote:
Quoted 5 lines · Tejun Heo

Percpu allocator should clear memory before returning it but the km allocator forgot to do it. Fix it.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

(fwiw, -tip uses Reported-by)

fyi, Documentation/SubmittingPatches does also.

Yeah, and, FYI, there's observed strong correlation between the maintenance policies of -tip and Documentation/SubmittingPatches. [i'd even venture so far to claim causation]

Oh, I already updated it to be Reported-by. I use Spotted-by when to distinguish the cases where the root cause is diagnosed by the reporter as opposed to when the reporter just reported a bug but I suppose it's just causing more confusion. I'll just use Reproted-by from now on.

Thank you.

Signature · 1 line
tejun