Skip to content

Instantly share code, notes, and snippets.

@danthegoodman1
Last active July 4, 2025 18:36
Show Gist options
  • Select an option

  • Save danthegoodman1/4cabedec66846f30bf6c350f93652d0d to your computer and use it in GitHub Desktop.

Select an option

Save danthegoodman1/4cabedec66846f30bf6c350f93652d0d to your computer and use it in GitHub Desktop.
Setup AWS Timesync with the PTP clock for Ubuntu (x86 and arm tested)
#!/usr/bin/env bash
set -euo pipefail
echo "[1/6] Install build deps…"
sudo apt-get update -qq
sudo apt-get install -y --no-install-recommends \
build-essential git linux-headers-$(uname -r)
echo "[2/6] Fetch & compile ENA with PHC…"
workdir=$(mktemp -d)
git -C "$workdir" clone --depth 1 https://github.com/amzn/amzn-drivers.git
cd "$workdir"/amzn-drivers/kernel/linux/ena
ENA_PHC_INCLUDE=1 make -j"$(nproc)" # pulls in PHC support
echo "[3/6] Install the new module…"
sudo mkdir -p /lib/modules/$(uname -r)/updates
sudo cp -f ena.ko /lib/modules/$(uname -r)/updates/
sudo depmod -a
echo "[4/6] Enable PHC on load…"
echo "options ena phc_enable=1" | sudo tee /etc/modprobe.d/ena-phc.conf
echo "[5/6] Re-generate initramfs…"
sudo update-initramfs -u -k $(uname -r)
echo "[6/6] Done – rebooting to activate PHC-enabled ENA."
sleep 5
sudo reboot
@danthegoodman1
Copy link
Author

danthegoodman1 commented Jul 3, 2025

then

cat /sys/module/ena/parameters/phc_enable   # should print “1”
ls /dev/ptp*                                # expect /dev/ptp0

@danthegoodman1
Copy link
Author

danthegoodman1 commented Jul 3, 2025

And a one-liner to grab a bounded-error timestamp from the ptp clock:

First, configure chronyd to use the ptp clock:

echo 'refclock PHC /dev/ptp0 poll 0 prefer' | sudo tee /etc/chrony/conf.d/phc.conf
sudo systemctl restart chronyd

Then grab a time with:

printf '%s ±%.9f s\n'  \
  "$(date +%s.%N)"     \
  $(chronyc tracking -n | awk '
      /Last[ ]offset/      {off=$(NF-1)}
      /Root[ ]delay/       {rd =$(NF-1)/2}
      /Root[ ]dispersion/  {rp =$(NF-1)}
      END {
          if(off=="") off=0; if(rd=="") rd=0; if(rp=="") rp=0;
          if(off<0) off=-off;
          printf "%.9f", off+rd+rp   # root-distance formula
      }')

You'll see something like:

1751514577.054060088 ±0.000002144 s

that's 2.144 microseconds!!!

@danthegoodman1
Copy link
Author

This will survive reboots as well

@danthegoodman1
Copy link
Author

clockbound should automatically use the ptp clock once you tell chronyd to use the PHC (ptp hardware clock)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment