From aad7798411691cacef09cdd00d3a05e1c1c4d1ec Mon Sep 17 00:00:00 2001 From: yowenter Date: Tue, 28 Mar 2023 14:32:07 +0800 Subject: [PATCH] cpuview: resolve cpu cgroup path separately from cpuset the cgroup path is different in kubernetes with containerd runtime. Signed-off-by: yowenter --- src/proc_cpuview.c | 21 ++++++++++++--------- src/proc_cpuview.h | 4 ++-- src/proc_fuse.c | 9 ++++++--- src/sysfs_fuse.c | 21 ++++++++++++++------- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/proc_cpuview.c b/src/proc_cpuview.c index 89f36668..f5464de7 100644 --- a/src/proc_cpuview.c +++ b/src/proc_cpuview.c @@ -496,20 +496,20 @@ static bool cfs_quota_disabled(const char *cg) * Return the maximum number of visible CPUs based on CPU quotas. * If there is no quota set, cpu number in cpuset value is returned. */ -int max_cpu_count(const char *cg) +int max_cpu_count(const char *cpuset_cg, const char *cpu_cg) { __do_free char *cpuset = NULL; int rv, nprocs; int64_t cfs_quota, cfs_period; int nr_cpus_in_cpuset = 0; - if (!read_cpu_cfs_param(cg, "quota", &cfs_quota)) + if (!read_cpu_cfs_param(cpu_cg, "quota", &cfs_quota)) cfs_quota = 0; - if (!read_cpu_cfs_param(cg, "period", &cfs_period)) + if (!read_cpu_cfs_param(cpu_cg, "period", &cfs_period)) cfs_period = 0; - cpuset = get_cpuset(cg); + cpuset = get_cpuset(cpuset_cg); if (cpuset) nr_cpus_in_cpuset = cpu_number_in_cpuset(cpuset); @@ -540,7 +540,7 @@ int max_cpu_count(const char *cg) return rv; } -int cpuview_proc_stat(const char *cg, const char *cpuset, +int cpuview_proc_stat(const char *cg, const char *cpu_cg, const char *cpuset, struct cpuacct_usage *cg_cpu_usage, int cg_cpu_usage_size, FILE *f, char *buf, size_t buf_size) { @@ -628,7 +628,7 @@ int cpuview_proc_stat(const char *cg, const char *cpuset, } /* Cannot use more CPUs than is available in cpuset. */ - max_cpus = max_cpu_count(cg); + max_cpus = max_cpu_count(cg, cpu_cg); if (max_cpus > cpu_cnt || !max_cpus) max_cpus = cpu_cnt; @@ -936,7 +936,7 @@ static inline bool cpuline_in_cpuset(const char *line, const char *cpuset) int proc_cpuinfo_read(char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { - __do_free char *cg = NULL, *cpuset = NULL, *line = NULL; + __do_free char *cg = NULL, *cpuset = NULL, *line = NULL, *cpu_cg = NULL; __do_free void *fopen_cache = NULL; __do_fclose FILE *f = NULL; struct fuse_context *fc = fuse_get_context(); @@ -973,7 +973,10 @@ int proc_cpuinfo_read(char *buf, size_t size, off_t offset, if (!cg) return read_file_fuse("proc/cpuinfo", buf, size, d); prune_init_slice(cg); - + cpu_cg = get_pid_cgroup(initpid, "cpu"); + if (!cpu_cg) + return read_file_fuse("proc/cpuinfo", buf, size, d); + prune_init_slice(cpu_cg); cpuset = get_cpuset(cg); if (!cpuset) return 0; @@ -983,7 +986,7 @@ int proc_cpuinfo_read(char *buf, size_t size, off_t offset, else use_view = false; if (use_view) - max_cpus = max_cpu_count(cg); + max_cpus = max_cpu_count(cg, cpu_cg); f = fopen_cached("/proc/cpuinfo", "re", &fopen_cache); if (!f) diff --git a/src/proc_cpuview.h b/src/proc_cpuview.h index 32eee540..b80f6dc0 100644 --- a/src/proc_cpuview.h +++ b/src/proc_cpuview.h @@ -21,7 +21,7 @@ struct cpuacct_usage { bool online; }; -extern int cpuview_proc_stat(const char *cg, const char *cpuset, +extern int cpuview_proc_stat(const char *cg, const char *cpu_cg, const char *cpuset, struct cpuacct_usage *cg_cpu_usage, int cg_cpu_usage_size, FILE *f, char *buf, size_t buf_size); @@ -31,7 +31,7 @@ extern int read_cpuacct_usage_all(char *cg, char *cpuset, struct cpuacct_usage **return_usage, int *size); extern bool init_cpuview(void); extern void free_cpuview(void); -extern int max_cpu_count(const char *cg); +extern int max_cpu_count(const char *cpuset_cg, const char *cpu_cg); #endif /* __LXCFS_PROC_CPUVIEW_FUSE_H */ diff --git a/src/proc_fuse.c b/src/proc_fuse.c index 7ed8be56..25af10a1 100644 --- a/src/proc_fuse.c +++ b/src/proc_fuse.c @@ -894,7 +894,7 @@ static int proc_uptime_read(char *buf, size_t size, off_t offset, static int proc_stat_read(char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { - __do_free char *cg = NULL, *cpuset = NULL, *line = NULL; + __do_free char *cg = NULL, *cpu_cg = NULL, *cpuset = NULL, *line = NULL; __do_free void *fopen_cache = NULL; __do_free struct cpuacct_usage *cg_cpu_usage = NULL; __do_fclose FILE *f = NULL; @@ -947,7 +947,10 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, if (!cg) return read_file_fuse("/proc/stat", buf, size, d); prune_init_slice(cg); - + cpu_cg = get_pid_cgroup(initpid, "cpu"); + if (!cpu_cg) + return read_file_fuse("/proc/stat", buf, size, d); + prune_init_slice(cpu_cg); cpuset = get_cpuset(cg); if (!cpuset) return 0; @@ -967,7 +970,7 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, */ if (read_cpuacct_usage_all(cg, cpuset, &cg_cpu_usage, &cg_cpu_usage_size) == 0) { if (cgroup_ops->can_use_cpuview(cgroup_ops) && opts && opts->use_cfs) { - total_len = cpuview_proc_stat(cg, cpuset, cg_cpu_usage, + total_len = cpuview_proc_stat(cg, cpu_cg, cpuset, cg_cpu_usage, cg_cpu_usage_size, f, d->buf, d->buflen); goto out; diff --git a/src/sysfs_fuse.c b/src/sysfs_fuse.c index e8671b61..d26d7c74 100644 --- a/src/sysfs_fuse.c +++ b/src/sysfs_fuse.c @@ -38,7 +38,7 @@ #include "utils.h" static off_t get_sysfile_size(const char *which); -static int do_cpuset_read(char *cg, char *buf, size_t buflen) +static int do_cpuset_read(char *cg, char *cpu_cg, char *buf, size_t buflen) { __do_free char *cpuset = NULL; struct fuse_context *fc = fuse_get_context(); @@ -57,7 +57,7 @@ static int do_cpuset_read(char *cg, char *buf, size_t buflen) use_view = false; if (use_view) - max_cpus = max_cpu_count(cg); + max_cpus = max_cpu_count(cg, cpu_cg); if (use_view) { if (max_cpus > 1) @@ -77,7 +77,7 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { - __do_free char *cg = NULL; + __do_free char *cg = NULL, *cpu_cg = NULL; struct fuse_context *fc = fuse_get_context(); struct file_info *d = INTTYPE_TO_PTR(fi->fh); char *cache = d->buf; @@ -108,8 +108,11 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size, if (!cg) return read_file_fuse("/sys/devices/system/cpu/online", buf, size, d); prune_init_slice(cg); - - total_len = do_cpuset_read(cg, d->buf, d->buflen); + cpu_cg = get_pid_cgroup(initpid, "cpu"); + if (!cpu_cg) + return read_file_fuse("/sys/devices/system/cpu/online", buf, size, d); + prune_init_slice(cpu_cg); + total_len = do_cpuset_read(cg, cpu_cg, d->buf, d->buflen); d->size = (int)total_len; d->cached = 1; @@ -124,7 +127,7 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size, static int sys_devices_system_cpu_online_getsize(const char *path) { - __do_free char *cg = NULL; + __do_free char *cg = NULL, *cpu_cg = NULL; struct fuse_context *fc = fuse_get_context(); pid_t initpid; char buf[BUF_RESERVE_SIZE]; @@ -137,9 +140,13 @@ static int sys_devices_system_cpu_online_getsize(const char *path) cg = get_pid_cgroup(initpid, "cpuset"); if (!cg) return get_sysfile_size(path); + cpu_cg = get_pid_cgroup(initpid, "cpu"); + if (!cpu_cg) + return get_sysfile_size(path); prune_init_slice(cg); + prune_init_slice(cpu_cg); - return do_cpuset_read(cg, buf, buflen); + return do_cpuset_read(cg, cpu_cg, buf, buflen); } static int filler_sys_devices_system_cpu(const char *path, void *buf,