Converting jiffies to milli seconds

前端 未结 4 1813
生来不讨喜
生来不讨喜 2020-12-04 11:42

How do I manually convert jiffies to milliseconds and vice versa in Linux? I know kernel 2.6 has a function for this, but I\'m working on 2.4 (homework) and though I looked

4条回答
  •  旧巷少年郎
    2020-12-04 12:02

    Jiffies are hard-coded in Linux 2.4. Check the definition of HZ, which is defined in the architecture-specific param.h. It's often 100 Hz, which is one tick every (1 sec/100 ticks * 1000 ms/sec) 10 ms.

    This holds true for i386, and HZ is defined in include/asm-i386/param.h.

    There are functions in include/linux/time.h called timespec_to_jiffies and jiffies_to_timespec where you can convert back and forth between a struct timespec and jiffies:

        #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
    
        static __inline__ unsigned long
        timespec_to_jiffies(struct timespec *value)
        {
                unsigned long sec = value->tv_sec;
                long nsec = value->tv_nsec;
    
                if (sec >= (MAX_JIFFY_OFFSET / HZ))
                        return MAX_JIFFY_OFFSET;
                nsec += 1000000000L / HZ - 1;
                nsec /= 1000000000L / HZ;
                return HZ * sec + nsec;
        }
    
        static __inline__ void
        jiffies_to_timespec(unsigned long jiffies, struct timespec *value)
        {
                value->tv_nsec = (jiffies % HZ) * (1000000000L / HZ);
                value->tv_sec = jiffies / HZ;
        }
    

    Note: I checked this info in version 2.4.22.

提交回复
热议问题