Chinaunix首页 | 论坛 | 博客
  • 博客访问: 57154
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-10 20:54
个人简介

多学习,多分享!

文章分类
文章存档

2018年(7)

我的朋友

分类: LINUX

2018-09-02 23:25:51

本文为作者原创,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。
作者:misteryoung
博客:http://blog.chinaunix.net/uid/20706239
=======================================================================

1 前言


    在很多用户态程序使用select实现定时器的功能。因为该函数既可以实现fd的多路复用,又可以实现超时的功能,可谓功能强大。那么Linux有没有给用户态提供标准的定时器接口呢?答案当然是:有。
    下面介绍几个标准的用户态定时器。讲解内容包括:
    1)原理及用法;
    2)实现细节(包括用户态及内核态),因为涉及系统调用,因此内核部分比较多;
    3)本文只介绍3个定时器:alarm、setitimer、timer_create

2 alarm定时器


    alarm函数大家应该并不陌生,在busybox版的ping源码中就使用了该函数,用以实现超时功能。
    其函数原型及man中的帮助信息如下所示:

点击(此处)折叠或打开

  1.     unsigned int alarm(unsigned int seconds);

  2. DESCRIPTION
  3.     alarm() arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds.
  4.     If seconds is zero, no new alarm() is scheduled.
  5.     In any event any previously set alarm() is canceled.
  6. RETURN VALUE
        alarm() returns the number of seconds remaining until any previously scheduled alarm was due to be delivered, or zero if there was no previously scheduled alarm.


2.1 原理


    简单来说,其功能实现包含两部分:
    1)注册SIGALRM信号处理函数;
    2)通过alarm设置超时时间(单位为秒),超时后信号处理函数被执行;
    alarm(seconds)代表设置新的定时器,并覆盖老的定时器(如果有的话)。若取消已有定时器可执行alarm(0)。

2.2 用法


    1)signal(SIGALRM, alarm_func)
    2)alarm(expire)
    expire秒后alarm_func函数被执行。

2.3 alarm跟踪

 
   我们都知道,alarm通过系统调用陷入到内核态,内核对应的函数为sys_alarm。

点击(此处)折叠或打开

  1. SYSCALL_DEFINE1(alarm, unsigned int, seconds)
  2. {
  3.     return alarm_setitimer(seconds);
  4. }
  5. 上面代码展开后如下所
  6. sys_alarm(unsigned int seconds)
    {
        return alarm_setitimer(seconds);
    }
    继续跟踪alarm_setitimer

点击(此处)折叠或打开

  1. unsigned int alarm_setitimer(unsigned int seconds)
  2. {
  3.     struct itimerval it_new, it_old;

  4. #if BITS_PER_LONG < 64
  5.     if (seconds > INT_MAX)
  6.         seconds = INT_MAX;
  7. #endif
  8.     it_new.it_value.tv_sec = seconds;    时间初始化
  9.     it_new.it_value.tv_usec = 0;
  10.     it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;

  11.     do_setitimer(ITIMER_REAL, &it_new, &it_old); 设置新的超时时间,并获取老的剩余的超时时间

  12.     /*
  13.      * We can't return 0 if we have an alarm pending ... And we'd
  14.      * better return too much than too little anyway
  15.      */
  16.     if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
  17.      it_old.it_value.tv_usec >= 500000)
  18.         it_old.it_value.tv_sec++;

  19.     return it_old.it_value.tv_sec;    返回老的剩余的超时时间
  20. }
    继续跟踪do_setitimer

点击(此处)折叠或打开

  1. int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  2. {
  3.     struct task_struct *tsk = current;
  4.     struct hrtimer *timer;
  5.     ktime_t expires;

  6.     /*
  7.      * Validate the timevals in value.
  8.      */
  9.     if (!timeval_valid(&value->it_value) ||
  10.      !timeval_valid(&value->it_interval))
  11.         return -EINVAL;

  12.     switch (which) {
  13.     case ITIMER_REAL:
  14. again:
  15.         spin_lock_irq(&tsk->sighand->siglock);
  16.         timer = &tsk->signal->real_timer; 获取当前进程对应的timer
  17.         if (ovalue) {
  18.             ovalue->it_value = itimer_get_remtime(timer);
  19.             ovalue->it_interval
  20.                 = ktime_to_timeval(tsk->signal->it_real_incr);
  21.         }
  22.         /* We are sharing ->siglock with it_real_fn() */
  23.         if (hrtimer_try_to_cancel(timer) < 0) {    取消老的timer
  24.             spin_unlock_irq(&tsk->sighand->siglock);
  25.             goto again;
  26.         }
  27.         expires = timeval_to_ktime(value->it_value);
  28.         if (expires.tv64 != 0) {
  29.             tsk->signal->it_real_incr =
  30.                 timeval_to_ktime(value->it_interval);
  31.             hrtimer_start(timer, expires, HRTIMER_MODE_REL);    启动新的timer
  32.         } else
  33.             tsk->signal->it_real_incr.tv64 = 0;

  34.         trace_itimer_state(ITIMER_REAL, value, 0);
  35.         spin_unlock_irq(&tsk->sighand->siglock);
  36.         break;
  37.     case ITIMER_VIRTUAL:
  38.         set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
  39.         break;
  40.     case ITIMER_PROF:
  41.         set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
  42.         break;
  43.     default:
  44.         return -EINVAL;
  45.     }
  46.     return 0;
  47. }
    函数hrtimer_start会调用enqueue_hrtimer将timer定时器加入相应的定时器队列,并等待超时。
    下面再跟踪timer到底是什么?
    从上面的代码“tsk->signal->real_timer”可知,timer是进程相关的。那我们再从进程的创建开始跟踪该timer。

sys_fork

-> do_fork

-> copy_process

-> copy_signal

点击(此处)折叠或打开

  1. static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
  2. {
  3.     struct signal_struct *sig;

  4.     if (clone_flags & CLONE_THREAD)
  5.         return 0;

  6.     sig = kmem_cache_alloc(signal_cachep, GFP_KERNEL);
  7.     tsk->signal = sig;
  8.     if (!sig)
  9.         return -ENOMEM;

  10.     。。。。。。
  11.     init_sigpending(&sig->shared_pending);
  12.     INIT_LIST_HEAD(&sig->posix_timers);

  13.     hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  14.     sig->it_real_incr.tv64 = 0;
  15.     sig->real_timer.function = it_real_fn;    timer响应函数为it_real_fn
  16.     。。。。。。。
  17.     return 0;
  18. }
    最终,timer响应函数被设置为it_real_fn。
    it_real_fn的执行依赖于内核的hrtimer框架,并且在超时后被执行。下面跟踪一下该函数。

点击(此处)折叠或打开

  1. enum hrtimer_restart it_real_fn(struct hrtimer *timer)
  2. {
  3.     struct signal_struct *sig =
  4.         container_of(timer, struct signal_struct, real_timer);

  5.     trace_itimer_expire(ITIMER_REAL, sig->leader_pid, 0);
  6.     kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);发送SIGALRM信号

  7.     return HRTIMER_NORESTART;
  8. }
    很显然,it_real_fn的作用就是给当前进程发送SIGALRM信号,这样的alarm就与SIGALRM信号关联起来了。

2.4 总结


    alarm定时器的原理是:内核hrtimer在定时器超时后,通过函数it_real_fn给当前进程发送SIGALRM信号,这样通过signal等函数注册的信号响应函数就会被调用。

3 setitimer定时器


    setitimer函数原型及man中的帮助信息如下所示:

点击(此处)折叠或打开

  1. int setitimer(int which, const struct itimerval *new_value,
                  struct itimerval *old_value);


  2. DESCRIPTION
  3.     The system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and
  4.  the timer (potentially) restarts.
  5.     ITIMER_REAL decrements in real time, and delivers SIGALRM upon expiration.
  6.     ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.
  7.     ITIMER_PROF decrements both when the process executes and when the system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL, this timer is usually
  8.  used to profile the time spent by the application in user and kernel space. SIGPROF is delivered upon expiration.

3.1 原理


    与alarm类似,setitimer也是注册信号处理函数,并且超时后处理函数被执行。同样,也可以通过将超时时间设置为全0来取消老的定时器。
    但与alarm不同的是:
        1)setitimer可以指定具体的信号。通常我们第一个参数设置为ITIMER_REAL,此时需要注册SIGALRM信号处理函数。
        2) 时间精度可以精确到微秒;
        3)老的定时器剩余时间通过参数返回,而返回值代表函数是否执行成功;

3.2 用法


    1)signal(SIGALRM, alarm_func)
    2)setitimer(ITIMER_REAL, &timerval, NULL)
    timerval指定的时间过后,alarm_func函数被执行。

3.3 setitimer跟踪


    setitimer是系统调用,直接跟进内核:

点击(此处)折叠或打开

  1. SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value,
  2.         struct itimerval __user *, ovalue)
  3. {
  4.     struct itimerval set_buffer, get_buffer;
  5.     int error;

  6.     if (value) {
  7.         if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
  8.             return -EFAULT;
  9.     } else
  10.         memset((char *) &set_buffer, 0, sizeof(set_buffer));

  11.     error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
  12.     if (error || !ovalue)
  13.         return error;

  14.     if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
  15.         return -EFAULT;
  16.     return 0;
  17. }
    setitimer陷入内核态与alarm一样,均调用do_setitimer。后面不再分析。

3.4 总结


    setitimer与alarm功能、用法相同,只是参数不太一样。
    下面再从函数调用的角度分析二者的区别:

点击(此处)折叠或打开

  1. alarm                     用户态
  2.     -> sys_alarm          内核态
  3.         -> alarm_setitimer
  4.             -> do_setitimer

  5. setitimer                 用户态
  6.     -> sys_setitimer      内核态
  7.         -> do_setitimer

4 timer_create定时器


    与alarm和setitimer的单一函数就能实现定时器功能不同,timer_create需要结合函数timer_settime使用。下面看一下这两个函数的函数原型及man中的帮助信息。如下所示:

点击(此处)折叠或打开

  1. int timer_create(clockid_t clockid, struct sigevent *sevp,
  2.                         timer_t *timerid);

  3. DESCRIPTION
  4.     timer_create() creates a new per-process interval timer. The ID of the new timer is returned in the buffer pointed to by timerid, which must be a non-NULL pointer.
  5.     This ID is unique within the process, until the timer is deleted. The new timer is initially disarmed.
  6.     The clockid argument specifies the clock that the new timer uses to measure time. It can be specified as one of the following values:
  7.     CLOCK_REALTIME
  8.         A settable system-wide real-time clock.
  9.     CLOCK_MONOTONIC
  10.         A nonsettable monotonically increasing clock that measures time from some unspecified point in the past that does not change after system startup.
  11.     CLOCK_PROCESS_CPUTIME_ID (since Linux 2.6.12)
  12.         A clock that measures (user and system) CPU time consumed by (all of the threads in) the calling process.
  13.     CLOCK_THREAD_CPUTIME_ID (since Linux 2.6.12)
  14.         A clock that measures (user and system) CPU time consumed by the calling thread.
    timer_settime函数:

点击(此处)折叠或打开

  1. int timer_settime(timer_t timerid, int flags,
                    const struct itimerspec *new_value,
                    struct itimerspec * old_value);

    DESCRIPTION
        timer_settime()  arms  or  disarms the timer identified by timerid.  The new_value argument is an itimerspec structure that specifies the new initial value and the new
     interval for the timer.  The itimerspec structure is defined as follows:

4.1 原理


    与alarm和setitimer不同的是,timer_create需要分两步操作:创建定时器(timer_create)和设置超时时间(timer_settime)。
    timer_create主要功能:注册超时处理函数及返回timerid供timer_settime使用;
    timer_settime主要功能:根据timerid找到定时器,并设置超时时间;
     

4.2 用法

    
    1)注册信号处理函数
        signal(SIGRTMIN, sig_handler)
        sv.sigev_notify = SIGEV_SIGNAL
        sv.sigev_signo = SIGRTMIN
        sv.sigev_value.sival_ptr = &timerid
        timer_create(CLOCK_REALTIME, &sv, &timerid)
    2)设置超时时间
        timer_settime(&timerfd, 0, &time, NULL)
        注:考虑到本文重点不在用法的讲解,因此只是举个简单例子来说明timer_create是如何使用的。事实上,timer_create功能比较强大,用法也比较复杂。

4.3 timer_create跟踪


    timer_create是系统调用,直接跟进内核:

点击(此处)折叠或打开

  1. SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
  2.         struct sigevent __user *, timer_event_spec,
  3.         timer_t __user *, created_timer_id)
  4. {
  5.     struct k_itimer *new_timer;

  6.     new_timer = alloc_posix_timer();

  7.     new_timer->it_clock = which_clock;
  8.     common_timer_create(new_timer)

  9.     if (timer_event_spec) {
  10.         if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  11.             error = -EFAULT;
  12.             goto out;
  13.         }
  14.     } else {
  15.         event.sigev_notify = SIGEV_SIGNAL默认为超时后触发信号
  16.         event.sigev_signo = SIGALRM;        默认为触发SIGALRM信号
  17.         event.sigev_value.sival_int = new_timer->it_id;
  18.         new_timer->it_pid = get_pid(task_tgid(current));
  19.     }

  20.     new_timer->it_sigev_notify = event.sigev_notify;
  21.     new_timer->sigq->info.si_signo = event.sigev_signo;
  22.     new_timer->sigq->info.si_value = event.sigev_value;
  23.     new_timer->sigq->info.si_tid = new_timer->it_id;
  24.     new_timer->sigq->info.si_code = SI_TIMER;

  25.     new_timer->it_signal = current->signal;
  26.     list_add(&new_timer->list, &current->signal->posix_timers);

  27.     return 0;
  28. }
    再跟踪timer_settime:

点击(此处)折叠或打开

  1. SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  2.         const struct itimerspec __user *, new_setting,
  3.         struct itimerspec __user *, old_setting)
  4. {
  5.     。。。。。。
  6.     error = CLOCK_DISPATCH(timr->it_clock, timer_set,
  7.              (timr, flags, &new_spec, rtn));
  8.     。。。。。。
  9.     return error;
  10. }
    再跟踪CLOCK_DISPATCH:

点击(此处)折叠或打开

  1. /*
  2.  * Call the k_clock hook function if non-null, or the default function.
  3.  */
  4. #define CLOCK_DISPATCH(clock, call, arglist) \
  5.      ((clock) < 0 ? posix_cpu_##call arglist : \
  6.       (posix_clocks[clock].call != NULL \
  7.       ? (*posix_clocks[clock].call) arglist : common_##call arglist))  
    CLOCK_DISPATCH被展开成common_timer_set,再跟踪该函数

点击(此处)折叠或打开

  1. static int
  2. common_timer_set(struct k_itimer *timr, int flags,
  3.          struct itimerspec *new_setting, struct itimerspec *old_setting)
  4. {
  5.     struct hrtimer *timer = &timr->it.real.timer;
  6.     enum hrtimer_mode mode;

  7.     if (hrtimer_try_to_cancel(timer) < 0)    取消老的定时器
  8.         return TIMER_RETRY;

  9.     mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
  10.     hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
  11.     timr->it.real.timer.function = posix_timer_fn;    设置定时器响应函数

  12.     hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));

  13.     /* Convert interval */    设置超时时间
  14.     timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);

  15.     hrtimer_start_expires(timer, mode);    启动新的定时器
  16.     return 0;
  17. }
    定时器超时后,posix_timer_fn被执行,再跟踪该函数:

点击(此处)折叠或打开

  1. static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  2. {
  3.     struct k_itimer *timr;

  4.     timr = container_of(timer, struct k_itimer, it.real.timer);
  5.     if (posix_timer_event(timr, si_private)) {
  6.         if (timr->it.real.interval.tv64 != 0) {
  7.             ktime_t now = hrtimer_cb_get_time(timer);

  8.             timr->it_overrun += (unsigned int)
  9.                 hrtimer_forward(timer, now,
  10.                         timr->it.real.interval);
  11.             ret = HRTIMER_RESTART;
  12.             ++timr->it_requeue_pending;
  13.         }
  14.     }
  15.     return ret;
  16. }
    再跟踪posix_timer_event:

点击(此处)折叠或打开

  1. int posix_timer_event(struct k_itimer *timr, int si_private)
  2. {
  3.     struct task_struct *task;

  4.     task = pid_task(timr->it_pid, PIDTYPE_PID);
  5.     if (task) {
  6.         shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
  7.         ret = send_sigqueue(timr->sigq, task, shared);    处理信号
  8.     }
  9.     return ret > 0;
  10. }
    再跟踪send_sigqueue:

点击(此处)折叠或打开

  1. int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
  2. {
  3.     int sig = q->info.si_signo;

  4.     ret = 1; /* the signal is ignored */
  5.     if (!prepare_signal(sig, t, 0))    如果进程t忽略sig信号,则直接返回
  6.         goto out;

  7.     ret = 0;

  8.     signalfd_notify(t, sig);
  9.     pending = group ? &t->signal->shared_pending : &t->pending;
  10.     list_add_tail(&q->list, &pending->list);
  11.     sigaddset(&pending->signal, sig);
  12.     complete_signal(sig, t, group);    给进程t发送信号sig,这样信号响应函数就会被执行
  13.     return ret;
  14. }

4.4 总结


    本文只分析了timer_create常用用法:注册信号处理函数,并设置定时器。其原理也与alarm与setitmier类似:利用hrtimer定时器,当超时后信号处理函数会被调用。
阅读(3165) | 评论(0) | 转发(0) |
0

上一篇:ARM版glibc库介绍

下一篇:ARM版sleep函数分析

给主人留下些什么吧!~~