ImperialViolet

Just how important is a monotonic clock? (18 Mar 2005)

In discussions with Zooko I did a little test to see how important the addition of CLOCK_MONOTONIC is really.

The alternative to using CLOCK_MONOTONIC is to have an itimer which increments a global, volatile counter at some number of Hz. You can do that with something like the following:

struct itimerval itv;
memset(&itv, 0, sizeof(itv));
itv.it_interval.tv_sec = 1;
itv.it_value.tv_sec = 1;

struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sigalrm_handler;
sigaction(SIGALRM, &sa, NULL);

setitimer(ITIMER_REAL, &itv, NULL);

So I setup a test which tracks the difference between the true time elapsed (from CLOCK_MONOTONIC) and the count of global timer value. Firstly at 10Hz:

Nsecs elapsedGlobal Tick CountSkew in global count
3029049000300
6061401000600
9090753000900
121200890001201
151497730001501
181788020001801
212111430002102
242404800002402
272698220002702
302991660003002
333285130003303
363578670003603
393892030003903
424185580004204
454478990004504
484772420004804
515065930005105
545379640005405
575672850005705
605966330006005
636259870006306
666553240006606
696866770006906
727160180007207
757453630007507
787747140007807
818047020008108
848546410008408
878647660008708
908940980009008
939234440009309
969527940009609
999851630009909
103014486000102010

So, after 100 seconds the counter is already one second off. That's pretty terrible. Trying it again at 1Hz gives better results. I'm not going to give the whole table here but the skew is about 1 second lost every 20 minutes. Still not great.

Tags: