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);
}