这是我尝试获取资源的简单方法,特别是在程序执行时发生的总上下文切换。
#include<stdio.h>
#include <sys/resource.h>
int appgetrusage(struct rusage *);
int appgetdiffrusage(struct rusage *, struct rusage *);
int main() {
struct rusage begin, end;
appgetrusage(&begin);
/*
* core of the program goes here
* where lot of system threads are spawned and joined
*
*/
appgetrusage(&end);
appgetdiffrusage(&begin, &end);
return 0;
}
int appgetrusage(struct rusage *usage){
int who = RUSAGE_SELF;
struct timeval start, end;
getrusage(RUSAGE_SELF, usage);
return 1;
}
int appgetdiffrusage(struct rusage *oldr, struct rusage *newr){
printf("\n");
printf("user time (ms): %llu\n",1000 * ((newr->ru_utime).tv_sec - (oldr->ru_utime).tv_sec) +
((newr->ru_utime).tv_usec - (oldr->ru_utime).tv_usec) / 1000);
printf("system time (ms): %ld\n", 1000 * ((newr->ru_stime).tv_sec - (oldr->ru_stime).tv_sec) +
((newr->ru_stime).tv_usec - (oldr->ru_stime).tv_usec) / 1000);
printf("voluntary context switches : %ld\n", newr->ru_nvcsw - oldr->ru_nvcsw);
printf("involuntary context switches : %ld\n", newr->ru_nivcsw - oldr->ru_nivcsw);
return 1;
}
答案 0 :(得分:3)
有人可以建议替代方案吗?
使用perf(https://perf.wiki.kernel.org/index.php/Tutorial#Counting_with_perf_stat):
perf stat your-program
像这样:
>perf stat ./my_test 2
Thread 139828421826304:
Thread 139828411336448:
^[[A./my_test: Terminated
Performance counter stats for './my_test 2':
74333.536760 task-clock # 1.999 CPUs utilized
627 context-switches # 0.008 K/sec
26 cpu-migrations # 0.000 K/sec
282 page-faults # 0.004 K/sec
182727508914 cycles # 2.458 GHz [50.00%]
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
121168605770 instructions # 0.66 insns per cycle [75.00%]
30262379463 branches # 407.116 M/sec [74.99%]
1635031 branch-misses # 0.01% of all branches [75.01%]
37.181359478 seconds time elapsed
答案 1 :(得分:1)
Systemtap为分析/调试用户空间以及内核代码提供了出色的工具。
systemtap脚本,用于查找上下文切换 https://sourceware.org/systemtap/examples/process/chng_cpu.stp
在ARM中,由于uprobes(https://blueprints.launchpad.net/linux-linaro/+spec/arm-uprobes),用户空间分析尚不可用。但是X86,它运行正常。
答案 2 :(得分:0)
你们已经分享了最好的资源,比如perf和systemtap,但是如果有人只是不想深入了解那么深入了解RHEL6或内核2.6.23及更高版本的sysstat包就会有一些叫做pidstat的东西。将pidstat与-w
一起使用,它将为您提供自愿和非自愿上下文切换的总数。