Skip to contentSkip to conversation

perf: core, add group scheduling transactional APIs

RFC PATCH Superseded Lin Ming 3 participants 6 messages Apr 22, 2010 – Apr 23, 2010 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Lin Ming

author
#0 lore raw

RFC PATCH 1/4 perf: core, add group scheduling transactional APIs

Add group scheduling transactional APIs to struct pmu. These APIs will be implemented in arch code, based on Peter's idea as below.

Quoted 2 lines

the idea behind hw_perf_group_sched_in() is to not perform schedulability tests on each event in the group, but to add the group

as

Quoted 9 lines

a whole and then perform one test.

Of course, when that test fails, you'll have to roll-back the whole group again.

So start_txn (or a better name) would simply toggle a flag in the pmu implementation that will make pmu::enable() not perform the schedulablilty test.

Quoted 8 lines

Then commit_txn() will perform the schedulability test (so note the method has to have a !void return value, my mistake in the earlier email).

This will allow us to use the regular kernel/perf_event.c::group_sched_in() and all the rollback code. Currently each hw_perf_group_sched_in() implementation duplicates all the rolllback code (with various bugs).

---
 include/linux/perf_event.h |    8 +++++---
 kernel/perf_event.c        |   29 ++++++++++++++++-------------
 2 files changed, 21 insertions(+), 16 deletions(-)
include/linux/perf_event.h +5 -3
index ace31fb..b16cfba 100644
@@ -532,6 +532,8 @@ struct hw_perf_event {
 
 struct perf_event;
 
+#define PERF_EVENT_TRAN_STARTED 1
+
 /**
  * struct pmu - generic performance monitoring unit
  */
@@ -542,6 +544,9 @@ struct pmu {
 	void (*stop)			(struct perf_event *event);
 	void (*read)			(struct perf_event *event);
 	void (*unthrottle)		(struct perf_event *event);
+	void (*start_txn)		(const struct pmu *pmu);
+	void (*stop_txn)		(const struct pmu *pmu);
+	int (*commit_txn)		(const struct pmu *pmu);
 };
 
 /**
@@ -807,9 +812,6 @@ extern void perf_disable(void);
 extern void perf_enable(void);
 extern int perf_event_task_disable(void);
 extern int perf_event_task_enable(void);
-extern int hw_perf_group_sched_in(struct perf_event *group_leader,
-	       struct perf_cpu_context *cpuctx,
-	       struct perf_event_context *ctx);
 extern void perf_event_update_userpage(struct perf_event *event);
 extern int perf_event_release_kernel(struct perf_event *event);
 extern struct perf_event *
kernel/perf_event.c +16 -13
index 9dbe8cd..ffd2360 100644
@@ -83,14 +83,6 @@ extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
 void __weak hw_perf_disable(void)		{ barrier(); }
 void __weak hw_perf_enable(void)		{ barrier(); }
 
-int __weak
-hw_perf_group_sched_in(struct perf_event *group_leader,
-	       struct perf_cpu_context *cpuctx,
-	       struct perf_event_context *ctx)
-{
-	return 0;
-}
-
 void __weak perf_event_print_debug(void)	{ }
 
 static DEFINE_PER_CPU(int, perf_disable_count);
@@ -641,15 +633,15 @@ group_sched_in(struct perf_event *group_event,
 	       struct perf_cpu_context *cpuctx,
 	       struct perf_event_context *ctx)
 {
-	struct perf_event *event, *partial_group;
+	struct perf_event *event, *partial_group = NULL;
+	const struct pmu *pmu = group_event->pmu;
 	int ret;
 
 	if (group_event->state == PERF_EVENT_STATE_OFF)
 		return 0;
 
-	ret = hw_perf_group_sched_in(group_event, cpuctx, ctx);
-	if (ret)
-		return ret < 0 ? ret : 0;
+	if (pmu->start_txn)
+		pmu->start_txn(pmu);
 
 	if (event_sched_in(group_event, cpuctx, ctx))
 		return -EAGAIN;
@@ -664,9 +656,20 @@ group_sched_in(struct perf_event *group_event,
 		}
 	}
 
-	return 0;
+	if (pmu->commit_txn) {
+		ret = pmu->commit_txn(pmu);
+		if (!ret) {
+			if (pmu->stop_txn)
+				pmu->stop_txn(pmu);
+
+			return 0;
+		}
+	}
 
 group_error:
+	if (pmu->stop_txn)
+		pmu->stop_txn(pmu);
+
 	/*
 	 * Groups can be scheduled in as one unit only, so undo any
 	 * partial group before returning:
maintainer
#1 lore raw

kernel/perf_event.c group_sched_in(struct perf_event *group_event, · line 667

Quoted 9 lines · Lin Ming
kernel/perf_event.c +9 -0
+       if (pmu->commit_txn) {
+               ret = pmu->commit_txn(pmu);
+               if (!ret) {
+                       if (pmu->stop_txn)
+                               pmu->stop_txn(pmu);
+
+                       return 0;
+               }
+       } 

I think we can mandate that if one of the _txn methods is available, they all are, it would be weird otherwise.

author
#2 lore raw
Quoted 11 lines · Peter Zijlstra

Review

Quoted 9 lines · Lin Ming
(quoted diff) +9 -0
+       if (pmu->commit_txn) {
+               ret = pmu->commit_txn(pmu);
+               if (!ret) {
+                       if (pmu->stop_txn)
+                               pmu->stop_txn(pmu);
+
+                       return 0;
+               }
+       } 
Quoted 3 lines

I think we can mandate that if one of the _txn methods is available, they all are, it would be weird otherwise.

How about below?

kernel/perf_event.c +7 -4
index ffd2360..72ea25c 100644
@@ -635,12 +635,16 @@ group_sched_in(struct perf_event *group_event,
 {
 	struct perf_event *event, *partial_group = NULL;
 	const struct pmu *pmu = group_event->pmu;
+	bool txn = false;
 	int ret;
 
 	if (group_event->state == PERF_EVENT_STATE_OFF)
 		return 0;
 
 	if (pmu->start_txn)
+		txn = true;
+
+	if (txn)
 		pmu->start_txn(pmu);
 
 	if (event_sched_in(group_event, cpuctx, ctx))
@@ -656,18 +660,17 @@ group_sched_in(struct perf_event *group_event,
 		}
 	}
 
-	if (pmu->commit_txn) {
+	if (txn) {
 		ret = pmu->commit_txn(pmu);
 		if (!ret) {
-			if (pmu->stop_txn)
-				pmu->stop_txn(pmu);
+			pmu->stop_txn(pmu);
 
 			return 0;
 		}
 	}
 
 group_error:
-	if (pmu->stop_txn)
+	if (txn)
 		pmu->stop_txn(pmu);
 
 	/*
#3 lore raw
In reply to Lin Ming
Quoted 48 lines · Lin Ming

Add group scheduling transactional APIs to struct pmu. These APIs will be implemented in arch code, based on Peter's idea as below.

Quoted 2 lines

the idea behind hw_perf_group_sched_in() is to not perform schedulability tests on each event in the group, but to add the group

as

Quoted 9 lines

a whole and then perform one test.

Of course, when that test fails, you'll have to roll-back the whole group again.

So start_txn (or a better name) would simply toggle a flag in the pmu implementation that will make pmu::enable() not perform the schedulablilty test.

Quoted 8 lines

Then commit_txn() will perform the schedulability test (so note the method has to have a !void return value, my mistake in the earlier email).

This will allow us to use the regular kernel/perf_event.c::group_sched_in() and all the rollback code. Currently each hw_perf_group_sched_in() implementation duplicates all the rolllback code (with various bugs).

---
 include/linux/perf_event.h |    8 +++++---
 kernel/perf_event.c        |   29 ++++++++++++++++-------------
 2 files changed, 21 insertions(+), 16 deletions(-)
include/linux/perf_event.h +2 -0
index ace31fb..b16cfba 100644
@@ -532,6 +532,8 @@ struct hw_perf_event {
 
 struct perf_event;
 
+#define PERF_EVENT_TRAN_STARTED 1
+
 /**
  * struct pmu - generic performance monitoring unit
  */

include/linux/perf_event.h struct pmu { · line 549

Quoted 6 lines
include/linux/perf_event.h +3 -0
@@ -542,6 +544,9 @@ struct pmu {
 	void (*stop)			(struct perf_event *event);
 	void (*read)			(struct perf_event *event);
 	void (*unthrottle)		(struct perf_event *event);
+	void (*start_txn)		(const struct pmu *pmu);
+	void (*stop_txn)		(const struct pmu *pmu);
+	int (*commit_txn)		(const struct pmu *pmu);

Please add a few comments that briefly explain what these *_txn callbacks are supposed to mean.

Unless txn is an acronym that most kernel developers are used to.

Thanks.

author
#4 lore raw
Quoted 60 lines · Frederic Weisbecker
Quoted 48 lines · Lin Ming

Add group scheduling transactional APIs to struct pmu. These APIs will be implemented in arch code, based on Peter's idea as below.

Quoted 2 lines

the idea behind hw_perf_group_sched_in() is to not perform schedulability tests on each event in the group, but to add the group

as

Quoted 9 lines

a whole and then perform one test.

Of course, when that test fails, you'll have to roll-back the whole group again.

So start_txn (or a better name) would simply toggle a flag in the pmu implementation that will make pmu::enable() not perform the schedulablilty test.

Quoted 8 lines

Then commit_txn() will perform the schedulability test (so note the method has to have a !void return value, my mistake in the earlier email).

This will allow us to use the regular kernel/perf_event.c::group_sched_in() and all the rollback code. Currently each hw_perf_group_sched_in() implementation duplicates all the rolllback code (with various bugs).

---
 include/linux/perf_event.h |    8 +++++---
 kernel/perf_event.c        |   29 ++++++++++++++++-------------
 2 files changed, 21 insertions(+), 16 deletions(-)
include/linux/perf_event.h +2 -0
index ace31fb..b16cfba 100644
@@ -532,6 +532,8 @@ struct hw_perf_event {
 
 struct perf_event;
 
+#define PERF_EVENT_TRAN_STARTED 1
+
 /**
  * struct pmu - generic performance monitoring unit
  */

include/linux/perf_event.h struct pmu { · line 549

Quoted 6 lines
include/linux/perf_event.h +3 -0
@@ -542,6 +544,9 @@ struct pmu {
 	void (*stop)			(struct perf_event *event);
 	void (*read)			(struct perf_event *event);
 	void (*unthrottle)		(struct perf_event *event);
+	void (*start_txn)		(const struct pmu *pmu);
+	void (*stop_txn)		(const struct pmu *pmu);
+	int (*commit_txn)		(const struct pmu *pmu);

Please add a few comments that briefly explain what these *_txn callbacks are supposed to mean.

Quoted 1 line

Unless txn is an acronym that most kernel developers are used to.

How about below changes?

Thanks for review.

include/linux/perf_event.h +15 -3
index b16cfba..bba4c60 100644
@@ -544,9 +544,21 @@ struct pmu {
 	void (*stop)			(struct perf_event *event);
 	void (*read)			(struct perf_event *event);
 	void (*unthrottle)		(struct perf_event *event);
-	void (*start_txn)		(const struct pmu *pmu);
-	void (*stop_txn)		(const struct pmu *pmu);
-	int (*commit_txn)		(const struct pmu *pmu);
+
+	/*
+	 * group events scheduling is treated as a transaction,
+	 * add group events as a whole and perform one schedulability test.
+	 * If test fails, roll back the whole group
+	 */
+
+	/* start group events transaction  */
+	void (*start_group_trans)	(const struct pmu *pmu);
+
+	/* stop group events transaction  */
+	void (*stop_group_trans)	(const struct pmu *pmu);
+
+	/* commit group events transaction */
+	int (*commit_group_trans)	(const struct pmu *pmu);
 };
 
 /**
#5 lore raw
Quoted 84 lines · Lin Ming
Quoted 60 lines · Frederic Weisbecker
Quoted 48 lines · Lin Ming

Add group scheduling transactional APIs to struct pmu. These APIs will be implemented in arch code, based on Peter's idea as below.

Quoted 2 lines

the idea behind hw_perf_group_sched_in() is to not perform schedulability tests on each event in the group, but to add the group

as

Quoted 9 lines

a whole and then perform one test.

Of course, when that test fails, you'll have to roll-back the whole group again.

So start_txn (or a better name) would simply toggle a flag in the pmu implementation that will make pmu::enable() not perform the schedulablilty test.

Quoted 8 lines

Then commit_txn() will perform the schedulability test (so note the method has to have a !void return value, my mistake in the earlier email).

This will allow us to use the regular kernel/perf_event.c::group_sched_in() and all the rollback code. Currently each hw_perf_group_sched_in() implementation duplicates all the rolllback code (with various bugs).

---
 include/linux/perf_event.h |    8 +++++---
 kernel/perf_event.c        |   29 ++++++++++++++++-------------
 2 files changed, 21 insertions(+), 16 deletions(-)
include/linux/perf_event.h +2 -0
index ace31fb..b16cfba 100644
@@ -532,6 +532,8 @@ struct hw_perf_event {
 
 struct perf_event;
 
+#define PERF_EVENT_TRAN_STARTED 1
+
 /**
  * struct pmu - generic performance monitoring unit
  */

include/linux/perf_event.h struct pmu { · line 549

Quoted 6 lines
include/linux/perf_event.h +3 -0
@@ -542,6 +544,9 @@ struct pmu {
 	void (*stop)			(struct perf_event *event);
 	void (*read)			(struct perf_event *event);
 	void (*unthrottle)		(struct perf_event *event);
+	void (*start_txn)		(const struct pmu *pmu);
+	void (*stop_txn)		(const struct pmu *pmu);
+	int (*commit_txn)		(const struct pmu *pmu);

Please add a few comments that briefly explain what these *_txn callbacks are supposed to mean.

Quoted 1 line

Unless txn is an acronym that most kernel developers are used to.

How about below changes?

Thanks for review.

include/linux/perf_event.h +6 -3
index b16cfba..bba4c60 100644
@@ -544,9 +544,21 @@ struct pmu {
 	void (*stop)			(struct perf_event *event);
 	void (*read)			(struct perf_event *event);
 	void (*unthrottle)		(struct perf_event *event);
-	void (*start_txn)		(const struct pmu *pmu);
-	void (*stop_txn)		(const struct pmu *pmu);
-	int (*commit_txn)		(const struct pmu *pmu);
+
+	/*
+	 * group events scheduling is treated as a transaction,
+	 * add group events as a whole and perform one schedulability test.
+	 * If test fails, roll back the whole group
+	 */

include/linux/perf_event.h struct pmu { · line 562

Quoted 10 lines
include/linux/perf_event.h +9 -0
+
+	/* start group events transaction  */
+	void (*start_group_trans)	(const struct pmu *pmu);
+
+	/* stop group events transaction  */
+	void (*stop_group_trans)	(const struct pmu *pmu);
+
+	/* commit group events transaction */
+	int (*commit_group_trans)	(const struct pmu *pmu);
 };

Looks good!

Thanks.