How to implement elapsed time by jiffies

2.5k views Asked by At

I would like to understand how can I implement elapsed time using jiffies in C. Let's suppose that I have a series of instructions

#include <linux/jiffies.h>
unsigned long js,je,diff;


/***Start Time***/
/*Series of instructions*/
/***End Time***/

Using jiffies, what must I write on my code? Is it right to write so?

#include <linux/jiffies.h>
unsigned long js,je,diff;
unsigned int diffusec;


js = jiffies; /***Start Time***/
/*Series of instructions*/
je = jiffies; /***End Time***/

diff = je - js;
diffusec = jiffies_to_usecs(diff);

Is it right? Using jiffies is better than using getnstimeofday function?

1

There are 1 answers

7
Aymen Zayet On

You can do something like this :

struct timeval start, finish;
long delta_usecs;

do_gettimeofday(&start);
..
// execute your processing here
..
do_gettimeofday(&finish);

delta_usecs = (finish.tv_sec - start.tv_sec) * 1000000 +
              (finish.tv_usec - start.tv_usec);

Since you are working on ARM arch, it may help to check the available resolution of your system timer by insmoding a kernel module that prints on dmesg the resolution:

#include <linux/version.h>
#include <linux/module.h>
#include <linux/hrtimer.h>
#include <linux/time.h>

static struct hrtimer timer;

static int __init hrtimer_test_init(void)
{
        struct timespec time;

        hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
        hrtimer_get_res(CLOCK_MONOTONIC, &time);
        printk(KERN_ERR "resolution : %u secs and %u nsecs.\n",
               time.tv_sec, time.tv_nsec);
        return 0;
}

static void __exit hrtimer_test_exit(void)
{
        return ;
}

module_init(hrtimer_test_init);
module_exit(hrtimer_test_exit);
MODULE_AUTHOR("hrtimer test for demo only");
MODULE_DESCRIPTION("hrtimer resolution");
MODULE_LICENSE("GPL");

If the resolution is the number of ns in a jiffies period, then you are a bit limited on your platform, otherwise, you can think of using the hrtimers to monitor the processing time.

To compile the previous code : you can reuse the following Makefile :

KERNELDIR := /lib/modules/$(shell uname -r)/build

.PHONY: all clean

clean:
    $(MAKE) -C $(KERNELDIR) M=$(shell pwd) clean
    rm -rf *~

all:
    $(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules

obj-m := hrtimer-test.o

Hope that helps. Aymen.