zlacker

[parent] [thread] 8 comments
1. nonran+(OP)[view] [source] 2025-01-10 08:27:29
Nice descriptive article. I've done this on purpose too to debug remote filesystem syncs and cryptography problems where machines are out of sync. My GPS wall clock is handy for adjusting NTP, but the time it takes to scan my eyes from the wall back to the monitor.. you really do need two stacked like she did. So I now figured to use transparrent terminals each logged into a different host and lay them over one another running "watch -n1 date".

Would have been nice to have some more network, code and command line examples. You need to set up a local ntpd and need to point your local master at that temporarily. A better utility to write would be "timediff -s1 -s2" that takes two time servers and shows the offset. I bet there's a way to do that in one line. Anyone?

replies(1): >>fishst+996
2. fishst+996[view] [source] 2025-01-12 22:01:05
>>nonran+(OP)
> watch -n1 date

Um, that's a pretty inaccurate way to notice an offset in the millisecond range, isn't it?

replies(1): >>nonran+Zl6
◧◩
3. nonran+Zl6[view] [source] [discussion] 2025-01-12 23:30:34
>>fishst+996
That doesn't even show ms. Add something like +%s%N (ns) to the options if you want finer resolution.
replies(1): >>mkl+zx6
◧◩◪
4. mkl+zx6[view] [source] [discussion] 2025-01-13 01:11:42
>>nonran+Zl6
The problem is using watch you have no control* over when in each second it's getting the time, so it could be nearly a second late (e.g. it's getting the time once per second, but happens to be doing it when it's a few milliseconds away from ticking over to the next second).

* Okay, you have a little control in that you can press enter, or otherwise set it running, at a particular moment.

replies(1): >>Izkata+EX6
◧◩◪◨
5. Izkata+EX6[view] [source] [discussion] 2025-01-13 06:01:51
>>mkl+zx6
So don't have any delay:

  while true; do date +%s%N; done
replies(1): >>mkl+l67
◧◩◪◨⬒
6. mkl+l67[view] [source] [discussion] 2025-01-13 07:43:21
>>Izkata+EX6
There's still a delay with that, as date takes time to run. You need a program that gets the current time, waits an appropriate amount, prints, and repeats. It'll still be slightly off unless you can measure the printing and flow control accurately.

I have a program I use in shell scripting called sleepuntil that does something like this, but it doesn't try to be millisecond-accurate.

replies(1): >>nonran+Qb7
◧◩◪◨⬒⬓
7. nonran+Qb7[view] [source] [discussion] 2025-01-13 08:49:05
>>mkl+l67
The problem that I noticed is if you open two terms on different hosts (say by ssh) and run watch commands then, regardless of resolution, they'll be out of sync. There's no hard tick event common to both kernels. And as someone already said, you introduce sampling effects.

So I figured you need a time "diff", a single command that updates from both timeservers and then presents the offset in a single operation. So (I just researched this again and) there's three answers here [0], using ntpdate and something I've never seen before called clockdiff.

[0] https://stackoverflow.com/questions/2296981/how-can-i-work-o...

replies(1): >>mkl+8t7
◧◩◪◨⬒⬓⬔
8. mkl+8t7[view] [source] [discussion] 2025-01-13 11:46:05
>>nonran+Qb7
Right, as I said, watch is the wrong tool here. The event in common is the synchronised clocks, or an NTP server.

To see the NTP offsets of a machine you can run something like:

  ntpq -pn
ntpdate -q doesn't seem very consistent to me, even pointing it at a nearby server.

To see the clocks ticking visually you can do something like the following and run it on each machine (assuming they have the same ping).

  #include <time.h>
  #include <sys/time.h>
  #include <stdio.h>
  
  void main() {
      struct timeval tv;
      struct timespec ts;
      ts.tv_sec = 0;
      while(1) {
          gettimeofday(&tv, NULL);
          ts.tv_nsec = 1000000000 - 1000*tv.tv_usec; //number of nanoseconds left in the current second
          nanosleep(&ts, &ts);
          gettimeofday(&tv, NULL);
          printf("%lu.%06lu\n", (unsigned long) tv.tv_sec, (unsigned long) tv.tv_usec);
      }
  }
Note that the sleep and second gettimeofday call take a little time (between 70 and 300 μs for me, but it depends what else is running), so the tick times reported won't be exactly on the second.
replies(1): >>nonran+L6b
◧◩◪◨⬒⬓⬔⧯
9. nonran+L6b[view] [source] [discussion] 2025-01-14 12:32:00
>>mkl+8t7
Great detailed answer. I upvoted you.
[go to top]