测量守护进程在其挂钟运行时间的一部分内的 CPU 利用率

发布于 2024-09-03 14:38:57 字数 299 浏览 10 评论 0原文

我正在处理一个与网络相关的守护进程:它接收数据,处理它,然后吐出它。我想通过分析该守护进程并降低其 CPU 利用率来提高其性能。我可以使用 gprof 在 Linux 上轻松完成此操作。但是,我还想使用“时间”之类的东西来衡量一段时间内的总 CPU 利用率。如果可能的话,我想在小于其总运行时间的一段时间内进行计时:因此,我想启动守护进程,等待一段时间,生成CPU统计信息,停止生成它们,然后在稍后的时间停止守护进程。

“time”命令对我来说效果很好,但它似乎要求我将守护进程作为时间的子项来启动和停止。有没有办法仅测量守护进程挂钟时间的一部分的 CPU 利用率?

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. I can do this easily on Linux with gprof. However, I would also like to use something like "time" to measure it's total CPU utilization over a period of time. 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄暮涼年 2024-09-10 14:38:57

/proc//stat 文件包含必要的信息 - 您需要查看 utimestime 字段。这些是进程使用的用户模式和内核模式 CPU 时间的累积计数器;在测量间隔开始时读取它们,然后在测量结束时再次读取它们并计算差异。

这将为您提供以 jiffies 为单位的已用 CPU 时间。要确定以 jiffies 为单位的总运行挂钟时间(以便您可以转换为平均利用率),请将 /proc/statcpu0 行上的数字相加(之前和之后) ,就像/proc//stat)。

这是 /proc//stat 中前几个字段的布局,来自 Linux 源代码中的 Documentation/filesystems/proc.txt

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

The /proc/<pid>/stat file contains the necessary information - you're after the utime and stime fields. Those are cumulative counters of the process's user-mode and kernel-mode CPU time used; 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. 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).

This is the layout of the first few fields in /proc/<pid>/stat, from Documentation/filesystems/proc.txt in the Linux source:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文