[PATCH 2/3] perf/cputime: Fix idle time on NO_HZ config

From: Jiri Olsa
Date: Sun Nov 11 2018 - 16:04:42 EST


In case there's NO_HZ enabled we won't get proper numbers
for idle counter if the tick is disabled on the cpu.

Making up for it by counting the idle counter value if
the tick is stopped, which will keep the counter number
updated at the time it is read.

Link: http://lkml.kernel.org/n/tip-rw8kylf86mkfv60blwu5iyqr@xxxxxxxxxxxxxx
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
include/linux/tick.h | 1 +
kernel/events/cputime.c | 25 ++++++++++++++++++++++++-
kernel/time/tick-sched.c | 11 +++++++++++
3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/include/linux/tick.h b/include/linux/tick.h
index 55388ab45fd4..17aaaae18a3c 100644
--- a/include/linux/tick.h
+++ b/include/linux/tick.h
@@ -125,6 +125,7 @@ extern bool tick_nohz_idle_got_tick(void);
extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next);
extern unsigned long tick_nohz_get_idle_calls(void);
extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu);
+extern unsigned long tick_nohz_get_idle_jiffies_cpu(int cpu);
extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);

diff --git a/kernel/events/cputime.c b/kernel/events/cputime.c
index efad24543f13..c49e73713263 100644
--- a/kernel/events/cputime.c
+++ b/kernel/events/cputime.c
@@ -1,6 +1,7 @@
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/perf_event.h>
+#include <linux/tick.h>

enum perf_cputime_id {
PERF_CPUTIME_USER,
@@ -102,11 +103,33 @@ static const struct attribute_group *cputime_attr_groups[] = {
NULL,
};

+#ifdef CONFIG_NO_HZ_COMMON
+static u64 idle_fix(int cpu)
+{
+ u64 ticks;
+
+ if (!tick_nohz_tick_stopped_cpu(cpu))
+ return 0;
+
+ ticks = jiffies - tick_nohz_get_idle_jiffies_cpu(cpu);
+ return ticks * TICK_NSEC;
+}
+#else
+static u64 idle_fix(int cpu)
+{
+ return 0;
+}
+#endif
+
static u64 cputime_read_counter(struct perf_event *event)
{
int cpu = event->oncpu;
+ u64 val = kcpustat_cpu(cpu).cpustat[event->hw.config];
+
+ if (event->hw.config == PERF_CPUTIME_IDLE)
+ val += idle_fix(cpu);

- return kcpustat_cpu(cpu).cpustat[event->hw.config];
+ return val;
}

static void perf_cputime_update(struct perf_event *event)
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 69e673b88474..c5df46e3c9f5 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -1077,6 +1077,17 @@ unsigned long tick_nohz_get_idle_calls_cpu(int cpu)
return ts->idle_calls;
}

+/**
+ * tick_nohz_get_idle_jiffies_cpu - return the current idle jiffies counter value
+ * for a particular CPU.
+ */
+unsigned long tick_nohz_get_idle_jiffies_cpu(int cpu)
+{
+ struct tick_sched *ts = tick_get_tick_sched(cpu);
+
+ return ts->idle_jiffies;
+}
+
/**
* tick_nohz_get_idle_calls - return the current idle calls counter value
*
--
2.17.2