mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [patch 1/2] cpuacct: refactoring cpuusage_read / cpuusage_write
@ 2008-12-16  6:02 Ken Chen
  2008-12-16 11:15 ` Ingo Molnar
  0 siblings, 1 reply; 2+ messages in thread
From: Ken Chen @ 2008-12-16  6:02 UTC (permalink / raw)
  To: Ingo Molnar, Andrew Morton, Paul Menage, Li Zefan
  Cc: Linux Kernel Mailing List

In the thread regarding to 'export percpu cpuacct cgroup stats'
http://lkml.org/lkml/2008/12/7/13

akpm pointed out that current cpuacct code is inefficient.  This patch
refactoring the following:

* make cpu_rq locking only on 32-bit
* change iterator to each_present_cpu instead of each_possible_cpu to
  make it hotplug friendly.

It's a bit of code churn, but I was rewarded with 160 byte code size saving
on x86-64 arch and zero code size change on i386.


Signed-off-by: Ken Chen <kenchen@google.com>

diff --git a/kernel/sched.c b/kernel/sched.c
index e4bb1dd..124bd7a 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -9302,6 +9302,41 @@ cpuacct_destroy
 	kfree(ca);
 }

+static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
+{
+	u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
+	u64 data;
+
+#ifndef CONFIG_64BIT
+	/*
+	 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
+	 */
+	spin_lock_irq(&cpu_rq(cpu)->lock);
+	data = *cpuusage;
+	spin_unlock_irq(&cpu_rq(cpu)->lock);
+#else
+	data = *cpuusage;
+#endif
+
+	return data;
+}
+
+static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
+{
+	u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
+
+#ifndef CONFIG_64BIT
+	/*
+	 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
+	 */
+	spin_lock_irq(&cpu_rq(cpu)->lock);
+	*cpuusage = val;
+	spin_unlock_irq(&cpu_rq(cpu)->lock);
+#else
+	*cpuusage = val;
+#endif
+}
+
 /* return total cpu usage (in nanoseconds) of a group */
 static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
 {
@@ -9309,17 +9344,8 @@ static u64 cpuusage_read
 	u64 totalcpuusage = 0;
 	int i;

-	for_each_possible_cpu(i) {
-		u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
-
-		/*
-		 * Take rq->lock to make 64-bit addition safe on 32-bit
-		 * platforms.
-		 */
-		spin_lock_irq(&cpu_rq(i)->lock);
-		totalcpuusage += *cpuusage;
-		spin_unlock_irq(&cpu_rq(i)->lock);
-	}
+	for_each_present_cpu(i)
+		totalcpuusage += cpuacct_cpuusage_read(ca, i);

 	return totalcpuusage;
 }
@@ -9336,13 +9362,9 @@ static int cpuusage_write
 		goto out;
 	}

-	for_each_possible_cpu(i) {
-		u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
+	for_each_present_cpu(i)
+		cpuacct_cpuusage_write(ca, i, 0);

-		spin_lock_irq(&cpu_rq(i)->lock);
-		*cpuusage = 0;
-		spin_unlock_irq(&cpu_rq(i)->lock);
-	}
 out:
 	return err;
 }

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [patch 1/2] cpuacct: refactoring cpuusage_read / cpuusage_write
  2008-12-16  6:02 [patch 1/2] cpuacct: refactoring cpuusage_read / cpuusage_write Ken Chen
@ 2008-12-16 11:15 ` Ingo Molnar
  0 siblings, 0 replies; 2+ messages in thread
From: Ingo Molnar @ 2008-12-16 11:15 UTC (permalink / raw)
  To: Ken Chen; +Cc: Andrew Morton, Paul Menage, Li Zefan, Linux Kernel Mailing List


* Ken Chen <kenchen@google.com> wrote:

> In the thread regarding to 'export percpu cpuacct cgroup stats'
> http://lkml.org/lkml/2008/12/7/13
> 
> akpm pointed out that current cpuacct code is inefficient.  This patch
> refactoring the following:
> 
> * make cpu_rq locking only on 32-bit
> * change iterator to each_present_cpu instead of each_possible_cpu to
>   make it hotplug friendly.
> 
> It's a bit of code churn, but I was rewarded with 160 byte code size saving
> on x86-64 arch and zero code size change on i386.

applied to tip/sched/core, thanks Ken!

	Ingo

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-12-16 11:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-16  6:02 [patch 1/2] cpuacct: refactoring cpuusage_read / cpuusage_write Ken Chen
2008-12-16 11:15 ` Ingo Molnar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®