Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting point for CLOCK_MONOTONIC

Tags:

As I understand on Linux starting point for CLOCK_MONOTONIC is boot time. In my current work I prefer to use monotonic clock instead of CLOCK_REALTIME (for calculation) but in same time I need to provide human friendly timestamps (with year/month/day) in reporting. They can be not very precise so I was thinking to join monotonic counter with boot time.

From where I can get this time on linux system using api calls?

like image 869
rimas Avatar asked Feb 06 '13 10:02

rimas


2 Answers

Assuming the Linux kernel starts the uptime counter at the same time as it starts keeping track of the monotonic clock, you can derive the boot time (relative to the Epoch) by subtracting uptime from the current time.

Linux offers the system uptime in seconds via the sysinfo structure; the current time in seconds since the Epoch can be acquired on POSIX compliant libraries via the time function.

#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <sys/sysinfo.h>

int main(void) {
    /* get uptime in seconds */
    struct sysinfo info;
    sysinfo(&info);

    /* calculate boot time in seconds since the Epoch */
    const time_t boottime = time(NULL) - info.uptime;

    /* get monotonic clock time */
    struct timespec monotime;
    clock_gettime(CLOCK_MONOTONIC, &monotime);

    /* calculate current time in seconds since the Epoch */
    time_t curtime = boottime + monotime.tv_sec;

    /* get realtime clock time for comparison */
    struct timespec realtime;
    clock_gettime(CLOCK_REALTIME, &realtime);

    printf("Boot time = %s", ctime(&boottime));
    printf("Current time = %s", ctime(&curtime));
    printf("Real Time = %s", ctime(&realtime.tv_sec));

    return 0;
}

Unfortunately, the monotonic clock may not match up relative to boot time exactly. When I tested out the above code on my machine, the monotonic clock was a second off from the system uptime. However, you can still use the monotonic clock as long as you take the respective offset into account.

Portability note: although Linux may return current monotonic time relative to boot time, POSIX machines in general are permitted to return current monotonic time from any arbitrary -- yet consistent -- point in time (often the Epoch).


As a side note, you may not need to derive boot time as I did. I suspect there is a way to get the boot time via the Linux API, as there are many Linux utilities which display the boot time in a human-readable format. For example:

$ who -b
         system boot  2013-06-21 12:56

I wasn't able to find such a call, but inspection of the source code for some of these common utilities may reveal how they determine the human-readable boot time.

In the case of the who utility, I suspect it utilizes the utmp file to acquire the system boot time.

like image 74
Vilhelm Gray Avatar answered Oct 01 '22 22:10

Vilhelm Gray


http://www.kernel.org/doc/man-pages/online/pages/man2/clock_getres.2.html:

  CLOCK_MONOTONIC
          Clock that cannot be set and represents monotonic time since some
          unspecified starting point.

Means that you can use CLOCK_MONOTONIC for interval calculations and other things but you can't really convert it to a human readable representation.

Moreover, you prabably want CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC:

  CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
          Similar  to  CLOCK_MONOTONIC, but provides access to a raw hard‐
          ware-based time that is not subject to NTP adjustments.

Keep using CLOCK_REALTIME for human-readable times.

like image 43
Maxim Egorushkin Avatar answered Oct 01 '22 23:10

Maxim Egorushkin