1 | | [[WikiInclude(jazz/11-03-31)]] |
2 | | [[WikiInclude(jazz/10-06-07)]] |
3 | | [[WikiInclude(jazz/10-03-04)]] |
| 1 | == callgraph : 了解程式流程 == |
| 2 | |
| 3 | * ([wiki:jazz/11-03-31 2011-03-31]) |
| 4 | * C/C++ |
| 5 | * Rex 在 [http://people.debian.org.tw/~chihchun/2011/03/30/callgraphviz/ CallGraphviz – call graph visualzer based on csope, graphviz and xdot] 一文中介紹了許多不同的 callgraph 產生方式。 |
| 6 | * 搭配[http://en.wikipedia.org/wiki/Call_graph 維基百科 Call_graph]的軟體簡介,大概整理如下: |
| 7 | * [http://www.skynet.ie/~mel/projects/codeviz/ CodeViz] - 缺點:必須 patch GCC 然後用 Perl 來產生圖形 |
| 8 | * [http://students.ceid.upatras.gr/~sxanth/ncc/ ncc] - 目標是取代 cflow 跟 cscope,[http://blog.linux.org.tw/~jserv/archives/001723.html jserv 有介紹過] - 缺點:相依 GCC 環境版本 |
| 9 | * 有 [http://packages.debian.org/ncc Debian 套件 ncc] |
| 10 | * [http://www.gnu.org/software/cflow/ cflow] - 老字號 GNU 的流程分析工具 - [http://fred-zone.blogspot.com/2009/09/trace-cflow.html Fred 有寫過用在分析 Android Dalvik VM 的示範] - 產生文字輸出 |
| 11 | * 有 [http://packages.debian.org/cflow Debian 套件 cflow] |
| 12 | * [http://cscope.sourceforge.net/ cscope]/[http://www.gnu.org/software/global/ global] - Interactively examine a C program source - 我想 cscope 要強調的是 interactive 互動式除錯,而且照 Rex 的講法,可以只針對某個函數當進入點,而不是整個程式原始碼。 |
| 13 | * 有 [http://packages.debian.org/cscope Debian 套件 cscope] 跟 [http://packages.debian.org/global global] |
| 14 | * [http://www.vim.org/scripts/script.php?script_id=2368 CCTree] - C Call-Tree Explorer - Cscope based source-code browser, code flow analysis tool - 給 VIM 用的 cscope plugin |
| 15 | * [http://code.google.com/p/seascope/ seascope] - A pyQt GUI front-end for cscope - pyQt 寫的 cscope 圖形介面 |
| 16 | * [http://sourceforge.net/projects/kscope/ kscope] - A source-code browsing, analysis and editing environment for large C projects. - KDE 版本的 cscope 圖形介面 - 有 [http://packages.debian.org/kscope Debian 套件 kscope] |
| 17 | * [http://kcachegrind.sourceforge.net/ KCachegrind] - Profiling Visualization - 這個工具是搭配 Valgrind 這個 Profiler 可以在 runtime 產生 callgrah |
| 18 | * [http://code.google.com/p/jrfonseca/wiki/Gprof2Dot Gprof2Dot] - 也是透過 profiler 產生的輸出來作視覺化動作,最初是支援 gprof 現在已經支援更多不同 profiler 了。 |
| 19 | * [http://www.gson.org/egypt/ egypt] - 用 Perl 寫的 |
| 20 | * Python |
| 21 | * [http://pycallgraph.slowchop.com/ pycallgraph] - Python Call Graph |
| 22 | * Bash |
| 23 | * 我一直想找的是 Bash 的遞迴式 callgraph 產生器,雖然 Bash 可以加入 -X 參數來做 runtime 除錯,不過還沒有找到可以查出多層該怎麼追。 |
| 24 | * [http://bashdb.sourceforge.net/ bashdb] - BASH Debugger - 可以用類似 GDB 的指令來 DEBUG Bash |
| 25 | * shprof - a line profiler for shell scripts |
| 26 | * [http://dev.gentoo.org/~dberkholz/scripts/shprof 原始碼] |
| 27 | * 這邊有一招是蠻好玩的,居然只要設定 PS4 環境變數就可以秀出每個呼叫的開始時間。(這算是 bash 的簡易版 profiler 嘛?) [http://www.myunitsconverter.com/thread/885124/time%20profiling%20a%20linux%20command%20or%20script <參考討論> time profiling a linux command or script] |
| 28 | {{{ |
| 29 | Least invasive you may want to play with some variant of... |
| 30 | |
| 31 | set -x |
| 32 | PS4='$( date ): ' |
| 33 | sleep 1 |
| 34 | sleep 3 |
| 35 | sleep 2 |
| 36 | exit 0 |
| 37 | |
| 38 | which produces timestamps in front of the commands... |
| 39 | |
| 40 | Mon Mar 17 02:39:35 CET 2008: PS4='$( date ): ' |
| 41 | Mon Mar 17 02:39:35 CET 2008: sleep 1 |
| 42 | Mon Mar 17 02:39:36 CET 2008: sleep 3 |
| 43 | Mon Mar 17 02:39:39 CET 2008: sleep 2 |
| 44 | Mon Mar 17 02:39:41 CET 2008: exit 0 |
| 45 | }}} |
| 46 | |
| 47 | == Profiling / Profiler : 了解程式特性(CPU、記憶體、I/O) == |
| 48 | |
| 49 | * ([wiki:jazz/10-06-07 2010-06-07]) |
| 50 | * [http://www.pixelbeat.org/programming/profiling/ Linux Profiling tools and techniques] (2011-04-27) |
| 51 | * Java |
| 52 | * [http://www.eclipse.org/tptp/ Eclipse Test & Performance Tools Platform Project (TPTP)] - Eclipse 的 Profiling 工具 |
| 53 | * [http://www.eclipse.org/tptp/home/documents/tutorials/profilingtool/profilingexample_32.html An introduction to profiling Java applications] |
| 54 | * [http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.tptp.platform.doc.user/concepts/ceproftl.htm Overview of the Profiling Tool] - Eclipse 說明文件有蠻詳細的 Monitoring and profiling applications 觀念與操作步驟介紹 |
| 55 | * [http://profiler.netbeans.org/ Netbean Java Profiler] - CPU, memory and threads profiling - 搭配 Netbean |
| 56 | * [http://www.ej-technologies.com/company/profile.html JProfiler] - [http://en.wikipedia.org/wiki/JProfiler 維基百科] - 搭配 Eclipse |
| 57 | * [http://www.khelekore.org/jmp/ jmp] - Java Memory Profiler (主攻記憶體的 Profiler) |
| 58 | * [http://packages.debian.org/jmp Debian 套件 jmp] - Profiler for Java to trace object and method timings (舊的) |
| 59 | * [http://www.khelekore.org/jmp/tijmp/ tijmp] - [http://packages.debian.org/tijmp Debian 套件 tijmp] - Profiler for Java to trace object and method timings (比較新) |
| 60 | * Runtime Profiler |
| 61 | * http://profiler4j.sourceforge.net/ - 看起來不錯,不過大約 2006 之後就比較少更新了。 |
| 62 | * Monitoring - JMX |
| 63 | * [http://klaas.homeip.net/mcs/jmeasurement/ JMeasurement] - 支援 JMX 的 Profiler |
| 64 | * [http://xdprof.sourceforge.net/ xdprof] - a cross-platform tool that captures and analyzes stack traces sent at a fixed interval from Java Virtual Machines in a distributed system - 因為是針對分散式系統的 profiler,所以有可能可以用在 Hadoop 效能分析上 |
| 65 | |
| 66 | * C - check [raw-attachment:wiki:jazz/10-06-07:Advanced_C_Programming_Profiling.pdf slides of "Advanced C Programming - Profiling"] |
| 67 | * [http://www.gnu.org/software/binutils/ gprof] - 屬於 GNU binutils 的一部分 - [http://packages.debian.org/binutils Debian 套件 - binutils] |
| 68 | * [http://blog.superd.org/index.php/2004/11/02/39/ gprof 看 performance 分析…] |
| 69 | * [http://kprof.sourceforge.net/ kprof] - a KDE3 visual tool to help analyze profiling results - [http://packages.debian.org/kprof Debian 套件 kprof] |
| 70 | * [http://code.google.com/p/jrfonseca/wiki/Gprof2Dot#Example gprof2dot] - 把 gprof log 轉成 graphviz 的 dot 格式,就可以畫出圖形了 |
| 71 | * gcov - 統計 code coverage 的工具,可以在 gcc 編譯時加參數來達成。 |
| 72 | {{{ |
| 73 | Use GCC flags |
| 74 | |
| 75 | -fprofile-arcs: collect info about jumps |
| 76 | -ftest-coverage: collect info about code coverage |
| 77 | }}} |
| 78 | * mtrace() 函數 - 檢查記憶體溢出(memory leakage)問題 ([wiki:jazz/10-03-04 2010-03-04]) |
| 79 | * [http://itzone.hk/article/article.php?aid=200406221149442361 Linux C編程偵測記憶體溢出工具(一):mtrace ] |
| 80 | * [http://valgrind.org/ valgrind] - [http://packages.debian.org/valgrind Debian 套件 - valgrind] - ([wiki:jazz/10-03-04 2010-03-04]) |
| 81 | * [http://daydreamer.idv.tw/rewrite.php/read-18.html 檢查程式記憶體的小工具-valgrind] |
| 82 | * [http://www.jeffhung.net/blog/articles/jeffhung/1007/ Detecting C/C++ memory leaks with valgrind] |
| 83 | * 在 [raw-attachment:wiki:jazz/10-06-07:Advanced_C_Programming_Profiling.pdf slides of "Advanced C Programming - Profiling"] 投影片介紹中,有三個主要工具 memcheck、cachegrind、callgrind |
| 84 | * cachegrind - 模擬 L1/L2 Cache, 找出 cache misses |
| 85 | * [http://packages.debian.org/kcachegrind Debian 套件 kcachegrind] - visualisation tool for valgrind profiling output - 可以用視覺化圖形介面顯示 cachegrind 的輸出 |
| 86 | * [http://packages.debian.org/kcachegrind-converters Debian 套件 kcachegrind-converters] - format converters for KCachegrind profiling visualisation tool |
| 87 | * callgrind - 紀錄函數呼叫圖(call graph) |
| 88 | * [http://live.gnome.org/iogrind iogrind] - I/O profiling tool - 用來顯示應用程式的 I/O Profile |
| 89 | * [http://oprofile.sourceforge.net oprofile] - a system-wide profiler for Linux systems, capable of profiling all running code at low overhead. |
| 90 | * 有 [http://packages.debian.org/oprofile Debian 套件 - oprofile] |
| 91 | {{{ |
| 92 | #!sh |
| 93 | ~# opcontrol --init # to load module and daemon |
| 94 | ~# opcontrol -s # to start sampling |
| 95 | ~# opcontrol -t # to stop sampling |
| 96 | ~# opcontrol --dump # ?ushes the event log |
| 97 | ~# opcontrol --list-events # shows available performance counters |
| 98 | ~# opreport -l prog-name # gives breakdown of samples per function in prog-name |
| 99 | }}} |
| 100 | * [http://packages.debian.org/oprofile-gui Debian 套件 oprofile-gui] - system-wide profiler for Linux systems (GUI components) |
| 101 | * [http://hi.baidu.com/colin719/blog/item/3d78da8032ba72d79123d9ed.html 使用 gprof 和 oprofile 查找性能瓶頸] |
| 102 | * [http://packages.debian.org/sid/qprof qprof] - Profiling utilities for Linux |
| 103 | * sysprof - [http://packages.debian.org/sysprof Debian 套件 sysprof] - ([wiki:jazz/10-03-04 2010-03-04]) |
| 104 | * 2005-09 [http://blog.linux.org.tw/~jserv/archives/001302.html sysprof - System-wide Linux Profiler] - jserv 推薦 |
| 105 | |
| 106 | * Perl |
| 107 | * [http://search.cpan.org/dist/Devel-Profile/ Devel::Profile] - [http://packages.debian.org/libdevel-profile-perl Debian 套件 - libdevel-profile-perl] - a Perl code profiler |
| 108 | * [http://search.cpan.org/dist/Devel-Cover/ Devel::Cover] - Code coverage metrics for Perl - [http://packages.debian.org/libdevel-cover-perl Debian 套件 libdevel-cover-perl] |
| 109 | |
| 110 | * PHP - references for armorize |
| 111 | * [http://www.linuxjournal.com/article/7213 PHP Performance Profiling] |
| 112 | * [http://www.php-editors.com/php-tools/php-profiler.php php-profiler] |
| 113 | |
| 114 | * Bash |
| 115 | * 看樣子,bash 編譯時如果加入 [http://www.faqs.org/docs/bashman/bashref_120.html --enable-profiling] 就可以透過 gprof 進行 profiling。至於怎麼用,暫且還沒找到範例。 |
| 116 | |
| 117 | * MPI |
| 118 | * [http://packages.debian.org/tau Debian 套件 tau] - Tuning and Analysis Utilities - base profiling toolkit |
| 119 | * TAU is a profiling toolkit specially made for parallel computing (multi-thread, multi-process, MPI, PVM). - 針對平行程式的 Profiler 不過套件裡面包的只有 pthread 的支援,因此無法做 MPI, PVM 分析。 |
| 120 | * [http://packages.debian.org/tau-examples Debian 套件 tau-examples] - Tuning and Analysis Utilities - examples - 一些 TAU 的範例 |
| 121 | * [http://packages.debian.org/python-tau Debian 套件 python-tau] - Tuning and Analysis Utilities - support for python bindings |
| 122 | |
| 123 | * Python |
| 124 | * [http://packages.debian.org/python-profiler Debian 套件 python-profiler] - deterministic profiling of any Python programs |
| 125 | |
| 126 | * Ruby |
| 127 | * [http://packages.debian.org/ruby-prof Debian 套件 ruby-prof] - A fast code profiler for Ruby |
| 128 | |
| 129 | * C# / Mono |
| 130 | * [http://packages.debian.org/mono-profiler mono-profiler] - Mono profiler - [http://packages.debian.org/mono-profiler 有 Debian 套件 mono-profiler] |
| 131 | |
| 132 | == Systemtap / DTrace / ftrace == |
| 133 | |
| 134 | * ([wiki:jazz/10-03-04 2010-03-04]) |
| 135 | * [http://lwn.net/Articles/291091/ Tracing: no shortage of options] - Linux 追查 User Space / Kernel Space 的 trace 工具一覽 |
| 136 | * !SystemTap |
| 137 | * 官方網站 - http://sourceware.org/systemtap/ |
| 138 | * [http://packages.debian.org/systemtap Debian 套件 - systemtap] |
| 139 | * IBM Redbook - [http://www.redbooks.ibm.com/abstracts/redp4469.html SystemTap: Instrumenting the Linux Kernel for Analyzing Performance and Functional Problems] - 先前有看過一小段,目前暫時還沒有需要用到這種工具的時機。 |
| 140 | * [http://gnu.wildebeest.org/diary/2009/12/07/fudcon-success-systemtap-meets-python/ FudCon Success – Systemtap meets Python] |
| 141 | * [http://sourceware.org/systemtap/wiki/AddingUserSpaceProbingToApps Adding User Space Probing to an Application (heapsort example)] |
| 142 | * [http://people.redhat.com/roland/utrace/ utrace] - a new kernel-side API for kernel modules |
| 143 | * [http://lwn.net/Articles/224772/ Introducing utrace] |
| 144 | * 看起來是 !SystemTap 用在追蹤 User Space 的工具,但核心必須支援。 |
| 145 | * DTrace |
| 146 | * 3/3 聽完 Sun 的 Open Solaris 簡介,對於 DTrace 興起一些好奇,看了一些討論發現 DTrace 是比較友善,但要學 D 語言,而 !SystemTap 比較合適追 Kernel Space 的錯誤,User Space 必須靠 utrace 的支援。當然現階段最麻煩的還是授權,跟 ZFS, Lustre 一樣,DTrace 之所以沒有移植到 Linux 都是因為授權是 CDDL 而非 GPL。唉...自由軟體的授權真是一件複雜的事情啊!! |
| 147 | * 2010-07-25 - [http://dtracehol.com/ Observing and Optimizing your Application with DTrace] - [http://www.oscon.com/oscon2010/public/schedule/detail/13601 OSCON 2010 dTrace Tutorial] 的實作網站,共設計了 17 個練習。Cool~ |
| 148 | * [http://assets.en.oreilly.com/1/event/45/Observing%20and%20Optimizing%20your%20Application%20with%20DTrace%20Presentation.pdf OSCON 2010 dTrace 投影片] |
| 149 | * [http://blog.xuite.net/evereasy/solarpower/19400612 免費的私家偵探-跟著 DTrace 探索 Process] |
| 150 | * [http://www.solarisinternals.com/wiki/index.php/DTrace_Topics_Intro DTrace Topics Intro] |
| 151 | * [ftp://crisp.dynalias.com/pub/release/website/dtrace/ 不太合乎授權的 DTrace Porting for Linux] |
| 152 | * 2008-06 - [http://blogs.sun.com/bmc/entry/dtrace_on_linux DTrace on Linux] |
| 153 | * 2007-07 - [http://developers.sun.com/solaris/articles/dtrace_ajax.html End-to-End Tracing of Ajax/Java Applications Using DTrace] |
| 154 | * 2007-03 - [http://blogs.sun.com/jmr/entry/adding_dtrace_probes_to_mozilla Adding Dtrace Probes to Mozilla] |
| 155 | * 2006-09 - [http://blogs.sun.com/brendan/entry/dtrace_meets_javascript DTrace meets JavaScript] |
| 156 | |
| 157 | * [http://lttng.org/ LTTng] |
| 158 | * allow tracing of the kernel, of userspace, trace viewing and analysis and trace streaming. |
| 159 | * [http://sourceware.org/systemtap/wiki/SystemtapDtraceComparison SystemTap / DTrace / LTTng 比較一覽表] |
| 160 | |
| 161 | * Ftrace |
| 162 | * [http://www.linuxhq.com/kernel/v2.6/29/Documentation/ftrace.txt Documentation/ftrace.txt] @ Kernel 2.6.29 |
| 163 | * [http://lwn.net/Articles/322666/ A look at ftrace] |
| 164 | |
| 165 | * strace - 還算常用的 DEBUG 工具 |
| 166 | * [http://www.ibm.com/developerworks/cn/linux/l-tsl/ 這篇提到還有 ltrace, truss 兩種工具] |
| 167 | * strace (1): lists all system calls used - [http://packages.debian.org/strace Debian 套件 - strace] |
| 168 | * ltrace (1): lists all shared library calls - [http://packages.debian.org/ltrace Debian 套件 - ltrace] |
| 169 | * truss (1): trace system calls and signals - |
| 170 | * 【註】出於安全方面的考慮,在 FreeBSD 5 之後系統預設已經不再自動掛載 proc 檔案系統,因此,要想使用 truss 或 strace 跟蹤程序,你必須手工掛載 proc 檔案系統:mount -t procfs proc /proc;ltrace 不需要使用 procfs。 |
| 171 | * 較少聽過的: |
| 172 | * [http://people.redhat.com/jolsa/latrace/index.shtml latrace] - LD_AUDIT 2.4+ libc frontend |
| 173 | |
| 174 | * ptrace() - process trace |
| 175 | * 2008-03 : jserv - [http://blog.linux.org.tw/~jserv/archives/002027.html 以 ptrace 系統呼叫來追蹤/修改行程] |
| 176 | * 2002-11 : [http://www.linuxjournal.com/article/6100 Playing with ptrace, Part I] |
| 177 | * 2002-12 : [http://www.linuxjournal.com/article/6210 Playing with ptrace, Part II] |
| 178 | |
| 179 | == Code Peer Review == |
| 180 | |
| 181 | * http://groogle.sourceforge.net/ |
| 182 | * http://trac-hacks.org/wiki/PeerReviewPlugin |