Cpu Affinity and the sfptpd Threading Model - UG1602

Enhanced PTP User Guide (UG1602)

Document ID
UG1602
Release Date
2024-12-19
Revision
1.2 English

sfptpd is a multi-threaded application. The singular task of the main sfptpd process is to spawn a number of child processes which carry out the sfptpd processing tasks. The main sfptpd thread does no further work and, once started, will ignore attempts to affinitize to another CPU core.

At startup do the following command to ensure that sfptpd and its child threads will run on CPU core 1:

  • If running standalone, use the taskset command:
    # taskset -C 1 sfptpd -i <interface> -f config/ptp_slave.cfg
  • If running using systemd, use the CPUAffinity= unit file option:
    • You can add the CPUAffinity= option to the sfptpd unit file at /usr/lib/systemd/system/sfptpd.service
    • Alternatively, you can use the following command:
      systemctl set-property sfptpd CPUAffinity=1
    After setting the option, you must restart the sfptpd service:
    systemctl restart sfptpd

Thereafter attempts to move the sfptpd process to a different thread will fail because the main thread is doing no work, so takes not notice of affinity changes.

But the child/worker threads can be directed to other CPU cores while sfptpd is running. It is strongly recommended that all workers are affinitized to the same CPU core.

The following example (ptpthreads) script can be used as a simple shell script to identify sfptpd worker threads and affinitize all to a specific CPU core:

#!/bin/sh
#
# usage:   ptpthreads 2
#
DESIRED_CPU=$1

SFPTPD_PIDS=$(ps -T -p $(pidof sfptpd) | grep -v SPID | awk '{print $2}')
for i in ${SFPTPD_PIDS[@]}; do
  taskset -c -p $DESIRED_CPU $i;
done

# Confirm changes for sfptpd threads which are busy:
ps -Leo cpuid,lastcpu,pid | grep $(pidof sfptpd)