Skip to contentSkip to conversation

maximum latency tracking infrastructure

RFC In review Arjan van de Ven 8 participants 18 messages Aug 24, 2006 – Aug 25, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 maximum latency tracking infrastructure

Subject: [RFC] maximum latency tracking infrastructure
From: Arjan van de Ven <[email protected]>

The patch below adds infrastructure to track "maximum allowable latency" for power saving policies.

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again). The code that today makes this tradeoff just does a rather simple algorithm; however this is not good enough: There are devices and use cases where a lower latency is required than that the higher power saving states provide. An example would be audio playback, but another example is the ipw2100 wireless driver that right now has a very direct and ugly acpi hook to disable some higher power states randomly when it gets certain types of error.

The proposed solution is to have an interface where drivers can
* announce the maximum latency (in microseconds) that they can deal with
* modify this latency
* give up their constraint
and a function where the code that decides on power saving strategy can query the current global desired maximum.

This patch has a user of each side: on the consumer side, ACPI is patched to use this, on the producer side the ipw2100 driver is patched.

A generic maximum latency is also registered of 2 timer ticks (more and you lose accurate time tracking after all).

While the existing users of the patch are x86 specific, the infrastructure is not. I'd like to ask the arch maintainers of other architectures if the infrastructure is generic enough for their use (assuming the architecture has such a tradeoff as concept at all), and the sound/multimedia driver owners to look at the driver facing API to see if this is something they can use.

A sysrq key is registered to dump the list of registered latencies so that bugreports about too high latency / too high power usage can be analyzed.

---
 drivers/acpi/processor_idle.c  |    8 +
 drivers/net/wireless/ipw2100.c |   10 +
 include/linux/latency.h        |   18 +++
 kernel/Makefile                |    2 
 kernel/latency.c               |  245 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 280 insertions(+), 3 deletions(-)
drivers/acpi/processor_idle.c +6 -2
Index: linux-2.6.18-rc4-latency/drivers/acpi/processor_idle.c
===================================================================
@@ -38,6 +38,7 @@
 #include <linux/dmi.h>
 #include <linux/moduleparam.h>
 #include <linux/sched.h>	/* need_resched() */
+#include <linux/latency.h>
 
 #include <asm/io.h>
 #include <asm/uaccess.h>
@@ -453,7 +454,8 @@ static void acpi_processor_idle(void)
 	 */
 	if (cx->promotion.state &&
 	    ((cx->promotion.state - pr->power.states) <= max_cstate)) {
-		if (sleep_ticks > cx->promotion.threshold.ticks) {
+		if (sleep_ticks > cx->promotion.threshold.ticks &&
+			cx->promotion.state->latency <= get_acceptable_latency()) {
 			cx->promotion.count++;
 			cx->demotion.count = 0;
 			if (cx->promotion.count >=
@@ -494,8 +496,10 @@ static void acpi_processor_idle(void)
       end:
 	/*
 	 * Demote if current state exceeds max_cstate
+	 * or if the latency of the current state is unacceptable
 	 */
-	if ((pr->power.state - pr->power.states) > max_cstate) {
+	if ((pr->power.state - pr->power.states) > max_cstate ||
+		pr->power.state->latency > get_acceptable_latency()) {
 		if (cx->demotion.state)
 			next_state = cx->demotion.state;
 	}
include/linux/latency.h new file +18 -0
Index: linux-2.6.18-rc4-latency/include/linux/latency.h
===================================================================
@@ -0,0 +1,18 @@
+/*
+ * latency.h: Explicit system-wise latency-expectation infrastructure
+ *
+ * (C) Copyright 2006 Intel Corporation
+ * Author: Arjan van de Ven <[email protected]>
+ *
+ */
+
+#ifdef __KERNEL__
+
+void set_acceptable_latency(char *identifier, int usecs);
+void modify_acceptable_latency(char *identifier, int usecs);
+void remove_acceptable_latency(char *identifier);
+int get_acceptable_latency(void);
+
+#define INFINITE_LATENCY 1000000
+
+#endif
kernel/Makefile +1 -1
Index: linux-2.6.18-rc4-latency/kernel/Makefile
===================================================================
@@ -8,7 +8,7 @@ obj-y     = sched.o fork.o exec_domain.o
 	    signal.o sys.o kmod.o workqueue.o pid.o \
 	    rcupdate.o extable.o params.o posix-timers.o \
 	    kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \
-	    hrtimer.o rwsem.o
+	    hrtimer.o rwsem.o latency.o
 
 obj-$(CONFIG_STACKTRACE) += stacktrace.o
 obj-y += time/
kernel/latency.c new file +245 -0

245 lines not on this page · raw message

drivers/net/wireless/ipw2100.c +10 -0
Index: linux-2.6.18-rc4-latency/drivers/net/wireless/ipw2100.c
===================================================================
@@ -163,6 +163,7 @@ that only one external action is invoked
 #include <linux/firmware.h>
 #include <linux/acpi.h>
 #include <linux/ctype.h>
+#include <linux/latency.h>
 
 #include "ipw2100.h"
 
@@ -1697,6 +1698,11 @@ static int ipw2100_up(struct ipw2100_pri
 		return 0;
 	}
 
+	/* the ipw2100 hardware really doesn't want power management delays
+	 * longer than 500usec
+	 */
+	modify_acceptable_latency("ipw2100", 500);
+
 	/* If the interrupt is enabled, turn it off... */
 	spin_lock_irqsave(&priv->low_lock, flags);
 	ipw2100_disable_interrupts(priv);
@@ -1849,6 +1855,8 @@ static void ipw2100_down(struct ipw2100_
 	ipw2100_disable_interrupts(priv);
 	spin_unlock_irqrestore(&priv->low_lock, flags);
 
+	modify_acceptable_latency("ipw2100", INFINITE_LATENCY);
+
 #ifdef ACPI_CSTATE_LIMIT_DEFINED
 	if (priv->config & CFG_C3_DISABLED) {
 		IPW_DEBUG_INFO(": Resetting C3 transitions.\n");
@@ -6533,6 +6541,7 @@ static int __init ipw2100_init(void)
 
 	ret = pci_module_init(&ipw2100_pci_driver);
 
+	set_acceptable_latency("ipw2100", INFINITE_LATENCY);
 #ifdef CONFIG_IPW2100_DEBUG
 	ipw2100_debug_level = debug;
 	driver_create_file(&ipw2100_pci_driver.driver,
@@ -6553,6 +6562,7 @@ static void __exit ipw2100_exit(void)
 			   &driver_attr_debug_level);
 #endif
 	pci_unregister_driver(&ipw2100_pci_driver);
+	remove_acceptable_latency("ipw2100");
 }
 
 module_init(ipw2100_init);
#1 lore raw
Quoted 5 lines · Arjan van de Ven

Subject: [RFC] maximum latency tracking infrastructure
From: Arjan van de Ven <[email protected]>

The patch below adds infrastructure to track "maximum allowable latency" for power saving policies.

I like it.
Stating the constraints is much nicer than today's hack where ipw2100 reaches into ACPI and disables C3 when it notices that DMA overflows.

everything in usecs -- good.

-Len

#2 lore raw
In reply to Arjan van de Ven
Quoted 4 lines · Arjan van de Ven

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again).

What if a processor was already in a sleep state when a call to set_acceptable_latency() latency occurs? Should there be a callback so they can be woken up? A callback would also allow ACPI to tell the user "disabling C3 because of device <foo>" or somesuch, which might be nice.

Also, should subsystems have the ability to set a lower bound on latency? That would mean set_acceptable_latency() could fail, indicating that the user should buy a better device or a system with better realtime guarantees, which is also valuable info.

Comments aside, this is a nice interface, should help clarify things for devices with response time limits.

Thanks,
Jesse

#3 lore raw
Quoted 7 lines · Jesse Barnes
Quoted 4 lines · Arjan van de Ven

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again).

What if a processor was already in a sleep state when a call to set_acceptable_latency() latency occurs?

there's nothing sane that can be done in that case; any wake up already will cause the unwanted latency! A premature wakeup is only making it happen *now*, but now is as inconvenient a time as any... (in fact it may be a worst case time scenario, say, an audio interrupt...)

Quoted 1 line

Should there be a callback so they can be woken up? A callback would also allow ACPI to tell the user "disabling C3 because of device <foo>" or somesuch, which might be nice.

printk'ing would be evil, changes like this will be "semi frequent", like every time you start or stop playing audio. What ACPI could easily do is indicate in /proc/acpi/processor/*/power that a state will not be reachable because it violates the latency constraints. That would be entirely reasonable.

Quoted 1 line

Also, should subsystems have the ability to set a lower bound on latency? That would mean set_acceptable_latency() could fail, indicating that the user should buy a better device or a system with better realtime guarantees, which is also valuable info.

While it's valuable info.. there is nothing you can DO about it...
While the kernel can even do a latency of 1us by just not going into C1 even... so the kernel CAN honor it, even if it thinks it might not be a good idea. Can you give a more concrete example of a situation where you think your idea would be useful?

Greetings,
Arjan van de Ven

#4 lore raw
Quoted 7 lines · Arjan van de Ven
Quoted 6 lines · Jesse Barnes
Quoted 4 lines · Arjan van de Ven

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again).

Quoted 8 lines
Quoted 2 lines

What if a processor was already in a sleep state when a call to set_acceptable_latency() latency occurs?

there's nothing sane that can be done in that case; any wake up already will cause the unwanted latency! A premature wakeup is only making it happen *now*, but now is as inconvenient a time as any... (in fact it may be a worst case time scenario, say, an audio interrupt...)

Depends on what's going on. What if you have a two socket machine, and one CPU is in C3 when the latency setting occurs? Shouldn't you wake it up and prevent it from going that deep again? But you're right, you won't necessarily improve anything...

Quoted 11 lines
Quoted 5 lines

Should there be a callback so
they can be woken up? A callback would also allow ACPI to tell the user "disabling C3 because of device <foo>" or somesuch, which might be nice.

printk'ing would be evil, changes like this will be "semi frequent", like every time you start or stop playing audio. What ACPI could easily do is indicate in /proc/acpi/processor/*/power that a state will not be reachable because it violates the latency constraints. That would be entirely reasonable.

Right, that's the idea. A printk may be overkill but some kind of notification might be nice, for similar reasons to the below.

Quoted 10 lines
Quoted 4 lines

Also, should subsystems have the ability to set a lower bound on latency? That would mean set_acceptable_latency() could fail, indicating that the user should buy a better device or a system with better realtime guarantees, which is also valuable info.

While it's valuable info.. there is nothing you can DO about it... While the kernel can even do a latency of 1us by just not going into C1 even... so the kernel CAN honor it, even if it thinks it might not be a good idea. Can you give a more concrete example of a situation where you think your idea would be useful?

Well, I was imagining a scenario where you didn't really want to disallow C3 for whatever reason (maybe some standard you're following requires it), so your minimum latency would be N usec. You'd definitely want to know about any device that required < N usecs of latency (at boot or driver init time) so that you'd know you had a bad device or system.

Jesse

#5 lore raw
Quoted 11 lines · Jesse Barnes
Quoted 10 lines · Arjan van de Ven
Quoted 8 lines · Jesse Barnes
Quoted 4 lines · Arjan van de Ven

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again).

What if a processor was already in a sleep state when a call to set_acceptable_latency() latency occurs?

Quoted 8 lines
Quoted 5 lines

there's nothing sane that can be done in that case; any wake up already will cause the unwanted latency! A premature wakeup is only making it happen *now*, but now is as inconvenient a time as any... (in fact it may be a worst case time scenario, say, an audio interrupt...)

Depends on what's going on. What if you have a two socket machine, and one CPU is in C3 when the latency setting occurs?

I didn't think there were currently any multi socket machines with C3 support? The best you get is dual core.

Quoted 3 lines

Shouldn't you wake it
up and prevent it from going that deep again? But you're right, you won't necessarily improve anything...

Generally there are so many events that wake up CPUs that the case is pretty academic -- all CPUs will eventually wake up in a reasonable time (before your driver initialization finished likely) and then follow the new latency settings.

Maybe at some point if all the idle breaking events in Linux have been fixed up it might be a problem, but I think that's a long time off.

-Andi

#6 lore raw
In reply to Arjan van de Ven
Quoted 10 lines · Arjan van de Ven
Quoted 8 lines · Jesse Barnes
Quoted 4 lines · Arjan van de Ven

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again).

Quoted 6 lines
Quoted 1 line

What if a processor was already in a sleep state when a call to set_acceptable_latency() latency occurs?

there's nothing sane that can be done in that case; any wake up already will cause the unwanted latency! A premature wakeup is only making it happen *now*, but now is as inconvenient a time as any... (in fact it may be a worst case time scenario, say, an audio interrupt...)

Surely you would call set_acceptable_latency() *before* running such operation that requires the given latency? And that set_acceptable_latency would block the caller until all CPUs are set to wake within this latency.

That would be the API semantics I would expect, anyway.

Signature · 2 lines
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 
#7 lore raw
Quoted 18 lines · Nick Piggin
Quoted 10 lines · Arjan van de Ven
Quoted 8 lines · Jesse Barnes
Quoted 4 lines · Arjan van de Ven

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again).

Quoted 6 lines
Quoted 1 line

What if a processor was already in a sleep state when a call to set_acceptable_latency() latency occurs?

there's nothing sane that can be done in that case; any wake up already will cause the unwanted latency! A premature wakeup is only making it happen *now*, but now is as inconvenient a time as any... (in fact it may be a worst case time scenario, say, an audio interrupt...)

Quoted 5 lines

Surely you would call set_acceptable_latency() *before* running such operation that requires the given latency? And that set_acceptable_latency would block the caller until all CPUs are set to wake within this latency.

That would be the API semantics I would expect, anyway.

but that means it blocks, and thus can't be used in irq context

(the usage model I imagine happens most is a set_acceptable_latency() which can block during device init, with either no or a very course limit, and a modify_acceptable_latency(), which cannot block, from irq context or device open)

#8 lore raw
Quoted 10 lines · Arjan van de Ven
Quoted 5 lines · Nick Piggin

Surely you would call set_acceptable_latency() *before* running such operation that requires the given latency? And that set_acceptable_latency would block the caller until all CPUs are set to wake within this latency.

That would be the API semantics I would expect, anyway.

but that means it blocks, and thus can't be used in irq context

Is that a problem? I guess it could be, but you don't want to give a false sense of security either. Having an explicit _nosync version may make that clear?

Quoted 4 lines

(the usage model I imagine happens most is a set_acceptable_latency() which can block during device init, with either no or a very course limit, and a modify_acceptable_latency(), which cannot block, from irq context or device open)

OK. You'd know more about that than I ;)

Signature · 2 lines
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 
#9 lore raw
Quoted 9 lines · Nick Piggin
Quoted 8 lines · Arjan van de Ven
Quoted 6 lines · Nick Piggin

Surely you would call set_acceptable_latency() *before* running such operation that requires the given latency? And that set_acceptable_latency
would block the caller until all CPUs are set to wake within this latency.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

That would be the API semantics I would expect, anyway.

but that means it blocks, and thus can't be used in irq context

Is that a problem? I guess it could be, but you don't want to give a false sense of security either. Having an explicit _nosync version may make that clear?

well the api is already split between blocking and non-blocking so in principle that's easy. The problem is that I suspect most users will use the non-blocking variant.

Also the "what to do" can be treacherous; it'll need a callback list simply because many places can be using the latency values, more than just idle. (I can see pstate code for example also using it to limit which ones to use, and not use the ones it takes to long to get out of)

I'll investigate what it'll take to get the callback in place; for the C-state case it's not THAT critical (after all the cpu you are running on when making this call is not in a deep C state.. :-)

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

Subject: [RFC] maximum latency tracking infrastructure
From: Arjan van de Ven <[email protected]>

The patch below adds infrastructure to track "maximum allowable latency" for power saving policies.

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again). The code that today makes this tradeoff just does a rather simple algorithm;

I was just thinking that it might be cleaner to register a structure instead of tracking identifiers to usecs. You might get a speed up on some of the operations, like unregister.

Another thing I was thinking about is that this seems somewhat contrary to the idea of using dynamic tick (assuming it was in mainline) to heuristically pick a power state. Do you have any thoughts on how you would combine the two?

Daniel

#11 lore raw
Quoted 7 lines · Daniel Walker
Quoted 6 lines · Arjan van de Ven

Subject: [RFC] maximum latency tracking infrastructure
From: Arjan van de Ven <[email protected]>

The patch below adds infrastructure to track "maximum allowable latency" for power saving policies.

Quoted 8 lines
Quoted 4 lines

The reason for adding this infrastructure is that power management in the idle loop needs to make a tradeoff between latency and power savings (deeper power save modes have a longer latency to running code again). The code that today makes this tradeoff just does a rather simple algorithm;

I was just thinking that it might be cleaner to register a structure instead of tracking identifiers to usecs. You might get a speed up on some of the operations, like unregister.

it makes things a lot more complex for both the user and the infrastructure though, and I doubt it's going to be a performance gain; you need to walk all registered items anyway to decide the new minimum value if you unregister one for example.

Quoted 4 lines

Another thing I was thinking about is that this seems somewhat contrary to the idea of using dynamic tick (assuming it was in mainline) to heuristically pick a power state. Do you have any thoughts on how you would combine the two?

Actually it's designed in part FOR this case!
So how that will work (thought experiment, I don't have the code yet)

In idle, determine the time the next scheduled event is. Then given that time go over the C-states and pick the deepest C-state that
1) satisfies the requested latency
2) has a latency that is a small enough fraction of the total time

(2 is needed to not pick a 1 msec-latency C state for a 1ms idle, that won't save you power most likely, so you need to have enough time in "real" idle)

so when you know your latency requirements, you now can pick a DEEPER sleepstate than you could before (or at least the right one)... dynticks needs this more than anything :)

#12 lore raw
Quoted 8 lines · Arjan van de Ven
Quoted 3 lines

I was just thinking that it might be cleaner to register a structure instead of tracking identifiers to usecs. You might get a speed up on some of the operations, like unregister.

it makes things a lot more complex for both the user and the infrastructure though, and I doubt it's going to be a performance gain; you need to walk all registered items anyway to decide the new minimum value if you unregister one for example.

Might be time for a priority list (lib/plist.c), but that might be like swatting a gnat with a sledge hammer.

Quoted 14 lines
Quoted 4 lines

Another thing I was thinking about is that this seems somewhat contrary to the idea of using dynamic tick (assuming it was in mainline) to heuristically pick a power state. Do you have any thoughts on how you would combine the two?

Actually it's designed in part FOR this case!
So how that will work (thought experiment, I don't have the code yet)

In idle, determine the time the next scheduled event is. Then given that time go over the C-states and pick the deepest C-state that
1) satisfies the requested latency
2) has a latency that is a small enough fraction of the total time

Quoted 7 lines

(2 is needed to not pick a 1 msec-latency C state for a 1ms idle, that won't save you power most likely, so you need to have enough time in "real" idle)

so when you know your latency requirements, you now can pick a DEEPER sleepstate than you could before (or at least the right one)... dynticks needs this more than anything :)

Sounds pretty good. Since dynamic tick tracks timer events one could also add a method to track interrupts in general if they are regular enough to do so. That's just thinking while typing, so it might not be sane.

Daniel

#13 lore raw
In reply to Arjan van de Ven

drivers/net/wireless/ipw2100.c static int ipw2100_up(struct ipw2100_pri · line 1705

Quoted 5 lines · Arjan van de Ven
drivers/net/wireless/ipw2100.c +5 -0
+	/* the ipw2100 hardware really doesn't want power management delays
+	 * longer than 500usec
+	 */
+	modify_acceptable_latency("ipw2100", 500);
+

Hm. My BIOS claims that the C3 transition period is 85usec (and even my C4 is 185) , but I've hit the error path where C3 gets disabled. Is this really adequate? Also, by the looks of it, the C3 disabling path is still present - is it still theoretically necessary with the above, or is this just a belt and braces approach?

Signature · 1 line
Matthew Garrett | [email protected]
#14 lore raw
Quoted 11 lines · Matthew Garrett

drivers/net/wireless/ipw2100.c static int ipw2100_up(struct ipw2100_pri · line 1705

Quoted 5 lines · Arjan van de Ven
drivers/net/wireless/ipw2100.c +5 -0
+	/* the ipw2100 hardware really doesn't want power management delays
+	 * longer than 500usec
+	 */
+	modify_acceptable_latency("ipw2100", 500);
+

Hm. My BIOS claims that the C3 transition period is 85usec (and even my C4 is 185) , but I've hit the error path where C3 gets disabled. Is this really adequate?

first of all that 500 is a bit of a guess on my side; James (the Intel wireless guy) is on holiday so I couldn't get real numbers out of it. But as proof of concept it's pretty ok :)

Quoted 3 lines

Also, by the looks of it, the C3 disabling path is
still present - is it still theoretically necessary with the above, or is this just a belt and braces approach?

the "problem" is that bioses lie about these numbers all the time as well ;( (it's getting better but still).

Those numbers you gave, were those on batter or on AC ? (apparently for the problem machines C3 latency goes WAY up when on battery, and then the problem hits)

#15 lore raw
In reply to Arjan van de Ven
Quoted 5 lines · Arjan van de Ven

Subject: [RFC] maximum latency tracking infrastructure
From: Arjan van de Ven <[email protected]>

The patch below adds infrastructure to track "maximum allowable latency" for power saving policies.

Looks good. But it will also be important to have a user-level way to report who is constraining us from power saving and by how much of a margin.

Signature · 1 line
Mathematics is the supreme nostalgia of our time.
#16 lore raw
Quoted 11 lines · Matt Mackall
Quoted 5 lines · Arjan van de Ven

Subject: [RFC] maximum latency tracking infrastructure
From: Arjan van de Ven <[email protected]>

The patch below adds infrastructure to track "maximum allowable latency" for power saving policies.

Looks good. But it will also be important to have a user-level way to report who is constraining us from power saving and by how much of a margin.

there is in the patch:

echo l > /proc/sysreq-trigger

#17 lore raw
Quoted 9 lines · Arjan van de Ven
Quoted 8 lines · Matt Mackall
Quoted 6 lines · Arjan van de Ven

Subject: [RFC] maximum latency tracking infrastructure
From: Arjan van de Ven <[email protected]>

The patch below adds infrastructure to track "maximum allowable latency" for power
saving policies.

Quoted 8 lines
Quoted 4 lines

Looks good. But it will also be important to have a user-level way to report who is constraining us from power saving and by how much of a margin.

there is in the patch:

echo l > /proc/sysreq-trigger

Ahh, missed that. I suppose that'll suffice.

Signature · 1 line
Mathematics is the supreme nostalgia of our time.