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

249 views Asked by At

I would like to determine how many instructions it would take for a core to finish a process. Is there a way to determine that? Hence, in a microcontroller you are able to determine the instruction size of functions, I would like to do the same inside Linux. Thanks in advance.

EDIT:[SOLVED] Most suited to my applications was to just

perf stat -p <pid>

and

perf stat <command>
1

There are 1 answers

6
FrodeTennebo On BEST ANSWER

There are various ways to do that:

[]$ 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

The size in all cases is 0x39 (57) bytes.

If you want the number of instructions of a given function, you can still use objdump:

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

...will list the disassembly. For the exact number of instruction you need to subtract to to the line count:

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

For a dynamic solution you can use 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

If you run a command a few times you will most likely see that the values fluctuates between runs. Hence, this is no exact science.