2022-07-30 09:29:24

by Liu Chao

[permalink] [raw]
Subject: [PATCH] add miss time to CPUTIME_USER when kernel boots with highres=off

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 <[email protected]>
---
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



2022-07-30 12:03:23

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] add miss time to CPUTIME_USER when kernel boots with highres=off

Hi Liu,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tip/timers/core]
[also build test ERROR on linus/master v5.19-rc8 next-20220728]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Liu-Chao/add-miss-time-to-CPUTIME_USER-when-kernel-boots-with-highres-off/20220730-171718
base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git cceeeb6a6d02e7b9a74ddd27a3225013b34174aa
config: arc-allnoconfig (https://download.01.org/0day-ci/archive/20220730/[email protected]/config)
compiler: arc-elf-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/7ac63e1383163d700eb4212b689a272a4763e185
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Liu-Chao/add-miss-time-to-CPUTIME_USER-when-kernel-boots-with-highres-off/20220730-171718
git checkout 7ac63e1383163d700eb4212b689a272a4763e185
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash kernel/time/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

kernel/time/hrtimer.c:120:35: warning: initialized field overwritten [-Woverride-init]
120 | [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
| ^~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:120:35: note: (near initialization for 'hrtimer_clock_to_base_table[0]')
kernel/time/hrtimer.c:121:35: warning: initialized field overwritten [-Woverride-init]
121 | [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
| ^~~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:121:35: note: (near initialization for 'hrtimer_clock_to_base_table[1]')
kernel/time/hrtimer.c:122:35: warning: initialized field overwritten [-Woverride-init]
122 | [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
| ^~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:122:35: note: (near initialization for 'hrtimer_clock_to_base_table[7]')
kernel/time/hrtimer.c:123:35: warning: initialized field overwritten [-Woverride-init]
123 | [CLOCK_TAI] = HRTIMER_BASE_TAI,
| ^~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:123:35: note: (near initialization for 'hrtimer_clock_to_base_table[11]')
>> kernel/time/hrtimer.c:754:19: error: static declaration of 'hrtimer_is_hres_enabled' follows non-static declaration
754 | static inline int hrtimer_is_hres_enabled(void) { return 0; }
| ^~~~~~~~~~~~~~~~~~~~~~~
In file included from include/linux/sched.h:19,
from include/linux/ratelimit.h:6,
from include/linux/dev_printk.h:16,
from include/linux/device.h:15,
from include/linux/node.h:18,
from include/linux/cpu.h:17,
from kernel/time/hrtimer.c:25:
include/linux/hrtimer.h:329:12: note: previous declaration of 'hrtimer_is_hres_enabled' with type 'int(void)'
329 | extern int hrtimer_is_hres_enabled(void);
| ^~~~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c: In function '__run_hrtimer':
kernel/time/hrtimer.c:1648:14: warning: variable 'expires_in_hardirq' set but not used [-Wunused-but-set-variable]
1648 | bool expires_in_hardirq;
| ^~~~~~~~~~~~~~~~~~


vim +/hrtimer_is_hres_enabled +754 kernel/time/hrtimer.c

54cdfdb47f73b5a kernel/hrtimer.c Thomas Gleixner 2007-02-16 753
54cdfdb47f73b5a kernel/hrtimer.c Thomas Gleixner 2007-02-16 @754 static inline int hrtimer_is_hres_enabled(void) { return 0; }
75e3b37d059856a kernel/time/hrtimer.c Luiz Capitulino 2015-08-11 755 static inline void hrtimer_switch_to_hres(void) { }
54cdfdb47f73b5a kernel/hrtimer.c Thomas Gleixner 2007-02-16 756

--
0-DAY CI Kernel Test Service
https://01.org/lkp

2022-07-30 13:14:24

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] add miss time to CPUTIME_USER when kernel boots with highres=off

Hi Liu,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tip/timers/core]
[also build test ERROR on linus/master v5.19-rc8 next-20220728]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Liu-Chao/add-miss-time-to-CPUTIME_USER-when-kernel-boots-with-highres-off/20220730-171718
base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git cceeeb6a6d02e7b9a74ddd27a3225013b34174aa
config: s390-randconfig-r044-20220729 (https://download.01.org/0day-ci/archive/20220730/[email protected]/config)
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 52cd00cabf479aa7eb6dbb063b7ba41ea57bce9e)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install s390 cross compiling tool for clang build
# apt-get install binutils-s390x-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/7ac63e1383163d700eb4212b689a272a4763e185
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Liu-Chao/add-miss-time-to-CPUTIME_USER-when-kernel-boots-with-highres-off/20220730-171718
git checkout 7ac63e1383163d700eb4212b689a272a4763e185
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=s390 SHELL=/bin/bash kernel/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

In file included from kernel/time/hrtimer.c:32:
In file included from include/linux/tick.h:8:
In file included from include/linux/clockchips.h:14:
In file included from include/linux/clocksource.h:22:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:464:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __raw_readb(PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:477:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
#define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
^
include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
#define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
^
In file included from kernel/time/hrtimer.c:32:
In file included from include/linux/tick.h:8:
In file included from include/linux/clockchips.h:14:
In file included from include/linux/clocksource.h:22:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:490:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
#define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
^
include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
#define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
^
In file included from kernel/time/hrtimer.c:32:
In file included from include/linux/tick.h:8:
In file included from include/linux/clockchips.h:14:
In file included from include/linux/clocksource.h:22:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:501:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writeb(value, PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:511:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:521:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:609:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsb(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:617:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsw(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:625:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
readsl(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:634:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesb(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:643:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesw(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
include/asm-generic/io.h:652:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
writesl(PCI_IOBASE + addr, buffer, count);
~~~~~~~~~~ ^
kernel/time/hrtimer.c:120:21: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
[CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
^~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:118:27: note: previous initialization is here
[0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES,
^~~~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:121:22: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
[CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
^~~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:118:27: note: previous initialization is here
[0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES,
^~~~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:122:21: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
[CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
^~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:118:27: note: previous initialization is here
[0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES,
^~~~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:123:17: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
[CLOCK_TAI] = HRTIMER_BASE_TAI,
^~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:118:27: note: previous initialization is here
[0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES,
^~~~~~~~~~~~~~~~~~~~~~~
>> kernel/time/hrtimer.c:754:19: error: static declaration of 'hrtimer_is_hres_enabled' follows non-static declaration
static inline int hrtimer_is_hres_enabled(void) { return 0; }
^
include/linux/hrtimer.h:329:12: note: previous declaration is here
extern int hrtimer_is_hres_enabled(void);
^
16 warnings and 1 error generated.


vim +/hrtimer_is_hres_enabled +754 kernel/time/hrtimer.c

54cdfdb47f73b5 kernel/hrtimer.c Thomas Gleixner 2007-02-16 753
54cdfdb47f73b5 kernel/hrtimer.c Thomas Gleixner 2007-02-16 @754 static inline int hrtimer_is_hres_enabled(void) { return 0; }
75e3b37d059856 kernel/time/hrtimer.c Luiz Capitulino 2015-08-11 755 static inline void hrtimer_switch_to_hres(void) { }
54cdfdb47f73b5 kernel/hrtimer.c Thomas Gleixner 2007-02-16 756

--
0-DAY CI Kernel Test Service
https://01.org/lkp