如何实现jiffies的消逝时间 [英] How to implement elapsed time by jiffies

查看:118
本文介绍了如何实现jiffies的消逝时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何使用C语言中的Jiffies来实现经过的时间. 假设我有一系列说明

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***/

使用jiffies,我必须在代码上写些什么? 这样写对吗?

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

对吗?使用jiffies比使用getnstimeofday函数更好?

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

推荐答案

您可以执行以下操作:

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

由于您正在使用ARM arch,因此可以通过安装在dmesg上打印分辨率的内核模块来检查系统计时器的可用分辨率:

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

如果分辨率是一个抖动周期内的ns数,则说明您的平台受到限制,否则,您可以考虑使用hrtimers来监视处理时间.

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.

要编译前面的代码:您可以重复使用以下Makefile:

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

希望有帮助. 艾门.

这篇关于如何实现jiffies的消逝时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆