Excerpts from Andreas Kempe's message of June 21, 2022 11:50 am:
Hello everyone,
I was doing some profiling on my two relays running on FreeBSD 13.1 and noticed that they were spending a lot of time in clock_gettime() which prompted me to have a look at the implementation.
Time implementation
The time implementation is abstracted in src/lib/time/compat_time.c where different mechanisms are used for different operating systems. On Linux CLOCK_MONOTONIC_COARSE is a clock that gives worse precision than CLOCK_MONOTONIC, but is faster and the abstraction layer checks for its presense and provides more performat less precise time where applicable.
On FreeBSD, there is also a fast monotonic time source available called CLOCK_MONOTONIC_FAST. In the header file src/lib/time/compat_time.h, a comment references this clock, but it is not used. I thought it might be worth a shot seeing what difference it would make if I enable the use of CLOCK_MONOTONIC_FAST on FreeBSD and on the VM where I run my two FreeBSD relays, the difference was stunning.
I made did a quick patch simply replacing CLOCK_MONOTONIC_COARSE with CLOCK_MONOTONIC_FAST, see patches attached, compiled and tested. Tracing system calls to make sure the correct call was being used, which it was.
According to https://www.freebsd.org/cgi/man.cgi?query=clock_gettime, FreeBSD 13.1 has CLOCK_MONOTONIC_COARSE, which it says is an alias of CLOCK_MONOTONIC_FAST for compatibility with other systems. I suppose Tor could add #if !defined(CLOCK_MONOTONIC_COARSE) && defined(CLOCK_MONOTONIC_FAST) #define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC_FAST, but I'm not sure how useful that would be. OpenBSD and NetBSD don't seem to define either. Perhaps something like that would be appropriate for a FreeBSD ports patch.
Cheers, Alex.