我正在研究CFS调度程序。根据CFS,vruntime是进程在CPU上运行的时间。因此,一旦进程消耗了一些CPU,其vruntime就会增加。
要深入了解context_switch概念,我研究了kernel / sched / core.c文件的context_switch方法实现。
context_switch(struct rq *rq, struct task_struct *prev,
struct task_struct *next)
要了解上下文切换所涉及的流程,尤其是要了解计划了哪个流程和计划在其中的流程,我已经添加了
trace_printk(KERN_INFO "**$$,traceme,%d,%llu,%llu,%d,%llu,%llu\n", (int)(prev->pid),prev->se.vruntime,prev->se.sum_exec_runtime, (int)(next->pid),next->se.vruntime,next->se.sum_exec_runtime);
kernel / sched / core.c文件的context_switch()函数内部。
清理后的一些样本数据
//(prev->pid),prev->se.vruntime,prev->se.sum_exec_runtime, (int)(next->pid),next->se.vruntime,next->se.sum_exec_runtime
Line-1 : 7560,24498429469681,823155565,7566,24498418258892,1637962
Line-2 : 7566,24498418261234,1640304,7580,24498417733416,1018016
Line-3 : 7580,24498417752807,1037407,686,24498429468802,48339928895
Line-4 : 686,24498429469817,48339929910,7566,24498418261234,1640304
Line-5 : 7566,24498418263610,1642680,7581,24498417762357,1038126
Line-6 : 7581,24498417781339,1057108,7560,24498429469681,823155565
Line-7 : 7560,24498429470724,823156608,7566,24498418263610,1642680
Line-8 : 7566,24498418265980,1645050,7582,24498418395747,1202608
Line-9 : 7582,24498418414400,1221261,686,24498429469817,48339929910
Line-10: 686,24498429470804,48339930897,7566,24498418265980,1645050
Line-11: 7566,24498418268334,1647404,7583,24498417826636,1168325
Line-12: 7583,24498417845297,1186986,7560,24498429470724,823156608
Line-13: 7560,24498429471802,823157686,7566,24498418268334,1647404
Line-14: 7566,24498418270800,1649870,686,24498429470804,48339930897
// Up to this line vruntime of all process increased in each run as expected.
Line-15: 686,24498438028365,48348488458,7560,24498429471802,823157686
Line-16: 0,0,0,7,918077230457,2930949708
Line-17: 7,918077232097,2930951348,0,0,0
Line-18: 7560,6056741110796,823305719,7584,24498429478909,1156272 <---- Here vruntime of process 7560 is decreased . Why?
根据以上数据,我们可以推断出每个进程在调度之前要执行多少时间。
p_pid p_vrt p_sum_exe_rt n_pid n_vrt n_sum_exe_rt | prev_tslice next_tslice CPU
7560 , 24498429469681, 823155565, 7566 , 24498418258892, 1637962, | , 1191 , 2327
7566 , 24498418261234, 1640304, 7580 , 24498417733416, 1018016, | , 2342 , 19462
7580 , 24498417752807, 1037407, 686 , 24498429468802, 48339928895, | , 19391 , 1003
686 , 24498429469817, 48339929910, 7566 , 24498418261234, 1640304, | , 1015 , 2342
7566 , 24498418263610, 1642680, 7581 , 24498417762357, 1038126, | , 2376 , 18429
7581 , 24498417781339, 1057108, 7560 , 24498429469681, 823155565, | , 18982 , 1191
7560 , 24498429470724, 823156608, 7566 , 24498418263610, 1642680, | , 1043 , 2376
7566 , 24498418265980, 1645050, 7582 , 24498418395747, 1202608, | , 2370 , 19520
7582 , 24498418414400, 1221261, 686 , 24498429469817, 48339929910, | , 18653 , 1015
686 , 24498429470804, 48339930897, 7566 , 24498418265980, 1645050, | , 987 , 2370
7566 , 24498418268334, 1647404, 7583 , 24498417826636, 1168325, | , 2354 , 19617
7583 , 24498417845297, 1186986, 7560 , 24498429470724, 823156608, | , 18661 , 1043
7560 , 24498429471802, 823157686, 7566 , 24498418268334, 1647404, | , 1078 , 2354
7566 , 24498418270800, 1649870, 686 , 24498429470804, 48339930897, | , 2466 , 987
686 , 24498438028365, 48348488458, 7560 , 24498429471802, 823157686, | , 8557561 , 1078
//----------------------- Up to this line vruntime is increased
7560 , 6056741110796, 823305719, 7584 , 24498429478909, 1156272, | , 148033 , 18671
一切看起来都很完美-每次运行都会增加进程的运行时间。
在最后一行让我惊讶的是,正在运行的进程的vruntime减少了。
vruntime减少了(从24498429471802开始 到6056741110796),为什么?
如何减少正在运行的进程的vruntime,甚至 当进程固定到特定的CPU内核时(因此没有机会 迁移到其他CPU内核)?
我正在使用Debian 8.0和Ubuntu 16.04,内核3.16.35,这在两个操作系统中都发生过。
任何了解原因的链接都会有很大帮助。