- Linux Cgroups provide a framework to apply resource restrictions to Linux systems
Cgroups
can limit the amount of CPU cycles, available memory, and more- If processes are equal from a perspective of
Cgroups
, the Linuxnice
andrenice
commands can be used to manage priority
Understanding Cgroups
- In Cgroups, the Linux system is divided in 3 slices
- System: all systemd processes
- User: all user processes
- Machine: Virtual machines and containers
- Each slice has an equal CPU weight
- That means that if one or more processes within a slice requests a maximum amount of CPU cycles, each slice will get an equal amount of CPU shares
- So 20 systemd processes together gets as much as one user process that claims full CPU usage!
- In systemd, the CPUWeight can be set on individual systemd units
nice and renice
- nice and renice can’t change the priority if,
- specific cgroups are defined
- if the process is a realtime process (ex: Kernal Processes, these are in a different queue)
- Users can set their processes to a lower priority, to increase priorities you need root access
- Priority is always relative to other processes
Example
nice -n 19 dd if=/dev/zero of=/dev/null
dd if=/dev/zero of=/dev/null
:
dd
is a command-line utility for copying and converting data.if=/dev/zero
specifies the input file as/dev/zero
, which generates an endless stream of null bytes (zeros).of=/dev/null
specifies the output file as/dev/null
, a special file that discards all data written to it.- Effectively, this setup copies an infinite stream of zeros from
/dev/zero
and discards it into/dev/null
.
why these two processes getting the exact same priority even after we set the priority ?
- that’s because this has multiple CPUs.
- if you press
1
in top dashboard.- you can see each CPU
- in order to see the priority working correctly we have to disable a cpu
Disable CPU
- now you can see priority working file
- to enable the cpu again