Skip to contentSkip to conversation

Trace idle entry and exit times

PATCH In review Andi Kleen 2 participants 5 messages Dec 7, 2013

Subsystems (2)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/1 Trace idle entry and exit times

From: Andi Kleen <[email protected]>

This patch creates 4 new trace points: one at the beginning of idle, one before entering mwait, one after exiting mwait, and one after finishing the idle code. This is useful to trace down any additional latencies caused by the ever-growing idle code.

Idle latencies are a common source of performance problems, so it's important to be able to measure them.

I moved mwait out of line to avoid a nasty include loop.

To use:

trace-cmd record -e power:cpu_idle_end -e power:cpu_idle_start -e power:cpu_idle_mwait_end -e power:cpu_idle_mwait_start run workload
Ctrl-C
trace-cmd report

Compute per CPU intervals between power:cpu_idle_start and power:cpu_idle_mwait_start to get the entry latency

and

power:cpu_idle_mwait_end and power:cpu_idle_end
to get the exit latency (likely more important)

---
 arch/x86/include/asm/processor.h |  7 +------
 arch/x86/kernel/process.c        |  9 +++++++++
 include/trace/events/power.h     | 30 ++++++++++++++++++++++++++++++
 kernel/cpu/idle.c                |  3 +++
 4 files changed, 43 insertions(+), 6 deletions(-)
arch/x86/include/asm/processor.h +1 -6
index 7b034a4..22deb08 100644
@@ -708,12 +708,7 @@ static inline void __monitor(const void *eax, unsigned long ecx,
 		     :: "a" (eax), "c" (ecx), "d"(edx));
 }
 
-static inline void __mwait(unsigned long eax, unsigned long ecx)
-{
-	/* "mwait %eax, %ecx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xc9;"
-		     :: "a" (eax), "c" (ecx));
-}
+extern void __mwait(unsigned long eax, unsigned long ecx);
 
 static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
 {
arch/x86/kernel/process.c +9 -0
index 3fb8d95..9083e34 100644
@@ -469,3 +469,12 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
 	return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
 }
 
+void __mwait(unsigned long eax, unsigned long ecx)
+{
+	trace_cpu_idle_mwait_start(0);
+	/* "mwait %eax, %ecx;" */
+	asm volatile(".byte 0x0f, 0x01, 0xc9;"
+		     :: "a" (eax), "c" (ecx));
+	trace_cpu_idle_mwait_end(0);
+}
+EXPORT_SYMBOL(__mwait);
include/trace/events/power.h +30 -0
index cda100d..71261a7 100644
@@ -35,6 +35,36 @@ DEFINE_EVENT(cpu, cpu_idle,
 	TP_ARGS(state, cpu_id)
 );
 
+/* Dummy argument to work around ftrace's lack of no argument support */
+
+TRACE_EVENT(cpu_idle_start,
+	TP_PROTO(int x),
+	TP_ARGS(x),
+	TP_STRUCT__entry(__array(int, x, 0)),
+	TP_fast_assign((void)(x)),
+	TP_printk("%s", ""))
+
+TRACE_EVENT(cpu_idle_mwait_start,
+	TP_PROTO(int x),
+	TP_ARGS(x),
+	TP_STRUCT__entry(__array(int, x, 0)),
+	TP_fast_assign((void)(x)),
+	TP_printk("%s", ""));
+
+TRACE_EVENT(cpu_idle_mwait_end,
+	TP_PROTO(int x),
+	TP_ARGS(x),
+	TP_STRUCT__entry(__array(int, x, 0)),
+	TP_fast_assign((void)(x)),
+	TP_printk("%s", ""));
+
+TRACE_EVENT(cpu_idle_end,
+	TP_PROTO(int x),
+	TP_ARGS(x),
+	TP_STRUCT__entry(__array(int, x, 0)),
+	TP_fast_assign((void)(x)),
+	TP_printk("%s", ""));
+
 /* This file can get included multiple times, TRACE_HEADER_MULTI_READ at top */
 #ifndef _PWR_EVENT_AVOID_DOUBLE_DEFINING
 #define _PWR_EVENT_AVOID_DOUBLE_DEFINING
kernel/cpu/idle.c +3 -0
index 988573a..1cf1676 100644
@@ -6,6 +6,7 @@
 #include <linux/tick.h>
 #include <linux/mm.h>
 #include <linux/stackprotector.h>
+#include <trace/events/power.h>
 
 #include <asm/tlb.h>
 
@@ -68,6 +69,7 @@ void __weak arch_cpu_idle(void)
 static void cpu_idle_loop(void)
 {
 	while (1) {
+		trace_cpu_idle_start(0);
 		tick_nohz_idle_enter();
 
 		while (!need_resched()) {
@@ -114,6 +116,7 @@ static void cpu_idle_loop(void)
 				set_preempt_need_resched();
 		}
 		tick_nohz_idle_exit();
+		trace_cpu_idle_end(0);
 		schedule_preempt_disabled();
 	}
 }
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 9 lines · Andi Kleen

From: Andi Kleen <[email protected]>

This patch creates 4 new trace points: one at the beginning of idle, one before entering mwait, one after exiting mwait, and one after finishing the idle code. This is useful to trace down any additional latencies caused by the ever-growing idle code.

Idle latencies are a common source of performance problems, so it's important to be able to measure them.

You mean stuff like.. oh, say 60 cores doing zero work every 12ms munching ~20% cpu?

-  97.57%  [kernel]          [k] _raw_spin_lock_irqsave                                                                                                                                                                                    ▒
   - _raw_spin_lock_irqsave                                                                                                                                                                                                                ▒
      - 97.02% clockevents_notify                                                                                                                                                                                                          ◆
           intel_idle                                                                                                                                                                                                                      ▒
           cpuidle_idle_call                                                                                                                                                                                                               ▒
           cpu_idle                                                                                                                                                                                                                        ▒
      - 2.97% tick_broadcast_oneshot_control                                                                                                                                                                                               ▒
           tick_notify                                                                                                                                                                                                                     ▒
           notifier_call_chain                                                                                                                                                                                                             ▒
           clockevents_notify                                                                                                                                                                                                              ▒
           intel_idle                                                                                                                                                                                                                      ▒
           cpuidle_idle_call                                                                                                                                                                                                               ▒

cpu_idle

C2 is super green, it's hug'n the sh*t outta them trees :)

-Mike

author
#2 lore raw
Quoted 8 lines · Mike Galbraith
Quoted 7 lines · Andi Kleen

From: Andi Kleen <[email protected]>

This patch creates 4 new trace points: one at the beginning of idle, one before entering mwait, one after exiting mwait, and one after finishing the idle code. This is useful to trace down any additional latencies caused by the ever-growing idle code.

Quoted 5 lines
Quoted 2 lines

Idle latencies are a common source of performance problems, so it's important to be able to measure them.

You mean stuff like.. oh, say 60 cores doing zero work every 12ms munching ~20% cpu?

Well that one is obvious. I mean more the subtle problems.

BTW the timer problem is fixed on Westmere and later (always running APIC timer), but yes it's a problem before that. I looked at it some time ago, but eventually gave up after only fixing low hanging fruit. There are likely some more tricks that could be done here (see the old patches from back then)

-Andi

#3 lore raw
Quoted 9 lines · Andi Kleen
Quoted 8 lines · Mike Galbraith
Quoted 7 lines · Andi Kleen

From: Andi Kleen <[email protected]>

This patch creates 4 new trace points: one at the beginning of idle, one before entering mwait, one after exiting mwait, and one after finishing the idle code. This is useful to trace down any additional latencies caused by the ever-growing idle code.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Idle latencies are a common source of performance problems, so it's important to be able to measure them.

You mean stuff like.. oh, say 60 cores doing zero work every 12ms munching ~20% cpu?

Well that one is obvious. I mean more the subtle problems.

Yeah, not subtle. SGI could use C2 as power supply stress tester.

-Mike

author
#4 lore raw
Quoted 3 lines
Quoted 1 line

Well that one is obvious. I mean more the subtle problems.

Yeah, not subtle. SGI could use C2 as power supply stress tester.

This is only on system where the APIC timer does not tick in deep idle. At this point these are 5+ year old systems.

There were some ideas of breaking up that lock, but they were never fully implemented.

-Andi