编写系统调用以计数进程的上下文切换 [英] Writing a syscall to count context switches of a process

查看:247
本文介绍了编写系统调用以计数进程的上下文切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须进行系统调用才能计算自愿性&进程的非自愿上下文切换.我已经知道将新系统调用添加到linux内核的步骤,但是我不知道应该从哪里开始上下文切换功能.有什么主意吗?

解决方案

如果系统调用仅报告统计信息,则可以使用内核中已经存在的上下文切换计数代码.

wait3系统调用/bin/ls -R"是任何程序.

通过在内核源代码中搜索"struct rusage",您可以找到链接无效__sched schedule(void) :

4124     if (likely(prev != next)) {         // <= if we are switching between different tasks
4125            sched_info_switch(prev, next);
4126            perf_event_task_sched_out(prev, next);
4127
4128            rq->nr_switches++;          
4129            rq->curr = next;
4130            ++*switch_count;     // <= increment nvcsw or nivcsw via pointer
4131
4132            context_switch(rq, prev, next); /* unlocks the rq */

指针switch_count来自第4091行或同一文件的第4111行. /p>

PS:perreal的链接很棒: http://oreilly.com/catalog/linuxkernel/chapter/ch10.html (搜索context_swtch)

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?

解决方案

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:

struct rusage {
 ...
    long   ru_nvcsw;         /* voluntary context switches */
    long   ru_nivcsw;        /* involuntary context switches */
};

You can try it by running:

$ /usr/bin/time -v /bin/ls -R
....
    Voluntary context switches: 1669
    Involuntary context switches: 207

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 from struct task_struct *t; the fields t->nvcsw; and t->nivcsw;:

1477  static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1478  {
1479        r->ru_nvcsw += t->nvcsw;    // <<=== here
1480        r->ru_nivcsw += t->nivcsw;
1481        r->ru_minflt += t->min_flt;
1482        r->ru_majflt += t->maj_flt;

Then you should search nvcsw and nivcsw in kernel folder to find how they are updated by kernel.

asmlinkage void __sched schedule(void):

4124     if (likely(prev != next)) {         // <= if we are switching between different tasks
4125            sched_info_switch(prev, next);
4126            perf_event_task_sched_out(prev, next);
4127
4128            rq->nr_switches++;          
4129            rq->curr = next;
4130            ++*switch_count;     // <= increment nvcsw or nivcsw via pointer
4131
4132            context_switch(rq, prev, next); /* unlocks the rq */

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)

这篇关于编写系统调用以计数进程的上下文切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆