zircon的(de)兩種調度理解 - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 技術分享 >

zircon的(de)兩種調度理解

發表時(shí)間:2019-7-6

發布人(rén):融晨科技

浏覽次數:52

zircon 實現兩種調度機制,一種就(jiù)是(shì)fair 其實現在(zài)fair_scheduler.cpp中,一種是(shì)基于(yú)時(shí)間片的(de)其實現在(zài)sched.cpp 中,調度器的(de)入口都在(zài)sche_reschedule()這(zhè)個(gè)函數中。
例如fair的(de)實現如下:
void sched_reschedule() {
    FairScheduler::Reschedule();
}
fair的(de)實現是(shì)一個(gè)cpp的(de)類。
另一中sche_reschedule()的(de)實現在(zài)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))) {

#可見會首先判斷當前線程的(de)時(shí)間片是(shì)否用盡,用盡的(de)話,則加入到(dào)當前cpu 運行隊列的(de)末尾,否則就(jiù)插入到(dào)head,這(zhè)樣下次調用這(zhè)個(gè)函數的(de)時(shí)候就(jiù)會
優先調用這(zhè)個(gè)函數  
        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 挑選一個(gè)thread 來(lái)運行,類似于(yú)linux的(de)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;

    
#計算久進程的(de)時(shí)間記賬
    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);
}

相關案例查看更多