Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining size of c++ method

Tags:

c++

g++

I have a .cpp file with various methods defined:

// test.cpp

// foo() is not inlined
void foo() {
  ...
}

I compile it:

g++ test.cpp -o test.o -S

And now I want to determine, from examination of test.o, how many bytes of memory the instruction foo() takes up. How can I do this?

I ask because I have, through careful profiling and experimentation, determined that I am incurring instruction cache misses leading to significant slowdowns on certain critical paths. I'd therefore like to get a sense of how many bytes are occupied by various methods, to guide my efforts in shrinking my instruction set size.

like image 669
dshin Avatar asked Feb 12 '23 00:02

dshin


1 Answers

I wouldn't recommend the -S flag for that, unless you're in love with your ISA's manual and hand-calculating instruction sizes. Instead, just build and disassemble, presto - out comes the answer:

$ cc -c example.c 
$ objdump -d example.o 

example.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <f>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   8b 45 fc                mov    -0x4(%rbp),%eax
   a:   83 c0 03                add    $0x3,%eax
   d:   5d                      pop    %rbp
   e:   c3                      retq   

As you can see, this function is 15 bytes long.

like image 96
Carl Norum Avatar answered Feb 13 '23 14:02

Carl Norum