PostgreSQL SystemTap on Linux

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_PATH

stap现在正常了

[[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 others

This is free software; see the source for copying conditions.

enabled features: AVAHI LIBRPM LIBSQLITE3 NSS BOOST_SHARED_PTR TR1_UNORDERED_MAP NLS

Usage: 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 directory

Incorrect 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 kernel

kernel-headers-2.6.18-274.el5

kernel-xen-devel-2.6.18-274.el5

kernel-xen-2.6.18-274.el5

kernel-2.6.18-274.el5

[[email protected] ~]# uname -a

Linux 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_64

Package kernel-2.6.18-348.12.1.el5.x86_64 already installed and latest version

Package kernel-devel-2.6.18-348.12.1.el5.x86_64 already installed and latest version

vi /boot/grub/grub.conf

default=0

timeout=5

splashimage=(hd0,0)/boot/grub/splash.xpm.gz

hiddenmenu

title 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.d

http://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;

BEGIN

digoal=# select * from test for update;

id

----

1

2

3

4

5

6

7

8

9

10

(10 rows)

digoal=# end;

COMMIT

digoal=# 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 17112

pg94     17112 17005  0 15:15 ?        00:00:00 postgres: postgres digoal [local] idle

digoal=# select pg_backend_pid();

pg_backend_pid

----------------

17112

(1 row)

[小结]

1. 安装systemtap注意 :

-- 需要安装kernel相关, 并且版本要一致, 启动的kernel版本也要一致 :

kernel

kernel-debuginfo

kernel-devel

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

[[email protected] pg94]# cd /etc/yum.repos.d/

[[email protected] yum.repos.d]# ll

total 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 - Debuginfo

baseurl=http://debuginfo.centos.org/5/$basearch/

gpgcheck=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5

enabled=0

这里用到的源名为debug.

yum --enablerepo=debug list kernel-debuginfo

yum --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 match

semantic 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 world

Pass 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-systemtap

3. http://www.postgresql.org/docs/devel/static/dynamic-trace.html

4. http://www.postgresql.org/docs/devel/static/install-procedure.html

5. http://www.ppurl.com/2011/05/dtrace-dynamic-tracing-in-oracle-solaris-mac-os-x-and-freebsd.html

6. http://www.emm.usp.br/downloads/pg/PG_perf_bootcamp.pdf

7. https://wiki.postgresql.org/wiki/DTrace

8. http://www.ppurl.com/?s=systemtap

9. http://www.ibm.com/developerworks/cn/linux/l-systemtap/

10. http://sourceware.org/systemtap/wiki/HomePage

11. http://fruli.krunch.be/~krunch/systemtap-osdcfr-20101010.pdf

12. http://blog.endpoint.com/2009/05/postgresql-with-systemtap.html

13. https://www.evernote.com/shard/s48/sh/1ccb0466-79b7-4090-9a5d-9371358ac54d/b8434e3e3b3130ce72422b9ae067e7b9

14. http://pic.dhe.ibm.com/infocenter/lnxinfo/v3r0m0/topic/liaai.systemTap/liaaisystap_pdf.pdf

15. http://linux.chinaunix.net/techdoc/develop/2008/12/28/1055546.shtml

16. http://os.51cto.com/art/201305/395819.htm

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

时间: 2024-12-19 22:52:20

PostgreSQL SystemTap on Linux的相关文章

PostgreSQL SystemTap on Linux 转

PostgreSQL 支持动态跟踪, 可以通过dtrace或者systemtap工具统计相关探针的信息. 安装systemtap yum install systemtap kernel-debuginfo kernel-devel 将安装以下包 systemtap-devel-1.8-6.el5 systemtap-client-1.8-6.el5 systemtap-runtime-1.8-6.el5 后面将使用的stap命令 [[email protected]-172-16-3-39 ~

ubuntu+systemtap进行Linux内核和用户空间开发测试

ubuntu+systemtap进行Linux内核和用户空间开发测试 Sailor_forever  sailing_9806#163.com (本原创文章发表于Sailor_forever 的个人blog,未经本人许可,不得用于商业用途.任何个人.媒体.其他网站不得私自抄袭:网络媒体转载请注明出处,增加原文链接,否则属于侵权行为.如有任何问题,请留言或者发邮件给sailing_9806#163.com) [摘要]本文主要介绍在ubuntu平台 + 自定义内核上如何安装systemtap工具包及

利用systemtap学习Linux路由代码

http://bbs.chinaunix.net/thread-4090162-1-1.html 一.为什么要这样做读kernel route子系统代码,当我弄懂了数据结构之间的关系以及控制流程后,心里还是不妥贴,总有一种“纸上得来终觉浅,绝知此事要躬行”的感觉.此时,systemtap能起大作用. 二.准备工作安装systemtap, kernel, kernel-debuginfo, kernel-debuginfo-common等.uname -r2.6.38.6-26.rc1.fc15.

linux mint 18.2 install postgresql

https://www.postgresql.org/download/linux/ubuntu/ 1 check Xenial16.04 2  创建文件  /etc/apt/sources.list.d/pgdg.list 添加 deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main 3 添加key wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8

linux 搭建unixODBC ,并对接 PostgreSQL 9.3.4

环境:suse 11 ,64位的操作系统 unixODBC 版本:2.3.2 PostgreSQL 9.3.4 1 编译安装 unixODBC 下载 unixODBC :http://www.unixodbc.org/download.html 解压编译 tar -zxvf unixODBC-2.3.2.tar.gz cd unixODBC-2.3.2 ./configure --prefix=/opt/unixODBC make && make install 安装路径设置在 /opt/

Linux CentOS 7 安装PostgreSQL 9.5 --步骤详解

前言 版本:  PostgreSQL 9.5 linux:CentOS7 下载在postgresql的官方即可找到源码文件目录,地址如下:https://www.postgresql.org/ftp/source/,在下载列表中根据需求选择版本,如下图: 进入子目录后,可以看到文件列表: 如上图,可以看到提供了两种压缩格式,此处我们选择postgresql-9.5.5.tar.gz,下载完成后上传至CentOS服务器的指定目录即可. 配置编译安装首先进入pg压缩包目录通过tar -zxvf ./

用systemtap对sysbench IO测试结果的分析1

http://www.actionsky.com/docs/archives/171 2016年5月6日  黄炎 近期在一些简单的sysbench IO测试中, 遇到了一些不合常识的测试结果. 从结果看, 虽然不是有实际意义的测试结果, 但探索过程中, 利用到了Systemtap进行观测(动态追踪), 可在其他分析中借用. 目录 1 测试环境准备 2 现象1 2.1 步骤1 2.2 结果1 2.3 分析1 2.4 插曲1 2.4.1 观测工具使用不当带来测试偏差 2.4.2 使用另一观测工具用于

[学] CentOS 7 安装 PostgreSQL 安装

1. 安装 1 # yum install postgresql-server 2. 初始化 1 # service postgresql initdb 3. 设置自启动 1 # chkconfig postgresql on 参考:https://www.postgresql.org/download/linux/redhat/

【转】windows和linux中搭建python集成开发环境IDE

http://blog.csdn.net/pipisorry/article/details/39854707 使用的系统及软件Ubuntu / windowsPython 2.7 / python 3Pycharm 2.6.3Openjdk Postgresql 9.1VirtualenvVirtualenvwrapper{开始之前,可以给系统做一下备份.如误安装了Postgresql,出现了大问题就不得不把系统给重装了} 安装python 安装python 1. Ubuntu 12.04系统