linux – 编写系统调用来计算进程的上下文切换
发布时间:2020-12-14 02:21:26 所属栏目:Linux 来源:网络整理
导读:我必须做一个系统调用来计算志愿者和过程的非自愿上下文切换.我已经知道了向 Linux内核添加新系统调用的步骤,但我不知道应该从哪里开始上下文切换功能.任何的想法? 解决方法 如果您的系统调用只应报告统计信息,则可以使用内核中已有的上下文切换计数代码. w
我必须做一个系统调用来计算志愿者和过程的非自愿上下文切换.我已经知道了向
Linux内核添加新系统调用的步骤,但我不知道应该从哪里开始上下文切换功能.任何的想法?
解决方法
如果您的系统调用只应报告统计信息,则可以使用内核中已有的上下文切换计数代码.
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中找到更新rusage结构的this 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; 然后你应该在kernel文件夹中搜索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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |