在 C/C++ 应用程序中测量上下文切换总数的最佳方法是什么?



这是我尝试获取资源使用情况的简单方法,特别是在程序执行中发生的总上下文切换。

#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): %llun",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): %ldn", 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 : %ldn", newr->ru_nvcsw - oldr->ru_nvcsw);
    printf("involuntary context switches : %ldn", newr->ru_nivcsw - oldr->ru_nivcsw);
    return 1;
}
  1. 这是正确的方法吗?
  2. 有人可以提出替代方案吗?或更正?:-)

有人可以提出替代方案吗?

使用性能 (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

Systemtap 为分析/调试用户空间以及内核代码提供了极好的工具。

https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/SystemTap_Beginners_Guide/

用于查找上下文切换的系统点击脚本https://sourceware.org/systemtap/examples/process/chng_cpu.stp

在 ARM 中,由于 uprobes(https://blueprints.launchpad.net/linux-linaro/+spec/arm-uprobes),用户空间分析尚不可用。 但是X86,它工作正常。

你们已经分享了最好的资源,如perf和systemtap,但如果有人不想深入研究那么深入,那么RHEL6或内核2.6.23及更高版本的sysstat软件包有一个名为pidstat的东西。将 pidstat 与 -w 一起使用,它将为您提供自愿和非自愿上下文切换的总数。

相关内容

  • 没有找到相关文章

最新更新