深度剖析Linux内核《如何唤醒线程》

linux内核如何唤醒线程

//本文代码片段出自linux内核版本:4.1.15

linux内核唤醒线程主要使用wake_up_process()。

一、wake_up_process()分析

在linux内核中,唤醒线程由wake_up_process()函数实现。其定义在(/kernel/sched/core.c):

int wake_up_process(struct task_struct *p)

{

WARN_ON(task_is_stopped_or_traced(p));

return try_to_wake_up(p, TASK_NORMAL, 0);

}

wake_up_process()将调用try_to_wake_up()函数:

static int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)

{

unsigned long flags;

int cpu, success = 0;

/*

* If we are going to wake up a thread waiting for CONDITION we

* need to ensure that CONDITION=1 done by the caller can not be

* reordered with p->state check below. This pairs with mb() in

* set_current_state() the waiting thread does.

*/

smp_mb__before_spinlock();

raw_spin_lock_irqsave(&p->pi_lock, flags);

if (!(p->state & state))

goto out;

success = 1; /* we're going to change ->state */

cpu = task_cpu(p);

/* 使用内存屏障保证p->on_rq的数值是最新的。如果线程已经在运行队列rq里面了,即进程已经处于

runnable/running状态。ttwu_remote目的是由于线程 p已经在运行队列rq里面了,并且没有完全

取消调度,再次唤醒的话,需要将线程的状态翻转:将状态设置为TASK_RUNNING,这样

线程就一直在运行队列里面了。这种情况则直接退出后续流程,并对调度状态/数据进行统计 */

if (p->on_rq && ttwu_remote(p, wake_flags))

goto stat;

#ifdef CONFIG_SMP

/* 等待在其他cpu上的线程调度完成 */

while (p->on_cpu)

cpu_relax();

/*

* Pairs with the smp_wmb() in finish_lock_switch().

*/

smp_rmb();

p->sched_contributes_to_load = !!task_contributes_to_load(p);

p->state = TASK_WAKING;


/* 根据进程的所属的调度类调用相应的回调函数 */

if (p->sched_class->task_waking)

p->sched_class->task_waking(p);

/* 根据线程p相关参数和系统状态,为线程p选择合适的cpu */

cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);

/* 如果选择的cpu与线程p当前所在的cpu不相同,则将线程的wake_flags设置为需要迁移,然后将线程p迁移到cpu上 */

if (task_cpu(p) != cpu) {

wake_flags |= WF_MIGRATED;

set_task_cpu(p, cpu);

}

#endif /* CONFIG_SMP */

/* 线程p入队操作并标记线程p为runnable状态,同时唤醒抢占 */

ttwu_queue(p, cpu);

stat:

/* 与调度相关的统计 */

ttwu_stat(p, cpu, wake_flags);

out:

raw_spin_unlock_irqrestore(&p->pi_lock, flags);

return success;

}

第14~15行代码,如果进程状态不是:TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE,则就不是normal task,这时候直接退出wakeup流程。所以在内核里面只要使用wake_up_process(),函数都会将进程设置为TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE这两种状态之一。

第23~46行代码是SMP环境下的相关处理机制:(详见代码上的注释)

在try_to_wake_up()函数的最后将调用ttwu_queue(),下文将分析这个函数。

二、ttwu_queue()函数分析

ttwu_queue()函数定义如下:

static void ttwu_queue(struct task_struct *p, int cpu)

{

struct rq *rq = cpu_rq(cpu);

#if defined(CONFIG_SMP)

if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {

sched_clock_cpu(cpu); /* sync clocks x-cpu */

ttwu_queue_remote(p, cpu);

return;

}

#endif

raw_spin_lock(&rq->lock);

ttwu_do_activate(rq, p, 0);

raw_spin_unlock(&rq->lock);

}

static void ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)

{

#ifdef CONFIG_SMP

if (p->sched_contributes_to_load)

rq->nr_uninterruptible--;

#endif

//将线程p加入运行队列rq中

ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);

//将任务标记为可运行的,并执行唤醒抢占。

ttwu_do_wakeup(rq, p, wake_flags);

}

static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)

{

activate_task(rq, p, en_flags);

p->on_rq = TASK_ON_RQ_QUEUED;

/* if a worker is waking up, notify workqueue */

if (p->flags & PF_WQ_WORKER)

wq_worker_waking_up(p, cpu_of(rq));

}

static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)

{

update_rq_clock(rq);

sched_info_queued(rq, p);

p->sched_class->enqueue_task(rq, p, flags);

}

void activate_task(struct rq *rq, struct task_struct *p, int flags)

{

if (task_contributes_to_load(p))

rq->nr_uninterruptible--;

enqueue_task(rq, p, flags);

}

//将任务标记为可运行的,并执行唤醒抢占操作

static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)

{

check_preempt_curr(rq, p, wake_flags);

trace_sched_wakeup(p, true);


//将线程p的状态设置为TASK_RUNNING

p->state = TASK_RUNNING;

#ifdef CONFIG_SMP

if (p->sched_class->task_woken)

p->sched_class->task_woken(rq, p);

if (rq->idle_stamp) {

u64 delta = rq_clock(rq) - rq->idle_stamp;

u64 max = 2*rq->max_idle_balance_cost;

update_avg(&rq->avg_idle, delta);

if (rq->avg_idle > max)

rq->avg_idle = max;

rq->idle_stamp = 0;

}

#endif

}

(注:以上代码为了便于阅读而放置)

从以上代码片段可知,ttwu_queue()函数的功能是:

(1)将线程p入队操作并标记线程p为runnable状态。(ttwu_activate()部分),本质是调用与线程相关联的调度类的enqueue_task回调函数(以CFS调度策略为例,参见附录的enqueue_task_fair()函数),实现将线程p加入到rq运行队列中。

p->sched_class->enqueue_task(rq, p, flags);

(2)将当前线程标记为TASK_RUNNING,并执行唤醒抢占操作。 (ttwu_do_wakeup()部分)

三、后续与附录

1、在wake_up_process()中常常使用linux内核的内存屏障机制。

2、通过WALT算法:计算出运行队列当前线程和新唤醒的线程p相关的task load和运行队列相关的runnable_load的数值。

后续将学习、了解一下WALT算法、内存屏障机制的实现机制和思路。

/*在增加nr_running之前调用enqueue_task()函数。在这里,将更新公平调度统计数据,然后将线程

p放入rbtree红黑树中。*/

static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)

{

struct cfs_rq *cfs_rq;

struct sched_entity *se = &p->se;

for_each_sched_entity(se) {

if (se->on_rq)

break;

cfs_rq = cfs_rq_of(se);

enqueue_entity(cfs_rq, se, flags);

/*

* end evaluation on encountering a throttled cfs_rq

*

* note: in the case of encountering a throttled cfs_rq we will

* post the final h_nr_running increment below.

*/

if (cfs_rq_throttled(cfs_rq))

break;

cfs_rq->h_nr_running++;

flags = ENQUEUE_WAKEUP;

}

for_each_sched_entity(se) {

cfs_rq = cfs_rq_of(se);

cfs_rq->h_nr_running++;

if (cfs_rq_throttled(cfs_rq))

break;

update_cfs_shares(cfs_rq);

update_entity_load_avg(se, 1);

}

if (!se) {

update_rq_runnable_avg(rq, rq->nr_running);

add_nr_running(rq, 1);

}

hrtick_update(rq);

}

原文链接:,转发请注明来源!