How to assign a service to a specific core using systemd in RHEL 7 / CentOS 7

In my last article I have given an overview of systemd with examples.

If your service is a multithread service then it may run on multiple cores based on the availability of the threads and if the service runs on a single core than again it will run on different thread every time the service is restarted but then atleast it will continue to use single thread until this service is restarted.

Learn to check which thread or core a process is running on using below link

how can I assign a specific core to a service?

For example I have a test.service

# systemctl status test.service
â test.service - LSB: start any SW, when required
   Loaded: loaded (/etc/rc.d/init.d/test; enabled; vendor preset: disabled)
   Active: active (exited) since Sun 2018-01-21 00:04:55 IST; 31s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 22045 ExecStop=/etc/rc.d/init.d/test stop (code=exited, status=0/SUCCESS)
  Process: 22066 ExecStart=/etc/rc.d/init.d/test start (code=exited, status=0/SUCCESS)

Using below command you can check the core it is using

# grep -i cpu  /proc/2206?/status
Cpus_allowed:   1000
Cpus_allowed_list:      12

Now if I restart the servcie and check the PID of the service you will observe the core number will most likely change

# systemctl status test.service
â test.service - LSB: start any SW, when required
   Loaded: loaded (/etc/rc.d/init.d/test; enabled; vendor preset: disabled)
   Active: active (exited) since Sun 2018-01-21 00:06:07 IST; 7s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 22290 ExecStop=/etc/rc.d/init.d/test stop (code=exited, status=0/SUCCESS)
  Process: 22312 ExecStart=/etc/rc.d/init.d/test start (code=exited, status=0/SUCCESS)

As you see earlier this service was running on 12th core and now it is running on 6th core.

# grep -i cpu  /proc/2231?/status
Cpus_allowed:   0040
Cpus_allowed_list:      6

Lets assign a core on which we want this service to run always

# vim /etc/systemd/system/test.service
...
[Service]
CPUAffinity=13
Type=forking
Restart=no
...

Since we modified the unit file we must refresh the configuration before restarting the service

# systemctl daemon-reload

# systemctl restart test.service

Next check the status and PID of the service

# systemctl status test.service
â test.service - LSB: start any SW, when required
   Loaded: loaded (/etc/rc.d/init.d/test; enabled; vendor preset: disabled)
   Active: active (exited) since Sun 2018-01-21 00:01:51 IST; 2s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 21944 ExecStop=/etc/rc.d/init.d/test stop (code=exited, status=0/SUCCESS)
  Process: 21966 ExecStart=/etc/rc.d/init.d/test start (code=exited, status=0/SUCCESS)

So as expected now the service is running on the assigned core i.e. 13

# grep -i cpu  /proc/2196?/status
Cpus_allowed:   2000
Cpus_allowed_list:      13

Let's change it to a different core number

# vim /etc/systemd/system/test.service
...
[Service]
CPUAffinity=15
Type=forking
Restart=no
...

Followed by reloading the unit configuration file and restart of the respective service

# systemctl daemon-reload

# systemctl restart test.service

Next check the status and PID of the service

# systemctl status test.service
â test.service - LSB: start any SW, when required
   Loaded: loaded (/etc/rc.d/init.d/test; enabled; vendor preset: disabled)
   Active: active (exited) since Sat 2018-01-20 23:59:14 IST; 13s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 21760 ExecStop=/etc/rc.d/init.d/test stop (code=exited, status=0/SUCCESS)
  Process: 21782 ExecStart=/etc/rc.d/init.d/test start (code=exited, status=0/SUCCESS)

Using the below command we get the core on which this test service is running and as expected it is running on 15th core

# grep -i cpu  /proc/2178?/status
Cpus_allowed:   8000
Cpus_allowed_list:      15

I hope the article is useful.