Skip to content

Commit 255b7a7

Browse files
committed
proc_fuse: fix proc_stat_read reporting host cpu count under cgroup v2
Signed-off-by: Quanjin Lin <[email protected]>
1 parent 8a281f9 commit 255b7a7

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/proc_fuse.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,8 @@ static int proc_stat_read(char *buf, size_t size, off_t offset,
10741074
char *cache = d->buf + CPUALL_MAX_SIZE;
10751075
size_t cache_size = d->buflen - CPUALL_MAX_SIZE;
10761076
int cg_cpu_usage_size = 0;
1077+
bool use_view;
1078+
int max_cpus = 0;
10771079

10781080
if (offset) {
10791081
size_t left;
@@ -1139,6 +1141,13 @@ static int proc_stat_read(char *buf, size_t size, off_t offset,
11391141
lxcfs_v("proc_stat_read failed to read from cpuacct, falling back to the host's /proc/stat");
11401142
}
11411143

1144+
if (cgroup_ops->can_use_cpuview(cgroup_ops) && opts && opts->use_cfs)
1145+
use_view = true;
1146+
else
1147+
use_view = false;
1148+
if (use_view)
1149+
max_cpus = max_cpu_count(cg, cpu_cg);
1150+
11421151
while (getline(&line, &linelen, f) != -1) {
11431152
ssize_t l;
11441153
char cpu_char[10]; /* That's a lot of cores */
@@ -1166,12 +1175,15 @@ static int proc_stat_read(char *buf, size_t size, off_t offset,
11661175
if (sscanf(cpu_char, "%d", &physcpu) != 1)
11671176
continue;
11681177

1178+
if (use_view && max_cpus > 0 && (curcpu + 1) == max_cpus)
1179+
continue; // cannot break here because we need to consume all non-cpu lines
1180+
11691181
if (!cpu_in_cpuset(physcpu, cpuset))
11701182
continue;
11711183

11721184
curcpu++;
11731185

1174-
if (cgroup_ops->can_use_cpuview(cgroup_ops) && opts && opts->use_cfs)
1186+
if (use_view)
11751187
cpu_to_render = curcpu;
11761188
else
11771189
cpu_to_render = physcpu;

0 commit comments

Comments
 (0)