tsc_clock#

Fully qualified name: carb::clock::tsc_clock

Defined in carb/clock/TscClock.h

class tsc_clock#

Static class for a CPU time-stamp clock.

The functions and types within this class are used for sampling the CPU’s time-stamp counter—typically a clock- cycle resolution clock directly within the CPU hardware.

Public Types

using Sample = ::carb::Strong<uint64_t, struct SampleSig>#

Type definition of a sample from the CPU time-stamp counter.

The time units of this are unspecified. Use Freq to determine the frequency of the time-stamp counter.

using Freq = ::carb::Strong<uint64_t, struct FreqSig>#

The frequency of the timestamp counter, that is, the number of Samples per second.

Public Static Functions

static inline bool isInvariant() noexcept#

Checks whether the system supports an invariant (time-based) TSC clock.

Warning

If this returns false, results from sample() may appear to run backwards.

Returns:

true if the CPU supports an invariant TSC clock; false otherwise.

static inline Sample sample() noexcept#

Read a sample from the CPU time-stamp clock.

Note that this operation is intended to be as fast as possible to maintain accuracy.

Warning

If isInvariant() is false, results from this function may appear to run backwards.

Returns:

a time-stamp Sample at the time the function is called.

static inline Freq frequency() noexcept(!1)#

Computes the frequency of the CPU time-stamp clock.

The first call to this function can take longer as the CPU time-stamp clock is calibrated.

Note

This implementation assumes that the frequency never changes. Please verify with your CPU architecture that this assumption is correct.

Warning

If isInvariant() returns false, this frequency may not be stable and may throw std::runtime_error.

Throws:

std::runtime_error – if the TSC runs backwards and exceptions are enabled.

Returns:

the computed Freq of the CPU time-stamp clock, or 0 if the TSC runs backwards and exceptions are disabled.

template<class Duration>
static inline Duration duration(
Sample older,
Sample newer,
) noexcept#

Computes the difference of two samples as a std::chrono::duration.

Template Parameters:

Duration – a std::chrono::duration template to convert to.

Parameters:
  • older – The older (starting) sample

  • newer – The newer (ending) sample

Returns:

The difference in timestamps as the requested Duration representation