内核调度算法 cfs_Linux内核中CFS进程调度算法下进程的时间片长度

内核调度算法 cfs

Abstract
As is known, CFS (Completely Fair Scheduling) is a famous process scheduling algorithm in Linux Kernel but there is no convenient way for developers to get the timeslices of processes if CFS is chosen. In this article, I will introduce one way to hack the timeslices of process easily for CFS in Linux Kernel. Note that, the way introduced following is under Linux Kernel 3.16.39.

抽象
众所周知,CFS(完全公平调度)是Linux内核中著名的进程调度算法,但是如果选择了CFS,开发人员就没有便捷的方法来获取进程的时间片。 在本文中,我将介绍一种在Linux内核中轻松破解CFS进程时间片的方法。 注意,下面介绍的方法是在Linux Kernel 3.16.39下。

Details
Firstly, you need to export two flags from Linux kernel so that you can control them to output which process’s (PID) timeslice. The way to export them in ‘linux-3.16.39/kernel/sched/core.c’ is as follows.

细节
首先,您需要从Linux内核中导出两个标志,以便可以控制它们输出哪个进程的(PID)时间片。 在“ linux-3.16.39 / kernel / sched / core.c”中导出它们的方法如下。

//added by Weiwei Jia
#include <include/time.h>
int enable_flag = 0;
module_param(enable_flag, int, 0664);
EXPORT_SYMBOL_GPL(enable_flag);
//ended

In above source codes, ‘enable_flag’ controls whether to output the timeslice and ‘enable_pid’ controls which process’s timeslice to be outputed, which can be controled in userspace ("/sys/module/core/parameters/enable_flag" and "/sys/module/core/parameters/enable_pid") by users/developers. We also need to add some other source codes before context switch in core.c like following.

在上述源代码中,“ enable_flag”控制是否输出时间片,“ enable_pid”控制要输出哪个进程的时间片,可以在用户空间中进行控制(“ / sys / module / core / parameters / enable_flag”和“ / sys /模块/核心/参数/ enable_pid”)。 我们还需要在core.c中进行上下文切换之前添加其他一些源代码,如下所示。

2805 static void __sched __schedule(void)                                    
2806 { 
...
2869         //added by Weiwei Jia                                                                                                                                                                                                          
2870         do_gettimeofday(&(next->__ts));                                 
2871         next->__start_ts = (next->__ts).tv_sec * 1000 + (next->__ts).tv_usec;
2872         prev->__end_ts = next->__start_ts;                              
2873         if (enable_flag == 1 && prev->;pid == enable_pid) {              
2874             printk(KERN_INFO "%d\n", prev->;__end_ts - prev->__start_ts); 
2875         }                                                                                                  
2876         //ended
2877                                                                         
2878         context_switch(rq, prev, next); /* unlocks the rq */            
...
2895 }

Above source codes show us when there is a context switch from process A to process B, we need to get the end time-stamp of A and the start time-stamp of B (using start/end times-stamp of one process will get its timeslice). Then, we check whether we need to output the timeslice of specific process according to ‘enable_flag’ and ‘enable_pid’.

上面的源代码向我们展示了从流程A到流程B的上下文切换时,我们需要获取A的结束时间戳和B的开始时间戳(使用一个进程的开始/结束时间戳将得到它的时间片)。 然后,我们检查是否需要根据“ enable_flag”和“ enable_pid”输出特定进程的时间片。

Of course, we have to add some fields (like __start_ts, __end_ts as above) in task_struct structure (each process in linux Kernel is represented by one task_struct structure) like following.

当然,我们必须在task_struct结构中添加一些字段(如上述的__start_ts__end_ts )(Linux内核中的每个进程都由一个task_struct结构表示),如下所示。

1235 struct task_struct {
...
1327                                                                                                            
1328     //added by Weiwei Jia                                                                                                                                                                                                              
1329     struct timeval __ts;                                                                                   
1330     int __start_ts;                                                                                        
1331     int __end_ts;                                                                                          
1332     //ended                                                                                                
1333
...
1675 };

Up to here, everything is okay, we just need to recompile the kernel and load it. After the kernel is loaded, echo ‘1’ to "/sys/module/core/parameters/enable_flag" and process PID to "/sys/module/core/parameters/enable_pid" to find process’s timeslices (use ‘dmesg’ command to retrieve timeslices).

到这里为止,一切都很好,我们只需要重新编译内核并加载它即可。 加载内核后,将“ 1”回显到“ / sys / module / core / parameters / enable_flag”,并将PID处理到“ / sys / module / core / parameters / enable_pid”,以查找进程的时间片(使用dmesg命令)检索时间片)。

Conclusion
This article introduces a simple way to get timeslices of processes. You can also use some other ways, for example, export interface in proc FS or use one self-made module and so on. However, I think the way shown above is simplest and with high efficiency.

结论
本文介绍了一种获取进程时间片的简单方法。 您还可以使用其他方式,例如proc FS中的导出接口或使用一个自制模块等等。 但是,我认为上面显示的方法最简单且效率很高。

翻译自: https://www.systutorials.com/length-timeslice-processes-cfs-process-scheduling-algorithm-linux-kernel/

内核调度算法 cfs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值