• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

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


    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.

    Deepak Prasad

    is a techie and an author who is still trying to survive in this IT generation with very little knowledge he has on Linux/Unix, VMware, SAN Storage, Automation, networking etc

    You can follow him on Facebook or Google+

    Do you also have something to share here?

    Become an author and be a part of our GoLinuxHub Team, Click here for more information
    How to assign a service to a specific core using systemd in RHEL 7 / CentOS 7 How to assign a service to a specific core using systemd in RHEL 7 / CentOS 7 Reviewed by Deepak Prasad on Sunday, February 11, 2018 Rating: 5

    No comments:

    Powered by Blogger.