ex_cpu - hwloc - Docker
Explore if it's possible to detect if we are running under Docker or other containerization tools. Can we see how many CPUs are allocated / what cores they are on? If not, disable thread grouping capabilities.
When running with something like docker run --cpuset-cpus="0-2,4,9" which pins the application to specific cores, hwloc is able to correctly detect 5 total cores across the 3 separate L3 caches. So we should be able to use the regular thread creation and lassoing behavior for this.
However when running with docker run --cpus=2 which just limits the application to 2 CPUs worth of usage, but not any specific CPU, hwloc sees all 64 cores. For this we will need a different approach. Check the source of https://github.com/uber-go/automaxprocs which can detect the size of allocated resources with cgroups (rather than fixed cpuset). This behavior was just upstreamed into Go 1.25 recently so it can be considered reliable.
edit:
The data we need (for cgroups v2) can be read from inside the container: On my basic Docker setup these are at /sys/fs/cgroup// (since the cgroup has no name inside the container). But the cgroup may have a name - outside the container for example there is /sys/fs/cgroup/user.slice/.
For cpus=2:
# cat /sys/fs/cgroup/cpuset.cpus
# cat /sys/fs/cgroup/cpu.max
200000 100000
For cpuset=0-2,4,5:
# cat /sys/fs/cgroup/cpuset.cpus
0-2,4-5
# cat /sys/fs/cgroup/cpu.max
max 100000
Per this article https://vsoch.github.io/2023/resources-cgroups-kubernetes/ a fixed cpuset may be allocated by Kubernetes if certain requirements are met - this is probably something to note in the documentation.