Linux Containers (LXC) rely on Control Groups (cgroups), a Linux kernel feature to isolate, limit, and snapshot the resource usage of certain processes, for resource allocation including CPU resources.
The allocation of CPU resources between LXC containers can be configured as per the user needs.
1. Shares: This is the most common and simplest way to allocate CPU resources. It involves specifying the relative share of CPU resources for each container. If we have two containers, the first one could have a CPU share of 1024 and the second could have a CPU share of 512. This implies that the first container could use twice as much CPU time as the second one.
1. Quota and Period: Another way to allocate CPU resources is through CPU quota and CPU period. The quota refers to the amount of time a container can use the CPU during the period. If a container’s quota is less than the period, it won’t be able to use the CPU for the rest of the period. For example, if the period is 50000 microseconds and the quota is 20000 microseconds, the container can only use the CPU for 20000 microseconds and has to wait for the rest of 30000 microseconds to do more computation.
1. Core or CPU sets: This refers to assigning specific CPUs or cores to the container. If a system has 4 cores named 0, 1, 2, 3, two of them can be assigned to one container and the rest two can be assigned to another container. By doing this, each container will only use the assigned cores and won’t interfere with the computation of other containers.
1. Real-Time Scheduler: One other mechanism used to allocate CPU resources involves specifying real-time scheduling policies and priorities. This is only used in very specific cases when real-time processing is needed.
Any of these options can be configured through the lxc.cgroup configuration parameter in the LXC configuration file. The exact method of allocation will depend on specific user needs and system constraints.