From 95047f0b41cded281b05dd95f6dcf5f86c5ef329 Mon Sep 17 00:00:00 2001 From: Hongbo Yin Date: Mon, 10 Jul 2023 10:54:07 +0800 Subject: [PATCH] bugfix: cgroup v2 /proc/stat cpu count greater then cpu.max Signed-off-by: Hongbo Yin --- src/proc_fuse.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/proc_fuse.c b/src/proc_fuse.c index 25af10a1..3da82c05 100644 --- a/src/proc_fuse.c +++ b/src/proc_fuse.c @@ -904,6 +904,7 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, size_t linelen = 0, total_len = 0; int curcpu = -1; /* cpu numbering starts at 0 */ int physcpu = 0; + int max_cpu = 0; uint64_t user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0; uint64_t user_sum = 0, nice_sum = 0, system_sum = 0, idle_sum = 0, @@ -979,6 +980,8 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, lxcfs_v("proc_stat_read failed to read from cpuacct, falling back to the host's /proc/stat"); } + max_cpu = max_cpu_count(cg); + while (getline(&line, &linelen, f) != -1) { ssize_t l; char cpu_char[10]; /* That's a lot of cores */ @@ -1011,6 +1014,9 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, curcpu++; + if (max_cpu > 0 && (curcpu + 1 > max_cpu)) + continue; + if (cgroup_ops->can_use_cpuview(cgroup_ops) && opts && opts->use_cfs) cpu_to_render = curcpu; else