linux内核 任务调度
动机 (Motivation)
Recently, I find it is hard to know the percentage of time that one process uses to wait for synchronous I/O (eg, read, etc). One way is to use the taskstats API provided by Linux Kernel [1]. However, for this way, the precision may be one problem. With this problem, I dig into Linux Kernel source codes to see how "blkio_delay_total" (Delay time waiting for synchronous block I/O to complete) is calculated.
最近,我发现很难知道一个进程等待同步I / O(例如,读取等)所花费的时间百分比。 一种方法是使用Linux内核[1]提供的taskstats API。 但是,以这种方式,精度可能是一个问题。 有了这个问题,我将深入研究Linux内核源代码,以了解如何计算“ blkio_delay_total”(等待同步块I / O完成的延迟时间)。
细节 (Details)
Actually, "blkio_delay_total" is calculated in function "__delayacct_add_tsk" in "linux/kernel/delayacct.c" file as follows.
实际上,“ blkio_delay_total”是在“ linux / kernel / delayacct.c”文件中的函数“ __delayacct_add_tsk”中计算的,如下所示。
83 int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
84 {
...
121 tmp = d->blkio_delay_total + tsk->delays->blkio_delay;
122 d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp;
...
131
132 return 0;
133
}
From above source codes, we know that "blkio_delay_total" adds "blkio_delay" each time. And I find "blkio_delay" is the time delay which one process is waiting for synchronous I/O each time. It is calculated with following way.
从上面的源代码中,我们知道“ blkio_delay_total”每次都会添加“ blkio_delay”。 而且我发现“ blkio_delay”是一个进程每次等待同步I / O的时间延迟。 它是用以下方法计算的。
In "linux/kernel/core.c":
在“ linux / kernel / core.c”中:
4969 /*
4970 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
4971 * that process accounting knows that this is a task in IO wait state.
4972 */
4973 long __sched io_schedule_timeout(long timeout)
4974 {
4975 int old_iowait = current->in_iowait;
4976 struct rq *rq;
4977 long ret;
4978
4979 current->in_iowait = 1;
4980 blk_schedule_flush_plug(current);
4981
4982 delayacct_blkio_start();
4983 rq = raw_rq();
4984 atomic_inc(&rq->nr_iowait);
4985 ret = schedule_timeout(timeout);
4986 current->in_iowait = old_iowait;
4987 atomic_dec(&rq->nr_iowait);
4988 delayacct_blkio_end();
4989
4990 return ret;
4991 }
4992 EXPORT_SYMBOL(io_schedule_timeout);
When one process starts to wait for I/O, the start time will be recorded. And after it finishes sync I/O, it will get blkio_delay which equals to current time minus start time. At last, add this delta time (blkio_delay) to process’s "blkio_delay_total".
当一个进程开始等待I / O时,将记录启动时间。 并且在完成同步I / O之后,它将获得blkio_delay,该值等于当前时间减去开始时间。 最后,将此增量时间(blkio_delay)添加到进程的“ blkio_delay_total”。
结论 (Conclusion)
1, When current process is waiting for synchronous I/O, its blkio_delay will be calculated and added to blkio_delay_total.
2, blkio_delay is updated when current process finishes its sync I/O.
1,当当前进程正在等待同步I / O时,将计算其blkio_delay并将其添加到blkio_delay_total。
如图2所示,当当前进程完成其同步I / O时,将更新blkio_delay。
参考资料 (References)
[1] https://www.kernel.org/doc/Documentation/accounting/taskstats-struct.txt
[2] http://lxr.free-electrons.com/source/kernel/sched/core.c?v=4.7#L4988
[1] https://www.kernel.org/doc/Documentation/accounting/taskstats-struct.txt
[2] http://lxr.free-electrons.com/source/kernel/sched/core.c?v=4.7#L4988
翻译自: https://www.systutorials.com/linux-kernel-collect-task-stats-data/
linux内核 任务调度