Set SO_TIMESTAMP correctly for the various platforms

This commit is contained in:
Andreas Wrede
2026-04-10 11:19:47 -04:00
parent 9eedbafe97
commit 3426185383
3 changed files with 18 additions and 7 deletions
+1 -1
View File
@@ -9,7 +9,7 @@
"type": "debugpy", "type": "debugpy",
"request": "launch", "request": "launch",
"module": "hbd.server.cli", "module": "hbd.server.cli",
"args": ["-c", "~/.hb.yaml", "-f", "-v", "-x"], "args": ["-c", "~/.hb.yaml", "-f", "-v"],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"env": { "env": {
"PYTHONPATH": "${workspaceFolder}" "PYTHONPATH": "${workspaceFolder}"
+3 -3
View File
@@ -191,14 +191,14 @@ async def _run_async(config, config_path=None):
sock.bind(bind_addr) sock.bind(bind_addr)
logger.info("Starting UDP server on %s:%s", *bind_addr) logger.info("Starting UDP server on %s:%s", *bind_addr)
# Try to enable kernel receive timestamps (Linux SO_TIMESTAMPNS). # Try to enable kernel receive timestamps (Linux SO_TIMESTAMP).
# If supported, read datagrams via recvmsg() so RTT uses the kernel # If supported, read datagrams via recvmsg() so RTT uses the kernel
# timestamp rather than the time.time() call after asyncio scheduling. # timestamp rather than the time.time() call after asyncio scheduling.
use_kernel_ts = udp.enable_kernel_timestamps(sock) use_kernel_ts = udp.enable_kernel_timestamps(sock)
if use_kernel_ts: if use_kernel_ts:
logger.info("SO_TIMESTAMPNS enabled: using kernel receive timestamps for RTT") logger.info("SO_TIMESTAMP enabled: using kernel receive timestamps for RTT")
else: else:
logger.info("SO_TIMESTAMPNS not available: using time.time() for RTT") logger.info("SO_TIMESTAMP not available: using time.time() for RTT")
def udp_handler(msg, addr, transport, recv_ts=None): def udp_handler(msg, addr, transport, recv_ts=None):
ctx = dict( ctx = dict(
+14 -3
View File
@@ -7,6 +7,8 @@ import time
import zlib import zlib
import logging import logging
from platform import system as platform_system
from ..common.proto import stodict, oldmtodict from ..common.proto import stodict, oldmtodict
from ..common.utils import dur from ..common.utils import dur
from . import notify as notify_mod from . import notify as notify_mod
@@ -16,9 +18,18 @@ eventlog = notify_mod.eventlog
# SO_TIMESTAMP: kernel attaches a struct timeval to each received datagram. # SO_TIMESTAMP: kernel attaches a struct timeval to each received datagram.
# Supported on Linux, FreeBSD, and macOS. The constant is not exposed by # Supported on Linux, FreeBSD, and macOS. The constant is not exposed by
# Python's socket module on all platforms, so fall back to the Linux value (29) # Python's socket module on all platforms
# when absent. platform = platform_system()
_SO_TIMESTAMP = getattr(socket, 'SO_TIMESTAMP', 29) if platform == "Darwin":
_SO_TIMESTAMP = 1024 # SO_TIMESTAMP on macOS (not in Python's socket module)
elif platform == "Linux":
_SO_TIMESTAMP = 29 # Linux value (not in older Python versions)
elif platform == "FreeBSD":
_SO_TIMESTAMP = 32 # FreeBSD value (not in older Python versions)
else:
logger.warning("SO_TIMESTAMP may not be supported on this platform (%s)", platform)
_SO_TIMESTAMP = None
# struct timeval uses two native C longs: tv_sec and tv_usec # struct timeval uses two native C longs: tv_sec and tv_usec
_TIMEVAL = struct.Struct('@ll') _TIMEVAL = struct.Struct('@ll')