简体   繁体   English

在部分挂钟运行时间内测量守护程序CPU利用率

[英]Measuring daemon CPU utilization over a portion of its wall clock run time

I am dealing with a network-related daemon: it takes data in, processes it, and spits it out. 我正在处理与网络相关的守护进程:它将数据接收,处理并吐出。 I would like to increase the performance of this daemon by profiling it and reducing it's CPU utilization. 我想通过分析该守护程序并降低其CPU使用率来提高其性能。 I can do this easily on Linux with gprof. 我可以使用gprof在Linux上轻松完成此操作。 However, I would also like to use something like "time" to measure it's total CPU utilization over a period of time. 但是,我还想使用“时间”之类的方法来衡量一段时间内的CPU总利用率。 If possible, I would like to time it over a period that is less than its total run time: thus, I would like to start the daemon, wait awhile, generate CPU statistics, stop generating them, then stop the daemon at some later time. 如果可能,我希望将其计时的时间少于其总运行时间:因此,我想启动守护程序,稍等片刻,生成CPU统计信息,停止生成它们,然后在稍后的某个时间停止守护程序。

The "time" command would work well for me, but it seems to require that I start and stop the daemon as a child of time. “时间”命令对我来说将很好地工作,但是似乎需要我作为时间的孩子来启动和停止守护程序。 Is there a way to measure CPU utilization for only a portion of the daemon's wall clock time? 有没有一种方法可以仅测量守护进程壁钟时间的一部分的CPU使用率?

The /proc/<pid>/stat file contains the necessary information - you're after the utime and stime fields. /proc/<pid>/stat文件包含必要的信息-您在utimestime字段之后。 Those are cumulative counters of the process's user-mode and kernel-mode CPU time used; 这些是所使用的进程的用户模式和内核模式CPU时间的累积计数器。 read them at the start of the measuring interval, then read them again at the end and calculate the difference. 在测量间隔开始时读取它们,然后在结束时再次读取它们并计算差值。

That will give you used CPU time in jiffies. 这将使您用尽CPU时间。 To determine the total elapsed wallclock time in jiffies (so you can convert to an average utilisation), sum the numbers on the cpu0 line in /proc/stat (before and after, just like /proc/<pid>/stat ). 要确定以秒为单位的总挂钟时间(以便转换为平均利用率),请对/proc/statcpu0行中的数字求和(前后如/proc/<pid>/stat )。

This is the layout of the first few fields in /proc/<pid>/stat , from Documentation/filesystems/proc.txt in the Linux source: 这是Linux源码中Documentation/filesystems/proc.txt/proc/<pid>/stat前几个字段的布局:

Table 1-3: Contents of the stat files (as of 2.6.22-rc3)
..............................................................................
 Field          Content
  pid           process id
  tcomm         filename of the executable
  state         state (R is running, S is sleeping, D is sleeping in an
                uninterruptible wait, Z is zombie, T is traced or stopped)
  ppid          process id of the parent process
  pgrp          pgrp of the process
  sid           session id
  tty_nr        tty the process uses
  tty_pgrp      pgrp of the tty
  flags         task flags
  min_flt       number of minor faults
  cmin_flt      number of minor faults with child's
  maj_flt       number of major faults
  cmaj_flt      number of major faults with child's
  utime         user mode jiffies
  stime         kernel mode jiffies
  cutime        user mode jiffies with child's
  cstime        kernel mode jiffies with child's

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM