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

时间:2023-01-21 15:15:02

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

有没有办法让Process.TotalProcessorTime反映一个进程PLUS它产生的任何进程?

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.

我的应用程序正在生成一个外部进程并等待它退出。使用样本数据需要5-20分钟才能完成;我没有猜测合理的最大超时时间。所以我的应用程序会定期检查另一个进程上的Process.TotalProcessorTime值,以确保它不断上升。

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.

这样可以正常工作,但我发现这个过程只是一个“包装器”,它产生一个子进程,从而产生一些消耗所有CPU时间的其他子进程。

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

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

2 个解决方案

#1


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

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

#2


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,
    • 通过PID维护累计CPU使用率的哈希值,

    • 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).

    这应该允许调用代码轻松检查进程(树)是否未挂起(I / O绑定进程可能在这里有问题,但这不适用于我的情况)。

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

这似乎都不困难,可能的例外是监控线程没有(直接)生成进程的CPU使用率。

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

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

#1


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

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

#2


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,
    • 通过PID维护累计CPU使用率的哈希值,

    • 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).

    这应该允许调用代码轻松检查进程(树)是否未挂起(I / O绑定进程可能在这里有问题,但这不适用于我的情况)。

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

这似乎都不困难,可能的例外是监控线程没有(直接)生成进程的CPU使用率。

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

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