bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CLOCK_MONOTONIC time is not in nanoseconds?


From: Aaron B. Olowin
Subject: CLOCK_MONOTONIC time is not in nanoseconds?
Date: Thu, 27 Sep 2001 15:41:29 -0400

I compile the following code with gcc -lrt -o timer_delay timer_delay.c:

#include <unistd.h>
#include <time.h>
#include <stdio.h>

int main(void)
{
   struct timespec resolution;
   struct timespec then, now, then_m, now_m, nap;
  
   clock_getres(CLOCK_REALTIME, &resolution);
   printf("CLOCK_REALTIME resolution %d nsec or %.4f usec\n", 
           resolution.tv_nsec, resolution.tv_nsec*1e-3);

   clock_getres(CLOCK_MONOTONIC, &resolution);
   printf("CLOCK_MONOTONIC resolution %d nsec or %.4f usec\n", 
           resolution.tv_nsec, resolution.tv_nsec*1e-3);


   nap.tv_sec = 0;
   nap.tv_nsec = 10000000;  // 10 ms
   clock_gettime(CLOCK_REALTIME, &then);
   clock_gettime(CLOCK_MONOTONIC, &then_m);
   nanosleep(&nap,NULL);
   clock_gettime(CLOCK_MONOTONIC, &now_m);
   clock_gettime(CLOCK_REALTIME, &now);
   
   printf("sleep time was %.4f 
msec\n",((now.tv_sec-then.tv_sec)+(now.tv_nsec-then.tv_nsec)*1.0e-9)*1e3);
   printf("realtime delta is %d sec %d nsec\n",(now.tv_sec-then.tv_sec), 
(now.tv_nsec-then.tv_nsec));

   printf("sleep time was %.4f 
msec\n",((now_m.tv_sec-then_m.tv_sec)+(now_m.tv_nsec-then_m.tv_nsec)*1.0e-9)*1e3);
   printf("monotonic delta is %d sec %d nsec\n",(now_m.tv_sec-then_m.tv_sec), 
(now_m.tv_nsec-then_m.tv_nsec));

   return 1;
}

prints the following information:

CLOCK_REALTIME resolution 10000000 nsec or 10000.0000 usec
CLOCK_MONOTONIC resolution 2006 nsec or 2.0060 usec
sleep time was 15.3930 msec
realtime delta is 0 sec 15393000 nsec
sleep time was 15340.0666 msec
monotonic delta is 16 sec -659933436 nsec

It looks like CLOCK_MONOTONIC is returning a value in usec rather than in nsec. 
 This may be true for the resolution as well but that's not clear from my test.

I am running under Red Hat 7.0, kernel 2.2.16, i686 processor, and with 
librt.so.1.

Thanks for any help,
-Aaron





reply via email to

[Prev in Thread] Current Thread [Next in Thread]