编写系统调用来计算进程的上下文切换
我必须做一个系统调用来计算自愿的人数和人数。进程的非自愿上下文切换。我已经知道向 Linux 内核添加新系统调用的步骤,但我不知道应该从哪里开始上下文切换功能。有什么想法吗?
I have to do a system call to count the voluntary & involuntary context switches of a process. I already know the steps to add a new system call to a linux kernel but i have no clue of where i should start for the context-switch function. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的系统调用只应报告统计信息,则可以使用内核中已有的上下文切换计数代码。
wait3 系统调用 或 getrusage syscall 已经在
struct rusage
字段中报告上下文切换计数:您可以通过运行来尝试:
where "
/bin/ls -R
”是任何程序。通过在内核源代码中搜索“struct rusage”,您可以找到 kernel/sys.c 中的这个
accumulate_thread_rusage
会更新 rusage 结构体。它从 struct task_struct *t 读取;字段t->nvcsw;
和t->nivcsw;
:那么您应该搜索
nvcsw
和nivcsw
在内核文件夹中查找内核如何更新它们。asmlinkage void __sched Schedule(void):
指针 < code>switch_count 来自 第 4091 行 或 行[第 4111 章]
PS:来自 perreal 的链接很棒: http://oreilly.com/catalog/linuxkernel/chapter /ch10.html(搜索
context_swtch
)If your syscall should only report statistics, you can use context switch counting code that is already in the kernel.
wait3 syscall or getrusage syscall already reports context switch count in
struct rusage
fields:You can try it by running:
where "
/bin/ls -R
" is any program.By searching an "struct rusage" in kernel sources, you can find this
accumulate_thread_rusage
in kernel/sys.c, which updates rusage struct. It reads fromstruct task_struct *t
; the fieldst->nvcsw;
andt->nivcsw;
:Then you should search
nvcsw
andnivcsw
in kernel folder to find how they are updated by kernel.asmlinkage void __sched schedule(void):
Pointer
switch_count
is from line 4091 or line 4111 of the same file.PS: Link from perreal is great: http://oreilly.com/catalog/linuxkernel/chapter/ch10.html (search
context_swtch
)这已经存在:虚拟文件
/proc/NNNN/status
(其中 NNNN 是您想要了解的进程的十进制进程 ID)包含自愿和非自愿上下文的计数等开关。与 getrusage 不同,它允许您了解任何进程(而不仅仅是子进程)的上下文切换计数。请参阅proc(5) 联机帮助页
了解更多详细信息。
This already exists: the virtual file
/proc/NNNN/status
(where NNNN is the decimal process ID of the process you want to know about) contains, among other things, counts of both voluntary and involuntary context switches. Unlikegetrusage
this allows you to learn the context switch counts for any process, not just children. See theproc(5)
manpage for more details.进程会在阻塞、时间片到期或中断等情况下进行上下文切换。最终调用 Schedule() 函数。由于您想单独计算每个进程的数量,因此必须为每个进程保留一个新变量来计算上下文切换的数量。您可以在当前进程的 Schedule fun 中每次更新此变量。使用系统调用您可以读取该值。这是 pintos 的调度函数的片段,
A process will make a context switch in case of blocking, time quantum expiring or for interrupts etc. Eventually schedule() function is called. Since you want to count it for each process separately you have to keep a new variable for each process for counting the no of context switches. And you can update this variable each time in schedule fun for current process. Using your system call you can read this value. Here is a snippet of the schedule function of pintos,
上下文切换总数
cat /proc/PID/sched|grep nr_switches
自愿上下文切换
cat /proc/PID/sched | grep nr_known_switches
非自愿上下文切换
cat /proc/PID/sched|grep nr_involent_switches
其中 PID 是您希望监视的进程的进程 ID。
但是,如果您想通过修补(创建挂钩)Linux 源代码来获取这些统计信息,则与调度相关的代码位于
kernel/sched/
源代码树的 文件夹中。
特别是
kernel/sched/core.c包含schedule()函数,它是linux调度程序的代码。
CFS(完全公平调度程序)的代码是 Linux 中存在的几种调度程序之一,最常用的代码位于
/kernel/sched/fair.c
中,当设置 TIF_NEED_RESCHED 标志时,就会执行 Scheduler() ,因此找出设置此标志的所有位置(在 Linux 源代码上使用 cscope),这将使您深入了解进程发生的上下文切换的类型。
Total number of context switches
cat /proc/PID/sched|grep nr_switches
Voluntary context switches
cat /proc/PID/sched | grep nr_voluntary_switches
Involuntary context switches
cat /proc/PID/sched|grep nr_involuntary_switches
where PID is process ID of the process you wish to monitor.
However if you want to get these statistics by patching (creating a hook) linux source, the code related to scheduling is present in
kernel/sched/
folder of the source tree.
In particular
kernel/sched/core.c contains the schedule() function, which is the code of linux scheduler.
The code of CFS (completely fair scheduler), which is one of the several schedulers present in Linux, and is most commonly used is present in
/kernel/sched/fair.c
scheduler() is executed when ever TIF_NEED_RESCHED flag is set, so find out from which all places this flag is being set (use cscope on linux source) which will give you an insight intsight into the types of context switches occurring for a process.