0

If I have a host with 8 cores and a Docker container given 1.0 CPU, how does Docker implement this?

Does it make make all 8 cpus available at the same time for 1/8 of the time?

If so, then a single-threaded process running full throttle won’t be able to take advantage of all the CPU available to it, right? When all 8 CPUs become available, 1 will be maxed out, the other 7 will sit idle.

Or does it do something closer to round-robin - each CPU is made available for 1/64 of the time, taking turns?

John Bachir
  • 2,364
  • 7
  • 29
  • 37

1 Answers1

1

This is configured by setting the period and quota for the CFS scheduler on the cgroup (encompassing all processes within the cgroup in aggregate). The period identifies how large the time slices are, and the quota says how much of the time within the slice can be used. So if you set it to 2.0 on an 8 core machine, it could use 2 cores at 100%, all 8 cores at 25%, or all sorts of variations as long as the quota for cpu time within a period is not exceeded. A decent description of the fields can be seen in these docs.

The actual files showing these settings is changing with cgroups v2, so some of these files may look different for your environment:

$ docker run -d --rm --cpus 1.5 --name test-cpu busybox tail -f /dev/null                                

$ docker inspect test-cpu | grep -i cpu                                                                  
        "Name": "/test-cpu",                                                                             
            "CpuShares": 0,                                                                              
            "NanoCpus": 1500000000,                                                                      
            "CpuPeriod": 0,                                                                              
            "CpuQuota": 0,                                                                               
            "CpuRealtimePeriod": 0,                                                                      
            "CpuRealtimeRuntime": 0,                                                                     
            "CpusetCpus": "",                                                                            
            "CpusetMems": "",                                                                            
            "CpuCount": 0,                                                                               
            "CpuPercent": 0,

$ docker exec -it test-cpu /bin/sh
/ # cat /sys/fs/cgroup/cpu.max                                                                           
150000 100000

See this answer with examples from cgroups v1.

BMitch
  • 5,966
  • 1
  • 25
  • 32
  • "it could use 2 cores at 100%, all 8 cores at 25%, or..." are you saying this depends on the `docker run` invocation and the flags you described before that? If so, could you show an example of each of those two scenarios? if not, could you explain? Thanks! – John Bachir Jul 14 '22 at 15:31
  • @JohnBachir it depends on the Linux kernel scheduling workload on available cores, and the limits you've given to the scheduler with the CFS settings. It's outside of the hands of Docker there, it's just the one knob to adjust the quota. You can try adjusting other flags in docker to lock down to specific cores, but I'd recommend against those for portability (pretty sure k8s doesn't let you set those, and they only limit your process, other processes can still use that core). – BMitch Jul 14 '22 at 17:33
  • assume we don't have control over docker and it's implemented with `--cpu=1.0` on an 8-core machine, what will happen if a single-threaded process runs at full throttle? will it be able to use all the available CPU? Still too many unknowns? – John Bachir Jul 15 '22 at 00:43
  • 1
    @JohnBachir remove the unknowns. Docker is just passing the setting through to cgroups, so eliminate docker from the question. This comes down to the Linux kernel scheduler, so I'd focus on learning about that. Will one process that has a quota of 1 CPU on an 8 core machine be able to use an entire core by itself? That depends. Is it the only thing running on the host? Even without cgroups, if there are more processes to run than cores to run them on, Linux will periodically swap out processes that are ready to run for others waiting their turn. – BMitch Jul 15 '22 at 01:24
  • thanks for the info - i created this question now - please let me know suggestions for changes, other tags, etc. https://serverfault.com/questions/1105778/can-1-thread-running-with-a-1-cpu-quota-on-an-n-core-machine-use-an-entire-core – John Bachir Jul 16 '22 at 17:31