Oprofile和gprof输出因相同代码而异

时间:2013-02-22 11:44:27

标签: linux profiling centos gprof oprofile

我正在运行我的代码AMD optron 6270机器。操作系统是Centos 6.2版本。 我做了一个简单的程序

#include<stdio.h>
#include<stdlib.h>
int calling (long a);
int calling1 (long a);
int calling2 (long a);
int calling3 (long a);
int calling4 (long a);
int calling5 (long a);

void main()
{
    long a,b=0;
    printf("hi");
    for (a=0; a<10000000; a++) b++;
    b =Calling(a);
    b =Calling5(a);
    b =Calling4(a);

}

int Calling(long a)
{
    long b=0;
    for (a=0; a<100; a++) b = Calling1(a);
    return 0;
}

int Calling1(long a)
{
    long b=0;

    for (a=0; a<10000000; a++) b++;
    b =Calling2(a);
    return 0;
}

int Calling2(long a)
{
    long b=0;
    for (a=0;a<10000000;a++) b++;

    b =Calling3(a);
    return 0;
}
int Calling3(long a)
{
    long b=0;
    for (a=0;a<10000000;a++) b++;
    b =Calling4(a);
    return 0;
}
int Calling4(long a)
{
    long b=0;
    for (a=0; a<10000000; a++) b++;
    return 0;
}

int Calling5(long a)
{
    long b=0;
    for (a=0; a<10000000; a++) b++;
    b=0;
    for (a=0; a<10000000; a++) b++;
    b=0;
    for (a=0 ;a<10000000; a++) b++;
    b=0;
    for (a=0; a<10000000; a++) b++;
    b=0;
    return 0;
}

在使用gprof和Oprofile进行此代码处理时。我得到了不同的报道。让我说我用gprof运行main.exe两次: 第一份报告与gprof 扁平型材:

每个样本计为0.01秒。

  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name

 24.80      2.96     2.96      101     0.03     0.03  Calling4
 24.71      5.91     2.95      100     0.03     0.12  Calling1
 24.63      8.84     2.94      100     0.03     0.06  Calling3
 23.78     11.68     2.84      100     0.03     0.09  Calling2
  1.01     11.80     0.12        1     0.12     0.12  Calling5
  0.34     11.84     0.04                             main
  0.00     11.84     0.00        1     0.00    11.65  Calling

使用gprof的第二份报告

扁平资料:

每个样本计为0.01秒。

累计自我总数  时间秒秒呼叫s /呼叫s /呼叫名称

 25.13      2.99     2.99      100     0.03     0.12  Calling1
 24.88      5.95     2.96      101     0.03     0.03  Calling4
 24.80      8.89     2.95      100     0.03     0.06  Calling3
 23.48     11.69     2.79      100     0.03     0.09  Calling2
  1.02     11.81     0.12        1     0.12     0.12  Calling5
  0.17     11.83     0.02                             main
  0.00     11.83     0.00        1     0.00    11.66  Calling

两份报告都有所不同。每次我运行我的main.exe我都会得到不同的分析报告。 当我尝试Oprofile时,我也会得到不同的结果:

Oprofile报告1 使用/ var / lib / oprofile / samples /作为samples目录。 警告:/ no-vmlinux无法找到。 CPU:AMD64系列15h,速度2.2e + 06 MHz(估计) 计数CPU_CLK_UNHALTED事件(CPU时钟未暂停),单位掩码为0x00(无单位掩码)计数为100000

samples  %        image name               symbol name
92552    24.7672  main                     Calling4
91610    24.5151  main                     Calling3
91566    24.5033  main                     Calling1
91469    24.4774  main                     Calling2
3665      0.9808  main                     Calling5
1892      0.5063  no-vmlinux               /no-vmlinux
916       0.2451  main                     main
10        0.0027  libc-2.12.so             profil_counter
1        2.7e-04  ld-2.12.so               _dl_cache_libcmp
1        2.7e-04  ld-2.12.so               _dl_relocate_object
1        2.7e-04  ld-2.12.so               _dl_sysdep_start
1        2.7e-04  ld-2.12.so               strcmp
1        2.7e-04  libc-2.12.so             __libc_fini
1        2.7e-04  libc-2.12.so             _dl_addr
1        2.7e-04  libc-2.12.so             _int_malloc
1        2.7e-04  libc-2.12.so             exit

Oprofile report2

使用/ var / lib / oprofile / samples /作为samples目录。 警告:/ no-vmlinux无法找到。 CPU:AMD64系列15h,速度2.2e + 06 MHz(估计) 计数CPU_CLK_UNHALTED事件(CPU时钟未暂停),单位掩码为0x00(无单位掩码)计数为100000

示例%图像名称符号名称

92254    24.7719  main                     Calling4
91482    24.5646  main                     Calling1
91480    24.5641  main                     Calling3
91340    24.5265  main                     Calling2
3658      0.9822  main                     Calling5
1270      0.3410  no-vmlinux               /no-vmlinux
916       0.2460  main                     main
6         0.0016  libc-2.12.so             profil_counter
1        2.7e-04  ld-2.12.so               _dl_lookup_symbol_x
1        2.7e-04  ld-2.12.so               _dl_setup_hash
1        2.7e-04  ld-2.12.so               _dl_sysdep_start
1        2.7e-04  ld-2.12.so               bcmp
1        2.7e-04  libc-2.12.so             __mcount_internal
1        2.7e-04  libc-2.12.so             _dl_addr
1        2.7e-04  libc-2.12.so             _int_free
1        2.7e-04  libc-2.12.so             mcount

任何人都可以告诉他们为什么会这样吗?造成这种情况的可能原因是什么? 我怎样才能避免这种情况,以便我可以获得持续的分析结果?

1 个答案:

答案 0 :(得分:2)

我不会担心后续报道的不同。根据程序的执行方式,报告可能会有很大差异。而且,很难说两个配置文件之间发生了什么。根据正在运行的其他进程,系统的缓存和TLB肯定会处于与第一个配置文件期间不同的状态。除非您能确保受控制的机器状态,否则不要期望一致的结果。

很容易理解为什么每个工具的报告都不一致。这两个工具根本不同。 Oprofile是一个基于采样的分析器,实质上是定期中断CPU。 Gprof是基于仪器的;它必须编译到您的程序中。如果没有使用gprof,这会生成不同的二进制文件。结果,gprof将过度估计时间。将oprofile用于CPU绑定进程,将gprof用于I / O绑定进程。