Python: How to get number of mili seconds per jiffy

前端 未结 3 1331
自闭症患者
自闭症患者 2020-12-06 10:20

I\'d like to know the HZ of the system, i.e. how many mili seconds is one jiffy from Python code.

相关标签:
3条回答
  • 2020-12-06 10:46

    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 that it will be easy to edit if you need millisecond conversion, you only have to extend JiffyTimeConverter python class

    0 讨论(0)
  • 2020-12-06 10:51

    There is USER_HZ

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

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

    From the time(7) manual page:

    The Software Clock, HZ, and Jiffies

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

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

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

    If 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 250 on my laptop (which sounds about right) and 1000000000 in a VM…

    0 讨论(0)
  • 2020-12-06 11:02

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

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

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

    0 讨论(0)
提交回复
热议问题