cfs_quota
Abstract
Currently, the most famous process scheduling algorithm in Linux Kernel is Completely Fair Scheduling (CFS) algorithm. The core idea of CFS is to let each process share the same proportional CPU resources to run so that it is fair to each process. In this article, I will introduce how sched_min_granularity_ns and sched_latency_ns work internal CFS to affect the timeslice of processes. (This article discusses Linux Kernel 3.16.x; other versions may have some differences.)
抽象
当前, Linux内核中最著名的进程调度算法是完全公平调度(CFS)算法。 CFS的核心思想是让每个进程共享相同比例的CPU资源来运行,以便对每个进程公平。 在本文中,我将介绍sched_min_granularity_ns和sched_latency_ns如何工作内部CFS以影响进程的时间片。 (本文讨论了Linux Kernel 3.16.x;其他版本可能有所不同。)
Details
细节
616 static u64 __sched_period(unsigned long nr_running)
617 {
618 u64 period = sysctl_sched_latency; /* get from "/proc/sys/kernel/sched_latency_ns" */
619 unsigned long nr_latency = sched_nr_latency; /* get from "sched_latecy_ns divides sched_min_granularity" */
620
621 if (unlikely(nr_running > nr_latency)) { /* if the real processes on the run queue is bigger than nr_latency, that means sched_latency_ns cannot satisfy all the tasks's minimum granularity time requirements. */
622 period = sysctl_sched_min_granularity; /* get from "/proc/sys/kernel/sched_min_granularity_ns" */
623 period *= nr_running;
624 }
625
626 return period;
627 }
635 static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
636 {
637 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
638
639 for_each_sched_entity(se) {
640 struct load_weight *load;
641 struct load_weight lw;
642
643 cfs_rq = cfs_rq_of(se);
644 load = &cfs_rq->load;
645
646 if (unlikely(!se->on_rq)) {
647 lw = cfs_rq->load;
648
649 update_load_add(&lw, se->load.weight);
650 load = &lw;
651 }
652 slice = __calc_delta(slice, se->load.weight, load);
653 }
654 return slice;
655 }