Linux 中确定进程/函数调用的指令大小

Determining Instruction Size of A process / A function call in Linux

我想确定一个内核完成一个进程需要多少条指令。有办法确定吗?因此,在微控制器中,您可以确定函数的指令大小,我想在 Linux 中做同样的事情。提前致谢。

编辑:[已解决] 最适合我的应用程序是

perf stat -p <pid>

perf stat <command>

有多种方法可以做到这一点:

[]$ readelf -s /usr/lib64/libc.so.6|grep usleep
  1542: 00000000000f9090    57 FUNC    GLOBAL DEFAULT   12 usleep@@GLIBC_2.2.5
  1560: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS usleep.c
  7073: 00000000000f9090    57 FUNC    GLOBAL DEFAULT   12 usleep

[]$ objdump -t /usr/lib64/libc.so.6|grep usleep
0000000000000000 l    df *ABS*  0000000000000000              usleep.c
00000000000f9090 g     F .text  0000000000000039              usleep

[]$ nm -S /usr/lib64/libc.so.6|grep usleep
00000000000f9090 0000000000000039 T usleep

所有情况下的大小都是 0x39 (5​​7) 字节。

如果你想要给定函数的指令数,你仍然可以使用objdump:

[]$ objdump -d /usr/lib64/libc.so.6 | perl -ne 'BEGIN { $/="\n\n" }; print if $_ =~ /usleep/;'

...将列出反汇编。对于您需要减去行数的确切指令数:

[]$ echo $(objdump -d /lib32/libc.so.6 | perl -ne 'BEGIN { $/="\n\n" }; print if $_ =~ /usleep/;'|wc -l)-2| bc -l

对于动态解决方案,您可以使用 perf stat:

[]$ perf stat uptime
 10:57:28 up 21 days, 10:30,  4 users,  load average: 2.00, 1.98, 2.16

 Performance counter stats for 'uptime':

          0.719094      task-clock (msec)         #    0.802 CPUs utilized          
                 0      context-switches          #    0.000 K/sec                  
                 0      cpu-migrations            #    0.000 K/sec                  
               123      page-faults               #    0.171 M/sec                  
         2,297,093      cycles                    #    3.194 GHz                    
   <not supported>      stalled-cycles-frontend  
   <not supported>      stalled-cycles-backend   
         1,985,122      instructions              #    0.86  insns per cycle        
           389,193      branches                  #  541.227 M/sec                  
            15,847      branch-misses             #    4.07% of all branches        

       0.000896079 seconds time elapsed

如果您 运行 执行几次命令,您很可能会看到值在 运行 之间波动。因此,这不是精确的科学。