Mark As Completed Discussion

Time Synchronization and High-Resolution Timing

Accurate time is the referee in HFT — if your clocks disagree, your order/market-data timestamps lie, audits fail, and latency measurements become meaningless. Think of PTP/GPS as the league office keeping all courts' clocks in sync so your shot-clock (order timestamps) and the official game clock (exchange time) agree.

  • Why it matters for HFT (short)

    • Trading decisions, order sequencing, and regulatory audit trails all depend on consistent timestamps across machines and network cards.
    • Tail-latency debugging uses timestamps from NIC hardware and application logs — if the clocks drift, you can't correlate events correctly.
  • Key primitives & jargon

    • PTP (Precision Time Protocol) — network time sync with sub-microsecond accuracy when using hardware timestamping.
    • PHC / PHC2SYS — kernel PTP clock exposed by some NICs (the NIC's hardware clock).
    • TSC (Timestamp Counter) — very fast CPU cycle counter; great resolution but needs careful handling (invariant TSC, constant-rate, pinned cores).
    • SO_TIMESTAMPING / hardware timestamping — get timestamps from NIC hardware (preferred for accurate packet timing).
    • clock_gettime(CLOCK_REALTIME) vs CLOCK_MONOTONIC / steady_clock — pick the right clock for measuring intervals vs wall-time.

ASCII visual: packet timestamp flow (simplified)

[Exchange NIC HW] ---hw-ts---> (packet on wire) ---> [Your NIC hw-ts] | v (kernel / socket timestamp) | v (app capture time)

The important offsets: wire delay + NIC hardware timestamp offset + kernel/syscall latency + app capture jitter.

  • Common tools to inspect & verify
    • ptp4l -m and phc2sys (show PTP status and sync progress)
    • ethtool -T eth0 (check NIC hardware timestamping support)
    • tcpdump -tt -n -i eth0 with hardware timestamps (or pcap with hw ts)
    • chronyc tracking / timedatectl for NTP info

Practical notes for you (beginner in C++/Python, coming from Java/C/JS):

  • Use CLOCK_MONOTONIC or std::chrono::steady_clock to measure durations (like latency). Use system_clock only for wall-clock labeling (logs, audits).
  • If you prototype in Python, still rely on the NIC's hardware timestamp (via socket options) when you need accuracy — user-space timestamps are noisy.
  • TSC is like reading the CPU cycle counter directly (ultra-fast). It is great for microbenchmarks, but requires the system to guarantee the TSC rate is constant across cores. If you treat TSC like a simple Java System.nanoTime() replacement, test it carefully on your hardware.

Challenge: run the C++ helper below. It simulates a hardware timestamp (earlier) and an application timestamp and prints the offset in nanoseconds and an estimated TSC-cycle count using an input CPU frequency. Tweak cpu_freq_ghz and simulated_skew_ns to see how skew and CPU frequency affect cycle-counts and perceived offsets. Try to relate the printed offsets to the ptp4l offsets you'd see on a real machine.

Hands-on checks to attempt after this screen

  • On a lab box with a PTP-capable NIC: run ptp4l -m and note the reported offset (should be sub-microsecond when synced).
  • Use ethtool -T <iface> to confirm hardware timestamping.
  • Replay a pcap into your stack (or use a packet generator) and compare NIC hw timestamps vs application timestamps.

Ready? Tweak the code: change simulated_skew_ns and cpu_freq_ghz, or add a simulated jitter loop (like a busy spin) to see how application capture time moves relative to the NIC hw-ts. If you're into basketball analogies: try increasing simulated_skew_ns as if the scorekeepers in two arenas started one second apart — you'd lose the ability to compare who hit a buzzer-beater first.

CPP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment