Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get clock_gettime(2) clock in shell?

Tags:

linux

time

I see no such option for date

/proc/uptime is bootbased, not monotonic.

And at last I found cat /proc/timer_list | grep now which yields number of nsecs which is obtained via ktime_get which is returning monotonic time if I understand correctly, but that's quite cumbersome.

update: the returned value must be the same as returned by clock_gettime

like image 298
Jan Matějka Avatar asked Aug 28 '13 14:08

Jan Matějka


People also ask

What does clock_ gettime return?

The clock_gettime() function shall return the current value tp for the specified clock, clock_id. The clock_settime() function shall set the specified clock, clock_id, to the value specified by tp.

Is clock_gettime a system call?

The clock_gettime system call is a successor to the gettimeofday system call with a few key changes: higher precision and the ability to request specific clocks. It fills in a structure containing two fields: a seconds and a nanosecond count of the time since the Epoch (00:00 1 January, 1970 UTC).

What is Clock_monotonic_raw?

CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific) Similar to CLOCK_MONOTONIC, but provides access to a raw hardware-based time that is not subject to NTP adjustments or the incremental adjustments performed by adjtime(3). This clock does not count time that the system is suspended.

What is Clockid_t?

The type clockid_t is used for constants that indicate which of several system clocks one wishes to use. All systems that support this family of functions will define at least this clock constant: Macro: clockid_t CLOCK_REALTIME. This clock uses the POSIX epoch, 00:00:00 on January 1, 1970, Coordinated Universal Time.


3 Answers

This does not answer the current question but answers the original one. So, it is being kept as it has been useful to some people so far.

In shell you could just use the date utility:

date +%s.%N
date +%s%N
nanoseconds_since_70=$(date +%s%N)

From man date:

       %s     seconds since 1970-01-01 00:00:00 UTC
       %N     nanoseconds (000000000..999999999)

The nanoseconds portion complement the seconds in the right way: when %N goes from 999999999 to 0 the %s increments one second. I dont have a reference for that (please edit if you can found it) but just works.

date utility x clock_gettime

Note that the number is not affected by changes in time zone but will be affected by changes in the system clock, like the changes made by the system administrator, NTP and adjtime function. However the CLOCK_MONOTONIC in the clock_gettime function is also affected, except by the administrator changes.

 CLOCK_MONOTONIC -- Clock  that  cannot  be set and represents monotonic time
 since some unspecified starting point.  This clock is not affected by 
 discontinuous jumps in the system time (e.g., if the system administrator 
 manually changes the clock), but is affected by the incremental adjustments
 performed by adjtime(3) and NTP.

Newer system has a better solution: CLOCK_MONOTIC_RAW. Despite that, this is a shell solution as requested.

To know more

Monotonic function in Wikipedia

The @caf user answer from Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?:

CLOCK_MONOTONIC represents the absolute elapsed wall-clock time since some
arbitrary, fixed point in the past. It isn't affected by changes in the 
system time-of-day clock. 

If you want to compute the elapsed time between two events observed on the one
machine without an intervening reboot, CLOCK_MONOTONIC is the best option.
like image 87
olivecoder Avatar answered Oct 16 '22 16:10

olivecoder


Consulting /proc/uptime is only cumbersome if you write out the full line each time. Why not wrap it up like this:

$ alias now="awk '/^now/ {print \$3; exit}' /proc/timer_list"
$ now
396751009584948

This also avoids the Useless Use of cat.

like image 40
PhilR Avatar answered Oct 16 '22 15:10

PhilR


Looks like it's available in python 3.3: http://www.python.org/dev/peps/pep-0418/

Failing that, you could write a small C program which calls clock_gettime: http://linux.die.net/man/3/clock_gettime

like image 7
We Are All Monica Avatar answered Oct 16 '22 16:10

We Are All Monica