<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]运维利器：万能的strace,使用strace追踪多个进程，如何使用strace+pstack利器分析程序性能，]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Wed, 25 May 2016 03:30:28 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：运维这个活其实是系统偏重的活，鄙人一直没做过运维，干了不到一个月，感觉发现其实也有点打杂的感觉，但都是围绕系统、权限、分配多机器、调度、维修、协调、沟通，特别是strace在启动一些服务时启动不了，用它就能很好的定位问题在哪儿。<br/>下面这个主要是讲常用 的一个命令，如下：<br/>运维利器：万能的strace:（批注：这个哥们写得好，赞一个：跟踪进程启动，跟踪命令的执行，根据进程号跟踪，启动时都访问了哪些文件。）<br/>http://www.yunweipai.com/archives/7334.html?utm_source=tuicool&amp;utm_medium=referral<br/><br/>1)跟踪进程启动,主要是系统调用:<br/>strace -tt -f&nbsp;&nbsp;./some_server ../conf/some_server.conf<br/><br/>2)通过它启动要跟踪的进程：<br/>strace ls -lh /var/log/messages<br/><br/>3）在运行的some_server服务：<br/>pidof some_server<br/>17553<br/><br/>得到其pid 17553然后就可以用strace跟踪其执行:<br/>strace -p 17553<br/><br/>strace常用选项：<br/>从一个示例命令来看：<br/>strace -tt -T -v -f -e trace=file -o /data/log/strace.log -s 1024 -p 23489<br/><br/>-tt 在每行输出的前面，显示毫秒级别的时间<br/>-T 显示每次系统调用所花费的时间<br/>-v 对于某些相关调用，把完整的环境变量，文件stat结构等打出来。<br/>-f 跟踪目标进程，以及目标进程创建的所有子进程<br/>-e 控制要跟踪的事件和跟踪行为,比如指定要跟踪的系统调用名称<br/>-o 把strace的输出单独写到指定的文件<br/>-s 当系统调用的某个参数是字符串时，最多输出指定长度的内容，默认是32个字节<br/>-p 指定要跟踪的进程pid, 要同时跟踪多个pid, 重复多次-p选项即可。<br/><br/><br/>实例：跟踪nginx, 看其启动时都访问了哪些文件<br/>strace -tt -T -f -e trace=file -o /data/log/strace.log -s 1024 ./nginx<br/><br/><br/><br/>附录：<br/>使用strace追踪多个进程：<br/>http://www.ttlsa.com/tools/use-strace-to-track-multiple-processes/<br/><br/>如何使用strace+pstack利器分析程序性能:<br/>http://www.cnblogs.com/bangerlee/archive/2012/04/30/2476190.html<br/>strace_pstack]# gcc server.c -g -o server<br/>strace_pstack]# gcc client.c -g -o client<br/>[root@iZ25dcp92ckZ strace_pstack]# ./server <br/>listening...<br/>hello<br/>hello<br/>hello<br/>hello<br/><br/>[root@iZ25dcp92ckZ strace_pstack]# ./client<br/><br/> ps -elf &#124; grep server &#124; grep -v grep<br/>0 S root&nbsp;&nbsp;&nbsp;&nbsp; 2327417130&nbsp;&nbsp;0&nbsp;&nbsp;80&nbsp;&nbsp; 0 -&nbsp;&nbsp;1041 hrtime 14:58 pts/1&nbsp;&nbsp;&nbsp;&nbsp;00:00:00 ./server<br/>[root@iZ25dcp92ckZ strace_pstack]# strace -o server.strace -Ttt -p 23274<br/>Process 23274 attached<br/>能看到生成的server.strace 脚本里时间相差1;s不对，nanosleep：<br/><textarea name="code" class="php" rows="15" cols="100">
15:07:10.761597 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0 &lt;0.000009&gt;
15:07:10.761632 nanosleep(&#123;1, 0&#125;, 0x7fff08d80360) = 0 &lt;1.000091&gt;
15:07:11.761768 sendto(4, &quot;hello&#92;0&quot;, 6, 0, NULL, 0) = 6 &lt;0.000065&gt;
15:07:11.761881 select(8, [3 4], NULL, NULL, &#123;1, 0&#125;) = 1 (in [4], left &#123;0, 761&#125;) &lt;0.999266&gt;
15:07:12.761221 recvfrom(4, &quot;hello&quot;, 6, 0, NULL, NULL) = 5 &lt;0.000012&gt;
</textarea><br/><br/>用pstack脚本找到是哪儿调用了nanosleep系统函数导致慢了1s：<br/>[root@iZ25dcp92ckZ strace_pstack]# sh pstack.sh 23274<br/>#0&nbsp;&nbsp;0x00007f75e5bec480 in __nanosleep_nocancel () from /lib64/libc.so.6<br/>#1&nbsp;&nbsp;0x00007f75e5bec334 in sleep () from /lib64/libc.so.6<br/>#2&nbsp;&nbsp;0x0000000000400813 in ha_ha ()<br/>#3&nbsp;&nbsp;0x0000000000400aea in main ()<br/><br/><br/>[root@iZ25dcp92ckZ strace_pstack]# vi pstack.sh <br/><textarea name="code" class="php" rows="15" cols="100">
#!/bin/sh
 
if test $# -ne 1; then
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;Usage: `basename $0 .sh` &lt;process-id&gt;&quot; 1&gt;&amp;2
&nbsp;&nbsp;&nbsp;&nbsp;exit 1
fi
 
if test ! -r /proc/$1; then
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;Process $1 not found.&quot; 1&gt;&amp;2
&nbsp;&nbsp;&nbsp;&nbsp;exit 1
fi
 
# GDB doesn&#039;t allow &quot;thread apply all bt&quot; when the process isn&#039;t
# threaded; need to peek at the process to determine if that or the
# simpler &quot;bt&quot; should be used.
 
backtrace=&quot;bt&quot;
if test -d /proc/$1/task ; then
&nbsp;&nbsp;&nbsp;&nbsp;# Newer kernel; has a task/ directory.
&nbsp;&nbsp;&nbsp;&nbsp;if test `/bin/ls /proc/$1/task &#124; /usr/bin/wc -l` -gt 1 2&gt;/dev/null ; then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backtrace=&quot;thread apply all bt&quot;
&nbsp;&nbsp;&nbsp;&nbsp;fi
elif test -f /proc/$1/maps ; then
&nbsp;&nbsp;&nbsp;&nbsp;# Older kernel; go by it loading libpthread.
&nbsp;&nbsp;&nbsp;&nbsp;if /bin/grep -e libpthread /proc/$1/maps &gt; /dev/null 2&gt;&amp;1 ; then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backtrace=&quot;thread apply all bt&quot;
&nbsp;&nbsp;&nbsp;&nbsp;fi
fi
 
GDB=$&#123;GDB:-/usr/bin/gdb&#125;
 
if $GDB -nx --quiet --batch --readnever &gt; /dev/null 2&gt;&amp;1; then
&nbsp;&nbsp;&nbsp;&nbsp;readnever=--readnever
else
&nbsp;&nbsp;&nbsp;&nbsp;readnever=
fi
 
# Run GDB, strip out unwanted noise.
$GDB --quiet $readnever -nx /proc/$1/exe $1 &lt;&lt;EOF 2&gt;&amp;1 &#124;
$backtrace
EOF
/bin/sed -n &#92;
&nbsp;&nbsp;&nbsp;&nbsp;-e &#039;s/^(gdb) //&#039; &#92;
&nbsp;&nbsp;&nbsp;&nbsp;-e &#039;/^#/p&#039; &#92;
&nbsp;&nbsp;&nbsp;&nbsp;-e &#039;/^Thread/p&#039;


</textarea>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]运维利器：万能的strace,使用strace追踪多个进程，如何使用strace+pstack利器分析程序性能，]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>