Skip to content

Commit

Permalink
Merge pull request #591 from yowenter/master
Browse files Browse the repository at this point in the history
 cpuset cgroup path maybe different from cpu cgroup path in kubernetes
  • Loading branch information
stgraber authored Apr 15, 2023
2 parents 79e15d6 + aad7798 commit 0dbe340
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
21 changes: 12 additions & 9 deletions src/proc_cpuview.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/proc_cpuview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */

9 changes: 6 additions & 3 deletions src/proc_fuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
21 changes: 14 additions & 7 deletions src/sysfs_fuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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,
Expand Down

0 comments on commit 0dbe340

Please sign in to comment.