本文主要是介绍9. Lottery Scheduling,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Lottery Scheduling
0. Basic Concept
A : 75 tickets (0~74)
B:25 tickets (75~99)
A1 + A2:1000 tickets
B1:10 tickets
注意:要区分一个进程和一个线程的tickets。
如下所示,A和B分别有100tickets,A中有两个需要处理的任务A1和A2,总共有1000tickets,每个任务各分配500tickets,相当于CPU给A分配的100tickets的50tickets。其实就是CPU给一个进程分配了100tickets,该进程给里面两个线程分配了1000tickets。
1. Stride Scheduling
A: 100 tickets
B: 50 tickets
C: 250 tickets
用10000除以每个job的tickets,得到了对应的stride_value。
A: stride_value = 10000 / 100 = 100
B: stride_value = 10000 / 50 = 200
C: stride_value = 10000 / 250 = 40
刚开始每个job的stride_value都是0,所以依次运行A、B、C。
每次运行后都会增加对应job的stride_value。
当所有job都运行一次后再判断stride_value的值的大小,选取最小的值对应的job运行。
2. The Linux Completely Fair Scheduler (CFS)
A simple counting-based technique: virtual runtime
time_slice = sched_latency / n
sched_latency是系统给出的,n 代表job数量,上图中sched_latency = 48ms,n = 4
故每个未完成的job的time_slice = 12 ms
当job C、D完成后,job A、B的 time_slice = sched_latency / n = 48ms / 2 = 24ms
但是有个问题就是如果n很大,那每个job分配的time_slice不就很小,这样每个job运行的不得劲,故引入了min_granularity保证每个job有最小的time_slice。
CFS也做了另外一个改善就是存储结构从链表变成了红黑树。
链表插入一个元素时间复杂度为O(n),而红黑树只需要O(log)。
Homework (Simulation)
This program, lottery.py, allows you to see how a lottery scheduler
works. See the README for details.
用的学校机房的电脑~
Question & Answer
1. Compute the solutions for simulations with 3 jobs and random seeds of 1, 2, and 3.
2. Now run with two specific jobs: each of length 10, but one (job 0) with just 1 ticket and the other (job 1) with 100 (e.g., -l 10:1,10:100). What happens when the number of tickets is so imbalanced? Will job 0 ever run before job 1 completes? How often? In general, what does such a ticket imbalance do to the behavior of lottery scheduling?
3. When running with two jobs of length 100 and equal ticket allocations of 100 (-l 100:100,100:100), how unfair is the scheduler? Run with some different random seeds to determine the (probabilistic) answer; let unfairness be determined by how much earlier one job finishes than the other.
我觉得不管seed为多少,两个job完成时间都不会差距很大。但是我也只测验了一部分,样本太小,希望大家能自己多测验…
4. How does your answer to the previous question change as the quantum size (-q) gets larger?
可以看出来 the length of time slice (- q)对job完成时间的影响非常大。
5. Can you make a version of the graph that is found in the chapter? What else would be worth exploring? How would the graph look with a stride scheduler?
to be continue…
这篇关于9. Lottery Scheduling的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!