How to implement elapsed time by jiffies

你说的曾经没有我的故事 提交于 2019-12-06 06:24:47

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!