内核时钟的频率是由CONFIG_HZ决定的,以前默认是100HZ,现在内核默认是250HZ。而1个jiffy是1个时钟滴答,时间间隔是有CONFIG_HZ决定的,频率是250HZ,也就是周期为4ms。每4ms,增加一个时钟滴答,也即jiffies++。 原理比较简单,如何查看自己的Linux的CONFIG_HZ的值呢? root@manu:~/code/c/self/ticks# grep ^CONFIG_HZ /boot/config-3.2.0-29-generic-pae CONFIG_HZ_250=y CONFIG_HZ=250 有意思的是,systemtap tutorial有个比较好玩的实验,也可以确定CONFIG_HZ的大小。 global count_jiffies, count_ms probe timer.jiffies(100) { count_jiffies ++ } probe timer.ms(100) { count_ms ++ } probe timer.ms(543210) { hz=(1000*count_jiffies) / count_ms printf ("jiffies:ms ratio %d:%d => CONFIG_HZ=%d\n", count_jiffies, count_ms, hz) exit () } 输出如下: jiffies:ms ratio 1358:5420 => CONFIG_HZ=250 实验等待的时间有点久,需要等待543210ms,9分钟左右,时间越久,误差越小,如果等待的时间段一些,会出现误差。感兴趣的筒子自行实验。 除此外,还有一个值是USER_HZ,不了解这个的,可能会了解times系统调用,这个系统调用是统计进程时间消耗的, #include clock_t times(struct tms *buf); times系统调用的时间单位是由USER_HZ决定的,所以,times系统调用统计的时间是以10ms为单位的。说100HZ空口无凭,如何获取USER_HZ。 #include #include #include #include #include int main() { int user_ticks_per_second ; user_ticks_per_second = (int)sysconf(_SC_CLK_TCK); if(user_ticks_per_second == -1) { fprintf(stderr,"failed to get ticks per second by sysconf\n"); return -1; } printf("The Number of USER ticks per second is %d\n",user_ticks_per_second); return 0; } 输出如下: The Number of USER ticks per second is 100 如果你嫌用C代码调用SYSCONF太麻烦了,可以通过shell命令getconf获得USER_HZ的值: root@manu:~/code/c/self/ticks# getconf CLK_TCK 100 times系统调用来统计进程信息我不建议使用了,精度太低了。提出这个USER_HZ,只是希望不要困惑,为什么CONFIG_HZ是250,而sysconf(_SC_CLK_TCK)却是100. |
关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )
GMT+8, 2025-8-1 02:27 , Processed in 0.109375 second(s), 28 queries , Gzip On.
地址:深圳市南山区科技生态园2栋A座805 电话:19926409050