a processes cpu usage

  • Thread starter Thread starter Dirk Reske
  • Start date Start date
D

Dirk Reske

Hello,

why doesn't this code work correctly?

private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep(100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}

I get wrong values...perhaps the Idle process has an average cpu usage of
2950% ????
 
But I get at some processes more than 100%, which is not possible...
Perhaps the Idle process is always nearly 2600%
 
No, I really use this code:

AlexS said:
Dirk,

judging by this you use some modification of the code you posted. I would
suggest to look what is different.

When I did some tests I never had more than 2%. Usually (8 out of 10) - 0.

HTH
Alex
 
private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep(100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}
 
I pass each process...
foreach (Process proc in Process.GetProcesses())
......

when I put a proc.Refresh() at the first line of the GetCpuUsage funktion
all works fine...
some little mistakes..

System: 10%
Idle: 100%

-> this are 110% this can't be??
 
acn you modify my code so?

thx

AlexS said:
Dirk,

if you want to measure activity of process - you must measure it using as a
start time when process was started, not some arbitrary moment as in your
sample. When you go through all processes on machine and measure activity
against 100ms interval of course you get senseless figures.
Check once again Refresh. In your foreach you get total milliseconds since
the process started + some delta which process used during sleep.

HTH
Alex
 
Yes, but I need this values...
If there were any way to get the values from the taskmanager, I would use
it.
And I need the filename of the main module...
there are also performance counters that represent the cpu usage of each
process...but there I cant get the filenames,
because if the same process is running more than one time, the performance
counter are named svchost#1 #2 and so on....
 
Hi Dirk

what kind of wrong value you get? Your code produces consistently returns 0
as result, which is Ok.
Because resulting value on my machine is less than 1%, so when converted to
Int32 it gives 0.

HTH
Alex
 
Dirk,

judging by this you use some modification of the code you posted. I would
suggest to look what is different.

When I did some tests I never had more than 2%. Usually (8 out of 10) - 0.

HTH
Alex
 
Dirk,

then take a good look which process you pass in. I tested with current
process. Code is fine with it.
However if you pass reference of process which was obtained long before the
call and was not refreshed since - you might get huge value in cpudiff. Take
a good look at process.Refresh method description.

HTH
Alex
 
Dirk,

if you want to measure activity of process - you must measure it using as a
start time when process was started, not some arbitrary moment as in your
sample. When you go through all processes on machine and measure activity
against 100ms interval of course you get senseless figures.
Check once again Refresh. In your foreach you get total milliseconds since
the process started + some delta which process used during sleep.

HTH
Alex
 
Dirk,

take a look at Process.StartTime property. Then I am pretty sure you can
modify your code yourself as you like.

HTH
Alex
 
Right, then you get average value.

If you want snapshot - I would suggest a bit different approach:
- get list of processes and times
- delay for 1 sec - better do it using thread timer than thread sleep
- get another list of processes and times
- compare both list and calculate percentage for each existing process in
second array using either related process Total from first array or process
start time whatever is latest and saved timestamps.
In reality process is even more complex if you want to have really precise
picture. However it is not very clear what for so much effort?

Maybe it would be easier to use Task Manager?
Actual value is always some kind of approximation
HTH
Alex
 
Dirk,

please take a look at your code once more,
Looks like you call your routine from foreach loop - which means that you
get statistics for different processes during different intervals. I think
it is the issue.

Your routine as posted is Ok for measuring single process %. However you did
not post code which uses it.

I hope now it makes things a bit more clear for you. And sorry for
StartTime - it is not much relevant for you - I hoped it would help you to
find out where you make mistake in reasoning together with Refresh
description.

So you have to loop through all processes getting most recent data, then do
some delay, then loop once again (new list processes or refreshes for all of
them). Only then you will get balanced figures. And then some processes may
disappear during measuring interval and other start. So it will be not that
simple.

Cheers!
Alex
PS: "Wise are learning from mistakes of others, not very wise - from their
own"
 
Perphaps you have already solved your problem by now. Anyway, I came
across your posting when I was lookin for the information on how to
calculate process CPU usage. My code is essential the same as yours
except that mine multiply the system elapsed time (your cpudiff) by the
number of processors. Without the processors multipler, I was getting
similiar incorrect values which exceeded 100%. Then I realized that
the OS (or the BIOS) reported 2 processors for my hyperthreading CPU.
 
Back
Top