[ACCEPTED]-Python: How to get number of mili seconds per jiffy-linux

Accepted answer
Score: 25

There is USER_HZ

>>> import os
>>> os.sysconf_names['SC_CLK_TCK']
2
>>> os.sysconf(2)
100

which is what the kernel 30 uses to report time in /proc.

From the time(7) manual 29 page:

The Software Clock, HZ, and Jiffies

The accuracy of various system 28 calls that set timeouts, (e.g., select(2), sigtimedwait(2)) and 27 measure CPU time (e.g., getrusage(2)) is 26 limited by the resolution of the software 25 clock, a clock maintained by the kernel 24 which measures time in jiffies. The size 23 of a jiffy is determined by the value 22 of the kernel constant HZ.

The value of 21 HZ varies across kernel versions and hardware 20 platforms. On i386 the situation is as 19 follows: on kernels up to and including 2.4.x, HZ 18 was 100, giving a jiffy value of 0.01 seconds; starting 17 with 2.6.0, HZ was raised to 1000, giving 16 a jiffy of 0.001 seconds. Since kernel 15 2.6.13, the HZ value is a kernel configuration 14 parameter and can be 100, 250 (the default) or 13 1000, yielding a jiffies value of, respec‐ tively, 0.01, 0.004, or 12 0.001 seconds. Since kernel 2.6.20, a further frequency 11 is available: 300, a number that divides 10 evenly for the com‐ mon video frame rates 9 (PAL, 25 HZ; NTSC, 30 HZ).

The times(2) system 8 call is a special case. It reports times 7 with a granularity defined by the kernel 6 constant USER_HZ. Userspace applica‐ tions 5 can determine the value of 4 this constant using sysconf(_SC_CLK_TCK).

If 3 you absolutely must know SYSTEM_HZ:

>>> from ctypes import *
>>> rt = CDLL('librt.so')
>>> CLOCK_REALTIME = 0
>>> class timespec(Structure):
...     _fields_ = [("tv_sec", c_long), ("tv_nsec", c_long)]
... 
>>> res = timespec()
>>> rt.clock_getres(CLOCK_REALTIME, byref(res))
0
>>> res.tv_sec, res.tv_nsec
(0, 4000250)
>>> SYSTEM_HZ = round(1/(res.tv_sec + (res.tv_nsec/10.0**9)))

Gives 2 250 on my laptop (which sounds about right) and 1 1000000000 in a VM…

Score: 5

sysconf(SC_CLK_TCK) does not give the frequency 10 of the timer interrupts in Linux. It gives 9 the frequency of jiffies which is visible 8 to userspace in things like the counters 7 in various directories in /proc

The actual 6 frequency is hidden from userspace, deliberately. Indeed, some 5 systems use dynamic ticks or "tickless" systems, so 4 there aren't really any at all.

All the userspace 3 interfaces use the value from SC_CLK_TCK, which 2 as far as I can see is always 100 under 1 Linux.

Score: 0

I wrote this:

https://github.com/peppelinux/xt_recent_parser

output is like this:

python3 xt_recent_parser.py 
XT_RECENT python parser
<giuseppe.demarco@unical.it>


114.241.108.160, last seen: 2017-03-25 18:21:42 after 13 Connections 
46.165.210.17, last seen: 2017-03-25 13:07:54 after 10 Connections 
61.53.219.162, last seen: 2017-03-25 17:39:17 after 20 Connections 
179.37.141.232, last seen: 2017-03-25 18:08:23 after 2 Connections 
114.42.117.39, last seen: 2017-03-25 13:22:14 after 18 Connections 
177.12.84.234, last seen: 2017-03-25 16:22:14 after 17 Connections 

I think 3 that it will be easy to edit if you need 2 millisecond conversion, you only have to 1 extend JiffyTimeConverter python class

More Related questions