问题
How can I measure the time spent dynamically linking at program startup?
Solutions that come to mind and why I'm hesitant:
1) Print something the time right before running the program and at the start of main.
This doesn't take into account possible code that runs before main -- initialization of globals and any resulting function calls. It's not automated out of the box and well, it strikes me as crude and of dubious accurately.
2) time command on an empty program that has all the same headers as the program to be tested and is dynamically linked to the same libs.
I'm not sure about this one. I don't know if the compiler and linker can collude to abbreviate dynamic linking based on the contents of client code but it seems possible.
Edit: 6 minutes after asking this I double checked the obvious search terms on Google, "measure time spent dynamically linking", and ended up back here as the first hit.
回答1:
Use LD_DEBUG
.
try this:
LD_DEBUG=statistics ./myprog
It'll output a short list of various statistics. An example:
xenon-lornix:~> LD_DEBUG=statistics ls
22833:
22833: runtime linker statistics:
22833: total startup time in dynamic loader: 2500702 clock cycles
22833: time needed for relocation: 757822 clock cycles (30.3%)
22833: number of relocations: 155
22833: number of relocations from cache: 8
22833: number of relative relocations: 1253
22833: time needed to load objects: 1325154 clock cycles (52.9%)
Shows info about loading/running the ls
command.
Using LD_DEBUG=help ./myprog
will show names of other options available to use with LD_DEBUG
.
More information can be found in the ld.so, ld-linux.so man pages (these are identical actually). A Google search can help with more specifics.
来源:https://stackoverflow.com/questions/24625386/measure-time-taken-dynamically-linking-at-program-startup