转载请注明出处:使用strace追踪多个进程 http://www.ttlsa.com/html/1841.html

http://blog.itpub.net/26250550/viewspace-1339818/

strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的系统信息。追踪程序运行时的整个生命周期,输出每一个系统调用的名字,参数,返回值和执行消耗的时间等。

strace常用参数:

-p 跟踪指定的进程

-f 跟踪由fork子进程系统调用

-F 尝试跟踪vfork子进程系统调吸入,与-f同时出现时, vfork不被跟踪

-o filename 默认strace将结果输出到stdout。通过-o可以将输出写入到filename文件中

-ff 常与-o选项一起使用,不同进程(子进程)产生的系统调用输出到filename.PID文件

-r 打印每一个系统调用的相对时间

-t 在输出中的每一行前加上时间信息。

-tt 时间确定到微秒级。还可以使用-ttt打印相对时间 -v 输出所有系统调用。默认情况下,一些频繁调用的系统调用不会输出

-s 指定每一行输出字符串的长度,默认是32。文件名一直全部输出 -c 统计每种系统调用所执行的时间,调用次数,出错次数。

-e expr 输出过滤器,通过表达式,可以过滤出掉你不想要输出

1. strace追踪多个进程方法: 当有多个子进程的情况下,比如php-fpm、nginx等,用strace追踪显得很不方便。可以使用下面的方法来追踪所有的子进程。

# vim /root/.bashrc //添加以下内容
function straceall {
strace $(pidof "${1}" | sed 's/\([0-9]*\)/-p \1/g')
}
# source /root/.bashrc
1
2
3
4
5
# vim /root/.bashrc //添加以下内容
function straceall {
strace $(pidof "${1}" | sed 's/\([0-9]*\)/-p \1/g')
}
# source /root/.bashrc

执行:

# traceall php-fpm
1
# traceall php-fpm

2. 追踪web服务器系统调用情况

# strace -f -F -s 1024 -o nginx-strace /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# strace -f -F -o php-fpm-strace /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.conf
1
2
# strace -f -F -s 1024 -o nginx-strace /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# strace -f -F -o php-fpm-strace /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.conf

3. 追踪mysql执行语句

# strace -f -F -ff -o mysqld-strace -s 1024 -p mysql_pid
# find ./ -name "mysqld-strace*" -type f -print |xargs grep -n "SELECT.*FROM"
1
2
# strace -f -F -ff -o mysqld-strace -s 1024 -p mysql_pid
# find ./ -name "mysqld-strace*" -type f -print |xargs grep -n "SELECT.*FROM"

4. whatisdong---查看程序在干啥

#!/bin/bash
# This script is from http://poormansprofiler.org/
nsamples=1
sleeptime=0
pid=$(pidof $1)

for x in $(seq 1 $nsamples)
do
gdb -ex "set pagination 0" -ex "thread apply all bt" -batch -p $pid
sleep $sleeptime
done | \
awk '
BEGIN { s = ""; }
/^Thread/ { print s; s = ""; }
/^\#/ { if (s != "" ) { s = s "," $4} else { s = $4 } }
END { print s }' | \
sort | uniq -c | sort -r -n -k 1,1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# This script is from http://poormansprofiler.org/
nsamples=1
sleeptime=0
pid=$(pidof $1)
 
for x in $(seq 1 $nsamples)
  do
    gdb -ex "set pagination 0" -ex "thread apply all bt" -batch -p $pid
    sleep $sleeptime
  done | \
awk '
  BEGIN { s = ""; }
  /^Thread/ { print s; s = ""; }
  /^\#/ { if (s != "" ) { s = s "," $4} else { s = $4 } }
  END { print s }' | \
sort | uniq -c | sort -r -n -k 1,1

输出:

# profiler.sh mysqld
727 pthread_cond_wait@@GLIBC_2.3.2,cache_thread,put_in_cache=true),handle_one_connection,start_thread,??
4 pthread_cond_wait@@GLIBC_2.3.2,os_event_wait_low,os_aio_simulated_handle,fil_aio_wait,io_handler_thread,start_thread,??
4 ??,??
2 read,my_real_read,my_net_read,do_command,handle_one_connection,start_thread,??
1 pthread_cond_wait@@GLIBC_2.3.2,os_event_wait_low,srv_master_thread,start_thread,??
1 pthread_cond_wait@@GLIBC_2.3.2,MYSQL_BIN_LOG::wait_for_update,mysql_binlog_send,dispatch_command,do_command,handle_one_connection,start_thread,??
1 do_sigwait,sigwait,signal_hand,start_thread,??
1
1
2
3
4
5
6
7
8
9
# profiler.sh mysqld
727 pthread_cond_wait@@GLIBC_2.3.2,cache_thread,put_in_cache=true),handle_one_connection,start_thread,??
  4 pthread_cond_wait@@GLIBC_2.3.2,os_event_wait_low,os_aio_simulated_handle,fil_aio_wait,io_handler_thread,start_thread,??
  4 ??,??
  2 read,my_real_read,my_net_read,do_command,handle_one_connection,start_thread,??
  1 pthread_cond_wait@@GLIBC_2.3.2,os_event_wait_low,srv_master_thread,start_thread,??
  1 pthread_cond_wait@@GLIBC_2.3.2,MYSQL_BIN_LOG::wait_for_update,mysql_binlog_send,dispatch_command,do_command,handle_one_connection,start_thread,??
  1 do_sigwait,sigwait,signal_hand,start_thread,??
  1

linux strace追踪mysql执行语句 (mysqld --debug)的更多相关文章

  1. strace追踪mysql执行语句

    一.strace参数 strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的系统信息.追踪程序运行时的整个生命周期,输出每一个系统调用的名字,参数,返回值和 ...

  2. 8.mysql执行语句的顺序

    mysql执行语句的顺序     一.group by + where group by 字句和where条件语句结合在一起使用,where在前,group by 在后.即先对select xx fr ...

  3. MySQL执行语句的顺序

    MySQL的语句一共分为11步,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入,只是这些虚拟的表对用户来说是透明的,但是只有 ...

  4. MySql 执行语句错误 Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

    关于用Power Designer 生成sql文件出现 错误  [Err] 1064 - You have an error in your SQL syntax; check the manual ...

  5. mysql 执行语句

    连接数据库: $con = mysql_connect(服务器地址,用户名,密码): 选择数据库: $select = mysql_select_db(数据库名称); $select = mysql_ ...

  6. Linux中连接mysql执行sql文件

    数据量小的时候可以把sql语句内容粘贴执行,但是文件很大的时候,这样执行效率很慢很慢,需要使用source执行sql文件 1.客户端连接mysql数据库 [root@iZbp1bb2egi7w0uey ...

  7. mysql执行语句提示Table 'performance_schema.session_variables' doesn't exist

    用管理员身份cmd进入mysql安装目录bin里,执行 mysql_upgrade -u root -p 如果杀毒软件拦截,添加为信任区

  8. Linux strace追踪命令详解

    strace介绍 strace命令是一个集诊断.调试.统计与一体的工具,我们可以使用strace对应用的系统调用和信号传递的跟踪结果来对应用进行分析,以达到解决问题或者是了解应用工作过程的目的.当然s ...

  9. mysql执行语句汇总

    插入select的数据 INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, ...

随机推荐

  1. python基础学习之路No.5 数学函数以及操作

    python的基本数学函数 函数 返回值 ( 描述 ) abs(x) 返回数字的绝对值,如abs(-10) 返回 10 ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5 c ...

  2. Selenium_多线程执行测试用例

    多线程执行测试用例 这里以百度搜索为例,通过不同的浏览器来启动不同的线程. #!/usr/bin/env python # _*_ coding:utf-8 _*_ __author__ = 'Yin ...

  3. DevExpress CxGrid 隐藏 Drag a column header to group by that column

  4. malloc和free的实现

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  5. C++ 必须使用初始化列表

    继承关系中,父类无默认构造函数 类类型类成员变量无默认构造函数 const类型成员变量 引用类型成员变量 不使用初始化列表,在创建对象调用构造函数之前会对所有的成员变量进行默认初始化,然后再执行构造函 ...

  6. xcode7 创建pch文件

    1.打开xcode 7.2 项目,在屏幕顶端的工具栏,选择File>New>File..>iOS>Other>PCH File,点击"next"下一步 ...

  7. java8 - 多线程时间安全问题

    import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeForma ...

  8. Sublime Text安装SVN插件

    下载插件 Sublime Text2/3 SVN插件 点击下载 安装插件 点击设置(Preferences)->浏览程序包(Browse Packages,,,),新建TortoiseSVN文件 ...

  9. jquery选择里存在特殊字符,需要加双转义字符

    //元素为:<input type="checkbox" value="abc/index" /> //处理选择器转义问题 //去除值 $val = ...

  10. Adapter 适配器

    意图 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 动机 在软件开发中,有的时候系统的数据和行为都正确,但接口不符合,我们应 ...