mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Liu Chao <liuchao173@huawei.com>
To: <linux-kernel@vger.kernel.org>, <tglx@linutronix.de>
Cc: <hewenliang4@huawei.com>, <linfeilong@huawei.com>
Subject: [PATCH] add miss time to CPUTIME_USER when kernel boots with highres=off
Date: Sat, 30 Jul 2022 17:14:56 +0800	[thread overview]
Message-ID: <20220730091456.3741503-1-liuchao173@huawei.com> (raw)

When kernel boots with highres=off and the cpu load isn't full, almost
every tick will account towards CPUTIME_IDLE. But in /proc/stat, idle
time is obtained from get_idle_time.
So the sum time will lose the time of user tasks.

For example:
 # stress-ng --cpu 2 --taskset 66 --timeout 600 --cpu-load 50 &
 # cat /proc/stat | grep "cpu66";sleep 10; cat /proc/stat | grep "cpu66"
 cpu66 1038 0 72 50124 75 17 1 0 0 0
 cpu66 1038 0 72 50569 75 18 2 0 0 0

the detla of sum time is 447, which should be 1000 actually. It will
casue the data in top is imprecise.

Signed-off-by: Liu Chao <liuchao173@huawei.com>
---
 fs/proc/stat.c          | 5 +++++
 include/linux/hrtimer.h | 2 ++
 kernel/time/hrtimer.c   | 2 +-
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/fs/proc/stat.c b/fs/proc/stat.c
index 4fb8729a68d4..c5b9116e74ad 100644
--- a/fs/proc/stat.c
+++ b/fs/proc/stat.c
@@ -141,6 +141,9 @@ static int show_stat(struct seq_file *p, void *v)
 		sum		+= kstat_cpu_irqs_sum(i);
 		sum		+= arch_irq_stat_cpu(i);
 
+		if (hrtimer_is_hres_enabled() == 0)
+			user	+= cpustat[CPUTIME_IDLE] - get_idle_time(&kcpustat, i);
+
 		for (j = 0; j < NR_SOFTIRQS; j++) {
 			unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
 
@@ -179,6 +182,8 @@ static int show_stat(struct seq_file *p, void *v)
 		steal		= cpustat[CPUTIME_STEAL];
 		guest		= cpustat[CPUTIME_GUEST];
 		guest_nice	= cpustat[CPUTIME_GUEST_NICE];
+		if (hrtimer_is_hres_enabled() == 0)
+			user	+=  cpustat[CPUTIME_IDLE] - idle;
 		seq_printf(p, "cpu%d", i);
 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(user));
 		seq_put_decimal_ull(p, " ", nsec_to_clock_t(nice));
diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index 0ee140176f10..4fa1bbe86287 100644
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -326,6 +326,8 @@ extern unsigned int hrtimer_resolution;
 
 #endif
 
+extern int hrtimer_is_hres_enabled(void);
+
 static inline ktime_t
 __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
 {
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 0ea8702eb516..3ba2d9fe492c 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -722,7 +722,7 @@ __setup("highres=", setup_hrtimer_hres);
 /*
  * hrtimer_high_res_enabled - query, if the highres mode is enabled
  */
-static inline int hrtimer_is_hres_enabled(void)
+int hrtimer_is_hres_enabled(void)
 {
 	return hrtimer_hres_enabled;
 }
-- 
2.23.0


             reply	other threads:[~2022-07-30  9:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-30  9:14 Liu Chao [this message]
2022-07-30 11:59 ` kernel test robot
2022-07-30 13:10 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220730091456.3741503-1-liuchao173@huawei.com \
    --to=liuchao173@huawei.com \
    --cc=hewenliang4@huawei.com \
    --cc=linfeilong@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®