2.7 Simple Profiling of Interface Usage
The dynamic linker in the GNU C library allows easy profiling of the way a DSO is used without recompiling any code. By setting the value of the environment variable LD PROFILE to the shared object name (SONAME) of a DSO, all uses of interfaces defined in this DSO which go through a PLT are recorded.In addition time-based sampling happens as well and all samples which happen in the profiled DSO are recorded. This is all similar to the gprof-based profiling which is available on Unix systems for a long time.
gnu的c库动态链接器允许简单分析DSO的性能,不用重编代码。通过设置环境变量LD PROFILE,所有使用这个DSO的通过PLT的调用都会被记录。另外,会执行时间采样和相关的调用。这个一个简单的gprof的分析,在Unix系统上长时间可用。
The LD PROFILE variable can be set for arbitrarily many applications running at the same time. All profiling activity is recorded in real-time in the same output file which is usually stored in /var/tmp. Without having to stop any or all applications currently profiled, the current status can be examined by running the sprof program. The options this program understands are similar to those gprof understand. There are basically three report types, call pairs, flat profiles, and graph profiles which can all be requested at once.
环境变量LD PROFILE可以用于任意多的应用运行分析。所有的记录输出通常在/var/tmp。不需要停止运行的应用,当前的状态可以通过sprof查看。这个命令同grpof一样简单。可同时使用的三个基本类型,调用、profile、图形profiles。
How to use these profile results is specific to the application. It is easy to locate often called functions and, given sufficient runtime, functions inside the DSO which run for a long time.
怎样使用这些结果是通过应用确定的。容易定位经常调用的函数,如果有足够的运行时间。
Everybody who used gprof should feel right at home although DSO profiling has its limitations.
尽管DSO分析有其局限性,但使用gprof的人应该感觉很舒服。(有道翻译)
The call data is accumulated by intercepting the calls made through the PLT. The dynamic linker can do this without the application noticing it. But if the PLT is not used, data is not recorded. This is the case if an application looks a symbol up using dlsym and then calls it directly. This might make the profiling data useless.
调用数据通过拦截PLT调用过程获取。动态链接器可用在应用无感知的情况下完成操作。但是如果没有使用PLT,就没有记录数据。一个情况是,应用查找语法表,使用dlsym,之后直接调用。这样profiling失效。
The solution for this problem is to use the DL CALL FCT macro the GNU C library’s <dlfcn.h> header defines. If a looked-up function pointer fctp has been used like this
这样情况解决办法是使用宏DL CALL FCT。如果fctp如下使用
foo = fctp (arg1, arg2);
or if you prefer
或者
foo = (*fctp) (arg1, arg2);
the code should be rewritten to look like this:
代码重写:
foo = DL CALL FCT (fctp, (arg1, arg2));
The DL CALL FCT macro contains the necessary magic to record the calling of the function pointed to by fctp. If the DSO which contains the symbol is not profiled nothing happens. It is therefore safe to always use this macro to call symbols in dynamically loaded DSOs.
空包含调用必要的内容用于记录调用过程。如果DSO的内容不能被profiled,没有内容被执行。因此,使用这个宏是安全的。
The DL CALL FCT macro has a low overhead and so could be used unconditionally but for production code it is probably best to avoid it.
DL CALL FCT宏的开销很低,所以可以无条件使用,但对于生产代码来说,最好避免使用它。(有道翻译)
todo:实际操作看看情况