Skip to contentSkip to conversation

irq: Track the interrupt timings

PATCH In review Daniel Lezcano 3 participants 6 messages Jun 23, 2017 – Jul 19, 2017 IRQ

lore
Share & tools
lore
#0 lore raw

PATCH v12 1/2 irq: 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.

This patch provides a mean 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 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 48 bits are used for the timestamp and the next 16 bits are for the irq number.

A static key is introduced so when the irq prediction is switched off at runtime, we can reduce the overhead near to zero.

It results in most of the code in internals.h for inline reason 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.

Note this code is by default *not* compiled in the kernel.

Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Vincent Guittot <[email protected]>

---

Changelog:

V12:
- Changed struct irq_timings comments to kerneldoc format
- Changed struct irq_timings name to be tabular aligned
- Renamed remove_timings to irq_remove_timings
- Renamed setup_timings to irq_setup_timings
- Used macro DECLARE_STATIC_KEY_FALSE instead of raw declaration
- Made irq_timing_decode() to return the irq number
V11:
- No changes
V10:
- No changes
V9:
- Changed indentation level by inverting the static key condition
- Encoded interrupt and timestamp into a u64 variable
- Boolean enable instead of refcount for the static key
V8:
- Replaced percpu field in the irqdesc by a percpu array containing the timings and the associated irq. The function irq_timings_get_next()is no longer needed, so it is removed
- Removed all unused code resulting from the conversion irqdesc->percpu timings storage
V7:
- Mentionned in the irq_timings_get_next() function description, the function must be called inside a rcu read locked section
V6:
- Renamed handle_irq_timings to record_irq_time
- Stored the event time instead of the interval time
- Removed the 'timestamp' field from the timings structure
- Moved _handle_irq_timings content inside record_irq_time
V5:
- Changed comment about 'deterministic' as the comment is confusing
- Added license comment in the header
- Replaced irq_timings_get/put by irq_timings_enable/disable
- Moved IRQS_TIMINGS check in the handle_timings inline function
- Dropped 'if !prev' as it is pointless
- Stored time interval in nsec basis with u64 instead of u32
- Removed redundant store
- Removed the math
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

---
 include/linux/interrupt.h |  5 +++
 kernel/irq/Kconfig        |  3 ++
 kernel/irq/Makefile       |  1 +
 kernel/irq/handle.c       |  2 ++
 kernel/irq/internals.h    | 89 +++++++++++++++++++++++++++++++++++++++++++++++
 kernel/irq/manage.c       |  3 ++
 kernel/irq/timings.c      | 30 ++++++++++++++++
 7 files changed, 133 insertions(+)
 create mode 100644 kernel/irq/timings.c
include/linux/interrupt.h +5 -0
index a6fba48..9f61723 100644
@@ -703,6 +703,11 @@ static inline void init_irq_proc(void)
 }
 #endif
 
+#ifdef CONFIG_IRQ_TIMINGS
+void irq_timings_enable(void);
+void irq_timings_disable(void);
+#endif
+
 struct seq_file;
 int show_interrupts(struct seq_file *p, void *v);
 int arch_show_interrupts(struct seq_file *p, int prec);
kernel/irq/Kconfig +3 -0
index 3bbfd6a..38e551d 100644
@@ -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
kernel/irq/Makefile +1 -0
index 1d3ee31..efb5f14 100644
@@ -10,3 +10,4 @@ obj-$(CONFIG_PM_SLEEP) += pm.o
 obj-$(CONFIG_GENERIC_MSI_IRQ) += msi.o
 obj-$(CONFIG_GENERIC_IRQ_IPI) += ipi.o
 obj-$(CONFIG_SMP) += affinity.o
+obj-$(CONFIG_IRQ_TIMINGS) += timings.o
kernel/irq/handle.c +2 -0
index d3f2490..eb4d3e8 100644
@@ -138,6 +138,8 @@ irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags
 	unsigned int irq = desc->irq_data.irq;
 	struct irqaction *action;
 
+	record_irq_time(desc);
+
 	for_each_action_of_desc(desc, action) {
 		irqreturn_t res;
 
kernel/irq/internals.h +89 -0

104 lines not on this page · raw message

kernel/irq/manage.c +3 -0
index 4c34696..45ad6ee 100644
@@ -1388,6 +1388,8 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
 
 	raw_spin_unlock_irqrestore(&desc->lock, flags);
 
+	irq_setup_timings(desc, new);
+
 	/*
 	 * Strictly no need to wake it up, but hung_task complains
 	 * when no hard interrupt wakes the thread up.
@@ -1516,6 +1518,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);
+		irq_remove_timings(desc);
 	}
 
 #ifdef CONFIG_SMP
kernel/irq/timings.c new file +30 -0
new file mode 100644
index 0000000..56cf687
@@ -0,0 +1,30 @@
+/*
+ * linux/kernel/irq/timings.c
+ *
+ * Copyright (C) 2016, Linaro Ltd - Daniel Lezcano <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#include <linux/percpu.h>
+#include <linux/static_key.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+
+#include "internals.h"
+
+DEFINE_STATIC_KEY_FALSE(irq_timing_enabled);
+
+DEFINE_PER_CPU(struct irq_timings, irq_timings);
+
+void irq_timings_enable(void)
+{
+	static_branch_enable(&irq_timing_enabled);
+}
+
+void irq_timings_disable(void)
+{
+	static_branch_disable(&irq_timing_enabled);
+}
Signature · 1 line
2.7.4
#1 lore raw

PATCH v12 2/2 irq: Compute the periodic interval for interrupts

An interrupt behaves with a burst of activity with periodic interval of time followed by one or two peaks of longer interval.

As the time intervals are periodic, statistically speaking they follow a normal distribution and each interrupts can be tracked individually.

This patch does statistics on all interrupts, except the timers which are deterministic by essence. The goal is to extract the periodicity for each interrupt, with the last timestamp and sum them, so we have the next event.

Taking the earliest prediction gives the expected wakeup on the system (assuming a timer won't expire before).

As stated in the previous patch, this code is not enabled in the kernel by default.

Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc Nicolas Pitre <[email protected]>

---
Changelog:

V12:
- Folded unsigned int -> int change into the first patch
- Made struct irqt_stat fields names more explicit and tabulate spaced
- Removed comment warning for irq_disabled in irqs_update()
- Moved function warning for irq_disabled with a check and comment in irq_timings_next_event()
- Inverted irq_timings_alloc() variable declaratin order
V11:
- No changes
V10:
- Simplified index/count computation
V9:
- Deal with 48+16 bits encoded values
- Changed irq_stat => irqt_stat to prevent name collision on s390
- Changed div64 by constant IRQ_TIMINGS_SHIFT bit shift for average
- Changed div64 by constant IRQ_TIMINGS_SHIFT bit shift for variance

---
 include/linux/interrupt.h |   1 +
 kernel/irq/internals.h    |  19 +++
 kernel/irq/timings.c      | 339 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 359 insertions(+)
include/linux/interrupt.h +1 -0
index 9f61723..37f8e35 100644
@@ -706,6 +706,7 @@ static inline void init_irq_proc(void)
 #ifdef CONFIG_IRQ_TIMINGS
 void irq_timings_enable(void);
 void irq_timings_disable(void);
+u64 irq_timings_next_event(u64 now);
 #endif
 
 struct seq_file;
kernel/irq/internals.h +19 -0
index ec86e0e..3d989a0 100644
@@ -247,13 +247,21 @@ struct irq_timings {
 
 DECLARE_PER_CPU(struct irq_timings, irq_timings);
 
+extern void irq_timings_free(int irq);
+extern int irq_timings_alloc(int irq);
+
 static inline void irq_remove_timings(struct irq_desc *desc)
 {
 	desc->istate &= ~IRQS_TIMINGS;
+
+	irq_timings_free(irq_desc_get_irq(desc));
 }
 
 static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *act)
 {
+	int irq = irq_desc_get_irq(desc);
+	int ret;
+
 	/*
 	 * We don't need the measurement because the idle code already
 	 * knows the next expiry event.
@@ -261,6 +269,17 @@ static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *ac
 	if (act->flags & __IRQF_TIMER)
 		return;
 
+	/*
+	 * In case the timing allocation fails, we just want to warn,
+	 * not fail, so letting the system boot anyway.
+	 */
+	ret = irq_timings_alloc(irq);
+	if (ret) {
+		pr_warn("Failed to allocate irq timing stats for irq%d (%d)",
+			irq, ret);
+		return;
+	}
+
 	desc->istate |= IRQS_TIMINGS;
 }
 
kernel/irq/timings.c +339 -0

358 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

[tip:irq/core] genirq/timings: Add infrastructure for estimating the next interrupt arrival time

genirq/timings: Add infrastructure for estimating the next interrupt arrival time
Commit-ID:  e1c921495534002d727b15a76a2f8c20b6b108b5
Gitweb:     http://git.kernel.org/tip/e1c921495534002d727b15a76a2f8c20b6b108b5
Author:     Daniel Lezcano <[email protected]>
AuthorDate: Fri, 23 Jun 2017 16:11:08 +0200
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Sat, 24 Jun 2017 11:44:39 +0200

genirq/timings: Add infrastructure for estimating the next interrupt arrival time

An interrupt behaves with a burst of activity with periodic interval of time followed by one or two peaks of longer interval.

As the time intervals are periodic, statistically speaking they follow a normal distribution and each interrupts can be tracked individually.

Add a mechanism to compute the statistics on all interrupts, except the timers which are deterministic from a prediction point of view, as their expiry time is known.

The goal is to extract the periodicity for each interrupt, with the last timestamp and sum them, so the next event can be predicted to a certain extent.

Taking the earliest prediction gives the expected wakeup on the system (assuming a timer won't expire before).

Cc Nicolas Pitre <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Vincent Guittot <[email protected]>
Cc "Rafael J . Wysocki" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Bjorn Helgaas <[email protected]>
---
 include/linux/interrupt.h |   1 +
 kernel/irq/internals.h    |  19 +++
 kernel/irq/timings.c      | 339 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 359 insertions(+)
include/linux/interrupt.h +1 -0
index 9f61723..37f8e35 100644
@@ -706,6 +706,7 @@ static inline void init_irq_proc(void)
 #ifdef CONFIG_IRQ_TIMINGS
 void irq_timings_enable(void);
 void irq_timings_disable(void);
+u64 irq_timings_next_event(u64 now);
 #endif
 
 struct seq_file;
kernel/irq/internals.h +19 -0
index b95b749..9da14d1 100644
@@ -275,13 +275,21 @@ struct irq_timings {
 
 DECLARE_PER_CPU(struct irq_timings, irq_timings);
 
+extern void irq_timings_free(int irq);
+extern int irq_timings_alloc(int irq);
+
 static inline void irq_remove_timings(struct irq_desc *desc)
 {
 	desc->istate &= ~IRQS_TIMINGS;
+
+	irq_timings_free(irq_desc_get_irq(desc));
 }
 
 static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *act)
 {
+	int irq = irq_desc_get_irq(desc);
+	int ret;
+
 	/*
 	 * We don't need the measurement because the idle code already
 	 * knows the next expiry event.
@@ -289,6 +297,17 @@ static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *ac
 	if (act->flags & __IRQF_TIMER)
 		return;
 
+	/*
+	 * In case the timing allocation fails, we just want to warn,
+	 * not fail, so letting the system boot anyway.
+	 */
+	ret = irq_timings_alloc(irq);
+	if (ret) {
+		pr_warn("Failed to allocate irq timing stats for irq%d (%d)",
+			irq, ret);
+		return;
+	}
+
 	desc->istate |= IRQS_TIMINGS;
 }
 
kernel/irq/timings.c +339 -0

358 lines not on this page · raw message

#3 lore raw

Re: [tip:irq/core] genirq/timings: Add infrastructure for estimating the next interrupt arrival time

Quoted 19 lines · tip-bot for Daniel Lezcano
(quoted diff) +19 -0
+	/*
+	 * Look in the list of interrupts' statistics, the earliest
+	 * next event.
+	 */
+	idr_for_each_entry(&irqt_stats, s, i) {
+
+		irqs = this_cpu_ptr(s);
+
+		if (!irqs->valid)
+			continue;
+
+		if (irqs->next_evt <= now) {
+			irq = i;
+			next_evt = now;
+
+			/*
+			 * This interrupt mustn't use in the future
+			 * until new events occur and update the
+			 * statistics.

Review

Quoted 10 lines
(quoted diff) +10 -0
+			 */
+			irqs->valid = 0;
+			break;
+		}
+
+		if (irqs->next_evt < next_evt) {
+			irq = i;
+			next_evt = irqs->next_evt;
+		}
+	}

That is a fairly expensive thing to do.. sure the 'continue' avoids the very worst of it, but its still painful.

How about something like so on top? (compile tested only)

---
 kernel/irq/internals.h |  4 +++-
 kernel/irq/irqdesc.c   |  2 ++
 kernel/irq/timings.c   | 35 ++++++++++++++++++++++-------------
 3 files changed, 27 insertions(+), 14 deletions(-)
kernel/irq/internals.h +3 -1
index dbfba9933ed2..b96511ca7cd3 100644
@@ -275,6 +275,7 @@ struct irq_timings {
 
 DECLARE_PER_CPU(struct irq_timings, irq_timings);
 
+extern void irq_timings_init(void);
 extern void irq_timings_free(int irq);
 extern int irq_timings_alloc(int irq);
 
@@ -357,9 +358,10 @@ static __always_inline void record_irq_time(struct irq_desc *desc)
 	}
 }
 #else
+static inline void irq_timings_init(void) {}
 static inline void irq_remove_timings(struct irq_desc *desc) {}
 static inline void irq_setup_timings(struct irq_desc *desc,
-				     struct irqaction *act) {};
+				     struct irqaction *act) {}
 static inline void record_irq_time(struct irq_desc *desc) {}
 #endif /* CONFIG_IRQ_TIMINGS */
 
kernel/irq/irqdesc.c +2 -0
index 73be2b3909bd..17e95592f291 100644
@@ -493,6 +493,7 @@ int __init early_irq_init(void)
 	struct irq_desc *desc;
 
 	init_irq_default_affinity();
+	irq_timings_init();
 
 	/* Let arch update nr_irqs and return the nr of preallocated irqs */
 	initcnt = arch_probe_nr_irqs();
@@ -532,6 +533,7 @@ int __init early_irq_init(void)
 	struct irq_desc *desc;
 
 	init_irq_default_affinity();
+	irq_timings_init();
 
 	printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
 
kernel/irq/timings.c +22 -13
index c8c1d073fbf1..6bddfbb8d419 100644
@@ -24,15 +24,17 @@
 DEFINE_STATIC_KEY_FALSE(irq_timing_enabled);
 
 DEFINE_PER_CPU(struct irq_timings, irq_timings);
+static DEFINE_PER_CPU(struct list_head, irqt_list);
 
 struct irqt_stat {
-	u64	next_evt;
-	u64	last_ts;
-	u64	variance;
-	u32	avg;
-	u32	nr_samples;
-	int	anomalies;
-	int	valid;
+	u64			next_evt;
+	u64			last_ts;
+	u64			variance;
+	u32			avg;
+	u32			nr_samples;
+	int			anomalies;
+	int			valid;
+	struct list_head	list;
 };
 
 static DEFINE_IDR(irqt_stats);
@@ -197,6 +199,7 @@ static void irqs_update(struct irqt_stat *irqs, u64 ts)
 	 * the next event on it.
 	 */
 	irqs->valid = 1;
+	list_add(&irqs->list, this_cpu_ptr(&irqt_list));
 
 	/*
 	 * Online average algorithm:
@@ -254,8 +257,8 @@ static void irqs_update(struct irqt_stat *irqs, u64 ts)
 u64 irq_timings_next_event(u64 now)
 {
 	struct irq_timings *irqts = this_cpu_ptr(&irq_timings);
-	struct irqt_stat *irqs;
 	struct irqt_stat __percpu *s;
+	struct irqt_stat *irqs, *tmp;
 	u64 ts, next_evt = U64_MAX;
 	int i, irq = 0;
 
@@ -297,11 +300,8 @@ u64 irq_timings_next_event(u64 now)
 	 * Look in the list of interrupts' statistics, the earliest
 	 * next event.
 	 */
-	idr_for_each_entry(&irqt_stats, s, i) {
-
-		irqs = this_cpu_ptr(s);
-
-		if (!irqs->valid)
+	list_for_each_entry_safe(irqs, tmp, this_cpu_ptr(&irqt_list), list) {
+		if (WARN_ON_ONCE(!irqs->valid))
 			continue;
 
 		if (irqs->next_evt <= now) {
@@ -314,6 +314,7 @@ u64 irq_timings_next_event(u64 now)
 			 * statistics.
 			 */
 			irqs->valid = 0;
+			list_del(&irqs->list);
 			break;
 		}
 
@@ -367,3 +368,11 @@ int irq_timings_alloc(int irq)
 
 	return 0;
 }
+
+void __init irq_timings_init(void)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu)
+		INIT_LIST_HEAD(&per_cpu(irqt_list, cpu));
+}
#4 lore raw

Re: [tip:irq/core] genirq/timings: Add infrastructure for estimating the next interrupt arrival time

In reply to tip-bot for Daniel Lezcano
Quoted 22 lines · tip-bot for Daniel Lezcano
(quoted diff) +22 -0
+	/*
+	 * The interrupt is considered stable enough to try to predict
+	 * the next event on it.
+	 */
+	irqs->valid = 1;
+
+	/*
+	 * Online average algorithm:
+	 *
+	 *  new_average = average + ((value - average) / count)
+	 *
+	 * The variance computation depends on the new average
+	 * to be computed here first.
+	 *
+	 */
+	irqs->avg = irqs->avg + (diff >> IRQ_TIMINGS_SHIFT);
+
+	/*
+	 * Online variance algorithm:
+	 *
+	 *  new_variance = variance + (value - average) x (value - new_average)
+	 *

Review

Quoted 10 lines
(quoted diff) +10 -0
+	 * Warning: irqs->avg is updated with the line above, hence
+	 * 'interval - irqs->avg' is no longer equal to 'diff'
+	 */
+	irqs->variance = irqs->variance + (diff * (interval - irqs->avg));
+
+	/*
+	 * Update the next event
+	 */
+	irqs->next_evt = ts + irqs->avg;
+}

This implements the CDF bias I spoke of in that other thread. Very much RFC, has not in fact been near a compiler.

---
Subject: irq/timings: Add estimation bias
From: Peter Zijlstra <[email protected]>
Date: Wed Jul 19 14:59:17 CEST 2017

When estimating a normal random variable the average is, per definition, too long 50% of the time.

When we use this variable to select idle states, picking too deep an idle state results in increased exit latency, which, if the estimate was wrong, results in decreased performance.

Hence we'd like to be able to bias our estimate to be short a specific percent of the time. We can readily compute this using the Cumulative Distribution Function, since the CDF(x) gives P(X <= x).

Implement this using a inverse Z table for the normal CDF.

======8<====[ cdf.cc ]====>8======

#include <boost/math/special_functions/erf.hpp>
#include <math.h>
#include <stdio.h>

#define CDF_BITS 16

int main(void)

{
        double z;
        int i;

        printf("#define CDF_BITS\t%d\n\n", CDF_BITS);
        printf("static const int normal_inv_cdf[] = {\n");

        for (i=50; i<100; i++) {
                z = sqrt(2.0) * boost::math::erf_inv( (2*(double)i)/100.0 - 1.0 );

                printf("\t/* %02d%% */ 0x%05x,\n", i, (int)round((1 << CDF_BITS) * z));
        }

        printf("};\n");

        return 0;
}
---
 include/linux/normal-cdf.h |   74 +++++++++++++++++++++++++++++++++++++++++++++
 kernel/irq/timings.c       |   23 +++++++++++++
 2 files changed, 97 insertions(+)
include/linux/normal-cdf.h new file +74 -0
@@ -0,0 +1,74 @@
+#ifndef _LINUX_NORMAL_CDF_H
+#define _LINUX_NORMAL_CDF_H
+
+#define NORMAL_CDF_BITS	16
+
+/*
+ *     x - mu
+ * Z = ------
+ *     sigma
+ *
+ *
+ *          1              Z
+ * CDF(x) = - [1 + erf( ------ )] = p
+ *          2           sqrt(2)
+ *
+ *                  -1
+ * Z = sqrt(2) * erf  (2p - 1)
+ */
+
+static const int normal_inv_cdf_Z[] = {
+	/* 50% */ 0x00000,
+	/* 51% */ 0x0066b,
+	/* 52% */ 0x00cd7,
+	/* 53% */ 0x01345,
+	/* 54% */ 0x019b6,
+	/* 55% */ 0x0202b,
+	/* 56% */ 0x026a6,
+	/* 57% */ 0x02d27,
+	/* 58% */ 0x033af,
+	/* 59% */ 0x03a40,
+	/* 60% */ 0x040db,
+	/* 61% */ 0x04781,
+	/* 62% */ 0x04e34,
+	/* 63% */ 0x054f4,
+	/* 64% */ 0x05bc4,
+	/* 65% */ 0x062a4,
+	/* 66% */ 0x06997,
+	/* 67% */ 0x0709e,
+	/* 68% */ 0x077bb,
+	/* 69% */ 0x07ef0,
+	/* 70% */ 0x0863f,
+	/* 71% */ 0x08dab,
+	/* 72% */ 0x09535,
+	/* 73% */ 0x09ce1,
+	/* 74% */ 0x0a4b2,
+	/* 75% */ 0x0acab,
+	/* 76% */ 0x0b4d0,
+	/* 77% */ 0x0bd25,
+	/* 78% */ 0x0c5ae,
+	/* 79% */ 0x0ce72,
+	/* 80% */ 0x0d774,
+	/* 81% */ 0x0e0be,
+	/* 82% */ 0x0ea55,
+	/* 83% */ 0x0f444,
+	/* 84% */ 0x0fe95,
+	/* 85% */ 0x10954,
+	/* 86% */ 0x11490,
+	/* 87% */ 0x1205b,
+	/* 88% */ 0x12ccc,
+	/* 89% */ 0x139fe,
+	/* 90% */ 0x14814,
+	/* 91% */ 0x1573c,
+	/* 92% */ 0x167b3,
+	/* 93% */ 0x179cd,
+	/* 94% */ 0x18e06,
+	/* 95% */ 0x1a515,
+	/* 96% */ 0x1c02d,
+	/* 97% */ 0x1e17c,
+	/* 98% */ 0x20dc2,
+	/* 99% */ 0x2538c,
+};
+
+#endif /* _LINUX_NORMAL_CDF_H */
+
kernel/irq/timings.c +23 -0
@@ -16,6 +16,7 @@
 #include <linux/idr.h>
 #include <linux/irq.h>
 #include <linux/math64.h>
+#include <linux/normal-cdf.h>
 
 #include <trace/events/irq.h>
 
@@ -26,6 +27,21 @@ DEFINE_STATIC_KEY_FALSE(irq_timing_enabl
 DEFINE_PER_CPU(struct irq_timings, irq_timings);
 static DEFINE_PER_CPU(struct list_head, irqt_list);
 
+/*
+ * Used to reduce estimation error; where the average of the distribution
+ * is too long 50% of the time, we can compute the value for which we're no
+ * more than @pct too long using the CDF.
+ *
+ * This is important for performance reasons, picking too deep a C state
+ * results in increased exit latencies when we were wrong, and hence impacts
+ * performance. By changing the estimate to be shorter, we alleviate this.
+ *
+ * Default to 50% for a decent power / performance ratio.
+ *
+ * 1 <= irq_timings_pct <= 50
+ */
+int irq_timings_pct = 50;
+
 struct irqt_stat {
 	u64			next_evt;
 	u64			last_ts;
@@ -226,6 +242,13 @@ static void irqs_update(struct irqt_stat
 	 * Update the next event
 	 */
 	irqs->next_evt = ts + irqs->avg;
+
+	if (irq_timings_pct != 50) {
+		int Z = normal_inv_cdf_Z[50 - irq_timings_pct];
+		u64 stdev = int_sqrt(irqs->variance);
+
+		irqs->next_evt -= (Z * stdev) >> NORMAL_CDF_BITS;
+	}
 }
 
 /**
#5 lore raw

[tip:irq/core] genirq/timings: Add infrastructure to track the interrupt timings

In reply to Daniel Lezcano
genirq/timings: Add infrastructure to track the interrupt timings
Commit-ID:  b2d3d61adb7b73cfe5f82404f7a130a76fc64232
Gitweb:     http://git.kernel.org/tip/b2d3d61adb7b73cfe5f82404f7a130a76fc64232
Author:     Daniel Lezcano <[email protected]>
AuthorDate: Fri, 23 Jun 2017 16:11:07 +0200
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Sat, 24 Jun 2017 11:44:11 +0200

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.

---
 include/linux/interrupt.h |  5 +++
 kernel/irq/Kconfig        |  3 ++
 kernel/irq/Makefile       |  1 +
 kernel/irq/handle.c       |  2 ++
 kernel/irq/internals.h    | 90 +++++++++++++++++++++++++++++++++++++++++++++++
 kernel/irq/manage.c       |  3 ++
 kernel/irq/timings.c      | 30 ++++++++++++++++
 7 files changed, 134 insertions(+)
include/linux/interrupt.h +5 -0
index a6fba48..9f61723 100644
@@ -703,6 +703,11 @@ static inline void init_irq_proc(void)
 }
 #endif
 
+#ifdef CONFIG_IRQ_TIMINGS
+void irq_timings_enable(void);
+void irq_timings_disable(void);
+#endif
+
 struct seq_file;
 int show_interrupts(struct seq_file *p, void *v);
 int arch_show_interrupts(struct seq_file *p, int prec);
kernel/irq/Kconfig +3 -0
index fcbb1d6..27c4e77 100644
@@ -85,6 +85,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
kernel/irq/Makefile +1 -0
index c61fc9c..e4aef73 100644
@@ -1,5 +1,6 @@
 
 obj-y := irqdesc.o handle.o manage.o spurious.o resend.o chip.o dummychip.o devres.o
+obj-$(CONFIG_IRQ_TIMINGS) += timings.o
 obj-$(CONFIG_GENERIC_IRQ_CHIP) += generic-chip.o
 obj-$(CONFIG_GENERIC_IRQ_PROBE) += autoprobe.o
 obj-$(CONFIG_IRQ_DOMAIN) += irqdomain.o
kernel/irq/handle.c +2 -0
index d3f2490..eb4d3e8 100644
@@ -138,6 +138,8 @@ irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags
 	unsigned int irq = desc->irq_data.irq;
 	struct irqaction *action;
 
+	record_irq_time(desc);
+
 	for_each_action_of_desc(desc, action) {
 		irqreturn_t res;
 
kernel/irq/internals.h +90 -0

108 lines not on this page · raw message

kernel/irq/manage.c +3 -0
index 3577c09..5c11c17 100644
@@ -1348,6 +1348,8 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
 
 	raw_spin_unlock_irqrestore(&desc->lock, flags);
 
+	irq_setup_timings(desc, new);
+
 	/*
 	 * Strictly no need to wake it up, but hung_task complains
 	 * when no hard interrupt wakes the thread up.
@@ -1474,6 +1476,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);
+		irq_remove_timings(desc);
 	}
 
 #ifdef CONFIG_SMP
kernel/irq/timings.c new file +30 -0
new file mode 100644
index 0000000..56cf687
@@ -0,0 +1,30 @@
+/*
+ * linux/kernel/irq/timings.c
+ *
+ * Copyright (C) 2016, Linaro Ltd - Daniel Lezcano <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#include <linux/percpu.h>
+#include <linux/static_key.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+
+#include "internals.h"
+
+DEFINE_STATIC_KEY_FALSE(irq_timing_enabled);
+
+DEFINE_PER_CPU(struct irq_timings, irq_timings);
+
+void irq_timings_enable(void)
+{
+	static_branch_enable(&irq_timing_enabled);
+}
+
+void irq_timings_disable(void)
+{
+	static_branch_disable(&irq_timing_enabled);
+}