理解进程调度时机跟踪分析进程调度与进程切换的过程

来源:互联网 发布:手机版施工软件 编辑:程序博客网 时间:2024/05/21 11:27

张备《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

1.进程切换函数

schedule函数为调度函数,用以选择切换进程,其主要调用的函数有 pick_next_task,context_switch,在context_switch中调用switch_to (1)schedule

static void __sched __schedule(void){    struct task_struct *prev, *next;    unsigned long *switch_count;    struct rq *rq;    int cpu;need_resched:    preempt_disable();    cpu = smp_processor_id();    rq = cpu_rq(cpu);    rcu_note_context_switch(cpu);    prev = rq->curr;    schedule_debug(prev);    if (sched_feat(HRTICK))        hrtick_clear(rq);    smp_mb__before_spinlock();    raw_spin_lock_irq(&rq->lock);    switch_count = &prev->nivcsw;    if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {        if (unlikely(signal_pending_state(prev->state, prev))) {            prev->state = TASK_RUNNING;        } else {            deactivate_task(rq, prev, DEQUEUE_SLEEP);            prev->on_rq = 0;            if (prev->flags & PF_WQ_WORKER) {                struct task_struct *to_wakeup;                to_wakeup = wq_worker_sleeping(prev, cpu);                if (to_wakeup)                    try_to_wake_up_local(to_wakeup);            }        }        switch_count = &prev->nvcsw;    }    if (task_on_rq_queued(prev) || rq->skip_clock_update < 0)        update_rq_clock(rq);    next = pick_next_task(rq, prev);    clear_tsk_need_resched(prev);    clear_preempt_need_resched();    rq->skip_clock_update = 0;    if (likely(prev != next)) {        rq->nr_switches++;        rq->curr = next;        ++*switch_count;        context_switch(rq, prev, next); /* unlocks the rq */        cpu = smp_processor_id();        rq = cpu_rq(cpu);    } else        raw_spin_unlock_irq(&rq->lock);    post_schedule(rq);    sched_preempt_enable_no_resched();    if (need_resched())        goto need_resched;}

(2) pick_next_task //进程调度算法都封装这个函数内部

static inline struct task_struct *pick_next_task(struct rq *rq, struct task_struct *prev){    const struct sched_class *class = &fair_sched_class;    struct task_struct *p;    if (likely(prev->sched_class == class &&           rq->nr_running == rq->cfs.h_nr_running)) {        p = fair_sched_class.pick_next_task(rq, prev);        if (unlikely(p == RETRY_TASK))            goto again;        /* assumes fair_sched_class->next == idle_sched_class */        if (unlikely(!p))            p = idle_sched_class.pick_next_task(rq, prev);        return p;    }again:    for_each_class(class) {        p = class->pick_next_task(rq, prev);        if (p) {            if (unlikely(p == RETRY_TASK))                goto again;            return p;        }    }    BUG();}

(3)context_switch//进程上下文切换

context_switch(struct rq *rq, struct task_struct *prev,           struct task_struct *next){    struct mm_struct *mm, *oldmm;    prepare_task_switch(rq, prev, next);    mm = next->mm;    oldmm = prev->active_mm;    arch_start_context_switch(prev);    if (!mm) {        next->active_mm = oldmm;        atomic_inc(&oldmm->mm_count);        enter_lazy_tlb(oldmm, next);    } else        switch_mm(oldmm, mm, next);    if (!prev->mm) {        prev->active_mm = NULL;        rq->prev_mm = oldmm;    }       spin_release(&rq->lock.dep_map, 1, _THIS_IP_);    context_tracking_task_switch(prev, next);    switch_to(prev, next, prev);    barrier();    finish_task_switch(this_rq(), prev);}

(4)switch_to//利用了prev和next两个参数:prev指向当前进程,next指向被调度的进程,switch_to内部是汇编代码,用以切换进程

#define switch_to(prev, next, last)                    do {                                  unsigned long ebx, ecx, edx, esi, edi;               asm volatile("pushfl\n\t"      /* 保存当前进程的flags */              "pushl %%ebp\n\t"        /* 把当前进程的当前的ebp压入当前进程的栈   */            "movl %%esp,%[prev_sp]\n\t"  /*保存当前的esp到prev->thread.sp指向的内存中   */            "movl %[next_sp],%%esp\n\t"  /* 重置esp,把下个进程的next->thread.sp赋予esp */            "movl $1f,%[prev_ip]\n\t"    /*把1:的代码在内存中存储的地址保存到prev->thread.ip中*/            "pushl %[next_ip]\n\t"   /*重置eip   */               __switch_canary                              "jmp __switch_to\n"            "1:\t"                                  "popl %%ebp\n\t"     /* 重置ebp  */               "popfl\n"         /* 重置flags*/             : [prev_sp] "=m" (prev->thread.sp),                  [prev_ip] "=m" (prev->thread.ip),                     "=a" (last),                              "=b" (ebx), "=c" (ecx), "=d" (edx),                   "=S" (esi), "=D" (edi)                          __switch_canary_oparam                           : [next_sp]  "m" (next->thread.sp),                     [next_ip]  "m" (next->thread.ip),                    [prev]     "a" (prev),                           [next]     "d" (next)                            __switch_canary_iparam                          "memory");                  } while (0)

进程调度时机

1、中断处理过程(包括时钟中断、I/O中断、系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule();

2、内核线程可以直接调用schedule()进行进程切换,也可以在中断处理过程中进行调度,也就是说内核线程作为一类的特殊的进程可以主动调度,也可以被动调度;

3、用户态进程无法实现主动调度,仅能通过陷入内核态后的某个时机点进行调度,即在中断处理过程中进行调度。 进程切换:为了控制进程的执行,内核必须有能力挂起正在CPU上运行的进程,并恢复以前挂起的某个进程的执行。这种行为被称为进程切换(process switch)、任务切换(task switch)或上下文切换(context switch)。 挂起正在CPU上执行的进程,与中断时保存现场是不同的,中断前后是在同一个进程上下文中,只是由用户态转向内核态执行。

进程上下文包含了进程执行需要的所有信息,包括: 1、用户地址空间:包括程序代码,数据,用户堆栈等

2、控制信息:进程描述符,内核堆栈等

3、硬件上下文(与中断保存硬件上下文的方法不同)

gdb跟踪schedule

schedule函数

context_switch函数

Linux系统的一般执行过程

最一般情况:正在运行的用户态进程X切换到运行用户态进程Y的过程

1.正在运行的用户态进程X

2.发生中断——save cs:eip/esp/eflags(current) to kernel stack,then load cs:eip(entry of a specific ISR) and ss:esp(point to kernel stack).

  1. SAVE_ALL //保存现场

  2. 中断处理过程中或中断返回前调用了schedule(),其中的switch_to做了关键的进程上下文切换

  3. 标号1之后开始运行用户态进程Y(这里Y曾经通过以上步骤被切换出去过因此可以从标号1继续执行)

  4. restore_all //恢复现场

  5. iret - pop cs:eip/ss:esp/eflags from kernel stack

  6. 继续运行用户态进程Y

几种特殊的情况:

  1. 通过中断处理过程中的调度时机,用户态进程与内核线程之间互相切换和内核线程之间互相切换,与最一般的情况非常类似,只是内核线程运行过程中发生中断没有进程用户态和内核态的转换;

  2. 内核线程主动调用schedule(),只有进程上下文的切换,没有发生中断上下文的切换,与最一般的情况略简略;

  3. 创建子进程的系统调用在子进程中的执行起点及返回用户态,如fork;

  4. 加载一个新的可执行程序后返回到用户态的情况,如execve;

0 0