Message ID | 1460545556-15085-1-git-send-email-daniel.lezcano@linaro.org |
---|---|
State | New |
Headers | show |
On Wed, Apr 13, 2016 at 01:05:56PM +0200, Daniel Lezcano wrote: > The interrupt framework gives a lot of information about each interrupt. > > It does not keep track of when those interrupts occur though. > > This patch provides a mean to record the elapsed time between successive > interrupt occurrences in a per-IRQ per-CPU circular buffer to help with the > prediction of the next occurrence using a statistical model. > > A new function is added to browse the different interrupts and retrieve the > timing information stored in it. > > A static key is introduced so when the irq prediction is switched off at > runtime, we can reduce the overhead near to zero. The irq timings is > supposed to be potentially used by different sub-systems and for this reason > the static key is a ref counter, so when the last use releases the irq > timings that will result on the effective deactivation of the irq measurement. > > Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> > Acked-by: Nicolas Pitre <nicolas.pitre@linaro.org> > --- > V4: > - Added a static key > - Added more comments for irq_timings_get_next() > - Unified some function names to be prefixed by 'irq_timings_...' > - Fixed a rebase error > V3: > - Replaced ktime_get() by local_clock() > - Shared irq are not handled > - Simplified code by adding the timing in the irqdesc struct > - Added a function to browse the irq timings > V2: > - Fixed kerneldoc comment > - Removed data field from the struct irq timing > - Changed the lock section comment > - Removed semi-colon style with empty stub > - Replaced macro by static inline > - Fixed static functions declaration > RFC: > - initial posting > --- Hi Thomas, do you think this patch is acceptable ?
On Wed, Apr 13, 2016 at 01:05:56PM +0200, Daniel Lezcano wrote: > The interrupt framework gives a lot of information about each interrupt. > > It does not keep track of when those interrupts occur though. > > This patch provides a mean to record the elapsed time between successive > interrupt occurrences in a per-IRQ per-CPU circular buffer to help with the > prediction of the next occurrence using a statistical model. > > A new function is added to browse the different interrupts and retrieve the > timing information stored in it. > > A static key is introduced so when the irq prediction is switched off at > runtime, we can reduce the overhead near to zero. The irq timings is > supposed to be potentially used by different sub-systems and for this reason > the static key is a ref counter, so when the last use releases the irq > timings that will result on the effective deactivation of the irq measurement. > > Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> > Acked-by: Nicolas Pitre <nicolas.pitre@linaro.org> > --- > V4: > - Added a static key > - Added more comments for irq_timings_get_next() > - Unified some function names to be prefixed by 'irq_timings_...' > - Fixed a rebase error Hi Thomas, I believe all the comments were addressed. I can imagine you are very busy at the moment. Is there something else I should fix or is the patch ok for you ? Thanks. -- Daniel
On Fri, 10 Jun 2016, Thomas Gleixner wrote: > Thanks, > > On Wed, 13 Apr 2016, Daniel Lezcano wrote: > > + timings = this_cpu_ptr(desc->timings); > > + now = local_clock(); > > + prev = timings->timestamp; > > + timings->timestamp = now; > > + > > + /* > > + * If it is the first interrupt of the series, we can't > > + * compute an interval, just store the timestamp and exit. > > + */ > > + if (unlikely(!prev)) > > + return; > > The delta will be large enough that you drop out in the check below. So you > can spare that conditional. Fair enough. > > + diff = now - prev; > > + > > + /* > > + * microsec (actually 1024th of a milisec) precision is good > > + * enough for our purpose. > > + */ > > + diff >>= 10; > > And that shift instruction is required because of the following? > > > * Otherwise we know the magnitude of diff is > > + * well within 32 bits. > > AFAICT that's pointless. You are not saving anything because NSEC_PER_SEC is > smaller than 2^32 and your 8 values are not going to overflow 64 bit in the > sum. Those values are squared later, so we really want 32 bits here. > > + */ > > + if (unlikely(diff > USEC_PER_SEC)) { > > + memset(timings, 0, sizeof(*timings)); > > + timings->timestamp = now; > > Redundant store. We just trashed all our data with the memset so the current timestamp needs to be restored. > > + return; > > + } > > + > > + /* The oldest value corresponds to the next index. */ > > + timings->w_index = (timings->w_index + 1) & IRQ_TIMINGS_MASK; > > + > > + /* > > + * Remove the oldest value from the summing. If this is the > > + * first time we go through this array slot, the previous > > + * value will be zero and we won't substract anything from the > > + * current sum. Hence this code relies on a zero-ed structure. > > + */ > > + timings->sum -= timings->values[timings->w_index]; > > + timings->values[timings->w_index] = diff; > > + timings->sum += diff; > > Now the real question is whether you really need all that math, checks and > memsets in the irq hotpath. If you make the storage slightly larger then you > can just store the values unconditionally in the circular buffer and do all > the computational stuff when you really it. Well... given that you need an IRQ everytime you come out of idle that means there will always be more IRQs than entries into idle, so you're probably right. Nicolas
On Fri, 10 Jun 2016, Thomas Gleixner wrote: > On Fri, 10 Jun 2016, Nicolas Pitre wrote: > > On Fri, 10 Jun 2016, Thomas Gleixner wrote: > > > > + diff = now - prev; > > > > + > > > > + /* > > > > + * microsec (actually 1024th of a milisec) precision is good > > > > + * enough for our purpose. > > > > + */ > > > > + diff >>= 10; > > > > > > And that shift instruction is required because of the following? > > > > > > > * Otherwise we know the magnitude of diff is > > > > + * well within 32 bits. > > > > > > AFAICT that's pointless. You are not saving anything because NSEC_PER_SEC is > > > smaller than 2^32 and your 8 values are not going to overflow 64 bit in the > > > sum. > > > > Those values are squared later, so we really want 32 bits here. > > Well, you can do sum >> 10 exaclty once when you calculate stuff. Given your later argument I agree. > > > > + */ > > > > + if (unlikely(diff > USEC_PER_SEC)) { > > > > + memset(timings, 0, sizeof(*timings)); > > > > + timings->timestamp = now; > > > > > > Redundant store. > > > > We just trashed all our data with the memset so the current timestamp > > needs to be restored. > > So why doing a full memset and not only on the array ? Go figure. This code has come a long way. > > > Now the real question is whether you really need all that math, checks and > > > memsets in the irq hotpath. If you make the storage slightly larger then you > > > can just store the values unconditionally in the circular buffer and do all > > > the computational stuff when you really it. > > > > Well... given that you need an IRQ everytime you come out of idle that > > means there will always be more IRQs than entries into idle, so you're > > probably right. > > Glad you agree. > > Thanks, > > tglx >
On 06/10/2016 04:52 PM, Thomas Gleixner wrote: Hi Thomas, [ ... ] >> + now = local_clock(); >> + prev = timings->timestamp; >> + timings->timestamp = now; >> + >> + /* >> + * If it is the first interrupt of the series, we can't >> + * compute an interval, just store the timestamp and exit. >> + */ >> + if (unlikely(!prev)) >> + return; > > The delta will be large enough that you drop out in the check below. So you > can spare that conditional. Good point. >> + >> + diff = now - prev; >> + >> + /* >> + * microsec (actually 1024th of a milisec) precision is good >> + * enough for our purpose. >> + */ >> + diff >>= 10; > > And that shift instruction is required because of the following? > >> * Otherwise we know the magnitude of diff is >> + * well within 32 bits. > > AFAICT that's pointless. You are not saving anything because NSEC_PER_SEC is > smaller than 2^32 and your 8 values are not going to overflow 64 bit in the > sum. > >> + */ >> + if (unlikely(diff > USEC_PER_SEC)) { >> + memset(timings, 0, sizeof(*timings)); >> + timings->timestamp = now; > > Redundant store. Yeah, I thought it was more efficient than: memset(timings->value, 0, sizeof(timings->value)); timings->w_index = 0; timings->sum = 0; >> + return; >> + } >> + >> + /* The oldest value corresponds to the next index. */ >> + timings->w_index = (timings->w_index + 1) & IRQ_TIMINGS_MASK; >> + >> + /* >> + * Remove the oldest value from the summing. If this is the >> + * first time we go through this array slot, the previous >> + * value will be zero and we won't substract anything from the >> + * current sum. Hence this code relies on a zero-ed structure. >> + */ >> + timings->sum -= timings->values[timings->w_index]; >> + timings->values[timings->w_index] = diff; >> + timings->sum += diff; > > Now the real question is whether you really need all that math, checks and > memsets in the irq hotpath. If you make the storage slightly larger then you > can just store the values unconditionally in the circular buffer and do all > the computational stuff when you really it. Yes, that was one concern when I wrote the code: do some basic computation when an interrupt occurs, and the rest after or do the entire math when entering idle. If the storage is a bit larger (let's say 16 values) and there is no memset and the sum is not computed, at least we need a count for the number of values in the array before this one is fulfilled, otherwise the statistics will be wrong as we will take into account the entire array with old values, no ? -- <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog
On Tue, 14 Jun 2016, Daniel Lezcano wrote: > On 06/10/2016 04:52 PM, Thomas Gleixner wrote: > > > > + timings->sum -= timings->values[timings->w_index]; > > > + timings->values[timings->w_index] = diff; > > > + timings->sum += diff; > > > > Now the real question is whether you really need all that math, checks and > > memsets in the irq hotpath. If you make the storage slightly larger then you > > can just store the values unconditionally in the circular buffer and do all > > the computational stuff when you really it. > > Yes, that was one concern when I wrote the code: do some basic computation > when an interrupt occurs, and the rest after or do the entire math when > entering idle. > > If the storage is a bit larger (let's say 16 values) and there is no memset > and the sum is not computed, at least we need a count for the number of values > in the array before this one is fulfilled, otherwise the statistics will be > wrong as we will take into account the entire array with old values, no ? The point is not to change from 8 to 16 entries, but to store raw 64-bit timestamps instead of computed 32-bit deltas. Whether or not those timestamps are too far apart and discarded can be done at idle entry time. Nicolas
On 06/14/2016 05:10 PM, Nicolas Pitre wrote: > On Tue, 14 Jun 2016, Daniel Lezcano wrote: > >> On 06/10/2016 04:52 PM, Thomas Gleixner wrote: >> >>>> + timings->sum -= timings->values[timings->w_index]; >>>> + timings->values[timings->w_index] = diff; >>>> + timings->sum += diff; >>> >>> Now the real question is whether you really need all that math, checks and >>> memsets in the irq hotpath. If you make the storage slightly larger then you >>> can just store the values unconditionally in the circular buffer and do all >>> the computational stuff when you really it. >> >> Yes, that was one concern when I wrote the code: do some basic computation >> when an interrupt occurs, and the rest after or do the entire math when >> entering idle. >> >> If the storage is a bit larger (let's say 16 values) and there is no memset >> and the sum is not computed, at least we need a count for the number of values >> in the array before this one is fulfilled, otherwise the statistics will be >> wrong as we will take into account the entire array with old values, no ? > > The point is not to change from 8 to 16 entries, but to store raw 64-bit > timestamps instead of computed 32-bit deltas. Whether or not those > timestamps are too far apart and discarded can be done at idle entry > time. Ah, ok. That makes sense. Thanks for the clarification. -- Daniel -- <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog
diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h index 0e95fcc..11a8d20 100644 --- a/include/linux/interrupt.h +++ b/include/linux/interrupt.h @@ -665,6 +665,24 @@ static inline void init_irq_proc(void) } #endif +#ifdef CONFIG_IRQ_TIMINGS + +#define IRQ_TIMINGS_SHIFT 3 +#define IRQ_TIMINGS_SIZE (1 << IRQ_TIMINGS_SHIFT) +#define IRQ_TIMINGS_MASK (IRQ_TIMINGS_SIZE - 1) + +struct irq_timings { + u32 values[IRQ_TIMINGS_SIZE]; /* our circular buffer */ + u64 sum; /* sum of values */ + u64 timestamp; /* latest timestamp */ + unsigned int w_index; /* current buffer index */ +}; + +struct irq_timings *irq_timings_get_next(int *irq); +void irq_timings_get(void); +void irq_timings_put(void); +#endif + struct seq_file; int show_interrupts(struct seq_file *p, void *v); int arch_show_interrupts(struct seq_file *p, int prec); diff --git a/include/linux/irqdesc.h b/include/linux/irqdesc.h index dcca77c..f4e29b2 100644 --- a/include/linux/irqdesc.h +++ b/include/linux/irqdesc.h @@ -12,6 +12,7 @@ struct proc_dir_entry; struct module; struct irq_desc; struct irq_domain; +struct irq_timings; struct pt_regs; /** @@ -51,6 +52,9 @@ struct irq_desc { struct irq_data irq_data; unsigned int __percpu *kstat_irqs; irq_flow_handler_t handle_irq; +#ifdef CONFIG_IRQ_TIMINGS + struct irq_timings __percpu *timings; +#endif #ifdef CONFIG_IRQ_PREFLOW_FASTEOI irq_preflow_handler_t preflow_handler; #endif diff --git a/kernel/irq/Kconfig b/kernel/irq/Kconfig index 3bbfd6a..38e551d 100644 --- a/kernel/irq/Kconfig +++ b/kernel/irq/Kconfig @@ -81,6 +81,9 @@ config GENERIC_MSI_IRQ_DOMAIN config HANDLE_DOMAIN_IRQ bool +config IRQ_TIMINGS + bool + config IRQ_DOMAIN_DEBUG bool "Expose hardware/virtual IRQ mapping via debugfs" depends on IRQ_DOMAIN && DEBUG_FS diff --git a/kernel/irq/Makefile b/kernel/irq/Makefile index 2ee42e9..e1debaa9 100644 --- a/kernel/irq/Makefile +++ b/kernel/irq/Makefile @@ -9,3 +9,4 @@ obj-$(CONFIG_GENERIC_IRQ_MIGRATION) += cpuhotplug.o obj-$(CONFIG_PM_SLEEP) += pm.o obj-$(CONFIG_GENERIC_MSI_IRQ) += msi.o obj-$(CONFIG_GENERIC_IRQ_IPI) += ipi.o +obj-$(CONFIG_IRQ_TIMINGS) += timings.o diff --git a/kernel/irq/handle.c b/kernel/irq/handle.c index a15b548..cd37536 100644 --- a/kernel/irq/handle.c +++ b/kernel/irq/handle.c @@ -138,6 +138,8 @@ irqreturn_t handle_irq_event_percpu(struct irq_desc *desc) unsigned int flags = 0, irq = desc->irq_data.irq; struct irqaction *action; + handle_timings(desc); + for_each_action_of_desc(desc, action) { irqreturn_t res; diff --git a/kernel/irq/internals.h b/kernel/irq/internals.h index eab521fc..781ecbf 100644 --- a/kernel/irq/internals.h +++ b/kernel/irq/internals.h @@ -56,6 +56,7 @@ enum { IRQS_WAITING = 0x00000080, IRQS_PENDING = 0x00000200, IRQS_SUSPENDED = 0x00000800, + IRQS_TIMINGS = 0x00001000, }; #include "debug.h" @@ -218,3 +219,61 @@ irq_pm_install_action(struct irq_desc *desc, struct irqaction *action) { } static inline void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) { } #endif + +#ifdef CONFIG_IRQ_TIMINGS +static inline int alloc_timings(struct irq_desc *desc) +{ + desc->timings = alloc_percpu(struct irq_timings); + if (!desc->timings) + return -ENOMEM; + + return 0; +} + +static inline void free_timings(struct irq_desc *desc) +{ + free_percpu(desc->timings); +} + +static inline void remove_timings(struct irq_desc *desc) +{ + desc->istate &= ~IRQS_TIMINGS; +} + +static inline void setup_timings(struct irq_desc *desc, struct irqaction *act) +{ + /* + * Timers are deterministic, so no need to do any measurement + * on them. + */ + if (act->flags & __IRQF_TIMER) + return; + + desc->istate |= IRQS_TIMINGS; +} + +extern struct static_key_false irq_timing_enabled; + +extern void __handle_timings(struct irq_desc *desc); + +/* + * The function handle_timings is only called in one place in the + * interrupts handler. We want this function always inline so the + * code inside is embedded in the function and the static key branching + * code can act at the higher level. Without the explicit __always_inline + * we can end up with a call to the 'handle_timings' function with a small + * overhead in the hotpath for nothing. + */ +static __always_inline void handle_timings(struct irq_desc *desc) +{ + if (static_key_enabled(&irq_timing_enabled)) + __handle_timings(desc); +} +#else +static inline int alloc_timings(struct irq_desc *desc) { return 0; } +static inline void free_timings(struct irq_desc *desc) {} +static inline void handle_timings(struct irq_desc *desc) {} +static inline void remove_timings(struct irq_desc *desc) {} +static inline void setup_timings(struct irq_desc *desc, + struct irqaction *act) {}; +#endif diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c index 0ccd028..bd74bc7 100644 --- a/kernel/irq/irqdesc.c +++ b/kernel/irq/irqdesc.c @@ -174,6 +174,9 @@ static struct irq_desc *alloc_desc(int irq, int node, struct module *owner) if (alloc_masks(desc, gfp, node)) goto err_kstat; + if (alloc_timings(desc)) + goto err_mask; + raw_spin_lock_init(&desc->lock); lockdep_set_class(&desc->lock, &irq_desc_lock_class); init_rcu_head(&desc->rcu); @@ -182,6 +185,8 @@ static struct irq_desc *alloc_desc(int irq, int node, struct module *owner) return desc; +err_mask: + free_masks(desc); err_kstat: free_percpu(desc->kstat_irqs); err_desc: @@ -193,6 +198,7 @@ static void delayed_free_desc(struct rcu_head *rhp) { struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu); + free_timings(desc); free_masks(desc); free_percpu(desc->kstat_irqs); kfree(desc); diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c index 3ddd229..132c2d7 100644 --- a/kernel/irq/manage.c +++ b/kernel/irq/manage.c @@ -1343,6 +1343,8 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new) __enable_irq(desc); } + setup_timings(desc, new); + raw_spin_unlock_irqrestore(&desc->lock, flags); /* @@ -1465,6 +1467,7 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id) irq_settings_clr_disable_unlazy(desc); irq_shutdown(desc); irq_release_resources(desc); + remove_timings(desc); } #ifdef CONFIG_SMP diff --git a/kernel/irq/timings.c b/kernel/irq/timings.c new file mode 100644 index 0000000..cb91a75 --- /dev/null +++ b/kernel/irq/timings.c @@ -0,0 +1,133 @@ +/* + * linux/kernel/irq/timings.c + * + * Copyright (C) 2016, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org> + * + */ +#include <linux/interrupt.h> +#include <linux/irq.h> +#include <linux/irqdesc.h> +#include <linux/percpu.h> +#include <linux/static_key.h> + +#include "internals.h" + +DEFINE_STATIC_KEY_FALSE(irq_timing_enabled); + +void irq_timings_get(void) +{ + static_branch_inc(&irq_timing_enabled); +} + +void irq_timings_put(void) +{ + static_branch_dec(&irq_timing_enabled); +} + +/** + * __handle_timings - stores an irq timing when an interrupt occurs + * + * @desc: the irq descriptor + * + * For all interruptions with their IRQS_TIMINGS flag set, the function + * computes the time interval between two interrupt events and store it + * in a circular buffer. + */ +void __handle_timings(struct irq_desc *desc) +{ + struct irq_timings *timings; + u64 prev, now, diff; + + if (!(desc->istate & IRQS_TIMINGS)) + return; + + timings = this_cpu_ptr(desc->timings); + now = local_clock(); + prev = timings->timestamp; + timings->timestamp = now; + + /* + * If it is the first interrupt of the series, we can't + * compute an interval, just store the timestamp and exit. + */ + if (unlikely(!prev)) + return; + + diff = now - prev; + + /* + * microsec (actually 1024th of a milisec) precision is good + * enough for our purpose. + */ + diff >>= 10; + + /* + * There is no point to store intervals from interrupts more + * than ~1 second apart. Furthermore that increases the risk + * of overflowing our variance computation. Reset all values + * in that case. Otherwise we know the magnitude of diff is + * well within 32 bits. + */ + if (unlikely(diff > USEC_PER_SEC)) { + memset(timings, 0, sizeof(*timings)); + timings->timestamp = now; + return; + } + + /* The oldest value corresponds to the next index. */ + timings->w_index = (timings->w_index + 1) & IRQ_TIMINGS_MASK; + + /* + * Remove the oldest value from the summing. If this is the + * first time we go through this array slot, the previous + * value will be zero and we won't substract anything from the + * current sum. Hence this code relies on a zero-ed structure. + */ + timings->sum -= timings->values[timings->w_index]; + timings->values[timings->w_index] = diff; + timings->sum += diff; +} + +/** + * irqtiming_get_next - return the next irq timing + * + * @irq: a pointer to an integer representing the interrupt number + * + * This function allows to browse safely the interrupt descriptors in order + * to retrieve the interrupts timings. The parameter gives the interrupt + * number to begin with and will return the interrupt timings for the next + * allocated irq. This approach gives us the possibility to go through the + * different interrupts without having to handle the sparse irq. + * + * The function changes @irq to the next allocated irq + 1, it should be + * passed back again and again until NULL is returned. Usually this function + * is called the first time with @irq = 0. + * + * Returns a struct irq_timings, NULL if we reach the end of the interrupts + * list. + */ +struct irq_timings *irq_timings_get_next(int *irq) +{ + struct irq_desc *desc; + int next; + +again: + /* Do a racy lookup of the next allocated irq */ + next = irq_get_next_irq(*irq); + if (next >= nr_irqs) + return NULL; + + *irq = next + 1; + + /* + * Now lookup the descriptor. It's RCU protected. This + * descriptor might belong to an uninteresting interrupt or + * one that is not measured. Look for the next interrupt in + * that case. + */ + desc = irq_to_desc(next); + if (!desc || !(desc->istate & IRQS_TIMINGS)) + goto again; + + return this_cpu_ptr(desc->timings); +}