Timestamp Types¶
Use this page to choose between TSC, system, or custom clock sources and understand their trade-offs.
Timestamp Recording¶
The library records the timestamp of the log statement on the caller thread at the point the call was made. This approach makes the log statement timestamp more accurate but introduces the possibility of the backend seeing a timestamp in the past, causing an out-of-order timestamp to appear in the log.
Due to the usage of different types of queues, it is possible in rare cases for the timestamp to be recorded but not immediately processed. For example, the caller thread might block (if a blocking queue is used) or take some time to reallocate a new non-blocking queue.
To mitigate this, there is a configurable BackendOptions::log_timestamp_ordering_grace_period option.
This option delays the reading of timestamps by the backend for a few microseconds (default 5μs), providing additional time for the frontend to push messages and reducing the chance of out-of-order timestamps within that grace window.
The backend assumes timestamps produced within a single frontend queue are normally non-decreasing.
If a wall clock or user-provided clock moves backwards, BackendOptions::ensure_monotonic_output_timestamps can be enabled to correct regular log and metric output.
Backtrace records preserve their original capture timestamps and may still appear out of timestamp order when flushed later.
Timestamp Methods¶
There are several ways to get the timestamp on the frontend:
TSC (Time Stamp Counter): This is the fastest method for the frontend, as it simply calls
__rdtscand pushes the raw TSC counter value. The backend runs anRdtscClockwhich periodically syncs with the wall time (default every 500ms). It takes therdtscvalue from the frontend and converts it to wall time using a lock-free algorithm. Although therdtscclock might drift between resyncs, the difference is generally negligible. However, your processor must support invariant TSC to ensure that TSC counters between different cores remain synchronized. When this timestamp type is used, the backend requires a few seconds for the initial calibration of theRdtscClockon first usage.Note: TSC timestamps in rare cases may appear out of chronological order by a few microseconds. This occurs because: (1) frontend threads on different CPU cores capture raw TSC values with potential slight variations, (2) the backend converts these TSC values to wall time using a periodically updated calibration point, and (3) when resync occurs between message captures, different messages may be converted using different calibration points. Since the RdtscClock is not monotonic across resyncs, timestamps from different cores converted with different calibrations can appear out of order even when the actual logging occurred in sequence. This is an inherent characteristic of TSC-based timing across multi-core systems. If strict chronological timestamp ordering is required, use the System clock method instead.
System: This method calls
std::chrono::system_clock::now()on the frontend and pushes the wall time value to the queue. It is slower than TSC but provides the most accurate and immediately usable timestamps. The backend thread does not require any additional initialization time since theRdtscClockis not used.User: This method allows the user to provide a custom timestamp in nanoseconds since epoch via
UserClockSource. This is useful, for example, in simulations where you want to display a custom timestamp rather than the current one in the log statements.
Providing a Custom Timestamp¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
Getting a Synchronized Timestamp with the Backend Thread TSC Clock¶
In some cases, when using TSC for log statements, you might want to obtain a timestamp that is synchronized with the timestamp seen in the log statements.
To achieve this, you can use the BackendTscClock. See the example below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |