Improved timestamp implementation
|Reported by:||Sebastian Huber||Owned by:||Sebastian Huber|
Description (last modified by Sebastian Huber)
Improved average-case and worst-case performance. Uni-processor configurations will also benefit (simpler clock drivers, better NTP support, support for PPS).
The timekeeping is an important part of an operating system. It includes
- timer services,
- timeout options for operating system operations, and
- time of day services, e.g. timestamps.
Timestamps are frequently used for example by the network stack to manage timeouts in various network protocols.
On RTEMS the timekeeping is implemented using
- a basic time representation in 64-bit nanoseconds (or struct timespec),
- watchdog delta chains for timer and timeout services,
- a clock tick function
- an optional nanoseconds extension to get the nanoseconds elapsed since the last clock tick.
TOD Lock Contention
The time of day (TOD) lock protects the current time of day and the uptime values (both are in 64-bit nanoseconds). During the
rtems_clock_tick() procedure these values are updated. In combination with the nanoseconds extension they deliver a time resolution below the clock tick if supported by the clock driver.
Profiling reveals lock contention on the time of day (TOD) lock. For example for the test program SMPMRSP 1 on the 200MHz NGMP we have the following SMP lock profile for the TOD lock:
<SMPLockProfilingReport name="TOD"> <MaxAcquireTime unit="ns">2695</MaxAcquireTime> <MaxSectionTime unit="ns">1785</MaxSectionTime> <MeanAcquireTime unit="ns">499</MeanAcquireTime> <MeanSectionTime unit="ns">734</MeanSectionTime> <TotalAcquireTime unit="ns">1535529630</TotalAcquireTime> <TotalSectionTime unit="ns">2254450075</TotalSectionTime> <UsageCount>3071324</UsageCount> <ContentionCount initialQueueLength="0">2049757</ContentionCount> <ContentionCount initialQueueLength="1">1018726</ContentionCount> <ContentionCount initialQueueLength="2">2840</ContentionCount> <ContentionCount initialQueueLength="3">1</ContentionCount> </SMPLockProfilingReport>
This show that the time of day (TOD) lock is heavily used in this test program and high contention is visible (users have a 50% chance, that the lock is not immediately free). It is used on every context switch and is the last global data structure in the thread dispatch path. This shared state among all processors is a performance penalty.
Problem Report #2180
The nanoseconds extension and the standard clock drivers are broken on SMP, see #2180. The problem is that the time reference points for the operating system (global TOD structure, containing the time of day and uptime) and the clock driver (nanoseconds extension) are inconsistent for a non-zero length time interval during the clock tick procedure.
The software updated reference point for the nanoseconds extension leads to difficult implementations. Naively written clock drivers usually fail in the test program SPNSEXT 1.
Expensive Operation to Convert 64-bit Nanoseconds
Converting 64-bit nanoseconds values into the common struct timeval or struct timespec formats requires a 64-bit division to get the seconds value. This is a potentially expensive operation depending on the hardware support (this is the case for SPARC, see
__divdi3() in libgcc.a).
In order to use the timecounters, the platform must provide
- one periodic interval interrupt to trigger
- one free running global counter with a resolution below the clock tick interval.
This change makes it necessary to touch every clock driver in the RTEMS sources. There are 40 clock drivers (20 of them with a nanoseconds extension) using the clock driver shell header file and 23 clock drivers (3 of them with a nanoseconds extension) with a custom implementation structure.
This free running global counter is an additional requirement, so it may be impossible to convert every clock driver. However it is feasible to adjust the FreeBSD timecounters implementation which uses ten timehands by default to avoid this additional requirement. Platforms lacking a free running global counter can reduce the timehands to one. In this case the periodic timer used to generate the clock tick interrupt can be used (like in the current nanoseconds extension).
Change History (21)
comment:2 Changed 2 years ago by Alexander Krutwig
comment:5 Changed 2 years ago by Sebastian Huber
|Summary:||Improved Timekeeping → Improved timestamp implementation|