Description
There has been significant discussion and feedback about the value of Environment.ProcessorCount
presented in containers. We know that there are scenarios that are underserved by the behavior of this API. We haven't exposed a new API because we haven't developed enough insight on what exactly it would do (as simple as it may seem).
This discussion relates to scenarios where the --cpus
or related arguments are used with docker run
. There are other ways of controlling CPU usage with docker like CPU affinity that are not related to this proposal.
- Current behavior:
Environment.ProcessorCount
returns the rounded up decimal value presented via--cpus
. For example--cpus 0.4
will result in a return value of 1. - Proposed OPT-IN behavior:
Environment.ProcessorCount
returns the value specified by an environment variable.
In terms of the the opt-in mode, for now I'm proposing we expose an environment variable: DOTNET_PROCESSOR_COUNT
The value is converted to an integer and returned as the new value of Environment.ProcessorCount
. It must be at > 0 and <= the actual processor count. If those requirements are not met, the actual processor count is returned.
Notes:
- There will be no check that the runtime is running in a container. This new ENV will be generic.
- This value is expected to affect
Environment.ProcessorCount
for managed code, and any analogues to it in the native runtime code.
Assuming we collect more information on this behavior providing significant benefit, we may provide a new API that provides this information. If you like this idea, please help motivate it with usage information.
We previously (extensively) researched offering the unclipped behavior as the default value for this property. The challenge is that we found that some subsystems always work best when honoring the clipped behavior and other subsystems provide neutral to (potentially much) better behavior when honoring the unclipped behavior. It's this distinction that is the challenge. We have to decide which value each subsystem should honor, and whether we need to expose more configuration options to further tune behavior. We need more findings in the real world to help us on this journey. Provided we have compelling data, multiple avenues are open on this topic.
Related issues:
- Environment.ProcessorCount incorrect reporting in containers in 3.1
- Question: Enviornment.ProcessorCount, Kubernetes/OpenShift and ThreadPool Min Threads
- Add Environment.TotalProcessorCount
- [Breaking change]
Environment.ProcessorCount
on Windows takes processor affinity into account - Ambiguity in what
Environment.ProcessorCount
returns - Container limits not supported by .NET runtime in process isolation mode -- Windows containers