Chinaunix首页 | 论坛 | 博客
  • 博客访问: 95567
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 158
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-29 17:33
文章存档

2016年(4)

2015年(11)

我的朋友

分类: 嵌入式

2015-12-29 10:03:11

工作太忙,只是看到的拿过来分享下内核中两大重要的线程,migration_thread负责cpu的负载均衡(将进程从本地队列移动到目标cpu的队列),kthreadd负责为kthread_create_list链表中的成员创建内核线程。

内核版本2.6.24中的引导部分,start_kernel()->rest_init():
static void noinline __init_refok rest_init(void)
        __releases(kernel_lock)
{
        int pid;

        kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND);
        numa_default_policy();
        pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
        kthreadd_task = find_task_by_pid(pid);
……
}
以上依次创建了kernel_init线程和kthreadd线程,rest_init()是在禁用抢占(preempt_disable)的情况下运行,因此保证了kernel_init()运行时kthreadd_task 已经指向ktheadd线程。

kernel_init()调用do_pre_smp_initcalls()->migration_init();创建了负责进程在cpu间移动(cpu负载均衡)的内核线程migration_thread,创建线程是通过将包含待运行函数及参数的kthread_create_info结构挂入kthread_create_list链表,然后唤醒kthreadd_task(即ktheadd线程),而ktheadd线程负责为链表上的每个结构创建相应的线程。
void __init migration_init(void)
{
        void *cpu = (void *)(long)smp_processor_id();
        int err;

        /* Start one for the boot CPU: */
        err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
……
}

static int __cpuinit
migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
{
        struct task_struct *p;
        int cpu = (long)hcpu;
        unsigned long flags;
        struct rq *rq;

        switch (action) {
        case CPU_LOCK_ACQUIRE:
                mutex_lock(&sched_hotcpu_mutex);
                break;

        case CPU_UP_PREPARE:
        case CPU_UP_PREPARE_FROZEN:
                p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
……
}

struct task_struct *kthread_create(int (*threadfn)(void *data),
                                   void *data,
                                   const char namefmt[],
                                   ...)
{
        struct kthread_create_info create;

        create.threadfn = threadfn;
        create.data = data;
        init_completion(&create.started);
        init_completion(&create.done);

        spin_lock(&kthread_create_lock);
        list_add_tail(&create.list, &kthread_create_list);
        wake_up_process(kthreadd_task);
……
}

阅读(1882) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~