Convert Jiffies to Seconds

Converting jiffies to milli seconds

As a previous answer said, the rate at which jiffies increments is fixed.

The standard way of specifying time for a function that accepts jiffies is using the constant HZ.

That's the abbreviation for Hertz, or the number of ticks per second. On a system with a timer tick set to 1ms, HZ=1000. Some distributions or architectures may use another number (100 used to be common).

The standard way of specifying a jiffies count for a function is using HZ, like this:

schedule_timeout(HZ / 10);  /* Timeout after 1/10 second */

In most simple cases, this works fine.

2*HZ     /* 2 seconds in jiffies */
HZ /* 1 second in jiffies */
foo * HZ /* foo seconds in jiffies */
HZ/10 /* 100 milliseconds in jiffies */
HZ/100 /* 10 milliseconds in jiffies */
bar*HZ/1000 /* bar milliseconds in jiffies */

Those last two have a bit of a problem, however, as on a system with a 10 ms timer tick, HZ/100 is 1, and the precision starts to suffer. You may get a delay anywhere between 0.0001 and 1.999 timer ticks (0-2 ms, essentially). If you tried to use HZ/200 on a 10ms tick system, the integer division gives you 0 jiffies!

So the rule of thumb is, be very careful using HZ for tiny values (those approaching 1 jiffie).

To convert the other way, you would use:

jiffies / HZ          /* jiffies to seconds */
jiffies * 1000 / HZ /* jiffies to milliseconds */

You shouldn't expect anything better than millisecond precision.

convert jiffies to seconds

You divide it by the number you get from sysconf(_SC_CLK_TCK).

However, I think this is probably always 100 under Linux regardless of the actual clock tick, it's always presented to userspace as 100.

See man proc(5).

Converting clock time (real world time) to jiffies and vice versa

[This answer is for the Linux kernel since linux-kernel was tagged in the question.]

mktime64 is nothing to do with jiffies. It converts the date specified by its parameters to the number of seconds (ignoring leap seconds) since 1970-01-01 00:00:00 (Unix time since the epoch if the parameters are for GMT).

The returned time64_t value can be converted back to year, month, day, hours, minutes, seconds using the time64_to_tm function in the kernel. It has this prototype:

void time64_to_tm(time64_t totalsecs, int offset, struct tm *result);

The offset parameter is a local timezone offset in seconds (number of seconds east of GMT). It should be set to 0 to undo the conversion done by mktime64.

Note that the tm_year member is set to the calculated year minus 1900 and the tm_mon member is set to the calculated month minus 1, so you could implement an unmktime64 function as follows:

void unmktime64(time64_t totalsecs,
int *year, unsigned int *month, unsigned int *day,
unsigned int *hour, unsigned int *minute, unsigned int *second)
{
struct tm tm;

time64_to_tm(totalsecs, 0, &tm);
*year = tm.tm_year + 1900;
*month = tm.tm_mon + 1;
*day = tm.tm_mday;
*hour = tm.tm_hour;
*minute = tm.tm_min;
*second = tm.tm_sec;
}

Convert jiffy time

In order to convert jiffies, you are going to have to start by finding your kernels constant value for HZ. You could do this by using os.sysconf:

import os

sc_clk_tck = os.sysconf_names['SC_CLK_TCK']
HZ = os.sysconf(sc_clk_tck)

From here you want to find the system boot time:

with open('/proc/stat') as f:
for ln in f.readlines():
if ln.startswith('btime'):
btime = int(ln.split()[1])

Take this value and add it to your jiffy values you want converted divided by HZ:

(packet_jiffy / HZ) + btime

You could pass this value on to time.ctime and/or subtract it from time.time.

import os
import time

sc_clk_tck = os.sysconf_names['SC_CLK_TCK']
HZ = os.sysconf(sc_clk_tck)

with open('/proc/stat') as f:
for ln in f.readlines():
if ln.startswith('btime'):
btime = ln.split()[1]


packet_time = (packet_jiffy / HZ) + btime
print time.ctime(packet_time)
print "Seconds ago %d" % (time.time() - packet_time)

This post has more information dealing with converting jiffies.

why can't I match jiffies to uptime?

What you're trying to do is how Linux used to work -- 10 years ago.

It's become more complicated since then. Some of the complications that I know of are:

  • There's an offset of -5 minutes so that the kernel always tests jiffy rollover.
  • The kernel command line can set a jiffy skip value so a 1000 Hz kernel can run at 250 or 100 or 10.
  • Various attempts at NoHZ don't use a timer tick at all and rely only on the timer ring and the HPET.
  • I believe there are some virtual guest extensions that disable the tick and ask the host hypervisor whenever a tick is needed. Such as the Xen or UML builds.

That's why the kernel has functions designed to tell you the time. Use them or figure out what they are doing and copy it.



Related Topics



Leave a reply



Submit