Jiffies - how to calculate seconds elapsed?

c, kernel, linux-kernel, performance, time

Solution

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);
...

Problem

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?

Original source