2011-07-04 29 views
3

我現在在使用ubuntu 11.04並使用v2lin從vxWorks tolinux移植我的程序。 clock_getres()有問題。clock_getres和kernel 2.6

與此代碼:

struct timespec res; 
clock_getres(CLOCK_REALTIME, &res); 

我有res.tv_nsec = 1,這是不知何故不正確的。

像這個人顯示:http://forum.kernelnewbies.org/read.php?6,377,423,有內核2.4和2.6之間的區別。

那麼應該是時鐘分辨率正確的值在2.6內核

感謝

+0

是什麼讓你覺得這是不正確的? – nos

回答

0

嘗試從procfs的得到它。

cat/proc/timer_list

0

爲什麼你認爲這是不正確的?例如,在現代的x86 CPU上,內核使用TSC來提供高分辨率時鐘 - 任何運行速度高於1Ghz的CPU都具有比每納秒快於tick的TSC,因此納秒分辨率非常普遍。

1

根據來自內核源文件的「include/linux/hrtimer.h」文件,clock_getres()將始終爲高分辨率定時器(如果系統中有這樣的定時器)返回1ns(1納秒)。該值是硬編碼,這意味着:(如果沒有hrtimer硬件和單調和實時時鐘)「定時器的值將被舍入到它」

http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/hrtimer.h

269 /* 
270 * The resolution of the clocks. The resolution value is returned in 
271 * the clock_getres() system call to give application programmers an 
272 * idea of the (in)accuracy of timers. Timer values are rounded up to 
273 * this resolution values. 
274 */ 
275 # define HIGH_RES_NSEC   1 
276 # define KTIME_HIGH_RES   (ktime_t) { .tv64 = HIGH_RES_NSEC } 
277 # define MONOTONIC_RES_NSEC  HIGH_RES_NSEC 
278 # define KTIME_MONOTONIC_RES KTIME_HIGH_RES 

對於低分辨率計時器,Linux的將返回1/HZ(典型HZ是從100至1000,所以值將是從1到10毫秒):

http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/ktime.h#L321

321 #define LOW_RES_NSEC   TICK_NSEC 
322 #define KTIME_LOW_RES   (ktime_t){ .tv64 = LOW_RES_NSEC } 

來自低分辨率定時器的值可能被舍入到如此低的精度(實際上它們就像jiffles,linux內核「ticks」)。

PS:This post http://forum.kernelnewbies.org/read.php?6,377,423據我所知,比較2.4 linux沒有啓用hrtimers(已實現)的2.6內核與可用的hrtimers。所以所有的值都是正確的。