2

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?

Develobeer
  • 425
  • 1
  • 8
  • 19
  • See: http://stackoverflow.com/questions/10885685/jiffies-how-to-calculate-seconds-elapsed – syntagma Nov 22 '14 at 17:48
  • 1
    Thank you for answer. I know that getnstimeofday, rdtsc, clock(), do_gettimeofday() are for user space. For kernel space we have only jiffies or another? – Develobeer Nov 22 '14 at 17:52

1 Answers1

2

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.

  • Hi Aymen. I tried this solution but I noted that getnstimeofday instruction has about 15 microsec of delay, and I think also do_gettimeofday. Now I'm trying with another solution but I have a problem. If you can, I link my question http://stackoverflow.com/questions/27092331/error-segv-in-test-c-code-on-elapsed-time-in-arm-cortex-m4-processor I hope that you can help me xD. Thank you – Develobeer Nov 23 '14 at 18:10
  • it depends on the hardware you are using. What is the CPU arch you are referring to in this case ? – Aymen Zayet Nov 23 '14 at 18:17
  • ARM Cortex-M4 on stm32f429 board – Develobeer Nov 23 '14 at 18:20
  • Ok, I am not sure about the resolution of the system timer used on ARM, but if you are looking for it, in that case you can check it by calling clock_getres(). Now, back to the measurements, in general, if you are looking for high resolution, you can use hrtimer which is posix compliant. That should cover the need. – Aymen Zayet Nov 23 '14 at 18:34
  • Here is an interesting article on lwn.net discussing hrtimer APIs in linux kernel : http://lwn.net/Articles/167897/ . You can use hrtimer_get_res() with clkid CLOCK_REALTIME and see whether hrtimers are enough for what you are doing. – Aymen Zayet Nov 23 '14 at 18:44
  • Hrtimer I never used... For my code I don't know where I can start... And now I'm trying to resolve SEGV problem xD Can you post me an example code to measure elapsed time with hrtimer.h? – Develobeer Nov 23 '14 at 18:56
  • Again this need to be checked on your platform. First of all you have to make sure that the driver CONFIG_HIGH_RES_TIMERS is enabled in your .config file. Then check the following : hrtimer_get_res(CLOCK_MONOTONIC, &time); where time is of type "struct timespec". This will help you to see the resolution of your local system timer. – Aymen Zayet Nov 23 '14 at 22:04