Fast high-resolution clock
author | Gareth Rees |
copyright | See section Copyright and License. |
date | 2016-03-06 |
index terms | pair: clock; design |
revision | //info.ravenbrook.com/project/mps/master/design/clock.txt#7 |
status | complete design |
tag | design.mps.clock |
Introduction
.intro: This is the design of the clock module, which implements a fast high-resolution clock for use by the telemetry system.
.readership: This document is intended for any MPS developer.
Requirements
.req.monotonic: Successive calls to EVENT_CLOCK() must yield values that are monotonically increasing. (So that comparing the timestamp on two events never gives false positives.)
.req.fast: EVENT_CLOCK() should take very little time; it should not require a system call. (So that programs that use the MPS remain usable when telemetry is turned on.)
.req.high-resolution: Successive calls to EVENT_CLOCK() should yield values that are strictly monotonically increasing (so that sorting the telemetry stream puts the events in the order they happened).
Interface
EventClock
.if.type: The type of timestamps. It must be an unsigned 64-bit integral type, for example a typedef for uint64_t or unsigned __int64.
EVENT_CLOCK_MAKE(lvalue, low, high)
.if.make: Construct an EventClock timestamp from its two halves. The first parameter is an lvalue with type EventClock, and the second and third parameters are 32-bit unsigned integers. The macro must assign a timestamp to lvalue with the value (high << 32) + low.
EVENT_CLOCK(lvalue)
.if.get: Assign an EventClock timestamp for the current time to lvalue, which is an lvalue with type EventClock.
EVENT_CLOCK_PRINT(stream, clock)
.if.print: Write the value of clock to the standard C output file handle stream as 16 hexadecimal digits (with leading zeros, and capital letters A to F).
EVENT_CLOCK_WRITE(stream, clock)
.if.write: Write the value of clock to the output stream stream as 16 hexadecimal digits (with leading zeros, and capital letters A to F). The macro should be implemented using WriteF().
Implementation
.impl.tsc: On IA-32 and x86-64, the Time Stamp Counter returned by the RDTSC instruction is a suitable clock for single-core CPUs, but on multiple-core CPUs, different cores may have different values or tick at different speeds, and so it may fail to meet .req.monotonic.
Document History
- 2016-03-06 GDR Created.
Copyright and License
Copyright © 2016–2020 Ravenbrook Limited.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.