监控子进程/我可以获得进程组的 TotalProcessorTime 吗?

发布于 2024-07-19 20:48:51 字数 423 浏览 5 评论 0原文

有没有办法获取反映进程加上它所生成的任何进程的Process.TotalProcessorTime

或者,我如何验证该进程(或其后代)是否仍在“主动”运行?


我的应用程序正在生成一个外部进程并等待它退出。 对于示例数据,需要 5-20 分钟才能完成; 我无法猜测合理的最大超时时间。 因此,我的应用程序会定期检查其他进程的 Process.TotalProcessorTime 值,以确保它不断上升。

这工作正常,但我发现该进程只是一个生成子进程的“包装器”,而子进程又生成了一些消耗所有 CPU 时间的其他子进程。

我发现 TotalProcessorTime 在 CPU 利用率达到 100% 几分钟后仅返回一小部分秒。

Is there a way to get the Process.TotalProcessorTime that reflects a process PLUS any processes that it has spawned?

Alternatively, how can I verify that the process (or it's descendants) are still "actively" running?


My app is spawning an external process and waiting for it to exit. With sample data it takes 5-20 minutes to complete; I don't have a guess for a reasonable maximum timeout. So my app checks the Process.TotalProcessorTime value on the other process at intervals, to ensure that it keeps rising.

This works fine, but I've discovered that the process is simply a "wrapper" that spawns a sub-process, which in turn spawns a handful of other sub-process that consume all of the CPU time.

And I've found that TotalProcessorTime returns only a small fraction of a second after several minutes of 100% CPU utilization.

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

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

发布评论

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

评论(2

自找没趣 2024-07-26 20:48:51

我还没有实现这个,但已经弄清楚它是如何工作的。 这很乏味,但并不那么复杂。

  1. 生成一个后台线程,每隔几秒就会:

    • 构建进程树(请参阅此答案用于代码的简化版本),
    • 如果进程已退出,则终止(监视线程)。
    • 通过 PID 维护累积 CPU 使用率的哈希值,
    • 维护所有进程的所有值的总和,
    • 维护与上次线程触发时总和的差异。
  2. 这应该允许调用代码轻松检查进程(树)是否
    未挂起(I/O 绑定进程可能会出现问题,但这并不适用
    对于我的情况
    )。

这一切似乎都不困难,除了获取不是由监视线程(直接)生成的进程的 CPU 使用情况之外。

如果/当我实现代码时,我会更新这个答案,但这可能需要很长时间。

I haven't implemented this, but have figured out how it might work. It's tedious but not all that complicated.

  1. Spawn a background thread that every few seconds will:

    • Build the process tree (see this answer for a simplified version of the code),
    • Terminate (the monitor thread) if the process has exited.
    • Maintain a hash of the accumulated CPU Usage by PID,
    • Maintain a sum of all values by all processes,
    • Maintain the difference of the sum from the last time the thread fired.
  2. This should allow the calling code to easily check that the process (tree) is
    not hung (I/O bound processes might have problems here, but that doesn't apply
    to my case
    ).

None of this seems difficult, with the possible exception of getting CPU usage of a process not spawned (directly) by the monitoring thread.

I'll update this answer if/when I implement the code, but it may be a long time.

忘羡 2024-07-26 20:48:51

您可以使用性能计数器来检索父进程,如下所示:

PerformanceCounter pc = new PerformanceCounter("Process", 
    "Creating Process Id", " windbg");
Process p = Process.GetProcessById((int)pc.RawValue);

有了该信息,您可以监视系统中的进程,维护整个进程树的结构并计算树的 TotalProcessorTime。 希望有帮助

You can use a performance counter to retrieve the parent process like this:

PerformanceCounter pc = new PerformanceCounter("Process", 
    "Creating Process Id", " windbg");
Process p = Process.GetProcessById((int)pc.RawValue);

Having that information you can monitor processes in the system, maintain a structure of the whole process tree and calculate TotalProcessorTime for tree. Hope that helps

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