Linux

如何使用性能跟踪記錄?

  • March 21, 2022

我可以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])

你的預感是正確的,perf trace record沒有記錄足夠的數據;man perf-trace建議它自己處理它,但您需要記錄系統呼叫:

perf trace record -e 'raw_syscalls:*' ...

然後

perf trace -i perf.data

將按您的預期工作。

引用自:https://unix.stackexchange.com/questions/696280