[lxc-devel] [lxcfs/master] fix the problem of counting the number of CPUs when using cpuset and …

yinhongbo on Github lxc-bot at linuxcontainers.org
Fri Sep 27 12:31:47 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1411 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190927/459cc9ea/attachment.bin>
-------------- next part --------------
From 0ca91394393b2a74910c0f58d3e858ed285c7842 Mon Sep 17 00:00:00 2001
From: Hongbo Yin <yinhongbo at bytedance.com>
Date: Fri, 27 Sep 2019 18:50:44 +0800
Subject: [PATCH] fix the problem of counting the number of CPUs when using
 cpuset and cpuset or using cpuset alone

Signed-off-by: Hongbo Yin <yinhongbo at bytedance.com>
---
 bindings.c   | 16 +++++++++++++++-
 sysfs_fuse.c |  6 ------
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/bindings.c b/bindings.c
index 89fe28d..ed1b4f9 100644
--- a/bindings.c
+++ b/bindings.c
@@ -3669,6 +3669,8 @@ int max_cpu_count(const char *cg)
 {
 	int rv, nprocs;
 	int64_t cfs_quota, cfs_period;
+	int nr_cpus_in_cpuset = 0;
+	char *cpuset = NULL;
 
 	if (!read_cpu_cfs_param(cg, "quota", &cfs_quota))
 		return 0;
@@ -3676,8 +3678,16 @@ int max_cpu_count(const char *cg)
 	if (!read_cpu_cfs_param(cg, "period", &cfs_period))
 		return 0;
 
-	if (cfs_quota <= 0 || cfs_period <= 0)
+	cpuset = get_cpuset(cg);
+	if (cpuset)
+		nr_cpus_in_cpuset = cpu_number_in_cpuset(cpuset);
+
+	if (cfs_quota <= 0 || cfs_period <= 0){
+		if(nr_cpus_in_cpuset > 0)
+			return nr_cpus_in_cpuset;
+
 		return 0;
+	}
 
 	rv = cfs_quota / cfs_period;
 
@@ -3692,6 +3702,10 @@ int max_cpu_count(const char *cg)
 	if (rv > nprocs)
 		rv = nprocs;
 
+	/* use min value in cpu quota and cpuset */
+	if(nr_cpus_in_cpuset > 0 && nr_cpus_in_cpuset < rv)
+		rv = nr_cpus_in_cpuset;
+
 	return rv;
 }
 
diff --git a/sysfs_fuse.c b/sysfs_fuse.c
index dc535d7..32a59b7 100644
--- a/sysfs_fuse.c
+++ b/sysfs_fuse.c
@@ -46,7 +46,6 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size,
 	bool use_view;
 
 	int max_cpus = 0;
-	int max_cpus_in_cpuset = 0;
 	pid_t initpid;
 	ssize_t total_len = 0;
 
@@ -78,11 +77,6 @@ static int sys_devices_system_cpu_online_read(char *buf, size_t size,
 	if (use_view)
 		max_cpus = max_cpu_count(cg);
 
-	max_cpus_in_cpuset = cpu_number_in_cpuset(cpuset);
-	// use min value in cpu quota and cpuset
-	if (max_cpus_in_cpuset > 0)
-		max_cpus = max_cpus_in_cpuset > max_cpus ? max_cpus : max_cpus_in_cpuset;
-
 	if (max_cpus == 0)
 		return read_file("/sys/devices/system/cpu/online", buf, size, d);
 	if (max_cpus > 1)


More information about the lxc-devel mailing list