Measure time taken dynamically linking at program startup?

可紊 提交于 2019-12-08 04:02:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!