如果您的系统调用应该只报告统计信息,您可以使用内核中已经存在的上下文切换计数代码。
wait3 syscall 或 getrusage syscall 已在 struct rusage 字段中报告上下文切换计数:
struct rusage {
...
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
你可以通过运行来试试:
$ /usr/bin/time -v /bin/ls -R
....
Voluntary context switches: 1669
Involuntary context switches: 207
其中“/bin/ls -R”是任何程序。
通过在内核源代码中搜索“struct rusage”,您可以在 kernel/sys.c 中找到this accumulate_thread_rusage,它会更新 rusage 结构。它来自struct task_struct *t; t->nvcsw; 和 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;
然后您应该在内核文件夹中搜索nvcsw 和nivcsw 以查找它们是如何被内核更新的。
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 */
指针switch_count 来自同一文件的line 4091 或line 4111。
PS:来自 perreal 的链接很棒:http://oreilly.com/catalog/linuxkernel/chapter/ch10.html(搜索context_swtch)