Skip to content

Commit b2d3d61

Browse files
dlezcanoKAGA-KOKO
authored andcommitted
genirq/timings: Add infrastructure to track the interrupt timings
The interrupt framework gives a lot of information about each interrupt. It does not keep track of when those interrupts occur though, which is a prerequisite for estimating the next interrupt arrival for power management purposes. Add a mechanism to record the timestamp for each interrupt occurrences in a per-CPU circular buffer to help with the prediction of the next occurrence using a statistical model. Each CPU can store up to IRQ_TIMINGS_SIZE events <irq, timestamp>, the current value of IRQ_TIMINGS_SIZE is 32. Each event is encoded into a single u64, where the high 48 bits are used for the timestamp and the low 16 bits are for the irq number. A static key is introduced so when the irq prediction is switched off at runtime, the overhead is near to zero. It results in most of the code in internals.h for inline reasons and a very few in the new file timings.c. The latter will contain more in the next patch which will provide the statistical model for the next event prediction. Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Nicolas Pitre <nicolas.pitre@linaro.org> Cc: Jens Axboe <axboe@kernel.dk> Cc: Hannes Reinecke <hare@suse.com> Cc: Vincent Guittot <vincent.guittot@linaro.org> Cc: "Rafael J . Wysocki" <rafael@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Bjorn Helgaas <bhelgaas@google.com> Link: http://lkml.kernel.org/r/1498227072-5980-1-git-send-email-daniel.lezcano@linaro.org
1 parent c2ce34c commit b2d3d61

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

include/linux/interrupt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,11 @@ static inline void init_irq_proc(void)
703703
}
704704
#endif
705705

706+
#ifdef CONFIG_IRQ_TIMINGS
707+
void irq_timings_enable(void);
708+
void irq_timings_disable(void);
709+
#endif
710+
706711
struct seq_file;
707712
int show_interrupts(struct seq_file *p, void *v);
708713
int arch_show_interrupts(struct seq_file *p, int prec);

kernel/irq/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ config GENERIC_MSI_IRQ_DOMAIN
8585
config HANDLE_DOMAIN_IRQ
8686
bool
8787

88+
config IRQ_TIMINGS
89+
bool
90+
8891
config IRQ_DOMAIN_DEBUG
8992
bool "Expose hardware/virtual IRQ mapping via debugfs"
9093
depends on IRQ_DOMAIN && DEBUG_FS

kernel/irq/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
obj-y := irqdesc.o handle.o manage.o spurious.o resend.o chip.o dummychip.o devres.o
3+
obj-$(CONFIG_IRQ_TIMINGS) += timings.o
34
obj-$(CONFIG_GENERIC_IRQ_CHIP) += generic-chip.o
45
obj-$(CONFIG_GENERIC_IRQ_PROBE) += autoprobe.o
56
obj-$(CONFIG_IRQ_DOMAIN) += irqdomain.o

kernel/irq/handle.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags
138138
unsigned int irq = desc->irq_data.irq;
139139
struct irqaction *action;
140140

141+
record_irq_time(desc);
142+
141143
for_each_action_of_desc(desc, action) {
142144
irqreturn_t res;
143145

kernel/irq/internals.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/irqdesc.h>
99
#include <linux/kernel_stat.h>
1010
#include <linux/pm_runtime.h>
11+
#include <linux/sched/clock.h>
1112

1213
#ifdef CONFIG_SPARSE_IRQ
1314
# define IRQ_BITMAP_BITS (NR_IRQS + 8196)
@@ -57,6 +58,7 @@ enum {
5758
IRQS_WAITING = 0x00000080,
5859
IRQS_PENDING = 0x00000200,
5960
IRQS_SUSPENDED = 0x00000800,
61+
IRQS_TIMINGS = 0x00001000,
6062
};
6163

6264
#include "debug.h"
@@ -255,6 +257,94 @@ static inline void
255257
irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) { }
256258
#endif
257259

260+
#ifdef CONFIG_IRQ_TIMINGS
261+
262+
#define IRQ_TIMINGS_SHIFT 5
263+
#define IRQ_TIMINGS_SIZE (1 << IRQ_TIMINGS_SHIFT)
264+
#define IRQ_TIMINGS_MASK (IRQ_TIMINGS_SIZE - 1)
265+
266+
/**
267+
* struct irq_timings - irq timings storing structure
268+
* @values: a circular buffer of u64 encoded <timestamp,irq> values
269+
* @count: the number of elements in the array
270+
*/
271+
struct irq_timings {
272+
u64 values[IRQ_TIMINGS_SIZE];
273+
int count;
274+
};
275+
276+
DECLARE_PER_CPU(struct irq_timings, irq_timings);
277+
278+
static inline void irq_remove_timings(struct irq_desc *desc)
279+
{
280+
desc->istate &= ~IRQS_TIMINGS;
281+
}
282+
283+
static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *act)
284+
{
285+
/*
286+
* We don't need the measurement because the idle code already
287+
* knows the next expiry event.
288+
*/
289+
if (act->flags & __IRQF_TIMER)
290+
return;
291+
292+
desc->istate |= IRQS_TIMINGS;
293+
}
294+
295+
extern void irq_timings_enable(void);
296+
extern void irq_timings_disable(void);
297+
298+
DECLARE_STATIC_KEY_FALSE(irq_timing_enabled);
299+
300+
/*
301+
* The interrupt number and the timestamp are encoded into a single
302+
* u64 variable to optimize the size.
303+
* 48 bit time stamp and 16 bit IRQ number is way sufficient.
304+
* Who cares an IRQ after 78 hours of idle time?
305+
*/
306+
static inline u64 irq_timing_encode(u64 timestamp, int irq)
307+
{
308+
return (timestamp << 16) | irq;
309+
}
310+
311+
static inline int irq_timing_decode(u64 value, u64 *timestamp)
312+
{
313+
*timestamp = value >> 16;
314+
return value & U16_MAX;
315+
}
316+
317+
/*
318+
* The function record_irq_time is only called in one place in the
319+
* interrupts handler. We want this function always inline so the code
320+
* inside is embedded in the function and the static key branching
321+
* code can act at the higher level. Without the explicit
322+
* __always_inline we can end up with a function call and a small
323+
* overhead in the hotpath for nothing.
324+
*/
325+
static __always_inline void record_irq_time(struct irq_desc *desc)
326+
{
327+
if (!static_branch_likely(&irq_timing_enabled))
328+
return;
329+
330+
if (desc->istate & IRQS_TIMINGS) {
331+
struct irq_timings *timings = this_cpu_ptr(&irq_timings);
332+
333+
timings->values[timings->count & IRQ_TIMINGS_MASK] =
334+
irq_timing_encode(local_clock(),
335+
irq_desc_get_irq(desc));
336+
337+
timings->count++;
338+
}
339+
}
340+
#else
341+
static inline void irq_remove_timings(struct irq_desc *desc) {}
342+
static inline void irq_setup_timings(struct irq_desc *desc,
343+
struct irqaction *act) {};
344+
static inline void record_irq_time(struct irq_desc *desc) {}
345+
#endif /* CONFIG_IRQ_TIMINGS */
346+
347+
258348
#ifdef CONFIG_GENERIC_IRQ_CHIP
259349
void irq_init_generic_chip(struct irq_chip_generic *gc, const char *name,
260350
int num_ct, unsigned int irq_base,

kernel/irq/manage.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,8 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
13481348

13491349
raw_spin_unlock_irqrestore(&desc->lock, flags);
13501350

1351+
irq_setup_timings(desc, new);
1352+
13511353
/*
13521354
* Strictly no need to wake it up, but hung_task complains
13531355
* when no hard interrupt wakes the thread up.
@@ -1474,6 +1476,7 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
14741476
irq_settings_clr_disable_unlazy(desc);
14751477
irq_shutdown(desc);
14761478
irq_release_resources(desc);
1479+
irq_remove_timings(desc);
14771480
}
14781481

14791482
#ifdef CONFIG_SMP

kernel/irq/timings.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* linux/kernel/irq/timings.c
3+
*
4+
* Copyright (C) 2016, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation.
9+
*
10+
*/
11+
#include <linux/percpu.h>
12+
#include <linux/static_key.h>
13+
#include <linux/interrupt.h>
14+
#include <linux/irq.h>
15+
16+
#include "internals.h"
17+
18+
DEFINE_STATIC_KEY_FALSE(irq_timing_enabled);
19+
20+
DEFINE_PER_CPU(struct irq_timings, irq_timings);
21+
22+
void irq_timings_enable(void)
23+
{
24+
static_branch_enable(&irq_timing_enabled);
25+
}
26+
27+
void irq_timings_disable(void)
28+
{
29+
static_branch_disable(&irq_timing_enabled);
30+
}

0 commit comments

Comments
 (0)