Click here to Skip to main content
15,889,992 members
Home / Discussions / C#
   

C#

 
GeneralRe: Output Type of Class library can not be started directly Pin
turbochimp13-Apr-05 6:49
turbochimp13-Apr-05 6:49 
GeneralRe: Output Type of Class library can not be started directly Pin
LiamD13-Apr-05 23:58
LiamD13-Apr-05 23:58 
GeneralRe: Output Type of Class library can not be started directly Pin
turbochimp14-Apr-05 2:34
turbochimp14-Apr-05 2:34 
GeneralPopmenu Pin
cishi_us13-Apr-05 5:39
cishi_us13-Apr-05 5:39 
GeneralRe: Popmenu Pin
Ashok Dhamija13-Apr-05 21:38
Ashok Dhamija13-Apr-05 21:38 
GeneralMultifile Assembly Hellllllp Pin
abohassan13-Apr-05 5:33
abohassan13-Apr-05 5:33 
GeneralRe: Multifile Assembly Hellllllp Pin
turbochimp13-Apr-05 5:45
turbochimp13-Apr-05 5:45 
GeneralProcess tree CPU Usage (WMI) Pin
Strangely13-Apr-05 4:40
Strangely13-Apr-05 4:40 
I've been trying to figure out how much of the CPU a process has been using within a time span. I need to know its usage as well as any child processes that are spawned. To get the process tree I used WMI so I can query the processes with a parent process Id.

Trouble is, I'm getting insane results. Here's my code:

<br />
<br />
// Create the process.<br />
Process pTool = ....<br />
<br />
// Object to use to get the process stats.<br />
ManagementScope oMs = new ManagementScope();<br />
UInt64 iPreviousTime = 0;<br />
DateTime dtPreviousTimestamp = DateTime.Now;<br />
<br />
// Start the process.<br />
pTool.Start();<br />
<br />
while (!pTool.HasExited)<br />
{<br />
   // Get the total time, in milliseconds, for this process.<br />
   pTool.Refresh();<br />
   UInt64 iCurrentTime = (UInt64)pTool.TotalProcessorTime.Milliseconds;<br />
<br />
   // Add the time for the child processes.<br />
   iCurrentTime += GetProcessTime(oMs, (uint)pTool.Id);<br />
<br />
   // Get the current time.<br />
   DateTime dtNow = DateTime.Now;<br />
<br />
   // Get the difference since the last call.<br />
   UInt64 iDiff = iCurrentTime - iPreviousTime;<br />
   TimeSpan tsDiff = dtNow - dtPreviousTimestamp;<br />
<br />
   // How much of the previous timespan was this process running?<br />
   float fPercent = (float)(iDiff) / (float)tsDiff.Milliseconds;<br />
<br />
   string strDebug = string.Format("Elapsed time: {0}\tProcess time: {1} ({2})",<br />
      tsDiff.TotalMilliseconds, iDiff.ToString(), fPercent.ToString());<br />
   Debug.WriteLine(strDebug);<br />
<br />
   // Remember the current values.<br />
   iPreviousTime = iCurrentTime;<br />
   dtPreviousTimestamp = dtNow;<br />
<br />
   // Wait a second before re-checking process status.<br />
   System.Threading.Thread.Sleep(1000);<br />
}<br />
<br />
private UInt64 GetProcessTime(ManagementScope oMs, uint iParentId)<br />
{<br />
   // Build the query.<br />
   ObjectQuery oQuery = new ObjectQuery("select Name,ProcessId,UserModeTime,KernelModeTime from win32_process where ParentProcessId=" + iParentId.ToString());<br />
   // Execute the query.<br />
   ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);<br />
   // Get the results<br />
   ManagementObjectCollection oReturnCollection = oSearcher.Get();   <br />
   // Loop through child processes.<br />
   UInt64 iTotalTime = 0;<br />
   foreach (ManagementObject oReturn in oReturnCollection)<br />
   {<br />
      string strName = oReturn["Name"].ToString();<br />
      uint iProcessId = uint.Parse(oReturn["ProcessId"].ToString());<br />
<br />
      // Time is in 100 nanosecond units, convert to milliseconds.<br />
      UInt64 iUserModeTime = UInt64.Parse(oReturn["UserModeTime"].ToString()) / 10000;<br />
      UInt64 iKernelModeTime = UInt64.Parse(oReturn["KernelModeTime"].ToString()) / 1000;<br />
<br />
      Debug.WriteLine("Parent: " + iParentId.ToString() + "\tProcess: " + iProcessId.ToString() + " (" + strName + ")\tTime: " + (iUserModeTime + iKernelModeTime).ToString());<br />
<br />
      // Get the time for the child processes.<br />
      UInt64 iChildTime = GetProcessTime(oMs, iProcessId);<br />
<br />
      // Get the total.<br />
      iTotalTime += (iChildTime + iUserModeTime + iKernelModeTime);<br />
   }<br />
   return iTotalTime;<br />
}<br />


Here's the output I get in the debug window:

Parent: 5248 Process: 5984 (java.exe) Time: 530
Elapsed time: 825.2365 Process time: 576 (0.6981818)
Parent: 5248 Process: 5984 (java.exe) Time: 2046
Parent: 5984 Process: 2744 (mtwrapper.exe) Time: 0
Elapsed time: 1152.217 Process time: 1516 (9.973684)
Parent: 5248 Process: 5984 (java.exe) Time: 2359
Elapsed time: 1043.2235 Process time: 313 (7.27907)
Parent: 5248 Process: 5984 (java.exe) Time: 2421
Parent: 5984 Process: 3216 (ipconfig.exe) Time: 171
Elapsed time: 1074.3645 Process time: 233 (3.148649)
Parent: 5248 Process: 5984 (java.exe) Time: 2468
Elapsed time: 1089.935 Process time: 18446744073709551492 (2.072668E+17)
Parent: 5248 Process: 5984 (java.exe) Time: 2499
Elapsed time: 1027.653 Process time: 31 (1.148148)

As you can see, it starts off fine in the first iteration and then it seems to start going crazy. In the second iteration the java process has over 2 seconds but the elapsed time is just over a second.Confused | :confused: Further down, the huge number must mean we got a negative result. Also confusing.

Can anyone see anything really dumb that I'm doing? If not, has anyone done something like this before?

Thanks,

Michael Stone
GeneralRe: Process tree CPU Usage (WMI) Pin
Strangely13-Apr-05 10:38
Strangely13-Apr-05 10:38 
GeneralRe: Process tree CPU Usage (WMI) Pin
leppie13-Apr-05 19:08
leppie13-Apr-05 19:08 
GeneralSimple DB2 question Pin
trk_wakil13-Apr-05 4:16
trk_wakil13-Apr-05 4:16 
GeneralRe: Simple DB2 question Pin
Vasudevan Deepak Kumar13-Apr-05 4:38
Vasudevan Deepak Kumar13-Apr-05 4:38 
GeneralRe: Simple DB2 question Pin
trk_wakil13-Apr-05 7:01
trk_wakil13-Apr-05 7:01 
GeneralRe: Simple DB2 question Pin
Vasudevan Deepak Kumar13-Apr-05 19:30
Vasudevan Deepak Kumar13-Apr-05 19:30 
GeneralRe: Simple DB2 question Pin
trk_wakil14-Apr-05 4:48
trk_wakil14-Apr-05 4:48 
GeneralAsymmetric encryption in config files Pin
Esmo200013-Apr-05 4:11
Esmo200013-Apr-05 4:11 
GeneralRe: Asymmetric encryption in config files Pin
turbochimp13-Apr-05 5:42
turbochimp13-Apr-05 5:42 
GeneralRe: Asymmetric encryption in config files Pin
Esmo200013-Apr-05 5:49
Esmo200013-Apr-05 5:49 
GeneralRe: Asymmetric encryption in config files Pin
turbochimp13-Apr-05 6:45
turbochimp13-Apr-05 6:45 
GeneralRe: Asymmetric encryption in config files Pin
Esmo200013-Apr-05 9:59
Esmo200013-Apr-05 9:59 
GeneralRe: Asymmetric encryption in config files Pin
turbochimp13-Apr-05 10:54
turbochimp13-Apr-05 10:54 
GeneralRe: Asymmetric encryption in config files Pin
Ashok Dhamija13-Apr-05 22:00
Ashok Dhamija13-Apr-05 22:00 
GeneralLimit the transfer speed of file copyies Pin
Andemann13-Apr-05 3:00
Andemann13-Apr-05 3:00 
GeneralRe: Limit the transfer speed of file copyies Pin
Dave Kreskowiak13-Apr-05 4:14
mveDave Kreskowiak13-Apr-05 4:14 
GeneralInheriting ProxyAttribute Pin
Zgaddo13-Apr-05 2:56
Zgaddo13-Apr-05 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.