CM5: PTP_SYS_OFFSET_EXTENDED on the macb PHC (RP1) is occasionally ~4 µs off - phc2sys inherits it

Aperta
#7,704 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
47/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
c, linux

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
  1. CM5 on CM5IO, PTP hardware timestamps from the MAC (ethtool --set-hwtimestamp-cfg eth0 index 1 qualifier precise; this is /dev/ptp1).
  2. Optionally run ptp4l so that the PHC is disciplined. The effect also shows without any servo running on CLOCK_REALTIME.
  3. 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:
    gcc -O2 -o sysoff ptp-sysoff.c
    ./sysoff /dev/ptp1 3000 100 > run.txt
    
    Columns: t 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;
}
Lingua principale
C
Stelle
13.3k
Fork
5.5k
Merge medio
1g 4h
PR unite (30g)
21

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di raspberrypi/linux

Tutte le issue di raspberrypi/linux

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.