[][src]Crate quanta

High-speed timing facility.

quanta provides a generalized interface to native high-speed timing mechanisms on different target platforms, including support for accessing the TSC (time stamp counter) directly from the processor.

Design

Internally, two clocks are used — a reference and a source — to provide high-speed access to timing values, while allowing for conversion back to a reference timescale that matches the underlying system.

Calibration between the reference and source happens at initialization time of Clock.

Platform support

quanta supports using the native high-speed timing facilities on the following platforms:

TSC support

Additionally, quanta has RDTSC/RDTSCP support for querying the time stamp counter directly from the processor. Using this mode has some caveats:

  • you need to be running nightly to have access to the asm! macro/feature (#29722)
  • your processor needs to be recent enough to support a stable TSC mode like invariant or constant TSC (source)

This feature is only usable when compiled with the asm feature flag.

Generally speaking, most modern operating systems will already be attempting to use the TSC on your behalf, along with switching to another clocksource if they determine that the TSC is unstable or providing unsuitable speed/accuracy. The primary reason to use TSC directly is that calling it from userspace is measurably faster — although the "slower" methods are still on the order of of tens of nanoseconds — and can be useful for timing operations which themselves are extremely fast.

If your operations run in the hundreds of nanoseconds or less range, or you're measuring in a tight loop, using the TSC support could help you avoid the normal overhead which would otherwise contribute to a large chunk of actual time spent and would otherwise consume valuable cycles.

Structs

Clock

Unified clock for taking measurements.

Mock

Controllable time source for use in tests.