zircon的两种调度理解 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

zircon的两种调度理解

发表时间:2019-7-6

发布人:葵宇科技

浏览次数:29

zircon 实现两种调度机制,一种就是fair 其实现在fair_scheduler.cpp中,一种是基于时间片的其实现在sched.cpp 中,调度器的入口都在sche_reschedule()这个函数中。
例如fair的实现如下:
void sched_reschedule() {
    FairScheduler::Reschedule();
}
fair的实现是一个cpp的类。
另一中sche_reschedule()的实现在sched.cpp 中,我们简单看下
void sched_reschedule() {
   
   
    current_thread->state = THREAD_READY;

    // idle thread doesn't go in the run queue
    if (likely(!thread_is_idle(current_thread))) {

#可见会首先判断当前线程的时间片是否用尽,用尽的话,则加入到当前cpu 运行队列的末尾,否则就插入到head,这样下次调用这个函数的时候就会
优先调用这个函数  
        if (current_thread->remaining_time_slice > 0) {
            insert_in_run_queue_head(curr_cpu, current_thread);
        } else {
            insert_in_run_queue_tail(curr_cpu, current_thread);
        }
    }

    sched_resched_internal();
}
void sched_resched_internal() {
    thread_t* current_thread = get_current_thread();
    uint cpu = arch_curr_cpu_num();

    
    CPU_STATS_INC(reschedules);

    // pick a new thread to run
#从当前cpu 挑选一个thread 来运行,类似于linux的RR调度
    thread_t* newthread = sched_get_top_thread(cpu);

    DEBUG_ASSERT(newthread);

    newthread->state = THREAD_RUNNING;

    thread_t* oldthread = current_thread;
    oldthread->preempt_pending = false;

    
#计算久进程的时间记账
    zx_time_t now = current_time();

    // account for time used on the old thread
    DEBUG_ASSERT(now >= oldthread->last_started_running);
    zx_duration_t old_runtime = zx_time_sub_time(now, oldthread->last_started_running);
    oldthread->runtime_ns = zx_duration_add_duration(oldthread->runtime_ns, old_runtime);
    oldthread->remaining_time_slice = zx_duration_sub_duration(
        oldthread->remaining_time_slice, MIN(old_runtime, oldthread->remaining_time_slice));

    // set up quantum for the new thread if it was consumed
    if (newthread->remaining_time_slice == 0) {
        newthread->remaining_time_slice = THREAD_INITIAL_TIME_SLICE;
    }

    newthread->last_started_running = now;
#切换mmu
    // see if we need to swap mmu context
    if (newthread->aspace != oldthread->aspace) {
        vmm_context_switch(oldthread->aspace, newthread->aspace);
    }
#进程切换
    // do the low level context switch
    final_context_switch(oldthread, newthread);
}

相关案例查看更多