各個函數消耗的時間profiling和記憶體流失valgrind

來源:互聯網
上載者:User

標籤:

 來源:http://06110120wxc.blog.163.com/blog/static/37788161201333112445844/ARM(hisi)上面的profiling和valgrind的安裝  

profiling的使用

GNU gprof能夠列印出程式運行中各個函數消耗的時間,可以協助程式員找出眾多函數中耗時最多的函數。產生程式運行時候的函數調用關係,包括調用次數,可以協助程式員剖析器的運行流程。有了函數的調用關係,這會讓開發人員大大提高工作效率

 

gprof的基本用法:

1.使用 -pg選項編譯和連結你的應用程式,在makefile中加。

 

2.執行你的應用程式使之產生供gprof分析的資料,運行可執行程式:./ v2vapp_m_v2v,這樣就產生了一個gmon.out檔案,該檔案就包含了profiling的資料。

 

注意:程式運行並“正常”退出後,會產生一個gmon.out檔案。也就是說,程式必須是從exit或者return終止的。

例如:在我們的工程裡面,可以在session.cpp中的session_run裡面加限制條件,比如打算看100S中各個函數使用方式:可加gettimeofday(&pro_num_old,NULL);在while(1)前面;

gettimeofday(&pro_num_new,NULL);在while(1)裡面。然後如下

if((pro_num_new.tv_sec - pro_num_old.tv_sec) > 100)

{

       printf("new = %d, old = %d\n", pro_num_new.tv_sec, pro_num_old.tv_sec);

       break;

}

 

3. 分析產生結果

        gprof [Options] <可執行檔> <profiler data file>。

如:gprof v2vapp_m_v2v gmon.out > profile.txt

附:

gprof產生的資訊
 %                        the percentage of the total running time of the

time                     program used by this function.

                           函數使用時間占所有時間的百分比。

cumulative          a running sum of the number of seconds accounted

 seconds             for by this function and those listed above it.

                           函數和上列函數累計執行的時間。

 self                    the number of seconds accounted for by this

seconds             function alone.  This is the major sort for this
                          listing.
                          函數本身所執行的時間。
calls                   the number of times this function was invoked, if
                          this function is profiled, else blank.
                          函數被調用的次數
 self                   the average number of milliseconds spent in this
ms/call               function per call, if this function is profiled,
                         else blank.
                          每一次調用花費在函數的時間microseconds。
 total                  the average number of milliseconds spent in this
ms/call               function and its descendents per call, if this 
                          function is profiled, else blank.
                          每一次調用,花費在函數及其衍生函數的平均時間microseconds。
name                 the name of the function.  This is the minor sort
                          for this listing. The index shows the location of
                          the function in the gprof listing. If the index is
                          in parenthesis it shows where it would appear in
                          the gprof listing if it were to be printed.
                          函數名

 

valgrind的使用

 

Valgrind能做什嗎?

  ?記憶體使用量檢測,包括:

    –記憶體流失

    –非法記憶體訪問

    –未初始設定變數使用

    –重複釋放記憶體

  ?多線程競爭

    –檢測死結

    -檢測競爭

Valgrind還能做什嗎?

  ?效能分析

    –Cachegrind+ cg_annotate

    它類比 CPU中的一級緩衝I1,D1和L2二級緩衝,能夠精確地指出程式中 cache的丟失和命中。如果需要,它還能夠為我們提供cache丟失次數,記憶體引用次數,以及每行代碼,每個函數,每個模組,整個程式產生的指令數,這些可以看成程式的執行成本。以上資料其實對於app開發意義不大,僅作為參考。

  使用方法:

    –Callgrind+ callgrind_annotate/kcachegrind

       比起Cachegrind而言Callgrind更有用。Callgrind在Cachegrind基礎上實現,可以可視化展示函數調用關係,以及每個函數在整個進程運行過程中所佔的成本。對於

     

  ?Helgrind/DRD

    –POSIXAPI誤用

    –潛在的死結提醒

    –資料競爭

  ?Massif

    –記憶體分析工具,統計進程使用的記憶體情況,包括堆、棧

Valgrind—— memcheck  

  用法:valgrind--tool=memcheck--leak-check=yes ./grog

  輸出:

Memcheck 工具主要檢查下面的程式錯誤:

使用未初始化的記憶體 (Use of uninitialised memory)    

使用已經釋放了的記憶體 (Reading/writing memory after it has been free’d)    

使用超過 malloc分配的記憶體空間(Reading/writing off the end of malloc’d blocks)    

對堆棧的非法訪問 (Reading/writing inappropriate areas on the stack)    

申請的空間是否有釋放 (Memory leaks – where pointers to malloc’d blocks are lost forever)    

malloc/free/new/delete

申請和釋放記憶體的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])    

src和dst的重疊(Overlapping src and dst pointers in memcpy() and related functions)    

Callgrind

Callgrind收集程式運行時的一些資料,函數調用關係等資訊,還可以有選擇地進行cache 類比。在運行結束時,它會把分析資料寫入一個檔案。callgrind_annotate可以把這個檔案的內容轉化成可讀的形式。

Cachegrind

它類比 CPU中的一級緩衝I1,D1和L2二級緩衝,能夠精確地指出程式中 cache的丟失和命中。如果需要,它還能夠為我們提供cache丟失次數,記憶體引用次數,以及每行代碼,每個函數,每個模組,整個程式產生的指令數。這對最佳化程式有很大的協助。

Helgrind

它主要用來檢查多線程程式中出現的競爭問題。Helgrind 尋找記憶體中被多個線程訪問,而又沒有一貫加鎖的地區,這些地區往往是線程之間失去同步的地方,而且會導致難以發掘的錯誤。Helgrind實現了名為” Eraser” 的競爭檢測演算法,並做了進一步改進,減少了報告錯誤的次數。

Massif

堆棧分析器,它能測配量序在堆棧中使用了多少記憶體,告訴我們堆塊,堆管理塊和棧的大小。Massif能協助我們減少記憶體的使用,在帶有虛擬記憶體的現代系統中,它還能夠加速我們程式的運行,減少程式停留在交換區中的幾率。

Valgrind 安裝1、 到www.valgrind.org下載最新版valgrind-3.2.3.tar.bz2
2、 解壓安裝包:tar –jxvf valgrind-3.2.3.tar.bz2
3、 解壓後組建目錄valgrind-3.2.3 
4、 cd valgrind-3.2.3
5、 ./configure
6、 Make;make installValgrind 使用

用法: valgrind [options] prog-and-args [options]: 常用選項,適用於所有Valgrind工具

-tool=<name> 最常用的選項。運行 valgrind中名為toolname的工具。預設memcheck。    

h –help 顯示協助資訊。    

-version 顯示valgrind核心的版本,每個工具都有各自的版本。    

q –quiet 安靜地運行,只列印錯誤資訊。    

v –verbose 更詳細的資訊, 增加錯誤數統計。    

-trace-children=no|yes 跟蹤子線程? [no]    

-track-fds=no|yes 跟蹤開啟的檔案描述?[no]    

-time-stamp=no|yes 增加時間戳記到LOG資訊? [no]    

-log-fd=<number> 輸出LOG到描述符檔案 [2=stderr]    

-log-file=<file> 將輸出的資訊寫入到filename.PID的檔案裡,PID是運行程式的進行ID    

-log-file-exactly=<file> 輸出LOG資訊到 file    

-log-file-qualifier=<VAR> 取得環境變數的值來做為輸出資訊的檔案名稱。 [none]    

-log-socket=ipaddr:port 輸出LOG到socket ,ipaddr:port    

LOG資訊輸出

-xml=yes 將資訊以xml格式輸出,只有memcheck可用    

-num-callers=<number> show <number> callers in stack traces [12]    

-error-limit=no|yes 如果太多錯誤,則停止顯示新錯誤? [yes]    

-error-exitcode=<number> 如果發現錯誤則返回錯誤碼 [0=disable]    

-db-attach=no|yes 當出現錯誤,valgrind會自動啟動調試器gdb。[no]    

-db-command=<command> 啟動調試器的命令列選項[gdb -nw %f %p]    

適用於Memcheck工具的相關選項:

-leak-check=no|summary|full 要求對leak給出詳細資料? [summary]    

-leak-resolution=low|med|high how much bt merging in leak check [low]    

-show-reachable=no|yes show reachable blocks in leak check? [no] 

 

註:5和6最好按照紅色部分執行

 

1、下載最新版valgrind-3.8.1.tar.bz2

 

2、解壓安裝包:tar –jxvf valgrind-3.8.1.tar.bz2

 

3、解壓後組建目錄valgrind-3.8.1

 

4、 cd valgrind-3.7.0

 

5、vim configure(把armv7*改為arm*) ./configure –host=arm-hisiv200-linux

由於會出現下面12所說的錯誤,做修改:

Vim configure添加如下幾行:

export PATH=$PATH:/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/bin

CC=arm-hisiv200-linux-gcc

CFLAGS=-I/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/usr/include

LDFLAGS=-L/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/usr/lib

 

然後make distclean; ./configure --prefix=/mnt/valgrind --host=arm-hisiv200-linux

 

6、 Make;(這裡執行make install會出現找不到arm-hisiv200-linux-gcc的情況,所以沒執行)

    Make install

 

7、cp coregrind/valgrind /tftpboot/( tftpboot是掛載在板子上的)

cp memcheck/memcheck-arm-linux /tftpboot/(前四步是在ubuntu上操作)

 

8、在板子的/usr/local中mkdir lib;cd lib;mkdir valgrind

 

9、把memcheck-arm-linux放到/usr/local/lib/valgrind/下

 

10、在/etc/profile下加export PATH=$PATH:/mnt

 

11、valgrind ls –l會出現

==980== Memcheck, a memory error detector

==980== Copyright (C) 2002-2012, and GNU GPL‘d, by Julian Seward et al.

==980== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info

==980== Command: ls -l

==980==

 

valgrind:  Fatal error at startup: a function redirection

valgrind:  which is mandatory for this platform-tool combination

valgrind:  cannot be set up.  Details of the redirection are:

valgrind: 

valgrind:  A must-be-redirected function

valgrind:  whose name matches the pattern:      memcpy

valgrind:  in an object with soname matching:   ld-linux.so.3

valgrind:  was not found whilst processing

valgrind:  symbols from the object with soname: ld-linux.so.3

valgrind: 

valgrind:  Possible fixes: (1, short term): install glibc‘s debuginfo

valgrind:  package on this machine.  (2, longer term): ask the packagers

valgrind:  for your Linux distribution to please in future ship a non-

valgrind:  stripped ld.so (or whatever the dynamic linker .so is called)

valgrind:  that exports the above-named function using the standard

valgrind:  calling conventions for this platform.  The package you need

valgrind:  to install for fix (1) is called

valgrind: 

valgrind:    On Debian, Ubuntu:                 libc6-dbg

valgrind:    On SuSE, openSuSE, Fedora, RHEL:   glibc-debuginfo

valgrind: 

valgrind:  Cannot continue -- exiting now.  Sorry.

 

12、修改燒到板子上面的檔案系統中的/lib目錄下面的.so檔案,因為編譯出來的庫檔案是經過載剪的:

         在/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/lib/目錄下面的庫檔案是未經過裁剪的標準C庫,把這裡面的庫檔案替換燒到板子上面的檔案系統中的/lib目錄下面

添加default.supp、vgpreload_core-arm-linux.so到/usr/local/lib/valgrind

但還是會出現諸如下面的錯誤:

……

Conditional jump or move depends on uninitialised value(s)

==1006==    at 0x4922160: strcmp (in /lib/libc-2.11.1.so)

==1006==

==1006== Conditional jump or move depends on uninitialised value(s)

==1006==    at 0x4922778: strlen (in /lib/libc-2.11.1.so

……

各個函數消耗的時間profiling和記憶體流失valgrind

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.