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庫動(dòng)態(tài)鏈接器允許簡(jiǎn)單分析DSO的性能扫皱,不用重編代碼。通過設(shè)置環(huán)境變量LD PROFILE迎变,所有使用這個(gè)DSO的通過PLT的調(diào)用都會(huì)被記錄瘸爽。另外,會(huì)執(zhí)行時(shí)間采樣和相關(guān)的調(diào)用能庆。這個(gè)一個(gè)簡(jiǎn)單的gprof的分析嘲恍,在Unix系統(tǒng)上長(zhǎng)時(shí)間可用嘀趟。
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.
環(huán)境變量LD PROFILE可以用于任意多的應(yīng)用運(yùn)行分析。所有的記錄輸出通常在/var/tmp绳姨。不需要停止運(yùn)行的應(yīng)用登澜,當(dāng)前的狀態(tài)可以通過sprof查看。這個(gè)命令同grpof一樣簡(jiǎn)單飘庄∧匀洌可同時(shí)使用的三個(gè)基本類型,調(diào)用跪削、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.
怎樣使用這些結(jié)果是通過應(yīng)用確定的碾盐。容易定位經(jīng)常調(diào)用的函數(shù)晃跺,如果有足夠的運(yùn)行時(shí)間。
Everybody who used gprof should feel right at home although DSO profiling has its limitations.
盡管DSO分析有其局限性毫玖,但使用gprof的人應(yīng)該感覺很舒服掀虎。(有道翻譯)
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.
調(diào)用數(shù)據(jù)通過攔截PLT調(diào)用過程獲取凌盯。動(dòng)態(tài)鏈接器可用在應(yīng)用無感知的情況下完成操作。但是如果沒有使用PLT涩盾,就沒有記錄數(shù)據(jù)十气。一個(gè)情況是,應(yīng)用查找語法表春霍,使用dlsym砸西,之后直接調(diào)用。這樣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.
空包含調(diào)用必要的內(nèi)容用于記錄調(diào)用過程。如果DSO的內(nèi)容不能被profiled莲趣,沒有內(nèi)容被執(zhí)行鸳慈。因此,使用這個(gè)宏是安全的喧伞。
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宏的開銷很低走芋,所以可以無條件使用,但對(duì)于生產(chǎn)代碼來說潘鲫,最好避免使用它翁逞。(有道翻譯)
todo:實(shí)際操作看看情況