Starting point for CLOCK_MONOTONIC

后端 未结 3 608
名媛妹妹
名媛妹妹 2020-12-09 04:07

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

相关标签:
3条回答
  • 2020-12-09 04:24

    CLOCK_MONOTONIC is generally not affected by any adjustments to system time. For example, if the system clock is adjusted via NTP, CLOCK_MONOTONIC has no way of knowing (nor does it need to).

    For this reason, don't use CLOCK_MONOTONIC if you need human-readable timestamps.

    See Difference between CLOCK_REALTIME and CLOCK_MONOTONIC? for a discussion.

    0 讨论(0)
  • 2020-12-09 04:31

    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.

    0 讨论(0)
  • 2020-12-09 04:39

    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.

    0 讨论(0)
提交回复
热议问题