Tuesday, November 10, 2015

gethrtime() on Linux

As far as I know, Linux has no equivalent to Solaris' gethrtime() function. So here's a quick routine to provide an equivalent value:
/*
 * gcc gethrtime.c -o gethrtime -O3 -lrt
 */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

#define NANOSEC 1000000000
unsigned long
gethrtime(void)
{
        struct timespec ts;
    
        if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0) {
                return (-1);
        }

        return ((ts.tv_sec * NANOSEC) + ts.tv_nsec);
}

No comments:

Post a Comment