欢迎来到天天文库
浏览记录
ID:19898777
大小:50.50 KB
页数:7页
时间:2018-10-07
《linux中jiffies变量》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、全局变量jiffies用来记录自系统启动以来产生的节拍的总数。启动时,内核将该变量初始化为0,此后,每次时钟中断处理程序都会增加该变量的值。一秒内时钟中断的次数等于Hz,所以jiffies一秒内增加的值也就是Hz。 系统运行时间以秒为单位,等于jiffies/Hz。注意,jiffies类型为无符号长整型(unsignedlong),其他任何类型存放它都不正确。将以秒为单位的时间转化为jiffies:seconds*Hz将jiffies转化为以秒为单位的时间:jiffies/Hz相比之下,内核中将秒转换为jiffies用的多些。·jiffies的内部表示 jiffies定义于文件2、Jiffies.h>中:·/*·*The64-bitvalueisnotatomic-youMUSTNOTreadit·*withoutsamplingthesequencenumberinxtime_lock.·*get_jiffies_64()willdothisforyouasappropriate.·*/·externu64__jiffy_datajiffies_64;·externunsignedlongvolatile__jiffy_datajiffies;ld(1)脚本用于连接主内核映像(在x86上位于arch/i386/kernel/vmlinux.lds.S中),然后用ji3、ffies_64变量的初值覆盖jiffies变量。因此jiffies取整个jiffies_64变量的低32位。 访问jiffies的代码只会读取jiffies_64的低32位,通过get_jiffies_64()函数就可以读取整个64位的值。在64位体系结构上,jiffies_64和jiffies指的是同一个变量。·#if(BITS_PER_LONG<64)·u64get_jiffies_64(void);·#else·staticinlineu64get_jiffies_64(void)·{·return(u64)jiffies;·}·#endif·在中·#i4、f(BITS_PER_LONG<64)·u64get_jiffies_64(void)·{· unsignedlongseq;· u64ret;··do{· seq=read_seqbegin(&xtime_lock);· ret=jiffies_64;· }while(read_seqretry(&xtime_lock,seq));·returnret;·}·jiffies的回绕wraparound 当jiffies的值超过它的最大存放范围后就会发生溢出。对于32位无符号长整型,最大取值为(2^32)-1,即429496795。如果节拍计数达到了最大值后还5、要继续增加,它的值就会回绕到0。 内核提供了四个宏来帮助比较节拍计数,它们能正确的处理节拍计数回绕的问题:·/*·* Theseinlinesdealwithtimerwrappingcorrectly.Youare·* stronglyencouragedtousethem·* 1.Becausepeopleotherwiseforget·* 2.Becauseifthetimerwrapchangesinfutureyouwon'thaveto·* alteryourdrivercode.·*·*time_after(a,b)returnstrueifthetimeaisaftert6、imeb.·*·*Dothiswith"<0"and">=0"toonlytestthesignoftheresult.A·*goodcompilerwouldgeneratebettercode(andareallygoodcompiler·*wouldn'tcare).Gcciscurrentlyneither.·*/·#definetime_after(a,b) · (typecheck(unsignedlong,a)&&· typecheck(unsignedlong,b)&&· ((long)(b)-(long)(a)<0))·#definetime_be7、fore(a,b) time_after(b,a)··#definetime_after_eq(a,b) · (typecheck(unsignedlong,a)&&· typecheck(unsignedlong,b)&&· ((long)(a)-(long)(b)>=0))·#definetime_before_eq(a,b)time_after_eq(b,a)··/
2、Jiffies.h>中:·/*·*The64-bitvalueisnotatomic-youMUSTNOTreadit·*withoutsamplingthesequencenumberinxtime_lock.·*get_jiffies_64()willdothisforyouasappropriate.·*/·externu64__jiffy_datajiffies_64;·externunsignedlongvolatile__jiffy_datajiffies;ld(1)脚本用于连接主内核映像(在x86上位于arch/i386/kernel/vmlinux.lds.S中),然后用ji
3、ffies_64变量的初值覆盖jiffies变量。因此jiffies取整个jiffies_64变量的低32位。 访问jiffies的代码只会读取jiffies_64的低32位,通过get_jiffies_64()函数就可以读取整个64位的值。在64位体系结构上,jiffies_64和jiffies指的是同一个变量。·#if(BITS_PER_LONG<64)·u64get_jiffies_64(void);·#else·staticinlineu64get_jiffies_64(void)·{·return(u64)jiffies;·}·#endif·在中·#i
4、f(BITS_PER_LONG<64)·u64get_jiffies_64(void)·{· unsignedlongseq;· u64ret;··do{· seq=read_seqbegin(&xtime_lock);· ret=jiffies_64;· }while(read_seqretry(&xtime_lock,seq));·returnret;·}·jiffies的回绕wraparound 当jiffies的值超过它的最大存放范围后就会发生溢出。对于32位无符号长整型,最大取值为(2^32)-1,即429496795。如果节拍计数达到了最大值后还
5、要继续增加,它的值就会回绕到0。 内核提供了四个宏来帮助比较节拍计数,它们能正确的处理节拍计数回绕的问题:·/*·* Theseinlinesdealwithtimerwrappingcorrectly.Youare·* stronglyencouragedtousethem·* 1.Becausepeopleotherwiseforget·* 2.Becauseifthetimerwrapchangesinfutureyouwon'thaveto·* alteryourdrivercode.·*·*time_after(a,b)returnstrueifthetimeaisaftert
6、imeb.·*·*Dothiswith"<0"and">=0"toonlytestthesignoftheresult.A·*goodcompilerwouldgeneratebettercode(andareallygoodcompiler·*wouldn'tcare).Gcciscurrentlyneither.·*/·#definetime_after(a,b) · (typecheck(unsignedlong,a)&&· typecheck(unsignedlong,b)&&· ((long)(b)-(long)(a)<0))·#definetime_be
7、fore(a,b) time_after(b,a)··#definetime_after_eq(a,b) · (typecheck(unsignedlong,a)&&· typecheck(unsignedlong,b)&&· ((long)(a)-(long)(b)>=0))·#definetime_before_eq(a,b)time_after_eq(b,a)··/
此文档下载收益归作者所有