docker or kubernetes, those use cgroup v1 or cgroupv2 to limit process's cpu usage, is widely used now.After starting a maxscale docker container or kubernetes pod, whose cpu is limited, and setting thread to auto in maxscale configuration, the maxscale use hardware_concurrency to get avaliable process, the value got is the avaliable process of the host machine.
All versions of maxscale has the same problem.
I think the best solution should be to parse the cgroup file to get the actual cpu usage which can be used.In cgroupv1, the different cgroup controller has the different hierarchies, so we should parse the /proc/self/cgroup and /proc/self/mountinfo to get the cpu controller hierarchy; meanwhile, in cgroupv2, all controllers is in one hierarchy.
the cpu share method use relative value to limit cpu usage, which is not the actual value, so all below is based on cpu quota method
my code's logic is like below:
1 parse /proc/cgroups to get the cgroup version, the difference between cgroup v1 and v2 is the hierarchy,that in cgroup v2 is zero while cgroupv1 now.
2 parse /proc/self/mountinfo to get the cpu controller's mount_root and mount_point
3 parse /proc/self/cgroup to get the cgroup path
4 compute the actual cpu controller file's directory use value got from 2 and 3
5 in cgroup v1, get the cpu.cfs_quota_us and cpu.cfs_period_us and compute the actual cpu that can be used.(use ceil)
6 in cgroup v2,get the value from cpu.max and compute the actual cpu that can be used.(use ceil)