Re: Implementation of system time
Posted: Tue Jun 08, 2021 10:47 am
Not that I know of, you don't. Can you name one protocol where it really matters? Internet time is second precision (see rfc822, rfc2822, and even rfc3339 states in section 5.3 that "A format which includes rarely used options is likely to cause interoperability problems. [...] The format defined below includes only one rarely used option: fractions of a second.").rdos wrote:I disagree. Timestamps are important and must have far better precision than one second.
That's why struct time in POSIX has two fields: a second precision (which comes from the wallclock and must be accurate), and a nanosec field which can be implemented with whatever precision the hardware supports (and allowed to be less accurate, it doesn't matter if subsec is a bit off).rdos wrote:You use them to log communication or events, for instance, and then you want decent precision (at least milliseconds, but I have microseconds).
And right there, that's where you lost accuracy for good. System time CAN'T be implemented by adding offsets, that just doesn't work. Not on real hardware, but gets a lot worse when you try to do this on a VM. (And indeed, many hobby OS loose keeping the time in a VM, I bet rdos does too. For the records, they will lost it on real hardware too, just after such a long time that their developer never notices, but eventually they will without a doubt.)rdos wrote:You also want a wall clock, but it might be implemented by adding an offset to the system clock
You're confusing your offsets with NTP protocol's drift. What RTP does, is knowing the exact time from an external universal clock source (typically a nuclear-clock), but when system time and that external clock source differs, it does not set that time at once, rather iterates to it in smaller steps. This has nothing to do with keeping the exact time using an unstable system time with whatever offsets (that's just not possible without an external clock source).rdos wrote:You can also compensate for a slightly unstable system time by slowly modifying the offset, or synching it with RTC, NTP, or some other time reference.
FYI, the RTC chip is such an external clock source, operates independently to the PC and the CPU, and works even when your computer is turned off. That's why your system time IRQ handler should read the time from CMOS, and never adding offsets.
Cheers,
bzt