Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert jiffies to seconds

I've got a piece of userspace code which is parsing /proc/PID/task/TID/stat to get the cpu usage. I can use HZ to get the jiffies per second but this code could move to another machine which has a different configured value. Is there any way to get the value of HZ from userspace at runtime?

like image 612
Brian Makin Avatar asked Oct 06 '10 18:10

Brian Makin


People also ask

What are Jiffies and Hz?

A jiffy is a kernel unit of time declared in <linux/jiffies. h> . To understand jiffies, we need to introduce a new constant, HZ, which is the number of times jiffies is incremented in one second. Each increment is called a tick.


2 Answers

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).

like image 114
MarkR Avatar answered Oct 01 '22 22:10

MarkR


To clarify the math behind MarkR's answer:

sysconf(_SC_CLK_TCK) will get you jiffies per second. Divide jiffies by the number you get from sysconf(_SC_CLK_TCK) to get the total number of seconds.

      jiffies                      jiffies              seconds
--------------------    =     -----------------    =    -------    =    seconds
sysconf(_SC_CLK_TCK)          (jiffies/second)             1
like image 35
Vilhelm Gray Avatar answered Oct 01 '22 22:10

Vilhelm Gray