0

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>
mozcelikors
  • 2,582
  • 8
  • 43
  • 77

1 Answers1

2

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.

FrodeTennebo
  • 531
  • 3
  • 13
  • Hello, thanks for your answer. Am I mistaken or the size should be of no unit? I mean, I want to obtain the instruction count. Is it possible to obtain that? – mozcelikors Jan 08 '17 at 16:05
  • Sorry. I have expanded on my answer. – FrodeTennebo Jan 08 '17 at 20:23
  • Thank you very much. One more thing please, for python or other apps that is run from terminal via a command how can I find the instruction count do u know? Thanks.. – mozcelikors Jan 10 '17 at 09:58
  • For Python and other interpreted languages or languages which are run within a virtual machine it is quite hard to determine the instruction count. It might not be very useful either as this might even vary from run to run based on environment or precompilation or JIT compilation of VM languages. However, I have updated the answer with a solution for a dynamic solution. – FrodeTennebo Jan 13 '17 at 09:56
  • Thank you very much. This seems like it should work for most of the things. But I have just another question. Is it possible to run a command like this on a running process and see how many instructions its running within a period. (I wanna be able to start and stop the instruction counting) Thanks again. – mozcelikors Jan 13 '17 at 18:01
  • Solved. perf stat -p did the trick. Thanks for the help. – mozcelikors Jan 14 '17 at 18:05