CM5: PTP_SYS_OFFSET_EXTENDED on the macb PHC (RP1) is occasionally ~4 µs off - phc2sys inherits it
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 47/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- c, linux
- Domain
- networking, operating-systems
Research direction
Start with drivers/net/ethernet/cadence/macb_ptp.c, especially gem_tsu_get_time(), which the report identifies as bracketing only the first TN read. Compare how PTP_SYS_OFFSET_EXTENDED and PTP_SYS_OFFSET collect timestamps, then reproduce on a CM5 using the attached probe and /dev/ptp1. Done means the extended method no longer shows the reported occasional ~4.1 µs offset; the cause is still unknown, so investigation will be needed.
Written by the indexing model from the issue text.
Description
Describe the bug
On CM5, PTP_SYS_OFFSET_EXTENDED on the Ethernet MAC's PHC (macb / gem-ptp-timer, behind RP1) occasionally returns a PHC/system time pair that is off by about 4.1 µs. PTP_SYS_OFFSET (basic) on the same clock, and a plain clock_gettime(CLOCK_REALTIME) / clock_gettime(PHC) / clock_gettime(CLOCK_REALTIME) from user space, never show it.
phc2sys (linuxptp 4.2) picks the extended ioctl automatically and has no option to avoid it. As a result, every phc2sys instance on the MAC clock shows minutes with max ≈ 4100 ns in its summary statistics, in both directions: PHC → CLOCK_REALTIME (PTP slave) and CLOCK_REALTIME → PHC (grandmaster). ptp4l itself is unaffected, because it uses packet timestamps.
Steps to reproduce the behaviour
- CM5 on CM5IO, PTP hardware timestamps from the MAC (
ethtool --set-hwtimestamp-cfg eth0 index 1 qualifier precise; this is/dev/ptp1). - Optionally run ptp4l so that the PHC is disciplined. The effect also shows without any servo running on CLOCK_REALTIME.
- Build and run the attached probe. It reads PHC minus CLOCK_REALTIME ten times a second in three ways, each as the best of 5 by bracket width:
Columns:gcc -O2 -o sysoff ptp-sysoff.c ./sysoff /dev/ptp1 3000 100 > run.txtt ext_off ext_width basic_off basic_width user_off user_width(ns).
Result over 3000 samples (5 min, nothing steering CLOCK_REALTIME):
| read method | sample-to-sample steps > 2 µs | bracket width (median) |
|---|---|---|
PTP_SYS_OFFSET_EXTENDED |
140, each ±4.1 µs: one sample off, the next back | 55 ns |
PTP_SYS_OFFSET |
0 | 1148 ns |
| user space | 0 | 1500 ns |
- The extended offset differs from the basic one by a median of 527 ns, rising to 4.6 µs in about 2 % of samples.
- Basic and user space agree within 37 ns, and their largest step between samples is 458 ns.
With phc2sys forced to the basic method (an LD_PRELOAD shim making the extended ioctls return EOPNOTSUPP), the 4.1 µs maxima disappear on both sides.
phc2sys summaries over 10 minutes, after one minute of settling:
| extended (default) | basic (shim) | |
|---|---|---|
| phc2sys CLOCK_REALTIME → PHC (grandmaster) | rms ~1 µs in most minutes, max ≈ 4100 ns | rms 24–30 ns, max ≤ 73 ns |
| phc2sys PHC → CLOCK_REALTIME (slave) | 18 of 20 minutes with max ≈ 4100 ns | rms 84–117 ns, max ≤ 333 ns |
| ptp4l on the slave, 16 s windows with max ≥ 1 µs | 12 of 37 | 1 of 38 |
Device (s)
Compute Module 5
System
- Raspberry Pi Compute Module 5 Rev 1.0 on CM5IO
- Debian GNU/Linux 13 (trixie), kernel
6.18.50+rpt-rpi-2712 - linuxptp 4.2-1+b1 (Debian package)
Logs
phc2sys summary (-u 60) with the default (extended) method, one line per minute:
CLOCK_REALTIME rms 1110 max 4207 ... delay 53
CLOCK_REALTIME rms 81 max 259 ... delay 53
CLOCK_REALTIME rms 1339 max 4292 ... delay 53
Additional context
In gem_tsu_get_time() (drivers/net/ethernet/cadence/macb_ptp.c), the pre/post system timestamps bracket only the first TN read. The 55 ns bracket is much narrower than a full PHC read over PCIe takes here (about 1.1 µs, measured by the basic method). We have not found the mechanism behind the occasional 4.1 µs, so this is an observation, not a diagnosis. Happy to run further tests.
ptp-sysoff.c
// Diagnostic: PTP clock minus CLOCK_REALTIME, three ways, side by side.
// ext - PTP_SYS_OFFSET_EXTENDED (the driver brackets its own read; phc2sys uses it)
// basic - PTP_SYS_OFFSET (the kernel brackets the driver's whole gettime)
// user - clock_gettime(REALTIME), clock_gettime(PHC), clock_gettime(REALTIME)
// Each: best of 5 by bracket width. One line per sample:
// t_s ext_off ext_w basic_off basic_w user_off user_w (ns)
// Usage: sysoff /dev/ptp1 <samples> <interval_ms>
#include <fcntl.h>
#include <linux/ptp_clock.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#define FD_TO_CLOCKID(fd) ((~(clockid_t)(fd) << 3) | 3)
static int64_t pct(const struct ptp_clock_time* t) { return (int64_t)t->sec * 1000000000LL + t->nsec; }
static int64_t tsn(const struct timespec* t) { return (int64_t)t->tv_sec * 1000000000LL + t->tv_nsec; }
int main(int argc, char** argv) {
if (argc < 4) {
fprintf(stderr, "usage: %s /dev/ptpN samples interval_ms\n", argv[0]);
return 2;
}
int fd = open(argv[1], O_RDONLY);
if (fd < 0) { perror(argv[1]); return 1; }
const int n = atoi(argv[2]);
const int ms = atoi(argv[3]);
const clockid_t phc = FD_TO_CLOCKID(fd);
struct timespec t0;
clock_gettime(CLOCK_MONOTONIC, &t0);
for (int k = 0; k < n; ++k) {
// ext
struct ptp_sys_offset_extended e = {0};
e.n_samples = 5;
int64_t extOff = 0, extW = -1;
if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, &e) == 0) {
for (unsigned i = 0; i < e.n_samples; ++i) {
int64_t pre = pct(&e.ts[i][0]), p = pct(&e.ts[i][1]), post = pct(&e.ts[i][2]);
int64_t w = post - pre;
if (extW < 0 || w < extW) { extW = w; extOff = p - (pre + w / 2); }
}
}
// basic: sys, phc, sys, phc, ..., sys
struct ptp_sys_offset b = {0};
b.n_samples = 5;
int64_t basOff = 0, basW = -1;
if (ioctl(fd, PTP_SYS_OFFSET, &b) == 0) {
for (unsigned i = 0; i < b.n_samples; ++i) {
int64_t pre = pct(&b.ts[2 * i]), p = pct(&b.ts[2 * i + 1]), post = pct(&b.ts[2 * i + 2]);
int64_t w = post - pre;
if (basW < 0 || w < basW) { basW = w; basOff = p - (pre + w / 2); }
}
}
// user
int64_t usrOff = 0, usrW = -1;
for (int i = 0; i < 5; ++i) {
struct timespec a, p, c;
clock_gettime(CLOCK_REALTIME, &a);
clock_gettime(phc, &p);
clock_gettime(CLOCK_REALTIME, &c);
int64_t w = tsn(&c) - tsn(&a);
if (usrW < 0 || w < usrW) { usrW = w; usrOff = tsn(&p) - (tsn(&a) + w / 2); }
}
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
printf("%.3f %lld %lld %lld %lld %lld %lld\n", (tsn(&now) - tsn(&t0)) / 1e9, (long long)extOff, (long long)extW,
(long long)basOff, (long long)basW, (long long)usrOff, (long long)usrW);
fflush(stdout);
usleep(ms * 1000);
}
close(fd);
return 0;
}
- Dominant language
- C
- Stars
- 13.3k
- Forks
- 5.5k
- Avg merge
- 4d 10h
- Merged PRs (30d)
- 31
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from raspberrypi/linux
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
raspberrypi/linux#7415 · 2 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
rp1-cfe doesn't forward V4L2_EVENT_SOURCE_CHANGE event from csi-2 sensor driver to userspace appOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
raspberrypi/linux#7399 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
raspberrypi/linux#7357 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
raspberrypi/linux#7054 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
raspberrypi/linux#7770 ·
Maintainers usually reply within 1 day
All issues in raspberrypi/linux
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
slice
Difficulty 2/5 Under an hour Newbie friendliness 72/100
Life-Framework/Life-Framework#3465 ·
Maintainers usually reply within 1 day