Jiffies - how to calculate seconds elapsed?
Solution 1
For your use case, you can use either:
jiffies_to_msecs or jiffies_to_usecs
All conversion routines below:
From include/linux/jiffies.h
...
/*
* Convert various time units to each other:
*/
extern unsigned int jiffies_to_msecs(const unsigned long j);
extern unsigned int jiffies_to_usecs(const unsigned long j);
extern unsigned long msecs_to_jiffies(const unsigned int m);
extern unsigned long usecs_to_jiffies(const unsigned int u);
extern unsigned long timespec_to_jiffies(const struct timespec *value);
extern void jiffies_to_timespec(const unsigned long jiffies,
struct timespec *value);
extern unsigned long timeval_to_jiffies(const struct timeval *value);
extern void jiffies_to_timeval(const unsigned long jiffies,
struct timeval *value);
extern clock_t jiffies_to_clock_t(unsigned long x);
extern unsigned long clock_t_to_jiffies(unsigned long x);
extern u64 jiffies_64_to_clock_t(u64 x);
extern u64 nsec_to_clock_t(u64 x);
extern u64 nsecs_to_jiffies64(u64 n);
extern unsigned long nsecs_to_jiffies(u64 n);
...
Solution 2
From http://www.kernel.org/doc/man-pages/online/pages/man7/time.7.html:
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, respectively, 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 common video frame rates (PAL, 25 HZ; NTSC, 30 HZ).
Just divide by HZ.
Raheel
Updated on March 12, 2020Comments
-
Raheel about 4 years
I have a piece of code, i want to calculate the time in seconds.. though i am getting time in jiffies, how can i convert it in seconds?
here is my kernel code:
#include <linux/module.h> #include <linux/kernel.h> #include <linux/jiffies.h> #include <linux/timer.h> unsigned long js, je, tet; int netblock_init_module(void){ js = jiffies; printk("\n[Jiffies start Time : %lu]\nModule Started.\n", js); return 0; } void netblock_cleanup_module(void) { je = jiffies; printk("\n[Jiffies End Time : %lu]\nModule Removed.\n", je); tet = je - js; printk("\nEnd Time [%lu] - Start Time [%lu]: \nTotlal elapsed Time [%lu]\n",js,je, tet); } module_init(netblock_init_module); module_exit(netblock_cleanup_module); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("JIFFIES EXAMPLE"); MODULE_AUTHOR("RAHEEL");
Output I am getting is like to this:
$insmod jiffexample.ko
[Jiffies start Time : 13363583]
Module Started
$rmmod jiffexample.ko
[Jiffies End Time : 13361588]
Module Removed.
End Time 13361588 - Start Time 1336358
Total Elapsed time [1605]
Now i want to get converted time in seconds.. how its possible to convert this elapsed time 1605 in seconds? or alternatively can you please tell me how many jiffies are in a second?