Skip to content

improve detection of CPU limits when running inside container #11933

@wfurt

Description

@wfurt

With dotnet/corefx#25193 in place Environment.ProcessorCount can now reflect limits imposed via docker. When running within docker run --cpus=2 -ti microsoft/dotnet-buildtools-prereqs:rhel7_prereqs_2 /bin/bash , this call returns 2 as expected on 8 core host.

However when limits are enforced in different way, it fails to detect it. Let say I limit container to only first two cores:

docker run --cpuset-cpus=0,1 -ti microsoft/dotnet-buildtools-prereqs:rhel7_prereqs_2 /bin/bash

[root@c39dcf61cf3a tests]# nproc
2

This shows that container is limited to to cores.
But Environment.ProcessorCount returns 8.

related to https://github.com/dotnet/corefx/issues/34920

Value obtained via sched_getaffinity() is also 2.

#define _GNU_SOURCE 1
#include <stdio.h>
#include <sched.h>

int main(int argc, char ** argv) {
    cpu_set_t set;
    if (sched_getaffinity (getpid(), sizeof (set), &set) == 0) {
        printf("count=%d\n", CPU_COUNT (&set));
     } else printf("FAILED!\n");
}
[root@c39dcf61cf3a tmp]# ./count
count=2

note that sched_getaffinity() does not work for first case when limits are enforced via limiting cycles.

Perhaps we need to do both and return lower value.

cc: @janvorli

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions