How To Write Shared Libraries(50)

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í)際操作看看情況

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市溉仑,隨后出現(xiàn)的幾起案子挖函,更是在濱河造成了極大的恐慌,老刑警劉巖浊竟,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件怨喘,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡振定,警方通過查閱死者的電腦和手機(jī)必怜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來后频,“玉大人棚赔,你說我怎么就攤上這事∨枪” “怎么了?”我有些...
    開封第一講書人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵丧肴,是天一觀的道長(zhǎng)残揉。 經(jīng)常有香客問我,道長(zhǎng)芋浮,這世上最難降的妖魔是什么抱环? 我笑而不...
    開封第一講書人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任壳快,我火速辦了婚禮,結(jié)果婚禮上镇草,老公的妹妹穿的比我還像新娘眶痰。我一直安慰自己,他們只是感情好梯啤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開白布竖伯。 她就那樣靜靜地躺著,像睡著了一般因宇。 火紅的嫁衣襯著肌膚如雪七婴。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評(píng)論 1 307
  • 那天察滑,我揣著相機(jī)與錄音打厘,去河邊找鬼。 笑死贺辰,一個(gè)胖子當(dāng)著我的面吹牛户盯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播饲化,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼莽鸭,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了滓侍?” 一聲冷哼從身側(cè)響起蒋川,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎撩笆,沒想到半個(gè)月后捺球,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡夕冲,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年氮兵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片歹鱼。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡泣栈,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出弥姻,到底是詐尸還是另有隱情南片,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布庭敦,位于F島的核電站疼进,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏秧廉。R本人自食惡果不足惜伞广,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一拣帽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嚼锄,春花似錦减拭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至刊苍,卻和暖如春既们,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背正什。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工啥纸, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人婴氮。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓斯棒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親主经。 傳聞我的和親對(duì)象是個(gè)殘疾皇子荣暮,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容