Skip to contentSkip to conversation

sched: Split cpuacct

PATCH In review Li Zefan 4 participants 18 messages Mar 29, 2013 – Apr 10, 2013 Scheduler

lore
Share & tools
lore

Li Zefan

author
#0 lore raw

v2 cover sched: Split cpuacct

v2: rebase against tip-tree

I don't rebase "[PATCH 0/5] cpuacct, cgroup: Remove cgroup_subsys.active", because it can be applied cleanly on top of this patchset.

============================

- This patchset splits cpuacct out of core scheduler code.
- Plus two small optimizations.

0001-sched-Split-cpuacct-code-out-of-core.c.patch 0002-sched-Split-cpuacct-out-of-sched.h.patch 0003-cpuacct-Add-cpuacct_init.patch 0004-cpuacct-Add-cpuacct_acount_field.patch 0005-cpuacct-Remove-redundant-NULL-checks-in-cpuacct_char.patch 0006-cpuacct-Remove-redundant-NULL-checks-in-cpuacct_acou.patch 0007-cpuacct-Clean-up-cpuacct.h.patch

---
 kernel/sched/Makefile  |   1 +
 kernel/sched/core.c    | 228 +----------------------------------------------
 kernel/sched/cpuacct.c | 303 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/cpuacct.h |  22 +++++
 kernel/sched/cputime.c |  18 +---
 kernel/sched/sched.h   |  48 +---------
 6 files changed, 330 insertions(+), 290 deletions(-)
author
#1 lore raw

PATCH v2 1/7 sched: Split cpuacct code out of core.c

---
 kernel/sched/Makefile  |   1 +
 kernel/sched/core.c    | 220 -----------------------------------------------
 kernel/sched/cpuacct.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+), 220 deletions(-)
 create mode 100644 kernel/sched/cpuacct.c
kernel/sched/Makefile +1 -0
index f06d249..deaf90e 100644
@@ -16,3 +16,4 @@ obj-$(CONFIG_SMP) += cpupri.o
 obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
 obj-$(CONFIG_SCHEDSTATS) += stats.o
 obj-$(CONFIG_SCHED_DEBUG) += debug.o
+obj-$(CONFIG_CGROUP_CPUACCT) += cpuacct.o
kernel/sched/core.c +0 -220

226 lines not on this page · raw message

kernel/sched/cpuacct.c new file +227 -0

227 lines not on this page · raw message

Signature · 1 line
1.8.0.2
#2 lore raw

[tip:sched/core] sched: Split cpuacct code out of core.c

sched: Split cpuacct code out of core.c
Commit-ID:  2e76c24d72372db35f226a49c2b99d0fd8cfd400
Gitweb:     http://git.kernel.org/tip/2e76c24d72372db35f226a49c2b99d0fd8cfd400
Author:     Li Zefan <[email protected]>
AuthorDate: Fri, 29 Mar 2013 14:36:31 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 10 Apr 2013 13:54:15 +0200

sched: Split cpuacct code out of core.c

---
 kernel/sched/Makefile  |   1 +
 kernel/sched/core.c    | 220 -----------------------------------------------
 kernel/sched/cpuacct.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+), 220 deletions(-)
kernel/sched/Makefile +1 -0
index f06d249..deaf90e 100644
@@ -16,3 +16,4 @@ obj-$(CONFIG_SMP) += cpupri.o
 obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
 obj-$(CONFIG_SCHEDSTATS) += stats.o
 obj-$(CONFIG_SCHED_DEBUG) += debug.o
+obj-$(CONFIG_CGROUP_CPUACCT) += cpuacct.o
kernel/sched/core.c +0 -220

226 lines not on this page · raw message

kernel/sched/cpuacct.c new file +227 -0

227 lines not on this page · raw message

author
#3 lore raw

PATCH v2 2/7 sched: Split cpuacct code out of sched.h

In reply to Li Zefan

Add cpuacct.h and let sched.h include it.

---
 kernel/sched/cpuacct.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h   | 48 +---------------------------------------------
 2 files changed, 53 insertions(+), 47 deletions(-)
 create mode 100644 kernel/sched/cpuacct.h
kernel/sched/cpuacct.h new file +52 -0
new file mode 100644
index 0000000..a7f3d4a
@@ -0,0 +1,52 @@
+/* Time spent by the tasks of the cpu accounting group executing in ... */
+enum cpuacct_stat_index {
+	CPUACCT_STAT_USER,	/* ... user mode */
+	CPUACCT_STAT_SYSTEM,	/* ... kernel mode */
+
+	CPUACCT_STAT_NSTATS,
+};
+
+#ifdef CONFIG_CGROUP_CPUACCT
+
+#include <linux/cgroup.h>
+/* track cpu usage of a group of tasks and its child groups */
+struct cpuacct {
+	struct cgroup_subsys_state css;
+	/* cpuusage holds pointer to a u64-type object on every cpu */
+	u64 __percpu *cpuusage;
+	struct kernel_cpustat __percpu *cpustat;
+};
+
+extern struct cgroup_subsys cpuacct_subsys;
+extern struct cpuacct root_cpuacct;
+
+/* return cpu accounting group corresponding to this container */
+static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
+{
+	return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
+			    struct cpuacct, css);
+}
+
+/* return cpu accounting group to which this task belongs */
+static inline struct cpuacct *task_ca(struct task_struct *tsk)
+{
+	return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
+			    struct cpuacct, css);
+}
+
+static inline struct cpuacct *parent_ca(struct cpuacct *ca)
+{
+	if (!ca || !ca->css.cgroup->parent)
+		return NULL;
+	return cgroup_ca(ca->css.cgroup->parent);
+}
+
+extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
+
+#else
+
+static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime)
+{
+}
+
+#endif
kernel/sched/sched.h +1 -47

66 lines not on this page · raw message

Signature · 1 line
1.8.0.2
#4 lore raw

[tip:sched/core] sched: Split cpuacct code out of sched.h

sched: Split cpuacct code out of sched.h
Commit-ID:  60fed7891d4115be0ed7ff9ce6851eda80533c64
Gitweb:     http://git.kernel.org/tip/60fed7891d4115be0ed7ff9ce6851eda80533c64
Author:     Li Zefan <[email protected]>
AuthorDate: Fri, 29 Mar 2013 14:36:43 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 10 Apr 2013 13:54:16 +0200

sched: Split cpuacct code out of sched.h

Add cpuacct.h and let sched.h include it.

---
 kernel/sched/cpuacct.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h   | 48 +---------------------------------------------
 2 files changed, 53 insertions(+), 47 deletions(-)
kernel/sched/cpuacct.h new file +52 -0
new file mode 100644
index 0000000..a7f3d4a
@@ -0,0 +1,52 @@
+/* Time spent by the tasks of the cpu accounting group executing in ... */
+enum cpuacct_stat_index {
+	CPUACCT_STAT_USER,	/* ... user mode */
+	CPUACCT_STAT_SYSTEM,	/* ... kernel mode */
+
+	CPUACCT_STAT_NSTATS,
+};
+
+#ifdef CONFIG_CGROUP_CPUACCT
+
+#include <linux/cgroup.h>
+/* track cpu usage of a group of tasks and its child groups */
+struct cpuacct {
+	struct cgroup_subsys_state css;
+	/* cpuusage holds pointer to a u64-type object on every cpu */
+	u64 __percpu *cpuusage;
+	struct kernel_cpustat __percpu *cpustat;
+};
+
+extern struct cgroup_subsys cpuacct_subsys;
+extern struct cpuacct root_cpuacct;
+
+/* return cpu accounting group corresponding to this container */
+static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
+{
+	return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
+			    struct cpuacct, css);
+}
+
+/* return cpu accounting group to which this task belongs */
+static inline struct cpuacct *task_ca(struct task_struct *tsk)
+{
+	return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
+			    struct cpuacct, css);
+}
+
+static inline struct cpuacct *parent_ca(struct cpuacct *ca)
+{
+	if (!ca || !ca->css.cgroup->parent)
+		return NULL;
+	return cgroup_ca(ca->css.cgroup->parent);
+}
+
+extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
+
+#else
+
+static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime)
+{
+}
+
+#endif
kernel/sched/sched.h +1 -47

66 lines not on this page · raw message

author
#5 lore raw

PATCH v2 3/7 cpuacct: Add cpuacct_init()

In reply to Li Zefan

So we don't open-coded initialization of cpuacct in core.c.

---
 kernel/sched/core.c    | 8 ++------
 kernel/sched/cpuacct.c | 7 +++++++
 kernel/sched/cpuacct.h | 5 +++++
 3 files changed, 14 insertions(+), 6 deletions(-)
kernel/sched/core.c +2 -6
index c4e2b81..c7016c1 100644
@@ -6936,12 +6936,8 @@ void __init sched_init(void)
 
 #endif /* CONFIG_CGROUP_SCHED */
 
-#ifdef CONFIG_CGROUP_CPUACCT
-	root_cpuacct.cpustat = &kernel_cpustat;
-	root_cpuacct.cpuusage = alloc_percpu(u64);
-	/* Too early, not expected to fail */
-	BUG_ON(!root_cpuacct.cpuusage);
-#endif
+	cpuacct_init();
+
 	for_each_possible_cpu(i) {
 		struct rq *rq;
 
kernel/sched/cpuacct.c +7 -0
index 50ec24b..48b5e91 100644
@@ -218,6 +218,13 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 	rcu_read_unlock();
 }
 
+void __init cpuacct_init(void)
+{
+	root_cpuacct.cpustat = &kernel_cpustat;
+	root_cpuacct.cpuusage = alloc_percpu(u64);
+	BUG_ON(!root_cpuacct.cpuusage); /* Too early, not expected to fail */
+}
+
 struct cgroup_subsys cpuacct_subsys = {
 	.name = "cpuacct",
 	.css_alloc = cpuacct_css_alloc,
kernel/sched/cpuacct.h +5 -0
index a7f3d4a..551acd7 100644
@@ -41,10 +41,15 @@ static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 	return cgroup_ca(ca->css.cgroup->parent);
 }
 
+extern void cpuacct_init(void);
 extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
 
 #else
 
+static inline void cpuacct_init(void)
+{
+}
+
 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
 }
Signature · 1 line
1.8.0.2
#6 lore raw

[tip:sched/core] sched/cpuacct: Add cpuacct_init()

sched/cpuacct: Add cpuacct_init()
Commit-ID:  dbe4b41f9800223949ce72e4289814697e0ea91a
Gitweb:     http://git.kernel.org/tip/dbe4b41f9800223949ce72e4289814697e0ea91a
Author:     Li Zefan <[email protected]>
AuthorDate: Fri, 29 Mar 2013 14:36:55 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 10 Apr 2013 13:54:16 +0200

sched/cpuacct: Add cpuacct_init()

So we don't open-coded initialization of cpuacct in core.c.

---
 kernel/sched/core.c    | 8 ++------
 kernel/sched/cpuacct.c | 7 +++++++
 kernel/sched/cpuacct.h | 5 +++++
 3 files changed, 14 insertions(+), 6 deletions(-)
kernel/sched/core.c +2 -6
index c28222f..92930a8 100644
@@ -6936,12 +6936,8 @@ void __init sched_init(void)
 
 #endif /* CONFIG_CGROUP_SCHED */
 
-#ifdef CONFIG_CGROUP_CPUACCT
-	root_cpuacct.cpustat = &kernel_cpustat;
-	root_cpuacct.cpuusage = alloc_percpu(u64);
-	/* Too early, not expected to fail */
-	BUG_ON(!root_cpuacct.cpuusage);
-#endif
+	cpuacct_init();
+
 	for_each_possible_cpu(i) {
 		struct rq *rq;
 
kernel/sched/cpuacct.c +7 -0
index 50ec24b..48b5e91 100644
@@ -218,6 +218,13 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 	rcu_read_unlock();
 }
 
+void __init cpuacct_init(void)
+{
+	root_cpuacct.cpustat = &kernel_cpustat;
+	root_cpuacct.cpuusage = alloc_percpu(u64);
+	BUG_ON(!root_cpuacct.cpuusage); /* Too early, not expected to fail */
+}
+
 struct cgroup_subsys cpuacct_subsys = {
 	.name = "cpuacct",
 	.css_alloc = cpuacct_css_alloc,
kernel/sched/cpuacct.h +5 -0
index a7f3d4a..551acd7 100644
@@ -41,10 +41,15 @@ static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 	return cgroup_ca(ca->css.cgroup->parent);
 }
 
+extern void cpuacct_init(void);
 extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
 
 #else
 
+static inline void cpuacct_init(void)
+{
+}
+
 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
 }
author
#7 lore raw

PATCH v2 4/7 cpuacct: Add cpuacct_acount_field()

In reply to Li Zefan

So we can remove open-coded cpuacct code in cputime.c.

---
 kernel/sched/cpuacct.c | 23 +++++++++++++++++++++++
 kernel/sched/cpuacct.h |  6 ++++++
 kernel/sched/cputime.c | 18 +-----------------
 3 files changed, 30 insertions(+), 17 deletions(-)
kernel/sched/cpuacct.c +23 -0
index 48b5e91..72bd971 100644
@@ -218,6 +218,29 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 	rcu_read_unlock();
 }
 
+/*
+ * Add user/system time to cpuacct.
+ *
+ * Note: it's the caller that updates the account of the root cgroup.
+ */
+void cpuacct_account_field(struct task_struct *p, int index, u64 val)
+{
+	struct kernel_cpustat *kcpustat;
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))
+		return;
+
+	rcu_read_lock();
+	ca = task_ca(p);
+	while (ca && (ca != &root_cpuacct)) {
+		kcpustat = this_cpu_ptr(ca->cpustat);
+		kcpustat->cpustat[index] += val;
+		ca = parent_ca(ca);
+	}
+	rcu_read_unlock();
+}
+
 void __init cpuacct_init(void)
 {
 	root_cpuacct.cpustat = &kernel_cpustat;
kernel/sched/cpuacct.h +6 -0
index 551acd7..bd0409b 100644
@@ -43,6 +43,7 @@ static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 
 extern void cpuacct_init(void);
 extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
+extern void cpuacct_account_field(struct task_struct *p, int index, u64 val);
 
 #else
 
@@ -54,4 +55,9 @@ static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
 }
 
+static inline void
+cpuacct_account_field(struct task_struct *p, int index, u64 val)
+{
+}
+
 #endif
kernel/sched/cputime.c +1 -17
index 024fe19..e82b82e 100644
@@ -115,10 +115,6 @@ static int irqtime_account_si_update(void)
 static inline void task_group_account_field(struct task_struct *p, int index,
 					    u64 tmp)
 {
-#ifdef CONFIG_CGROUP_CPUACCT
-	struct kernel_cpustat *kcpustat;
-	struct cpuacct *ca;
-#endif
 	/*
 	 * Since all updates are sure to touch the root cgroup, we
 	 * get ourselves ahead and touch it first. If the root cgroup
@@ -127,19 +123,7 @@ static inline void task_group_account_field(struct task_struct *p, int index,
 	 */
 	__get_cpu_var(kernel_cpustat).cpustat[index] += tmp;
 
-#ifdef CONFIG_CGROUP_CPUACCT
-	if (unlikely(!cpuacct_subsys.active))
-		return;
-
-	rcu_read_lock();
-	ca = task_ca(p);
-	while (ca && (ca != &root_cpuacct)) {
-		kcpustat = this_cpu_ptr(ca->cpustat);
-		kcpustat->cpustat[index] += tmp;
-		ca = parent_ca(ca);
-	}
-	rcu_read_unlock();
-#endif
+	cpuacct_account_field(p, index, tmp);
 }
 
 /*
Signature · 1 line
1.8.0.2
#8 lore raw

[tip:sched/core] sched/cpuacct: Add cpuacct_acount_field()

sched/cpuacct: Add cpuacct_acount_field()
Commit-ID:  1966aaf7d54608e8ddb7ac454b461840e763409a
Gitweb:     http://git.kernel.org/tip/1966aaf7d54608e8ddb7ac454b461840e763409a
Author:     Li Zefan <[email protected]>
AuthorDate: Fri, 29 Mar 2013 14:37:06 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 10 Apr 2013 13:54:17 +0200

sched/cpuacct: Add cpuacct_acount_field()

So we can remove open-coded cpuacct code in cputime.c.

---
 kernel/sched/cpuacct.c | 23 +++++++++++++++++++++++
 kernel/sched/cpuacct.h |  6 ++++++
 kernel/sched/cputime.c | 18 +-----------------
 3 files changed, 30 insertions(+), 17 deletions(-)
kernel/sched/cpuacct.c +23 -0
index 48b5e91..72bd971 100644
@@ -218,6 +218,29 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 	rcu_read_unlock();
 }
 
+/*
+ * Add user/system time to cpuacct.
+ *
+ * Note: it's the caller that updates the account of the root cgroup.
+ */
+void cpuacct_account_field(struct task_struct *p, int index, u64 val)
+{
+	struct kernel_cpustat *kcpustat;
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))
+		return;
+
+	rcu_read_lock();
+	ca = task_ca(p);
+	while (ca && (ca != &root_cpuacct)) {
+		kcpustat = this_cpu_ptr(ca->cpustat);
+		kcpustat->cpustat[index] += val;
+		ca = parent_ca(ca);
+	}
+	rcu_read_unlock();
+}
+
 void __init cpuacct_init(void)
 {
 	root_cpuacct.cpustat = &kernel_cpustat;
kernel/sched/cpuacct.h +6 -0
index 551acd7..bd0409b 100644
@@ -43,6 +43,7 @@ static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 
 extern void cpuacct_init(void);
 extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
+extern void cpuacct_account_field(struct task_struct *p, int index, u64 val);
 
 #else
 
@@ -54,4 +55,9 @@ static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 {
 }
 
+static inline void
+cpuacct_account_field(struct task_struct *p, int index, u64 val)
+{
+}
+
 #endif
kernel/sched/cputime.c +1 -17
index 699d597..33508dc 100644
@@ -115,10 +115,6 @@ static int irqtime_account_si_update(void)
 static inline void task_group_account_field(struct task_struct *p, int index,
 					    u64 tmp)
 {
-#ifdef CONFIG_CGROUP_CPUACCT
-	struct kernel_cpustat *kcpustat;
-	struct cpuacct *ca;
-#endif
 	/*
 	 * Since all updates are sure to touch the root cgroup, we
 	 * get ourselves ahead and touch it first. If the root cgroup
@@ -127,19 +123,7 @@ static inline void task_group_account_field(struct task_struct *p, int index,
 	 */
 	__get_cpu_var(kernel_cpustat).cpustat[index] += tmp;
 
-#ifdef CONFIG_CGROUP_CPUACCT
-	if (unlikely(!cpuacct_subsys.active))
-		return;
-
-	rcu_read_lock();
-	ca = task_ca(p);
-	while (ca && (ca != &root_cpuacct)) {
-		kcpustat = this_cpu_ptr(ca->cpustat);
-		kcpustat->cpustat[index] += tmp;
-		ca = parent_ca(ca);
-	}
-	rcu_read_unlock();
-#endif
+	cpuacct_account_field(p, index, tmp);
 }
 
 /*
author
#9 lore raw

PATCH v2 5/7 cpuacct: Remove redundant NULL checks in cpuacct_charge()

In reply to Li Zefan

This is a micro optimization for the hot path.

- We don't need to check if @ca is NULL in parent_ca().
- We don't need to check if @ca is NULL in the beginning of the for loop.

---
 kernel/sched/cpuacct.c | 6 +++++-
 kernel/sched/cpuacct.h | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
kernel/sched/cpuacct.c +5 -1
index 72bd971..b2aaaba 100644
@@ -210,9 +210,13 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 
 	ca = task_ca(tsk);
 
-	for (; ca; ca = parent_ca(ca)) {
+	while (true) {
 		u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
 		*cpuusage += cputime;
+
+		ca = parent_ca(ca);
+		if (!ca)
+			break;
 	}
 
 	rcu_read_unlock();
kernel/sched/cpuacct.h +1 -1
index bd0409b..45c1682 100644
@@ -36,7 +36,7 @@ static inline struct cpuacct *task_ca(struct task_struct *tsk)
 
 static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 {
-	if (!ca || !ca->css.cgroup->parent)
+	if (!ca->css.cgroup->parent)
 		return NULL;
 	return cgroup_ca(ca->css.cgroup->parent);
 }
Signature · 1 line
1.8.0.2
#10 lore raw

[tip:sched/core] sched/cpuacct: Remove redundant NULL checks in cpuacct_charge()

sched/cpuacct: Remove redundant NULL checks in cpuacct_charge()
Commit-ID:  543bc0e76e6bb84300eaf9833edc5a481f788678
Gitweb:     http://git.kernel.org/tip/543bc0e76e6bb84300eaf9833edc5a481f788678
Author:     Li Zefan <[email protected]>
AuthorDate: Fri, 29 Mar 2013 14:37:29 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 10 Apr 2013 13:54:18 +0200

sched/cpuacct: Remove redundant NULL checks in cpuacct_charge()

This is a micro optimization for the hot path.

- We don't need to check if @ca is NULL in parent_ca().
- We don't need to check if @ca is NULL in the beginning of the for loop.

---
 kernel/sched/cpuacct.c | 6 +++++-
 kernel/sched/cpuacct.h | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
kernel/sched/cpuacct.c +5 -1
index 72bd971..b2aaaba 100644
@@ -210,9 +210,13 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime)
 
 	ca = task_ca(tsk);
 
-	for (; ca; ca = parent_ca(ca)) {
+	while (true) {
 		u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
 		*cpuusage += cputime;
+
+		ca = parent_ca(ca);
+		if (!ca)
+			break;
 	}
 
 	rcu_read_unlock();
kernel/sched/cpuacct.h +1 -1
index bd0409b..45c1682 100644
@@ -36,7 +36,7 @@ static inline struct cpuacct *task_ca(struct task_struct *tsk)
 
 static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 {
-	if (!ca || !ca->css.cgroup->parent)
+	if (!ca->css.cgroup->parent)
 		return NULL;
 	return cgroup_ca(ca->css.cgroup->parent);
 }
author
#11 lore raw

PATCH v2 6/7 cpuacct: Remove redundant NULL checks in cpuacct_acount_field()

In reply to Li Zefan

This is a micro optimazation for a hot path.

- We don't need to check if @ca returned from task_ca() is NULL.
- We don't need to check if @ca returned from parent_ca() is NULL.

---
 kernel/sched/cpuacct.c | 4 ++--
 kernel/sched/cpuacct.h | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)
kernel/sched/cpuacct.c +2 -2
index b2aaaba..071ae8d 100644
@@ -237,10 +237,10 @@ void cpuacct_account_field(struct task_struct *p, int index, u64 val)
 
 	rcu_read_lock();
 	ca = task_ca(p);
-	while (ca && (ca != &root_cpuacct)) {
+	while (ca != &root_cpuacct) {
 		kcpustat = this_cpu_ptr(ca->cpustat);
 		kcpustat->cpustat[index] += val;
-		ca = parent_ca(ca);
+		ca = __parent_ca(ca);
 	}
 	rcu_read_unlock();
 }
kernel/sched/cpuacct.h +5 -0
index 45c1682..b2f79ad 100644
@@ -34,6 +34,11 @@ static inline struct cpuacct *task_ca(struct task_struct *tsk)
 			    struct cpuacct, css);
 }
 
+static inline struct cpuacct *__parent_ca(struct cpuacct *ca)
+{
+	return cgroup_ca(ca->css.cgroup->parent);
+}
+
 static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 {
 	if (!ca->css.cgroup->parent)
Signature · 1 line
1.8.0.2
#12 lore raw

[tip:sched/core] sched/cpuacct: Remove redundant NULL checks in cpuacct_acount_field()

sched/cpuacct: Remove redundant NULL checks in cpuacct_acount_field()
Commit-ID:  5f40d804325e925409907e29f46ecb012090b6c2
Gitweb:     http://git.kernel.org/tip/5f40d804325e925409907e29f46ecb012090b6c2
Author:     Li Zefan <[email protected]>
AuthorDate: Fri, 29 Mar 2013 14:37:43 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 10 Apr 2013 13:54:18 +0200

sched/cpuacct: Remove redundant NULL checks in cpuacct_acount_field()

This is a micro optimazation for a hot path.

- We don't need to check if @ca returned from task_ca() is NULL.
- We don't need to check if @ca returned from parent_ca() is NULL.

---
 kernel/sched/cpuacct.c | 4 ++--
 kernel/sched/cpuacct.h | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)
kernel/sched/cpuacct.c +2 -2
index b2aaaba..071ae8d 100644
@@ -237,10 +237,10 @@ void cpuacct_account_field(struct task_struct *p, int index, u64 val)
 
 	rcu_read_lock();
 	ca = task_ca(p);
-	while (ca && (ca != &root_cpuacct)) {
+	while (ca != &root_cpuacct) {
 		kcpustat = this_cpu_ptr(ca->cpustat);
 		kcpustat->cpustat[index] += val;
-		ca = parent_ca(ca);
+		ca = __parent_ca(ca);
 	}
 	rcu_read_unlock();
 }
kernel/sched/cpuacct.h +5 -0
index 45c1682..b2f79ad1 100644
@@ -34,6 +34,11 @@ static inline struct cpuacct *task_ca(struct task_struct *tsk)
 			    struct cpuacct, css);
 }
 
+static inline struct cpuacct *__parent_ca(struct cpuacct *ca)
+{
+	return cgroup_ca(ca->css.cgroup->parent);
+}
+
 static inline struct cpuacct *parent_ca(struct cpuacct *ca)
 {
 	if (!ca->css.cgroup->parent)
author
#13 lore raw

PATCH v2 7/7 cpuacct: Clean up cpuacct.h

In reply to Li Zefan

Now most of the code in cpuacct.h can be moved to cpuacct.c

---
 kernel/sched/cpuacct.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
 kernel/sched/cpuacct.h | 46 ----------------------------------------------
 2 files changed, 43 insertions(+), 47 deletions(-)
kernel/sched/cpuacct.c +43 -1
index 071ae8d..9305fd2 100644
@@ -16,7 +16,49 @@
  * ([email protected]).
  */
 
-struct cpuacct root_cpuacct;
+/* Time spent by the tasks of the cpu accounting group executing in ... */
+enum cpuacct_stat_index {
+	CPUACCT_STAT_USER,	/* ... user mode */
+	CPUACCT_STAT_SYSTEM,	/* ... kernel mode */
+
+	CPUACCT_STAT_NSTATS,
+};
+
+/* track cpu usage of a group of tasks and its child groups */
+struct cpuacct {
+	struct cgroup_subsys_state css;
+	/* cpuusage holds pointer to a u64-type object on every cpu */
+	u64 __percpu *cpuusage;
+	struct kernel_cpustat __percpu *cpustat;
+};
+
+/* return cpu accounting group corresponding to this container */
+static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
+{
+	return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
+			    struct cpuacct, css);
+}
+
+/* return cpu accounting group to which this task belongs */
+static inline struct cpuacct *task_ca(struct task_struct *tsk)
+{
+	return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
+			    struct cpuacct, css);
+}
+
+static inline struct cpuacct *__parent_ca(struct cpuacct *ca)
+{
+	return cgroup_ca(ca->css.cgroup->parent);
+}
+
+static inline struct cpuacct *parent_ca(struct cpuacct *ca)
+{
+	if (!ca->css.cgroup->parent)
+		return NULL;
+	return cgroup_ca(ca->css.cgroup->parent);
+}
+
+static struct cpuacct root_cpuacct;
 
 /* create a new cpu accounting group */
 static struct cgroup_subsys_state *cpuacct_css_alloc(struct cgroup *cgrp)
kernel/sched/cpuacct.h +0 -46

51 lines not on this page · raw message

Signature · 1 line
1.8.0.2
#14 lore raw

[tip:sched/core] sched/cpuacct: Clean up cpuacct.h

sched/cpuacct: Clean up cpuacct.h
Commit-ID:  d1712796a880bea0a44739941116001923f3275b
Gitweb:     http://git.kernel.org/tip/d1712796a880bea0a44739941116001923f3275b
Author:     Li Zefan <[email protected]>
AuthorDate: Fri, 29 Mar 2013 14:38:13 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 10 Apr 2013 13:54:19 +0200

sched/cpuacct: Clean up cpuacct.h

Now most of the code in cpuacct.h can be moved to cpuacct.c

---
 kernel/sched/cpuacct.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
 kernel/sched/cpuacct.h | 46 ----------------------------------------------
 2 files changed, 43 insertions(+), 47 deletions(-)
kernel/sched/cpuacct.c +43 -1
index 071ae8d..9305fd2 100644
@@ -16,7 +16,49 @@
  * ([email protected]).
  */
 
-struct cpuacct root_cpuacct;
+/* Time spent by the tasks of the cpu accounting group executing in ... */
+enum cpuacct_stat_index {
+	CPUACCT_STAT_USER,	/* ... user mode */
+	CPUACCT_STAT_SYSTEM,	/* ... kernel mode */
+
+	CPUACCT_STAT_NSTATS,
+};
+
+/* track cpu usage of a group of tasks and its child groups */
+struct cpuacct {
+	struct cgroup_subsys_state css;
+	/* cpuusage holds pointer to a u64-type object on every cpu */
+	u64 __percpu *cpuusage;
+	struct kernel_cpustat __percpu *cpustat;
+};
+
+/* return cpu accounting group corresponding to this container */
+static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
+{
+	return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
+			    struct cpuacct, css);
+}
+
+/* return cpu accounting group to which this task belongs */
+static inline struct cpuacct *task_ca(struct task_struct *tsk)
+{
+	return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
+			    struct cpuacct, css);
+}
+
+static inline struct cpuacct *__parent_ca(struct cpuacct *ca)
+{
+	return cgroup_ca(ca->css.cgroup->parent);
+}
+
+static inline struct cpuacct *parent_ca(struct cpuacct *ca)
+{
+	if (!ca->css.cgroup->parent)
+		return NULL;
+	return cgroup_ca(ca->css.cgroup->parent);
+}
+
+static struct cpuacct root_cpuacct;
 
 /* create a new cpu accounting group */
 static struct cgroup_subsys_state *cpuacct_css_alloc(struct cgroup *cgrp)
kernel/sched/cpuacct.h +0 -46

51 lines not on this page · raw message

maintainer Acked-by on the v2 series
#15 lore raw
In reply to Li Zefan
Quoted 4 lines · Li Zefan

- This patchset splits cpuacct out of core scheduler code.
- Plus two small optimizations.

Quoted 8 lines
---
 kernel/sched/Makefile  |   1 +
 kernel/sched/core.c    | 228 +----------------------------------------------
 kernel/sched/cpuacct.c | 303 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/cpuacct.h |  22 +++++
 kernel/sched/cputime.c |  18 +---
 kernel/sched/sched.h   |  48 +---------
 6 files changed, 330 insertions(+), 290 deletions(-)

Looks good from a quick glance, thanks for doing this.

#16 lore raw
Quoted 15 lines · Peter Zijlstra
Quoted 4 lines · Li Zefan

- This patchset splits cpuacct out of core scheduler code.
- Plus two small optimizations.

Quoted 8 lines
---
 kernel/sched/Makefile  |   1 +
 kernel/sched/core.c    | 228 +----------------------------------------------
 kernel/sched/cpuacct.c | 303 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/cpuacct.h |  22 +++++
 kernel/sched/cputime.c |  18 +---
 kernel/sched/sched.h   |  48 +---------
 6 files changed, 330 insertions(+), 290 deletions(-)
Quoted 3 lines

Looks good from a quick glance, thanks for doing this.

Looks good to me too, so I added it to tip:sched/core.

The only breakage it cased was on UML:

home/mingo/tip/kernel/sched/cpuacct.c:94:2: error: implicit declaration of function 'ERR_PTR' [-Werror=implicit-function-declaration]

I suspect it's a previously hidden header file dependency assumption and the patch below will fix it.

Thanks,

Ingo

#17 lore raw
Quoted 24 lines · Ingo Molnar
Quoted 15 lines · Peter Zijlstra
Quoted 4 lines · Li Zefan

- This patchset splits cpuacct out of core scheduler code.
- Plus two small optimizations.

Quoted 8 lines
---
 kernel/sched/Makefile  |   1 +
 kernel/sched/core.c    | 228 +----------------------------------------------
 kernel/sched/cpuacct.c | 303 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/cpuacct.h |  22 +++++
 kernel/sched/cputime.c |  18 +---
 kernel/sched/sched.h   |  48 +---------
 6 files changed, 330 insertions(+), 290 deletions(-)
Quoted 3 lines

Looks good from a quick glance, thanks for doing this.

Looks good to me too, so I added it to tip:sched/core.

Quoted 7 lines

The only breakage it cased was on UML:

home/mingo/tip/kernel/sched/cpuacct.c:94:2: error: implicit declaration of function 'ERR_PTR' [-Werror=implicit-function-declaration]

I suspect it's a previously hidden header file dependency assumption and the patch below will fix it.

kernel/sched/cpuacct.c +1 -0
index ef57ab6..dbb7e2c 100644
@@ -6,6 +6,7 @@
 #include <linux/seq_file.h>
 #include <linux/rcupdate.h>
 #include <linux/kernel_stat.h>
+#include <linux/err.h>
 
 #include "sched.h"