上一篇文章介绍了rdtsc指令的精确计时,不过由于CPU的频率不能保证是固定的,所以这样的方式还是有限制。其实linux下还有一种高精度的定时器,那就是posix_timer。我记得以前看代码的时候CLOCK_REALTIME的定时器似乎用的就是rdtsc指令,不过现在不确定了,先放到一边。原理上来说,可以在变频的时候也使用rdtsc指令,因为CPU的频率我们也是知道的,变频的时候内核也是知道的。
下面是我的timer_create的例子,编译的时候要加上rt库,这是linux的realtime库:
gcc -o test test.c -lrt
#include
<
stdio.h
>
#include
<
unistd.h
>
#include
<
signal.h
>
#include
<
stdlib.h
>
#include
<
time.h
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#define
rdtsc(low,high) __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
timer_t tt;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
handler (
int
sig, siginfo_t
*
extra,
void
*
cruft)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
static last_i=0;
unsigned int i, j;
rdtsc(i,j);
printf ("time:%u, %u, [%u] %uHZ ", j, i, i-last_i, (i-last_i)*10/1000000);
last_i = i;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
main ()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int i=0;
sigset_t sigset;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
sigfillset (&sigset);
sigdelset (&sigset, SIGRTMIN);
sigprocmask (SIG_SETMASK, &sigset, NULL);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
struct sigaction sa;
sigfillset (&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (sigaction (SIGRTMIN, &sa, NULL) < 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
perror ("sigaction failed ");
exit (-1);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
struct sigevent timer_event;
struct itimerspec timer;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_nsec = 100 * 1000 * 1000;
timer.it_value = timer.it_interval;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
timer_event.sigev_notify = SIGEV_SIGNAL;
timer_event.sigev_signo = SIGRTMIN;
timer_event.sigev_value.sival_ptr = (void *) &tt;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (timer_create (CLOCK_REALTIME, &timer_event, &tt) < 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
perror ("timer_create failed");
exit (-1);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (timer_settime (tt, 0, &timer, NULL) < 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
perror ("timer_settime failed");
exit (-1);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
while (i++ < 10)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
pause ();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return 0;
}
输出结果:
time:166081, 1934350847, [1934350847] 2163HZ
time:166081, 2120528291, [186177444] 1861HZ
time:166081, 2306679576, [186151285] 1861HZ
time:166081, 2494695630, [188016054] 1880HZ
time:166081, 2680865389, [186169759] 1861HZ
time:166081, 2867018473, [186153084] 1861HZ
time:166081, 3053152230, [186133757] 1861HZ
time:166081, 3239309935, [186157705] 1861HZ
time:166081, 3425467261, [186157326] 1861HZ
time:166081, 3611639266, [186172005] 1861HZ