If you're running CPU-intensive workloads on a host, it can be quite useful to restrict other services in their CPU usage and systemd makes this process an absolute breeze. I'll use the sshd-service for this example.
First, create the service directory which will allow overwriting only certain unit options:
[root@server ~]# mkdir -p /etc/systemd/system/sshd.service.d
Now create a config file that will overwrite certain sections:
[root@server ~]# cat << EOF >> /etc/systemd/system/sshd.service.d/resources.conf
[Service]
CPUAffinity=0
CPUQuota=20%
MemoryLimit=134217728
EOF
This config will sshd to
- only run on the first CPU thread
- only consume 20% of the CPU at max
- only consume 1GiB of RAM at max
Make systemd aware of the changes by reloading it and restarting sshd:
[root@server ~]# systemctl daemon-reload
[root@server ~]# systemctl restart sshd.service
Since sshd will spawn a new process for every login, the changes won't take effect for the current login session. So after a reconnect you can check the settings using a tool called 'tuna':
[root@server ~]# tuna -t sshd -P
Output:
thread ctxt_switches
pid SCHED_ rtpri affinity voluntary nonvoluntary cmd
344269 OTHER 0 0 8 2 sshd
344352 OTHER 0 0 41 4 sshd
344355 OTHER 0 0 46 0 sshd
The process will be the same for pretty much every other service assuming your system uses systemd as its init system.
Feel free to comment and / or suggest a topic.
Comments
Post a Comment