Partially improve support for `--cpus` from Docker CLI (dotnet/coreclr#23747)
authorLudovic Henry <luhenry@microsoft.com>
Fri, 5 Apr 2019 16:42:47 +0000 (09:42 -0700)
committerGitHub <noreply@github.com>
Fri, 5 Apr 2019 16:42:47 +0000 (09:42 -0700)
commit9c616649960625db76308a10b8e34ff8272985a0
treeac05fed2e363af982968bcb690ee1fda90146239
parent054bdc3752be8c46117c6d2685378d0cb79145c8
Partially improve support for `--cpus` from Docker CLI (dotnet/coreclr#23747)

* Round up the value of the CPU limit

In the case where `--cpus` is set to a value very close to the smaller
integer (ex: 1.499999999), it would previously be rounded down. This
would mean that the runtime would only try to take advantage of 1 CPU in
this example, leading to underutilization.

By rounding it up, we augment the pressure on the OS threads scheduler,
but even in the worst case scenario (`--cpus=1.000000001` previously
being rounded to 1, now rounded to 2), we do not observe any
overutilization of the CPU leading to performance degradation.

* Teach the ThreadPool of CPU limits

By making sure we do take the CPU limits into account when computing the
CPU busy time, we ensure we do not have the various heuristic of the
threadpool competing with each other: one trying to allocate more
threads to increase the CPU busy time, and the other one trying to
allocate less threads because there adding more doesn't improve the
throughput.

Let's take the example of a system with 20 cores, and a docker container
with `--cpus=2`. It would mean the total CPU usage of the machine is
2000%, while the CPU limit is 200%. Because the OS scheduler would never
allocate more than 200% of its total CPU budget to the docker container,
the CPU busy time would never get over 200%. From `PAL_GetCpuBusyTime`,
this would indicate that we threadpool threads are mostly doing non-CPU
bound work, meaning we could launch more threads.

Commit migrated from https://github.com/dotnet/coreclr/commit/aea3b1a80d6c114e3e67bc9521bf39a8a17371d1
src/coreclr/src/gc/unix/cgroup.cpp
src/coreclr/src/pal/src/misc/cgroup.cpp
src/coreclr/src/pal/src/thread/process.cpp