Subprocess monitoring / can I get the TotalProcessorTime for a process group?

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

Alternatively, how can I check that the process (or its descendants) is still "actively" running?


My application spawns an external process and waits for it to exit. Sample data takes 5-20 minutes; I have no guess for a reasonable maximum timeout. So my application checks the Process.TotalProcessorTime value on another process at intervals so that it keeps growing.

This works great, but I found that this process is just a "wrapper" that spawns a sub-process, which in turn spawns several other sub-processes that consume all the CPU time.

And I found that TotalProcessorTime only returns a small fraction of a second after a few minutes of 100% CPU usage.

0


a source to share


2 answers


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

  • Create a background thread that every few seconds will:

    • Create a process tree (see this answer for a simplified version of the code),
    • End (monitor thread) if the process exited.
    • Maintain a hash of accumulated CPU usage by PID,
    • Maintaining the sum of all values ​​by all processes,
    • Keep the difference in the amount since the last start of the stream.
  • This should allow the calling code to easily check that the process (tree) does not hang (I / O related processes may have problems here, but this does not apply to my case).



None of this seems difficult, except perhaps using the CPU of a process not spawned (directly) by the monitoring thread.

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

0


a source


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

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

      



With this information, you can monitor the processes in the system, maintain the structure of the entire process tree, and calculate the TotalProcessorTime for the tree. Hope it helps

0


a source







All Articles