我怎么知道为什么我的Perl代码在内核空间花了这么多时间(顶部的“sy”)?

时间:2021-08-31 15:38:15

When I run my program "top" reports that I am spending 30+% in "sy" (kernel space).

当我运行我的程序“top”报告我在“sy”(内核空间)中花费30 +%。

How can I get more information about this? What system call is eating up all this time, etc.

我怎样才能获得更多相关信息?系统调用一直在吃什么,等等。

Thanks responders:

  • It sounds like Devel::NYTProf is good for profiling the entire program -- but I had trouble installing it on Amazon Linux.

    听起来像Devel :: NYTProf很适合分析整个程序 - 但我在亚马逊Linux上安装它时遇到了麻烦。

  • If I just want a snapshot of system calls strace seems to be great.

    如果我只想要一个系统调用快照,那么strace似乎很棒。

Updated: I did this and the time spent in system calls is actually negligible. My best guess is the threads are stuck waiting on some resource. The answers are still good below but any advice would help.

更新:我这样做了,系统调用花费的时间实际上可以忽略不计。我最好的猜测是线程等待某些资源。答案仍然很好,但任何建议都会有所帮助。

2 个解决方案

#1


10  

You could use strace(1) - trace system calls and signals. -c flag will produce aggregate results grouped by syscall. -p NUM is a PID of running process to attach. Monitoring a crawler (for instance) looks like this:

您可以使用strace(1) - 跟踪系统调用和信号。 -c标志将生成由syscall分组的聚合结果。 -p NUM是要附加的运行进程的PID。监视爬虫(例如)如下所示:

stas@crawler2:~/ScrapMe$ strace -c -p 32184                                                                                                                                                                                              
Process 32184 attached - interrupt to quit
^CProcess 32184 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 48.00    0.000024           0       128           sendto
 26.00    0.000013           0        76           write
 26.00    0.000013           0      2652      2652 stat
  0.00    0.000000           0        24           close
  0.00    0.000000           0       226           poll
  0.00    0.000000           0        78           rt_sigaction
  0.00    0.000000           0        26           rt_sigprocmask
  0.00    0.000000           0        52           alarm
  0.00    0.000000           0        26           socket
  0.00    0.000000           0        26        26 connect
  0.00    0.000000           0        75           recvfrom
  0.00    0.000000           0        26           getsockname
  0.00    0.000000           0        26           getpeername
  0.00    0.000000           0        26           getsockopt
  0.00    0.000000           0       310           fcntl
  0.00    0.000000           0        13           epoll_wait
  0.00    0.000000           0        26           epoll_ctl
------ ----------- ----------- --------- --------- ----------------
100.00    0.000050                  3816      2678 total

#2


5  

I suppose you should profile your script then - and I'd strongly recommend using Devel::NYTProf module for this case.

我想你应该编写你的脚本 - 我强烈建议在这种情况下使用Devel :: NYTProf模块。

It's quite easy to run (what can be easier than just perl -d:NYTProf yourscript.pl?), and the results of profiler's work can be as easily transformed into a full-blown report - either with the utilities included in this package - or with more 'graphicky' tool KCachegrind.

它很容易运行(比perl -d更容易:NYTProf yourscript.pl?),分析器工作的结果可以很容易地转换成一个完整的报告 - 使用此包中包含的实用程序 - 或者使用更多'graphicky'工具KCachegrind。

#1


10  

You could use strace(1) - trace system calls and signals. -c flag will produce aggregate results grouped by syscall. -p NUM is a PID of running process to attach. Monitoring a crawler (for instance) looks like this:

您可以使用strace(1) - 跟踪系统调用和信号。 -c标志将生成由syscall分组的聚合结果。 -p NUM是要附加的运行进程的PID。监视爬虫(例如)如下所示:

stas@crawler2:~/ScrapMe$ strace -c -p 32184                                                                                                                                                                                              
Process 32184 attached - interrupt to quit
^CProcess 32184 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 48.00    0.000024           0       128           sendto
 26.00    0.000013           0        76           write
 26.00    0.000013           0      2652      2652 stat
  0.00    0.000000           0        24           close
  0.00    0.000000           0       226           poll
  0.00    0.000000           0        78           rt_sigaction
  0.00    0.000000           0        26           rt_sigprocmask
  0.00    0.000000           0        52           alarm
  0.00    0.000000           0        26           socket
  0.00    0.000000           0        26        26 connect
  0.00    0.000000           0        75           recvfrom
  0.00    0.000000           0        26           getsockname
  0.00    0.000000           0        26           getpeername
  0.00    0.000000           0        26           getsockopt
  0.00    0.000000           0       310           fcntl
  0.00    0.000000           0        13           epoll_wait
  0.00    0.000000           0        26           epoll_ctl
------ ----------- ----------- --------- --------- ----------------
100.00    0.000050                  3816      2678 total

#2


5  

I suppose you should profile your script then - and I'd strongly recommend using Devel::NYTProf module for this case.

我想你应该编写你的脚本 - 我强烈建议在这种情况下使用Devel :: NYTProf模块。

It's quite easy to run (what can be easier than just perl -d:NYTProf yourscript.pl?), and the results of profiler's work can be as easily transformed into a full-blown report - either with the utilities included in this package - or with more 'graphicky' tool KCachegrind.

它很容易运行(比perl -d更容易:NYTProf yourscript.pl?),分析器工作的结果可以很容易地转换成一个完整的报告 - 使用此包中包含的实用程序 - 或者使用更多'graphicky'工具KCachegrind。