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?
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With