内核定时器,schedule_timeout, ,POSIX timer

来源:互联网 发布:安利云服务软件下载 编辑:程序博客网 时间:2024/05/22 10:34

一、内核timer

1、内核定时器 init_timeout,mod_timer,add_timer ----------- 通过软中断实现。将定时器按照超时时间分为五组,每个tick都会触发执行定时器软中断检查是否有超时的定时器。

2、hrtimer ----------- 高分辨率定时器,HRTIMER_CB_SOFTIRQ类型的hrtimer通过软中断实现。

3、schedule_timeout使用内核定时器的内部实现来达到timeout的目的(使用setup_timer_on_stack,__mod_timer)。

       schedule_timeout在内核中被大量使用。

       使用场景如select,poll,socket操作超时,sleep,usleep等等。


void run_local_timers(void) ------ tick的增加会触发此函数
{
    hrtimer_run_queues();
    raise_softirq(TIMER_SOFTIRQ);
    softlockup_tick();
}

static void run_timer_softirq(struct softirq_action *h)  ----- TIMER_SOFTIRQ被触发后执行此函数。
{
    struct tvec_base *base = __get_cpu_var(tvec_bases);

    hrtimer_run_pending();

    if (time_after_eq(jiffies, base->timer_jiffies))
        __run_timers(base);
}

二、系统级的定时器相关函数:(原理目前还看不透。。。)

1)alarm,即ITIMER_REAL类型的setitimer;通过hrtimer实现。

2)setitimer:

  • ITIMER_REAL: 以系统真实的时间来计算,它送出SIGALRM信号。
  • ITIMER_VIRTUAL: -以该进程在用户态下花费的时间来计算,它送出SIGVTALRM信号。
  • ITIMER_PROF: 以该进程在用户态下和内核态下所费的时间来计算,它送出SIGPROF信号。

3)POSIX timer: timer_create等,

/*
 * NOTE! "signal_struct" does not have its own
 * locking, because a shared signal_struct always
 * implies a shared sighand_struct, so locking
 * sighand_struct is always a proper superset of
 * the locking of signal_struct.
 */
struct signal_struct {
atomic_t sigcnt;
atomic_t live;
int nr_threads;


wait_queue_head_twait_chldexit;/* for wait4() */


/* current thread group signal load-balancing target: */
struct task_struct*curr_target;


/* shared signal handling: */
struct sigpendingshared_pending;


/* thread group exit support */
int group_exit_code;
/* overloaded:
* - notify group_exit_task when ->count is equal to notify_count
* - everyone except group_exit_task is stopped during signal delivery
*   of fatal signals, group_exit_task processes the signal.
*/
int notify_count;
struct task_struct*group_exit_task;


/* thread group stop support, overloads group_exit_code too */
int group_stop_count;
unsigned int flags; /* see SIGNAL_* flags below */


/*
* PR_SET_CHILD_SUBREAPER marks a process, like a service
* manager, to re-parent orphan (double-forking) child processes
* to this process instead of 'init'. The service manager is
* able to receive SIGCHLD signals and is able to investigate
* the process until it calls wait(). All children of this
* process will inherit a flag if they should look for a
* child_subreaper process at exit.
*/
unsigned int is_child_subreaper:1;
unsigned int has_child_subreaper:1;


/* POSIX.1b Interval Timers */
struct list_head posix_timers;


/* ITIMER_REAL timer for the process */
struct hrtimer real_timer;



补充阅读:

http://www.cnblogs.com/hoys/archive/2011/11/14/2248586.html


hrtimer 框架作为内核中的一个 API 可用,用户空间应用程序也可以通过 nanosleepitimers 和 Portable Operating System Interface (POSIX)-timers interface 使用它。hrtimer 框架被主线化(mainlined)到 2.6.21 内核中。

hrtimer API 与传统 API 有些相似,但它们之间的一些根本差别是它能够进行额外的时间控制。应该注意的第一点是:时间不是用 jiffies 表示的,而是以一种名为 ktime 的特殊数据类型表示。这种表示方法隐藏了在这个粒度上有效管理时间的一些细节。hrtimer API 正式确认(formalize)了绝对时间和相对时间之间的区别,要求调用者指定类型。


原创粉丝点击