[ntp:questions] latency Was: double pps device

Brian Inglis Brian.Inglis at SystematicSw.ab.ca
Sat Nov 12 19:21:51 UTC 2016


On 2016-11-12 08:04, folkert wrote:
> Before I used my rpi-gpio-ntpd program. As I would like better latency  
> values, I'm going to try to use the kernel pps interface. This is all
> on a raspberry pi.

I found everything was fairly easy to get working, once you have all the
details of GPS and PPS configuration set up; rebuilding ntp with pps
support required only installing the pps dev packages, adding the source
repo to apt sources, and running `sudo apt-get source --compile ntp`.

To reduce latency I added in /etc/init.d/gps-setup:
*	`setserial /dev/gps0 low_latency`
   to hopefully reduce serial latency (if it is working properly, which
   varies with kernel releases and bug fixes);
* set the system clock from the GPS RTC to initialize system time;
* configure receiver settings and message output;
* download and preload available EPO almanac and ephemeris data to speed
   up acquisition, lock, UTC, and PPS output;
* if your GPS supports it, configure it to:
   - not output messages until it can report UTC time;
   - not generate PPS until TDOP is below about 10us;
   - set the PPS pulse length to as close to 1ms as possible for TIA232
     serial interfaces (from control signal spec).

At least some of these features are available and should be used on most GPS with PPS, but may not be on most sat-nav only receivers; whether you
*can* use them may depend on vendor docs and support, and your skills.

I also added the lines below the .sig to `/etc/init.d/ntp` to:
* set governor to performance, set max and min cpu speeds to max normal
   (without overclocking), to stabilize the clock frequency;
* start daemon running at high processor priority 20, I/O scheduling to
   high real-time priority 4, to improve serial and network performance
   and stability;
* set task affinity to the last cpu e.g. 3, 1, 0, to stabilize clock
   frequency, reduce cpu cache and tlb misses.

These changes should work on other Debian and (with minor changes) Linux
systems supporting these configuration interfaces and options. Similar
options are available in the BIOS and system settings of PC OSes.

I should move the governor and frequency setting into a separate startup
script: perhaps when I have to move everything to systemd, after some
more experience working with it.
-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada
.
.
.
```
# get last processor <base path>
get_last_cpu() {
         if [ -d $1/ ]; then
                 lp=$(cat $1/online)
                 lp=${lp##*,}
                 lp=${lp##*-}
         else
                 lp=0
         fi

         echo $lp
}

# set affinity to processor $1
set_task_affinity_cpu() {
         if [ -n "$1" ] && [ -n "$PIDFILE" ] && [ -r $PIDFILE ]; then
                 taskset -apc $1 $(cat < $PIDFILE)
         fi
}

# set cpu scaling for max performance <available settings path> \
#                                          <target settings path>...
set_cpu_scaling_max_perf() {
     if [ -r $1 ]; then          # if available readable
         avail=$(cat $1)         # get available
         avail=${avail% }        # strip trailing space
         max=${avail##* }        # get max available

         if [ -n "$max" ]; then  # if non-null
             while [ -n "$2" ]   # while targets
             do
      		# if target writable, set to max
                 [ -w $2 ] && echo $max > $2
                 shift           # next target
             done
         fi
     fi
}

# set cpu scaling frequency and governor for max performance
set_cpu_scaling_freq_gov_max_perf() {
         if [ -d $CPUF/ ]; then  # if base path available
# set frequencies
                 set_cpu_scaling_max_perf \
                         $CPUF/scaling_available_frequencies \
                         $CPUF/scaling_max_freq $CPUF/scaling_min_freq
# set governor
                 set_cpu_scaling_max_perf \
                         $CPUF/scaling_available_governors \
                         $CPUF/scaling_governor
         fi
}

# base path for cpu info
cpubp=/sys/devices/system/cpu

# base path for cpu frequency and governor
CPUF=$cpubp/cpu$cpulp/cpufreq

# get last processor
cpulp=$(get_last_cpu $cpubp)

and changed the daemon startup to do:

# set cpu scaling frequency and governor for max performance
                 set_cpu_scaling_freq_gov_max_perf
                 lock_ntpdate
# add procsched rt rr mid/hi and iosched rt mid
                 start-stop-daemon --start --quiet --oknodo \
			--pidfile $PIDFILE --startas $DAEMON \
			--procsched rr:20 --iosched real-time:4 \
                                         -- -p $PIDFILE $NTPD_OPTS
                 status=$?
                 unlock_ntpdate
# set affinity to last processor
                 set_task_affinity_cpu $cpulp
                 log_end_msg $status
                 ;;
```


More information about the questions mailing list