linux API---timerfd的使用方法

转载自 http://www.tuicool.com/articles/7bArem

            http://blog.csdn.net/yusiguyuan/article/details/22936707

timerfd是Linux为用户程序提供的一个定时器接口。这个接口基于文件描述符,通过文件描述符的可读事件进行超时通知,所以能够被用于select/poll的应用场景。 timerfd是linux内核2.6.25版本中加入的借口。

timerfd、eventfd、signalfd配合epoll使用,可以构造出一个零轮询的程序,但程序没有处理的事件时,程序是被阻塞的。这样的话在某些移动设备上程序更省电。

clock_gettime函数可以获取系统时钟,精确到纳秒。需要在编译时指定库:-lrt。可以获取两种类型事件:

CLOCK_REALTIME:相对时间,从1970.1.1到目前的时间。更改系统时间会更改获取的值。也就是,它以系统时间为坐标。

CLOCK_MONOTONIC:与CLOCK_REALTIME相反,它是以绝对时间为准,获取的时间为系统重启到现在的时间,更改系统时间对齐没有影响。

timerfd_create:

生成一个定时器对象,返回与之关联的文件描述符。接收两个入参,一个是clockid,填写CLOCK_REALTIME或者CLOCK_MONOTONIC,参数意义同上。第二个可以传递控制标志:TFD_NONBLOCK(非阻塞),TFD_CLOEXEC(同O_CLOEXEC)

注:timerfd的进度要比usleep要高。

timerfd_settime:能够启动和停止定时器;可以设置第二个参数:flags,0表示是相对定时器,TFD_TIMER_ABSTIME表示是绝对定时器。

第三个参数设置超时时间,如果为0则表示停止定时器。定时器设置超时方法:

1、设置超时时间是需要调用 clock_gettime 获取当前时间,如果是绝对定时器,那么需要获取 CLOCK_REALTIME,在加上要超时的时间。如果是相对定时器,要获取CLOCK_MONOTONIC时间。

2、数据结构:  

   struct timespec {

               time_t tv_sec;                /* Seconds */ 
               long   tv_nsec;               /* Nanoseconds */ 
           }; 
           struct itimerspec { 
               struct timespec it_interval;  /* Interval for periodic timer */ 
               struct timespec it_value;     /* Initial expiration */ 
           };

      it_value是首次超时时间,需要填写从 clock_gettime获取的时间,并加上要超时的时间。  it_interval是后续周期性超时时间,是多少时间就填写多少。

     注意一个容易犯错的地方:tv_nsec加上去后一定要判断是否超出1000000000(如果超过要秒加一),否则会设置失败。

     

     it_interval不为0则表示是周期性定时器。

     it_value和 it_interval都为0表示停止定时器。

注: timerfd_create第一个参数和 clock_gettime的第一个参数都是 CLOCK_REALTIME或者 CLOCK_MONOTONIC, timerfd_settime的第二个参数为0(相对定时器)或者TFD_TIMER_ABSTIME,三者的关系:

1、如果 timerfd_settime设置为 TFD_TIMER_ABSTIME(决定时间),则后面的时间必须用 clock_gettime来获取,获取时设置 CLOCK_REALTIME还是 CLOCK_MONOTONIC取决于 timerfd_create设置的值。

2、如果 timerfd_settime设置为 0(相对定时器),则后面的时间必须用相对时间,就是:

    new_value. it_value .tv_nsec = 500000000;

    new_value. it_value .tv_sec = 3;

    new_value. it_interval .tv_sec = 0;

    new_value. it_interval .tv_nsec = 10000000;

read函数可以读timerfd,读的内容为uint_64,表示超时次数。

看一段代码例子: 
#include <sys/timerfd.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>		  /* Definition of uint64_t */

#define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

void printTime()
{  
  struct timeval tv;  
  gettimeofday(&tv, NULL);  
  printf("printTime:  current time:%ld.%ld ", tv.tv_sec, tv.tv_usec);
}

int main(int argc, char *argv[])
{
  struct timespec now;
  if (clock_gettime(CLOCK_REALTIME, &now) == -1)
    handle_error("clock_gettime");

  struct itimerspec new_value;
  new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
  new_value.it_value.tv_nsec = now.tv_nsec;
  new_value.it_interval.tv_sec = atoi(argv[2]);
  new_value.it_interval.tv_nsec = 0;

  int fd = timerfd_create(CLOCK_REALTIME, 0);
  if (fd == -1)
    handle_error("timerfd_create");

  if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)
    handle_error("timerfd_settime");

  printTime();
  printf("timer started\n");
  
  for (uint64_t tot_exp = 0; tot_exp < atoi(argv[3]);) 
  {
     uint64_t exp;
    ssize_t s = read(fd, &exp, sizeof(uint64_t));
    if (s != sizeof(uint64_t))
      handle_error("read");

    tot_exp += exp;
    printTime();
    printf("read: %llu; total=%llu\n",exp, tot_exp);
  }

  exit(EXIT_SUCCESS);
}
root@node1:/home/c_test/unix_test# ./timerfd 20 3 4
printTime:  current time:1396594376.746760 timer started
printTime:  current time:1396594396.747705 read: 1; total=1
printTime:  current time:1396594399.747667 read: 1; total=2
printTime:  current time:1396594402.747728 read: 1; total=3
printTime:  current time:1396594405.746874 read: 1; total=4
第一个参数为第一次定时器到期间隔,第二个参数为定时器的间隔,第三个参数为定时器多少次则退出。 

timerfd简单的性能测试:

申请1000个定时器,超时间定位1s,每秒超时一次,发现cpu占用率在3.0G的cpu上大概为1%,10000个定时器的话再7%左右,而且不会出现同时超时两个的情况,如果有printf到前台,则一般会出现定时器超时多次(3-5)才回调。 
PS:linux内核新添加的API timerfd、signalfd、eventfd都有异曲同工之妙,都可以将本来复杂的处理转化思维变得简单。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值