PostgreSQL SystemTap on Linux

來源:互聯網
上載者:User

標籤:while   linux   小結   .sh   bug   lock   already   thread   rup   

http://digoal126.wap.blog.163.com/w2/blogDetail.do;jsessionid=3949B03DE151DA0E55D807466C5E630B.yqblog35-8010?blogId=fks_087064087086083071087082094069072087087064092081081067080086&showRest=true&p=2&[email protected]


德哥@Digoal 2013-08-14 15:57

這個錯誤的原因是庫檔案版本不正確, 用來老版本, 使用eu-readelf查看stap檔案的兩個環境變數, 如下 : 

 

[[email protected] ~]# eu-readelf -d /usr/bin/stap|grep -E "RPATH|RUNPATH"  RPATH             Library rpath: [/usr/lib64/systemtap]  RUNPATH           Library runpath: [/usr/lib64/systemtap]將路徑加入到LD_LIBRARY_PATH中.

 

[[email protected] ~]# export LD_LIBRARY_PATH=/usr/lib64/systemtap:$LD_LIBRARY_PATHstap現在正常了

 

[[email protected] ~]# stap A script must be specified.Systemtap translator/driver (version 1.8/0.152 non-git sources)Copyright (C) 2005-2012 Red Hat, Inc. and othersThis is free software; see the source for copying conditions.enabled features: AVAHI LIBRPM LIBSQLITE3 NSS BOOST_SHARED_PTR TR1_UNORDERED_MAP NLSUsage: stap [options] FILE         Run script in file.   or: stap [options] -            Run script on stdin.   or: stap [options] -e SCRIPT    Run given script.   or: stap [options] -l PROBE     List matching probes.   or: stap [options] -L PROBE     List matching probes and local variables.測試 : 

 

[[email protected] pg94]# vi tps.d probe begin{  printf("hello\n")  exit()}[[email protected] pg94]# stap tps.d Checking "/lib/modules/2.6.18-274.el5/build/.config" failed with error: No such file or directoryIncorrect version or missing kernel-devel package, use: yum install kernel-devel-2.6.18-274.el5.x86_64 這個錯誤是由於未安裝當前正在啟動並執行kernel對應的kernel-devel包.

 

[[email protected] pg94]# rpm -qa|grep kernelkernel-headers-2.6.18-274.el5kernel-xen-devel-2.6.18-274.el5kernel-xen-2.6.18-274.el5kernel-2.6.18-274.el5[[email protected] ~]# uname -aLinux db-172-16-3-39.sky-mobi.com 2.6.18-274.el5 #1 SMP Fri Jul 22 04:43:29 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux 安裝對應的kernel-devel版本.

 

yum install kernel-devel-2.6.18-274.el5.x86_64如果yum源中沒有這個版本的kernel-devel包, 可以去安裝光碟片中找一找.或者同時更新核心版本至新版本.

 

 yum install -y kernel.x86_64 kernel-devel.x86_64Package kernel-2.6.18-348.12.1.el5.x86_64 already installed and latest versionPackage kernel-devel-2.6.18-348.12.1.el5.x86_64 already installed and latest versionvi /boot/grub/grub.confdefault=0timeout=5splashimage=(hd0,0)/boot/grub/splash.xpm.gzhiddenmenutitle CentOS (2.6.18-348.12.1.el5)        root (hd0,0)        kernel /boot/vmlinuz-2.6.18-348.12.1.el5 ro root=LABEL=/ rhgb quiet        initrd /boot/initrd-2.6.18-348.12.1.el5.img重啟伺服器現在stap工作正常了 : 

 

[[email protected] postgresql-5e3e8e4]# stap -ve ‘probe begin { log("hello world") exit() }‘
Pass 1: parsed user script and 85 library script(s) using 146788virt/23676res/3000shr/21384data kb, in 170usr/0sys/173real ms.
Pass 2: analyzed script: 1 probe(s), 2 function(s), 0 embed(s), 0 global(s) using 147316virt/24396res/3224shr/21912data kb, in 10usr/0sys/6real ms.
Pass 3: using cached /root/.systemtap/cache/3b/stap_3b2eaec778ce9832b394535505dde575_838.c

Pass 4: using cached /root/.systemtap/cache/3b/stap_3b2eaec778ce9832b394535505dde575_838.ko
Pass 5: starting run.
hello world
Pass 5: run completed in 0usr/20sys/306real ms.

 

stap調試好後, 就可以用來跟蹤postgresql了.PostgreSQL編譯時間必須開啟dtrace支援. 開啟dtrace後, 資料庫將啟用代碼中的探針或跟蹤點.PostgreSQL內建探針參考如下 : src/backend/utils/probes.dhttp://www.postgresql.org/docs/devel/static/dynamic-trace.html檢查你的PostgreSQL是否開啟了dtrace支援, 如下 : 

 

[email protected]> pg_config --configure‘--prefix=/home/pg94/pgsql9.4devel‘ ‘--with-pgport=2999‘ ‘--with-perl‘ ‘--with-tcl‘ ‘--with-python‘ ‘--with-openssl‘ ‘--with-pam‘ ‘--without-ldap‘ ‘--with-libxml‘ ‘--with-libxslt‘ ‘--enable-thread-safety‘ ‘--with-wal-blocksize=16‘ ‘--enable-dtrace‘如果沒有--enable-dtrace, 那麼需要重新編譯一下你的PostgreSQL軟體. stap測試指令碼1.

 

[[email protected] pg94]# cat postgresql-query.stp global query_time, query_summary probe process("/home/pg94/pgsql9.4devel/bin/postgres").mark("query__start") {  query_time[tid(), $arg1] = gettimeofday_us();} probe process("/home/pg94/pgsql9.4devel/bin/postgres").mark("query__done") {  p = tid()  t = query_time[p, $arg1]; delete query_time[p, $arg1]  if (t) {    query_summary[p] <<< (gettimeofday_us() - t);  }} probe end {  printf("\ntid count min(us) avg(us) max(us)\n");  foreach (p in query_summary) {    printf("%d %d %d %d %d\n", p, @count(query_summary[p]),     @min(query_summary[p]), @avg(query_summary[p]), @max(query_summary[p]));  }}

 

執行stap : 

 

[[email protected] pg94]# stap postgresql-query.stp 執行以下SQL : 

 

[[email protected] pg94]# stap postgresql-query.stp digoal=# begin;BEGINdigoal=# select * from test for update; id ----  1  2  3  4  5  6  7  8  9 10(10 rows)digoal=# end;COMMITdigoal=# select txid_current(); txid_current --------------      5969062(1 row)結束stap, 輸出 : 

 

[[email protected] pg94]# stap postgresql-query.stp 按鍵Ctrl+C, 輸出 : tid count min(us) avg(us) max(us)17112 4 46 3794 14885這個tid對應PostgreSQL background process

 

[[email protected] pg94]# ps -ewf|grep 17112pg94     17112 17005  0 15:15 ?        00:00:00 postgres: postgres digoal [local] idledigoal=# select pg_backend_pid(); pg_backend_pid ----------------          17112(1 row) [小結]1. 安裝systemtap注意 : -- 需要安裝kernel相關, 並且版本要一致, 啟動的kernel版本也要一致 :  kernelkernel-debuginfokernel-devel

 

kernel-debuginfo的安裝要用到debug源.

 

[[email protected] pg94]# cd /etc/yum.repos.d/[[email protected] yum.repos.d]# lltotal 36-rw-r--r-- 1 root root 1926 Aug 29  2011 CentOS-Base.repo-rw-r--r-- 1 root root  631 Aug 29  2011 CentOS-Debuginfo.repo-rw-r--r-- 1 root root  626 Aug 29  2011 CentOS-Media.repo-rw-r--r-- 1 root root 5390 Aug 29  2011 CentOS-Vault.repo[[email protected] yum.repos.d]# cat CentOS-Debuginfo.repo # CentOS-Base.repo## The mirror system uses the connecting IP address of the client and the# update status of each mirror to pick mirrors that are updated to and# geographically close to the client.  You should use this for CentOS updates# unless you are manually picking other mirrors.# # All debug packages from all the various CentOS-5 releases# are merged into a single repo, split by BaseArch## Note: packages in the debuginfo repo are currently not signed# [debug]name=CentOS-5 - Debuginfobaseurl=http://debuginfo.centos.org/5/$basearch/gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5enabled=0

 

這裡用到的源名為debug.

 

yum --enablerepo=debug list kernel-debuginfoyum --enablerepo=debug install kernel-debuginfo安裝細節參考此文 : http://pic.dhe.ibm.com/infocenter/lnxinfo/v3r0m0/topic/liaai.systemTap/liaaisystap_pdf.pdf 2. postgresql編譯時間必須加上--enable-dtrace參數, 否則stap時會出現類似以下錯誤.

 

[[email protected] pg93]# stap test.stp semantic error: while resolving probe point: identifier ‘process‘ at test.stp:3:7        source: probe process("/opt/pgsql9.3beta2/bin/postgres").mark("lock__wait__start")                      ^ semantic error: no matchsemantic error: while resolving probe point: identifier ‘process‘ at :8:7        source: probe process("/opt/pgsql9.3beta2/bin/postgres").mark("lock__wait__done")                      ^ semantic error: while resolving probe point: identifier ‘process‘ at :17:7        source: probe process("/opt/pgsql9.3beta2/bin/postgres").mark("lwlock__wait__start")                      ^ semantic error: while resolving probe point: identifier ‘process‘ at :22:7        source: probe process("/opt/pgsql9.3beta2/bin/postgres").mark("lwlock__wait__done")                      ^ semantic error: while resolving probe point: identifier ‘process‘ at :32:7        source: probe process("/opt/pgsql9.3beta2/bin/postgres").mark("lwlock__condacquire__fail")                      ^ Pass 2: analysis failed.  Try again with another ‘--vp 01‘ option.

 

3. 允許stap需要root許可權, 或者將使用者加入stapdev或者stapsys, 以及stapusr組.如果普通使用者不加入這兩個組, 執行stap將報錯 : 

 

[email protected]> stap -ve ‘probe begin { log("hello world") exit() }‘You are trying to run systemtap as a normal user.You should either be root, or be part of the group "stapusr" and possibly one of the groups "stapsys" or "stapdev".Systemtap translator/driver (version 1.8/0.152 non-git sources)加完組後正常 # usermod -G stapdev,stapusr pg94[email protected]> stap -ve ‘probe begin { log("hello world") exit() }‘Pass 1: parsed user script and 85 library script(s) using 148892virt/23772res/3068shr/21396data kb, in 160usr/10sys/172real ms.Pass 2: analyzed script: 1 probe(s), 2 function(s), 0 embed(s), 0 global(s) using 149420virt/24492res/3292shr/21924data kb, in 10usr/0sys/6real ms.Pass 3: translated to C into "/tmp/stapcqtmUe/stap_758dbd41826239e5e3211a815f6bfc58_838_src.c" using 149420virt/24760res/3540shr/21924data kb, in 0usr/0sys/0real ms.Pass 4: compiled C into "stap_758dbd41826239e5e3211a815f6bfc58_838.ko" in 910usr/110sys/1028real ms.Pass 5: starting run.hello worldPass 5: run completed in 10usr/20sys/307real ms.

 

本文就介紹到這裡, 下次將介紹如何使用postgresql的探針. 

[參考]

1. http://pgfoundry.org/projects/dtrace/2. http://www.fosslc.org/drupal/content/probing-postgresql-dtrace-and-systemtap3. http://www.postgresql.org/docs/devel/static/dynamic-trace.html4. http://www.postgresql.org/docs/devel/static/install-procedure.html5. http://www.ppurl.com/2011/05/dtrace-dynamic-tracing-in-oracle-solaris-mac-os-x-and-freebsd.html6. http://www.emm.usp.br/downloads/pg/PG_perf_bootcamp.pdf7. https://wiki.postgresql.org/wiki/DTrace8. http://www.ppurl.com/?s=systemtap9. http://www.ibm.com/developerworks/cn/linux/l-systemtap/10. http://sourceware.org/systemtap/wiki/HomePage11. http://fruli.krunch.be/~krunch/systemtap-osdcfr-20101010.pdf12. http://blog.endpoint.com/2009/05/postgresql-with-systemtap.html13. https://www.evernote.com/shard/s48/sh/1ccb0466-79b7-4090-9a5d-9371358ac54d/b8434e3e3b3130ce72422b9ae067e7b914. http://pic.dhe.ibm.com/infocenter/lnxinfo/v3r0m0/topic/liaai.systemTap/liaaisystap_pdf.pdf15. http://linux.chinaunix.net/techdoc/develop/2008/12/28/1055546.shtml16. http://os.51cto.com/art/201305/395819.htm

17. https://access.redhat.com/site/documentation/Red_Hat_Enterprise_Linux/?locale=en-US

PostgreSQL SystemTap on Linux

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.