Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which clock does SO_TIMESTAMP socket option use?

I am receiving a stream of messages from a TCP server (a robot sending its status) every 0.1 s. I can not guarantee that I call recvmsg every 0.1 s, so I would like to have a timestamp on those received messages so that when I recvmsg them I know if I've just received them, or if they had been in the buffer for long enough to be outdated and I should call recvmsg again until I read a recent msg.

First of all, which clock does the struct timespec of the SO_TIMESTAMP timestamp use? I've found sources on google that point to CLOCK_REALTIME, but they are around 10 years old, so they might be outdated.

Which of all the time functions should I call to get a time that's comparable to that of the timestamp?

Is it possible to use CLOCK_BOOTTIME to avoid continuity problems?


I made a mistake. I mixed SO_TIMESTAMP (uses struct timeval) with SO_TIMESTAMPNS (uses struct timespec) (SO_TIMESTAMPNS doesn't seem available).

While SO_TIMESTAMPNS doesn't seem available per the documentation, when I #include <sys/socket.h> I have it defined as 35.

like image 996
alx Avatar asked Jun 13 '26 21:06

alx


1 Answers

It uses CLOCK_REALTIME.

Puff. So: SO_TIMESTAMP is mentioned in sock_setsockopt. From there I jumped to sock_enable_timestamp. But right above that function is sock_get_timestamp and sock_get_timestampns. They use ktime_get_real. Just to check sock_get_timestamp is used in inet_ioctl in af_inet.c. Just to be sore I researched documentation on ktime accessors that ktime_get_real is the same as CLOCK_REALTIME. And ktime_get_real is also used by __net_timestamp in socket buffer

like image 132
KamilCuk Avatar answered Jun 16 '26 07:06

KamilCuk