maximum latency tracking infrastructure (version 3)
Subsystems (2)
PATCH 1/1 maximum latency tracking infrastructure (version 3)
3rd version; only minor changes this time, the sysreq stuff is gone however.
I've decided to not use a namespace prefix; these functions are meant to be generic, and a namespace prefix is not common for such functions, and it would in addition cause a too narrow usage of this infrastructure.
I consider this 3rd version suitable for -mm inclusion, so that various subsystem owners can start looking into adding latency settings to their code.
Subject: [PATCH] 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.
--- drivers/acpi/processor_idle.c | 37 ++++- drivers/net/wireless/ipw2100.c | 10 + include/linux/latency.h | 25 +++ kernel/Makefile | 2 kernel/latency.c | 279 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 348 insertions(+), 5 deletions(-)
drivers/acpi/processor_idle.c +33 -4
@@ -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 <=system_latency_constraint ()) { 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 >system_latency_constraint ()) {if (cx->demotion.state) next_state = cx->demotion.state; }
@@ -1009,9 +1013,10 @@ static int acpi_processor_power_seq_showseq_printf (seq,"active state: C%zd\n" "max_cstate: C%d\n" -"bus master activity: %08x\n" , +"bus master activity: %08x\n" +"maximum allowed latency: %d usec\n" , pr->power.state ? pr->power.state - pr->power.states :0 , - max_cstate, (unsigned )pr->power.bm_activity); + max_cstate, (unsigned )pr->power.bm_activity,system_latency_constraint ());seq_puts (seq,"states:\n" );
@@ -1077,6 +1082,28 @@ static const struct file_operations acpi .release = single_release, }; + +static void smp_callback (void *v) +{ +/* we already woke the CPU up, nothing more to do */ +} + +/* +* This function gets called when a part of the kernel has a new latency requirement. +* This means we need to get all processors out of their C-state, and then recalculate +* a new suitable C-state. Just do a cross-cpu IPI; that wakes them all right up. +*/ +static int acpi_processor_latency_notify (struct notifier_block *b, +unsigned long l,void *v) +{ +smp_call_function (smp_callback,NULL ,0 ,1 ); +return NOTIFY_OK ; +} + +static struct notifier_block acpi_processor_latency_notifier = { + .notifier_call = acpi_processor_latency_notify, +}; +int acpi_processor_power_init (struct acpi_processor *pr,struct acpi_device *device) {
@@ -1093,6 +1120,7 @@ int acpi_processor_power_init(struct acp"ACPI: processor limited to max C-state %d\n" , max_cstate); first_run++; +register_latency_notifier (&acpi_processor_latency_notifier); }if (!pr)
@@ -1164,6 +1192,7 @@ int acpi_processor_power_exit(struct acp* copies of pm_idle before proceeding. */ cpu_idle_wait (); +unregister_latency_notifier (&acpi_processor_latency_notifier); }return 0 ;
include/linux/latency.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* latency.h: Explicit system-wide latency-expectation infrastructure +* +* (C) Copyright 2006 Intel Corporation +* Author: Arjan van de Ven <[email protected]> +* +*/ + +#ifndef _INCLUDE_GUARD_LATENCY_H_ +#define _INCLUDE_GUARD_LATENCY_H_ + +#include <linux/notifier.h> + +void set_acceptable_latency (char *identifier,int usecs); +void modify_acceptable_latency (char *identifier,int usecs); +void remove_acceptable_latency (char *identifier); +void synchronize_acceptable_latency (void ); +int system_latency_constraint (void ); + +int register_latency_notifier (struct notifier_block * nb); +int unregister_latency_notifier (struct notifier_block * nb); + +#define INFINITE_LATENCY 1000000 + +#endif
kernel/Makefile +1 -1
@@ -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 +279 -0
279 lines not on this page · raw message
drivers/net/wireless/ipw2100.c +10 -0
@@ -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_prireturn 0 ; } +/* the ipw2100 hardware really doesn't want power management delays +* longer than 175usec +*/ +modify_acceptable_latency ("ipw2100" ,175 ); +/* 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);
Hi,
Quoted 6 lines · Arjan van de Ven
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.
Nifty (aka "dumb") idea: would it make sense to enable drivers to register a callback "we're going to go idle now" to e.g. let a driver refill or service its hardware buffers the very moment before idling? That way a driver could increase its announced latency requirements, allowing longer idle sleeps until a hardware buffer overflows or whatever (but in many cases a hardware service issue would be covered by an IRQ then).
However the time scales involved here (a couple of microseconds per sleep or so versus a possibly comparably big amount of processing time per callback) could render such a thing impractical, especially when multiple drivers and thus multiple callbacks are involved (one might need to watch total callback processing time then).
Andreas Mohr
Quoted 10 lines · Andreas Mohr
Hi,
Quoted 6 lines · Arjan van de Ven
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.
Quoted 3 lines
Nifty (aka "dumb") idea: would it make sense to enable drivers to register a callback "we're going to go idle now" to e.g. let a driver refill or service its hardware buffers the very moment before idling?
I could have sworn there was an idle call notifier already\
ah there is on x86-64 but it is architecture specific...
Quoted 4 lines · Arjan van de Ven
I could have sworn there was an idle call notifier already\
ah there is on x86-64 but it is architecture specific...
The original implementation was on s390. But they're not really designed for doing anything slow, so I wouldn't recommend using them in drivers unless it's very fast.
-Andi
Hi,
Quoted 3 lines · Arjan van de Ven
I could have sworn there was an idle call notifier already\
ah there is on x86-64 but it is architecture specific...
So we would already have something that could be extended.
BTW, my IRQ statement was misplaced since this is the very thing we're caring about: de-idle activation latency right after hardware IRQ occurred. IOW, an idle callback really could be useful, since it does positively work against the buffer servicing IRQ wakeup latency issue.
Another question is how one would do callback processing in idle handler:
one could figure out the idle mode (latency) in advance and *then* call only all those idle callbacks that have more stringent requirements than the currently calculated idle mode's latency (and then calculate the idle mode again based on the current time after all those callbacks??), or one could just unconditionally call all idle handlers and then figure out idle length and go to sleep. Which one is better?
Andreas Mohr
Quoted 8 lines
Another question is how one would do callback processing in idle handler:
one could figure out the idle mode (latency) in advance and *then* call only all those idle callbacks that have more stringent requirements than the currently calculated idle mode's latency (and then calculate the idle mode again based on the current time after all those callbacks??), or one could just unconditionally call all idle handlers and then figure out idle length and go to sleep. Which one is better?
I'm not sure about either actually. Well if it's just to refill stuff etc then I would just call all. After all it may save interrupts and early wakeups if you do this, so there's a power advantage to be gained.
Quoted 7 lines · Arjan van de Ven
3rd version; only minor changes this time, the sysreq stuff is gone however.
I've decided to not use a namespace prefix; these functions are meant to be generic, and a namespace prefix is not common for such functions, and it would in addition cause a too narrow usage of this infrastructure.
Almost forgot--what about documentation? This is an addition to the driver API, so it should probably be described clearly somewhere, probably in Documentation/ somewhere...
Jesse
Quoted 10 lines · Jesse Barnes
Quoted 7 lines · Arjan van de Ven
3rd version; only minor changes this time, the sysreq stuff is gone however.
I've decided to not use a namespace prefix; these functions are meant to be generic, and a namespace prefix is not common for such functions, and it would in addition cause a too narrow usage of this infrastructure.
Almost forgot--what about documentation? This is an addition to the driver API, so it should probably be described clearly somewhere, probably in
Documentation/somewhere...
how about linuxdoc??
Quoted 10 lines · Arjan van de Ven
Quoted 9 lines · Jesse Barnes
Quoted 7 lines · Arjan van de Ven
3rd version; only minor changes this time, the sysreq stuff is gone however.
I've decided to not use a namespace prefix; these functions are meant to be generic, and a namespace prefix is not common for such functions, and it would in addition cause a too narrow usage of this infrastructure.
Quoted 5 lines
Quoted 3 lines
Almost forgot--what about documentation? This is an addition to the driver API, so it should probably be described clearly somewhere, probably in
Documentation/somewhere...how about linuxdoc??
I like to have stuff in Documentation/ too, but if you think the existing function comments are good enough that's fine.
Jesse