Skip to contentSkip to conversation

Negative values in /proc/latency_stats

DISCUSSION Corrado Zoccolo 5 participants 9 messages Jan 31, 2009 – Feb 6, 2009

lore
Share & tools
lore
#0 lore raw

Hello,
I found negative numbers sometimes appear in /proc/latency_stats (vanilla kernel 2.6.29-rc3, on x86 cpu, configuration attached)

[corrado@localhost ~]$ while sleep 1; do grep -- - /proc/latency_stats

Quoted 1 line
Quoted 1 line

neg_stats; done

^Z
[1]+ Stopped sleep 1
[corrado@localhost ~]$ cat neg_stats
1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call 1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call 1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call

I suspect this can be the cause for
https://bugs.launchpad.net/ubuntu/+source/latencytop/+bug/297776 , as I saw it happening on my machine during a kernel recompilation.

Corrado

#1 lore raw
Quoted 3 lines · Corrado Zoccolo

Hello,
I found negative numbers sometimes appear in /proc/latency_stats (vanilla kernel 2.6.29-rc3, on x86 cpu, configuration attached)

I'm seeing this back in 2.6.28 on an AMD64 X2 (64 bit) as well, or at least the enormous latency numbers in latencytop.

Config attached.

#2 lore raw
In reply to Corrado Zoccolo
Quoted 14 lines · On Sat, 31 Jan 2009 14:42:20 +0100 Corrado Zoccolo <[email protected]> wrote:

Hello,
I found negative numbers sometimes appear in /proc/latency_stats (vanilla kernel 2.6.29-rc3, on x86 cpu, configuration attached)

[corrado@localhost ~]$ while sleep 1; do grep -- - /proc/latency_stats

Quoted 1 line
Quoted 1 line

neg_stats; done

^Z
[1]+ Stopped sleep 1
[corrado@localhost ~]$ cat neg_stats
1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call 1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call 1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call

Quoted 4 lines

I suspect this can be the cause for
https://bugs.launchpad.net/ubuntu/+source/latencytop/+bug/297776 , as I saw it happening on my machine during a kernel recompilation.

<discovers kernel/latencytop.c>

- It implements a kernel/userspace interface yet it has zero documentation.

- It was committed with a 2-line changelog which tells us practically nothing.

- It implements an up-to-1536-loops loop followed by an up-to-384-loops loop on a scheduler hotpath.

All under spin_lock_irqsave()!

- store_stacktrace() unnecessarily initalises trace.skip.

- account_scheduler_latency() should be an inline:

	if (unlikely(latencytop_enabled))
		__account_scheduler_latency(...);

- ditto clear_all_latency_tracing()

- it's schizophrenic in its placement of spaces around semicolons in `for' statements.

- it seems to only be implemented if CONFIG_FAIR_GROUP_SCHED=y.

- lstats_fops should be const.

And it emits negative numbers too ;)

#3 lore raw

On Mon, 2 Feb 2009 20:55:45 -0800
Andrew Morton <[email protected]> wrote:

[ I'm on a plane and writing a patch to fix some of the issues you mention; I need to be tethered to test before sending]

Quoted 5 lines

- It implements an up-to-1536-loops loop followed by an up-to-384-loops loop on a scheduler hotpath.

All under spin_lock_irqsave()!

while you mention the theoretical worst case scenario numbers, the reality isn't this bleak. it's a linear array that stores a chain, and yes, the chain is searched lazy. but... the chain is stored "most unique -> least unique", so typical complexity is a lot lower than what you mention. And it is also only on when you are actually actively tracing.

Quoted 2 lines

- store_stacktrace() unnecessarily initalises trace.skip.

fixed in patch

Quoted 5 lines

- account_scheduler_latency() should be an inline:

	if (unlikely(latencytop_enabled))
		__account_scheduler_latency(...);

borderline but fair; function calls are cheap (or even free in some cases) but this one is in such a hotpath that this optimization makes some sense.

Quoted 2 lines

- ditto clear_all_latency_tracing()

this one is only called in slowpaths, so I wouldn't think this makes sense.

Quoted 3 lines

- it's schizophrenic in its placement of spaces around semicolons in `for' statements.

fixed in patch

Quoted 2 lines

- it seems to only be implemented if CONFIG_FAIR_GROUP_SCHED=y.

this is a misunderstanding; sched_fair.c is not related to the fair GROUP scheduler. It is the normal scheduler policy.

Quoted 2 lines

- lstats_fops should be const.

fixed

Quoted 1 line

And it emits negative numbers too ;)

the problem with this one is that the numbers probably really ARE negative... (but i need to test some more)
If time goes backwards during idle, the sleep time is considered negative.... which I can understand will confuse humans. The alternative possibility would be that we had a several-minutes latency, which I consider unlikely.... the worst latency I've seen under normal load is in the 10 second range, not the hundreds-of-seconds or thousands-of-seconds range. And you'd think people would notice such latencies and complain.

I'll see if I can just discard the "time goes backwards" cases rather than propagating the insanity to userspace.

Signature · 3 lines
Arjan van de Ven 	Intel Open Source Technology Centre
For development, discussion and tips for power savings, 
visit http://www.lesswatts.org
#4 lore raw
In reply to Andrew Morton
Quoted 18 lines · On Mon, 2 Feb 2009 20:55:45 -0800 Andrew Morton <[email protected]>…
Quoted 15 lines · On Sat, 31 Jan 2009 14:42:20 +0100 Corrado Zoccolo <[email protected]> wrote:

Hello,
I found negative numbers sometimes appear in /proc/latency_stats (vanilla kernel 2.6.29-rc3, on x86 cpu, configuration attached)

[corrado@localhost ~]$ while sleep 1; do grep --
- /proc/latency_stats

Quoted 1 line
Quoted 1 line

neg_stats; done

^Z
[1]+ Stopped sleep 1
[corrado@localhost ~]$ cat neg_stats
1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call 1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call 1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call

Quoted 6 lines
Quoted 4 lines

I suspect this can be the cause for
https://bugs.launchpad.net/ubuntu/+source/latencytop/+bug/297776 , as I saw it happening on my machine during a kernel recompilation.

<discovers kernel/latencytop.c>

how about this?

Quoted 1 line

From 4384fb13a27994949a75a31a8bbd2c8be10a2a2e Mon Sep 17 00:00:00 2001

From: Arjan van de Ven <[email protected]>
Date: Tue, 3 Feb 2009 16:12:48 -0800
Subject: [PATCH] latencytop: incorporate review feedback from Andrew Morton

Andrew had some suggestions for the latencytop file; this patch takes care of most of these:
* Add documentation
* Turn account_scheduler_latency into an inline function
* Don't report negative values to userspace
* Make the file operations struct const
* Fix a few checkpatch.pl warnings

---
 include/linux/latencytop.h |   10 +++++-
 kernel/latencytop.c        |   83 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 80 insertions(+), 13 deletions(-)
include/linux/latencytop.h +9 -1
index 901c2d6..b0e9989 100644
@@ -9,6 +9,7 @@
 #ifndef _INCLUDE_GUARD_LATENCYTOP_H_
 #define _INCLUDE_GUARD_LATENCYTOP_H_
 
+#include <linux/compiler.h>
 #ifdef CONFIG_LATENCYTOP
 
 #define LT_SAVECOUNT		32
@@ -24,7 +25,14 @@ struct latency_record {
 
 struct task_struct;
 
-void account_scheduler_latency(struct task_struct *task, int usecs, int inter);
+extern int latencytop_enabled;
+void __account_scheduler_latency(struct task_struct *task, int usecs, int inter);
+static inline void
+account_scheduler_latency(struct task_struct *task, int usecs, int inter)
+{
+	if (unlikely(latencytop_enabled))
+		__account_scheduler_latency(task, usecs, inter);
+}
 
 void clear_all_latency_tracing(struct task_struct *p);
 
kernel/latencytop.c +71 -12

145 lines not on this page · raw message

Signature · 7 lines
1.6.0.6


-- 
Arjan van de Ven 	Intel Open Source Technology Centre
For development, discussion and tips for power savings, 
visit http://www.lesswatts.org
#5 lore raw
Quoted 21 lines · On Tue, 3 Feb 2009 16:16:48 -0800 Arjan van de Ven <[email protected]> wrote:
Quoted 18 lines · On Mon, 2 Feb 2009 20:55:45 -0800 Andrew Morton <[email protected]>…
Quoted 15 lines · On Sat, 31 Jan 2009 14:42:20 +0100 Corrado Zoccolo <[email protected]> wrote:

Hello,
I found negative numbers sometimes appear in /proc/latency_stats (vanilla kernel 2.6.29-rc3, on x86 cpu, configuration attached)

[corrado@localhost ~]$ while sleep 1; do grep --
- /proc/latency_stats

Quoted 1 line
Quoted 1 line

neg_stats; done

^Z
[1]+ Stopped sleep 1
[corrado@localhost ~]$ cat neg_stats
1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call 1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call 1 -486373534 -486373534 sys_rt_sigsuspend sysenter_do_call

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

I suspect this can be the cause for
https://bugs.launchpad.net/ubuntu/+source/latencytop/+bug/297776 , as I saw it happening on my machine during a kernel recompilation.

<discovers kernel/latencytop.c>

how about this?

lavish ;)

Quoted 11 lines

...

+/*
+ * CONFIG_LATENCYTOP enables a kernel latency tracking infrastructure that is
+ * used by the "latencytop" userspace tool. The latency that is tracked is not
+ * the 'traditional' interrupt latency (which is primarily caused by something
+ * else consuming CPU), but instead, it is the latency an application encounters
+ * because the kernel sleeps on its behalf for various reasons.
+ *
+ * This code tracks 2 levels of statistics:
+ * 1) System level latency

Quoted 8 lines

+ * 2) Per process latency
+ *
+ * The latency is stored in fixed sized datastructures in an accumulated form;
+ * if the "same" latency cause is hit twice, this will be tracked as one entry
+ * in the datastructure. Both the count, total accumulated latency and maximum
+ * latency are tracked in this data structure. When the fixed size structure is
+ * full, no new causes are tracked until the buffer is flushed by writing to
+ * the /proc file; the userspace tool does this on a regular basis.

s/datastructure/data structure/

Quoted 9 lines

+ * A latency cause is identified by a stringified backtrace at the point that
+ * the scheduler gets invoked. The userland tool will use this string to
+ * identify the cause of the latency in human readable form.
+ *
+ * The information is exported via /proc/latency_stats and /proc/<pid>/latency.
+ * These files look like this:
+ *
+ * Latency Top version : v0.1
+ * 70 59433 4897 i915_irq_wait drm_ioctl vfs_ioctl do_vfs_ioctl sys_ioctl

Quoted 8 lines
+ * |    |    |    |
+ * |    |    |    +----> the stringified backtrace
+ * |    |    +---------> The maximum latency for this entry in microseconds
+ * |    +--------------> The accumulated latency for this entry (microseconds)

+ * +-------------------> The number of times this entry is hit
+ *
+ * (note: the average latency is the acummulated latency deviced by the number
+ * of times)

I was surprised at this. Is the stack backtracer sufficiently reliable/repeatable for this to work?

Quoted 2 lines

+ * Negative latencies (caused by time going backwards) are also explicitly
+ * skipped.

Are we sure that this was the cause of the problem?

Which time source is the caller using here, and why is it going backwards?

#6 lore raw

On Tue, 3 Feb 2009 21:46:09 -0800

Quoted 6 lines
Quoted 3 lines

+ * (note: the average latency is the acummulated latency deviced by the number
+ * of times)

I was surprised at this. Is the stack backtracer sufficiently reliable/repeatable for this to work?

yep it is, at least on x86 and x86-64.

Quoted 6 lines
Quoted 4 lines

+ * Negative latencies (caused by time going backwards) are also explicitly
+ * skipped.

Are we sure that this was the cause of the problem?

not 100%, but if you add only positive numbers..... the only other option is an overflow.. but that would be such a huge latency that that is not very believable.

Quoted 3 lines

Which time source is the caller using here, and why is it going backwards?

scheduler clock.

Signature · 3 lines
Arjan van de Ven 	Intel Open Source Technology Centre
For development, discussion and tips for power savings, 
visit http://www.lesswatts.org
#7 lore raw
Quoted 6 lines · Arjan van de Ven

On Tue, 3 Feb 2009 21:46:09 -0800

Quoted 4 lines
Quoted 3 lines

+ * Negative latencies (caused by time going backwards) are also explicitly
+ * skipped.

Quoted 6 lines
Quoted 1 line

Are we sure that this was the cause of the problem?

not 100%, but if you add only positive numbers..... the only other option is an overflow.. but that would be such a huge latency that that is not very believable.

I can confirm that those huge latencies were not real. I was using the machine, and it was responsive when this was measured. Maybe I can suggest an other option.. To measure a latency, you have to set up a variable when you enter a wait state, and compute the difference when you exit the waiting. If you forget to initialize the variable in one of the paths leading to the waiting, when you do the difference, you can end up with negative (or more generally, meaningless) numbers.
Anyway, skipping these numbers is surely better than propagating them to userspace, if the cause cannot be found.

An other random thought: since tracing can have noticeable overheads, as Ingo noticed, and is likely that distros will ship with various tracers enabled in the kernel, I think the scheme used to control them should be more robust.
Currently, you can enable/disable them by writing 1/0 in a pseudo-file under /proc, but what happens if the userspace program fails to write the 0 in the file after it finished tracing (it can even not have chance to do if it is killed -9)? I propose that tracers like this one, i.e. that record instantaneous data, should remain enabled only while the userspace process keeps the fd opened. When it's closed (or after a grace period), the tracer should be automatically disabled.

Corrado

#8 lore raw
In reply to Andrew Morton
Quoted 10 lines · Arjan van de Ven

+ * Latency Top version : v0.1
+ * 70 59433 4897 i915_irq_wait drm_ioctl vfs_ioctl do_vfs_ioctl sys_ioctl

+ * |    |    |    |
+ * |    |    |    +----> the stringified backtrace
+ * |    |    +---------> The maximum latency for this entry in microseconds
+ * |    +--------------> The accumulated latency for this entry (microseconds)

+ * +-------------------> The number of times this entry is hit
+ *
+ * (note: the average latency is the acummulated latency deviced by the number
+ * of times)

Perhaps:

s/acummulated/accumulated
s/deviced/divided

...jet lag? :)

Tim