2014-11-22 63 views
1

我想明白經過的時候,我怎麼能在C 使用jiffies的實施經過的時間讓我們假設我有一系列的指令如何落實的jiffies

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


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

使用jiffies的,我必須寫在我的代碼? 這樣寫是否正確?

#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函數更好?

+0

請參閱:http://stackoverflow.com/questions/10885685/jiffies-how-to-calculate-seconds-已過時 – syntagma 2014-11-22 17:48:42

+1

謝謝你的回答。我知道getnstimeofday,rdtsc,clock(),do_gettimeofday()是用於用戶空間的。對於內核空間,我們只有jiffies或其他? – Anth 2014-11-22 17:52:24

回答

0

你可以做這樣的事情:

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拱,它可以幫助通過insmoding內核模塊上dmesg的分辨率打印檢查您的系統計時器的可用的分辨率:

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

如果分辨率是在jiffies的週期數Ns,那麼你是你的平臺上的位有限,否則,您可以考慮使用hrtimers監察處理時間。

要編譯前面的代碼:您可以重複使用下面的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 

希望有所幫助。 Aymen。

+0

嗨艾門。我嘗試了這個解決方案,但我注意到getnstimeofday指令有大約15微秒的延遲,我想也是do_gettimeofday。現在我正在嘗試另一種解決方案,但我遇到了問題。如果你可以,我鏈接我的問題http://stackoverflow.com/questions/27092331/error-segv-in-test-c-code-on-elapsed-time-in-arm-cortex-m4-processor 我希望你可以幫我xD。謝謝 – Anth 2014-11-23 18:10:12

+0

這取決於你使用的硬件。在這種情況下,你指的是什麼? – 2014-11-23 18:17:29

+0

stm32f429板上的ARM Cortex-M4 – Anth 2014-11-23 18:20:14