Essay series · 2 essays

Keeping Time: Pacing Output and Following Clocks with a Phase-Locked Loop (PLL)

A program that sends or receives things at a steady rate has to keep time. Usually that sounds like a timer. Sometimes it means following a clock that isn’t yours, one that runs a little fast or slow and reports in late.

Those are different problems, and they need different tools. Pacing on your own clock needs exact arithmetic and no feedback at all. Following someone else’s clock needs a phase-locked loop, and a phase-locked loop needs answers to questions its textbook equations leave open.

The first essay, Pacing and Following, separates the two problems, follows the boundary between library calculations and application decisions, and asks how to check code that can only estimate. The second, Exact and Fast, shows how the pacer computes exact times with fixed-size arithmetic, and how to measure a pacing loop on your own machine. Elixir and Python implementations also exist, with language-specific APIs and checks against pinned Go behavior.

Implementations

The essays use the Go API. The Elixir and Python ports are not interchangeable packages. The implementation links above provide source-checkout documentation for all three languages.

  1. Part 1

    Pacing and Following

    Why pll was built for an MPEG transport-stream demuxer and muxer, how its Pacer and Follower work, and where else they are useful.

    9 min read

  2. Part 2

    Exact and Fast

    How pll's Go pacer calculates exact schedules cheaply enough for applications sending at Gbps rates.

    8 min read

See also

All writing