Skip to contentSkip to conversation

perf_counter: Provide a way to enable counters on exec

PATCH In review Paul Mackerras 2 participants 3 messages Jun 30, 2009 – Jul 6, 2009 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf_counter: Provide a way to enable counters on exec

This provides a way to mark a counter to be enabled on the next exec. This is useful for measuring the total activity of a program without including overhead from the process that launches it.

This also changes the perf stat command to use this new facility.

---
v2: only unclone if we enabled one or more counters.

This differs from Ingo's approach a little in that I don't keep the counter force-disabled until exec, and I reuse the comm hook instead of having a new perf_counter_exec hook (not that a new hook would necessarily be a bad idea). Also, my locking is a bit heavier, it's more or less like the old perf_counter_task_enable().

 include/linux/perf_counter.h |    3 +-
 kernel/perf_counter.c        |   50 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/builtin-stat.c    |    6 ++--
 3 files changed, 55 insertions(+), 4 deletions(-)
include/linux/perf_counter.h +2 -1
index 3078e23..5e970c7 100644
@@ -179,8 +179,9 @@ struct perf_counter_attr {
 				comm	       :  1, /* include comm data     */
 				freq           :  1, /* use freq, not period  */
 				inherit_stat   :  1, /* per task counts       */
+				enable_on_exec :  1, /* next exec enables     */
 
-				__reserved_1   : 52;
+				__reserved_1   : 51;
 
 	__u32			wakeup_events;	/* wakeup every n events */
 	__u32			__reserved_2;
kernel/perf_counter.c +50 -0

62 lines not on this page · raw message

tools/perf/builtin-stat.c +3 -3
index 201ef23..2e03524 100644
@@ -116,8 +116,9 @@ static void create_perf_stat_counter(int counter, int pid)
 					fd[cpu][counter], strerror(errno));
 		}
 	} else {
-		attr->inherit	= inherit;
-		attr->disabled	= 1;
+		attr->inherit	     = inherit;
+		attr->disabled	     = 1;
+		attr->enable_on_exec = 1;
 
 		fd[0][counter] = sys_perf_counter_open(attr, pid, -1, -1, 0);
 		if (fd[0][counter] < 0 && verbose)
@@ -262,7 +263,6 @@ static int run_perf_stat(int argc, const char **argv)
 	 * Enable counters and exec the command:
 	 */
 	t0 = rdclock();
-	prctl(PR_TASK_PERF_COUNTERS_ENABLE);
 
 	close(go_pipe[1]);
 	wait(&status);
Signature · 1 line
1.6.0.4
#1 lore raw
Commit-ID:  57e7986ed142417498155ebcd5eaf617ac37136d
Gitweb:     http://git.kernel.org/tip/57e7986ed142417498155ebcd5eaf617ac37136d
Author:     Paul Mackerras <[email protected]>
AuthorDate: Tue, 30 Jun 2009 16:07:19 +1000
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 30 Jun 2009 12:00:16 +0200

perf_counter: Provide a way to enable counters on exec

This provides a way to mark a counter to be enabled on the next exec. This is useful for measuring the total activity of a program without including overhead from the process that launches it.

This also changes the perf stat command to use this new facility.

LKML-Reference: <[email protected]>

---
 include/linux/perf_counter.h |    3 +-
 kernel/perf_counter.c        |   50 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/builtin-stat.c    |    6 ++--
 3 files changed, 55 insertions(+), 4 deletions(-)
include/linux/perf_counter.h +2 -1
index 3078e23..5e970c7 100644
@@ -179,8 +179,9 @@ struct perf_counter_attr {
 				comm	       :  1, /* include comm data     */
 				freq           :  1, /* use freq, not period  */
 				inherit_stat   :  1, /* per task counts       */
+				enable_on_exec :  1, /* next exec enables     */
 
-				__reserved_1   : 52;
+				__reserved_1   : 51;
 
 	__u32			wakeup_events;	/* wakeup every n events */
 	__u32			__reserved_2;
kernel/perf_counter.c +50 -0

62 lines not on this page · raw message

tools/perf/builtin-stat.c +3 -3
index 201ef23..2e03524 100644
@@ -116,8 +116,9 @@ static void create_perf_stat_counter(int counter, int pid)
 					fd[cpu][counter], strerror(errno));
 		}
 	} else {
-		attr->inherit	= inherit;
-		attr->disabled	= 1;
+		attr->inherit	     = inherit;
+		attr->disabled	     = 1;
+		attr->enable_on_exec = 1;
 
 		fd[0][counter] = sys_perf_counter_open(attr, pid, -1, -1, 0);
 		if (fd[0][counter] < 0 && verbose)
@@ -262,7 +263,6 @@ static int run_perf_stat(int argc, const char **argv)
 	 * Enable counters and exec the command:
 	 */
 	t0 = rdclock();
-	prctl(PR_TASK_PERF_COUNTERS_ENABLE);
 
 	close(go_pipe[1]);
 	wait(&status);
#2 lore raw
In reply to Paul Mackerras
Quoted 7 lines · Paul Mackerras

This provides a way to mark a counter to be enabled on the next exec. This is useful for measuring the total activity of a program without including overhead from the process that launches it.

This also changes the perf stat command to use this new facility.

Quoted 8 lines

---
v2: only unclone if we enabled one or more counters.

This differs from Ingo's approach a little in that I don't keep the counter force-disabled until exec, and I reuse the comm hook instead of having a new perf_counter_exec hook (not that a new hook would necessarily be a bad idea). Also, my locking is a bit heavier, it's more or less like the old perf_counter_task_enable().

Hmm, we might want to add that second hook since this will also trigger for things like prctl(PR_SET_NAME).

Also, we would probably want this below...

---
Subject: perf_counter: unify unclone context

We have grown multiple unclone sites, stick it in a function.

---
 kernel/perf_counter.c |   36 +++++++++++++++++-------------------
 1 files changed, 17 insertions(+), 19 deletions(-)
kernel/perf_counter.c +17 -19
index d55a50d..322e254 100644
@@ -146,6 +146,14 @@ static void put_ctx(struct perf_counter_context *ctx)
 	}
 }
 
+static void unclone_ctx(struct perf_counter_context *ctx)
+{
+	if (ctx->parent_ctx) {
+		put_ctx(ctx->parent_ctx);
+		ctx->parent_ctx = NULL;
+	}
+}
+
 /*
  * Get the perf_counter_context for a task and lock it.
  * This has to cope with with the fact that until it is locked,
@@ -1463,10 +1471,8 @@ static void perf_counter_enable_on_exec(struct task_struct *task)
 	/*
 	 * Unclone this context if we enabled any counter.
 	 */
-	if (enabled && ctx->parent_ctx) {
-		put_ctx(ctx->parent_ctx);
-		ctx->parent_ctx = NULL;
-	}
+	if (enabled)
+	       unclone_ctx(ctx);
 
 	spin_unlock(&ctx->lock);
 
@@ -1526,7 +1532,6 @@ __perf_counter_init_context(struct perf_counter_context *ctx,
 
 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
 {
-	struct perf_counter_context *parent_ctx;
 	struct perf_counter_context *ctx;
 	struct perf_cpu_context *cpuctx;
 	struct task_struct *task;
@@ -1586,11 +1591,7 @@ static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
  retry:
 	ctx = perf_lock_task_context(task, &flags);
 	if (ctx) {
-		parent_ctx = ctx->parent_ctx;
-		if (parent_ctx) {
-			put_ctx(parent_ctx);
-			ctx->parent_ctx = NULL;		/* no longer a clone */
-		}
+		unclone_ctx(ctx);
 		spin_unlock_irqrestore(&ctx->lock, flags);
 	}
 
@@ -4255,15 +4256,12 @@ void perf_counter_exit_task(struct task_struct *child)
 	 */
 	spin_lock(&child_ctx->lock);
 	child->perf_counter_ctxp = NULL;
-	if (child_ctx->parent_ctx) {
-		/*
-		 * This context is a clone; unclone it so it can't get
-		 * swapped to another process while we're removing all
-		 * the counters from it.
-		 */
-		put_ctx(child_ctx->parent_ctx);
-		child_ctx->parent_ctx = NULL;
-	}
+	/*
+	 * If this context is a clone; unclone it so it can't get
+	 * swapped to another process while we're removing all
+	 * the counters from it.
+	 */
+	unclone_ctx(child_ctx);
 	spin_unlock(&child_ctx->lock);
 	local_irq_restore(flags);