我是靠谱客的博主 贪玩海燕,这篇文章主要介绍Linux 下 进程运行时内部函数耗时的统计 工具:pstack,strace,perf trace,systemtap,现在分享给大家,希望可以做个参考。

简单记录一些 在linux下 统计进程内部函数运行耗时的统计工具,主要是用作性能瓶颈分析。当然以下工具除了pstack功能单一之外,其他的工具都非常强大,这里仅仅整理特定场景的特定用法,用作协同分析。

以下工具需要追踪具体的进程,如果想要打印信息更全,建议编译的时候将符号信息都编译到二进制文件之中,-g选项

  • strace
    strace -tttT -f -p $pid -o $save_file_name 追踪指定进程内部所有线程调用到的系统调用运行耗时,单位是秒,将统计结果保存到save_file_name的文件之中

    复制代码
    1
    2
    3
    4
    5
    6
    7
    [pid 35467] 1596874136.770976 <... clock_gettime resumed> {1479879, 802163984}) = 0 <0.000293> [pid 35466] 1596874136.770989 <... futex resumed> ) = 0 <0.000290> [pid 35462] 1596874136.770998 <... futex resumed> ) = 1 <0.000289> [pid 35460] 1596874136.771007 <... sched_yield resumed> ) = 0 <0.000286> [pid 35457] 1596874136.771016 <... clock_gettime resumed> {1479879, 802216718}) = 0 <0.000280> [pid 35454] 1596874136.771025 <... sched_yield resumed> ) = 0 <0.000275>
  • pstack
    pstack $pid 追踪正在运行的进程的调用栈

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Thread 445 (Thread 0x7f17e35fe700 (LWP 33120)): #0 0x00007f18638be945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0 #1 0x00007f18640a0cbc in __gthread_cond_wait (__mutex=<optimized out>, __cond=__cond@entry=0x13af91c8) at gthr-default.h:864 #2 std::condition_variable::wait (this=this@entry=0x13af91c8, __lock=...) at condition_variable.cc:53 #3 0x000000000065c21f in rocksdb::ThreadPoolImpl::Impl::BGThread (this=this@entry=0x13af9130, thread_id=thread_id@entry=29) at util/threadpool_imp.cc:196 #4 0x000000000065c5c5 in rocksdb::ThreadPoolImpl::Impl::BGThreadWrapper (arg=0x13afbaf0) at util/threadpool_imp.cc:306 #5 0x00007f18640a6f20 in execute_native_thread_routine_compat () at thread.cc:94 #6 0x00007f18638bae25 in start_thread () from /lib64/libpthread.so.0 #7 0x00007f18635e834d in clone () from /lib64/libc.so.6
  • perf trace
    sudo perf trace -p $pid --duration 50 --call-graph dwarf -o $save_file_name
    统计运行耗时超过50ms的系统调用,并打印该系统调用的calltrace,并将打印的结果保存到save_file_name的文件之中,一般用于追踪IO性能问题

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    563.060 (61.970 ms): rocksdb:high0/33123 fdatasync(fd: 8396 ) = 0 [0xffff80e79cbff9dd] (/usr/lib64/libc-2.17.so) rocksdb::PosixWritableFile::Sync (test) rocksdb::WritableFileWriter::SyncInternal (test) rocksdb::WritableFileWriter::Sync (test) rocksdb::BuildTable (test) rocksdb::FlushJob::WriteLevel0Table (test) rocksdb::FlushJob::Run (test)
  • systemtap
    这本身是一个非常强大的黑科技工具,除了内核态的调试之外,拥有符号表的用户进程也能够进行调试
    如下stap脚本trace_function_time,抓取运行时进程内部指定函数的耗时情况,并将耗时结果打印出来

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #!/bin/stap global sends #打印出来的单位是微妙 probe process("test").function("rocksdb::DBImpl::GetImpl").return { sends <<< gettimeofday_us() - @entry(gettimeofday_us()) } probe timer.s(1) { #每隔一秒打印一次 print(ctime(gettimeofday_s())) print("n") print(@hist_log(sends)) delete sends }

    需要sudo权限,直接sudo ./trace_function_time即可

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Fri Aug 7 03:24:18 2020 value |-------------------------------------------------- count 0 | 0 1 | 0 2 | 182 4 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 21280 8 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 14666 16 |@@@@@@@@@@@@@@@ 6518 32 |@@@@@@@@@@ 4608 64 |@@@@@@@@ 3421 128 |@@@@@ 2148 256 |@@ 923 512 | 361 1024 | 194 2048 | 75 4096 | 30 8192 | 0 16384 | 0

    打印的一秒内,统计处于各个微妙时间段内的请求个数

最后

以上就是贪玩海燕最近收集整理的关于Linux 下 进程运行时内部函数耗时的统计 工具:pstack,strace,perf trace,systemtap的全部内容,更多相关Linux内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部