linux 系统调用时怎么知道当前上下文属于那个进程,linux – 编写系统调用来计算进程的上下文切换...

如果您的系统调用只应报告统计信息,则可以使用内核中已有的上下文切换计数代码.

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 accumulate_thread_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;

然后你应该在kernel文件夹中搜索nvcsw和nivcsw来查找内核如何更新它们.

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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值