我可以用作perf trace
的低开销替代品strace
,例如跟踪所有 Apache 实例:
perf trace -p $(pidof apache2 | tr ' ' ',')
仅运行跟踪最多 10 秒:
perf trace -p $(pidof apache2 | tr ' ' ',') -- sleep 10
一些示例输出:
server ~ # perf trace -p $(pidof apache2 | tr ' ' ',') -- sleep 10 2>&1 | head
? ( ): apache2/8661 ... [continued]: poll()) = 0 Timeout
0.022 ( 0.005 ms): apache2/8661 close(fd: 28 ) = 0
0.066 ( 0.007 ms): apache2/8661 read(fd: 13<pipe:[3452760950]>, buf: 0x7ffe815038ff, count: 1 ) = -1 EAGAIN Resource temporarily unavailable
? ( ): apache2/26492 ... [continued]: semop()) = 0
0.088 ( ): apache2/8661 semop(semid: 557481986, tsops: 0x7f846e0cfd6c, nsops: 1 ) ...
? ( ): apache2/7580 ... [continued]: epoll_wait()) = 1
46.136 ( ): apache2/26492 epoll_wait(epfd: 27<anon_inode:[eventpoll]>, events: 0x7f846dd0c698, maxevents: 5, timeout: 10000) ...
46.081 ( 0.013 ms): apache2/7580 accept4(fd: 12<socket:[3452759675]>, upeer_sockaddr: 0x7ffe81503830, upeer_addrlen: 0x7ffe81503810, flags: 524288) = 28
46.100 ( 0.010 ms): apache2/7580 semop(semid: 557481986, tsops: 0x7f846e0cfd60, nsops: 1 ) = 0
46.116 ( 0.002 ms): apache2/7580 getsockname(fd: 28<socket:[3465711918]>, usockaddr: 0x7f846dd0a130, usockaddr_len: 0x7f846dd0a110) = 0
这按预期工作。现在我想将这些事件记录在一个文件中,以便以后可以详细分析它们。我预料到perf trace record
会这样做,但我什至不确定这是否正确记录:
server ~ # perf trace record -p $(pidof apache2 | tr ' ' ',') -- sleep 10
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0,312 MB perf.data (67 samples) ]
perf trace ... | wc -l
总计约 12000 行,那么为什么record
只记录 67 个样本呢?
我什至不确定读取该文件的正确命令是什么;不幸的是,手册页没有说明。我假设它是perf trace -i perf.data
,但它不会打印任何内容:
server ~ # perf trace -i perf.data
server ~ #
perf script
确实打印了一些东西,但它看起来不像输出perf trace
:
server ~ # perf script | head
apache2 10215 [002] 29556325.787512: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 20085 [006] 29556325.787597: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 20754 [000] 29556325.790512: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 7580 [007] 29556325.790757: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 8661 [001] 29556325.796044: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 10215 [006] 29556325.796845: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 20085 [004] 29556325.798481: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 10215 [004] 29556325.802922: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 20754 [001] 29556325.815999: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
apache2 20085 [003] 29556325.816025: 1 cycles:ppp: ffffffff83e5a704 native_write_msr+0x4 ([kernel.kallsyms])
答案1
您的预感是正确的,perf trace record
没有记录足够的数据;man perf-trace
建议它自行处理,但您需要记录系统调用:
perf trace record -e 'raw_syscalls:*' ...
然后
perf trace -i perf.data
会如你所期望的那样工作。