473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can I use the "Idle" process to figure out why my processor utilization has been?

I don't want to know what the CPU utilization is right now. I want to
get the average utilization over the last, for example, hour. So I
came up with a method where I would get a Process object representing
the "Idle" process. I would figure out what percentage of time the
idle process has been running since my function last was called. Then
the average CPU utilization would be 100 - average idle percentage. I
figure out the average idle percentage by dividing the difference in
TotalProcessorTime by the difference in total time since the function
was called last. Here's the code

DateTime _oldTime;
TimeSpan _oldCPUUsage;

private void CPUUsage()
{
//Find out how much processor has been idle. Usage is 100% -
idle%.
DateTime newTime = DateTime.Now;
TimeSpan newCPUUsage = Process.GetProcessesByName
("Idle")[0].TotalProcessorTime;
double idlePercent =
(newCPUUsage.Subtract(_oldCPUUsage).TotalHours /
newTime.Subtract(_oldTime).TotalHours) * 100;

Debug.WriteLine((100.0 - idlePercent).ToString());
_oldCPUUsage = newCPUUsage;
_oldTime = newTime;
}

It seems to work great some of the time, but not all the time.
Sometimes my CPU usage (100.0 - idlePercent) ends up being negative or
really big (> 100000). So there's got to be something wrong in my
algorithm, but I don't know what. My thinking is that the "idle"
process might not always be an accurate way to get what I think it's
giving me. Any ideas?

Or is there a better way to do what I am trying to do?

thanks

John
Nov 15 '05 #1
3 2435
Use the PerformanceCounters, so just like PerfMon, you can get accurate data
and average it out.
-mike
MVP

"john" <jo********@yahoo.com> wrote in message
news:29**************************@posting.google.c om...
I don't want to know what the CPU utilization is right now. I want to
get the average utilization over the last, for example, hour. So I
came up with a method where I would get a Process object representing
the "Idle" process. I would figure out what percentage of time the
idle process has been running since my function last was called. Then
the average CPU utilization would be 100 - average idle percentage. I
figure out the average idle percentage by dividing the difference in
TotalProcessorTime by the difference in total time since the function
was called last. Here's the code

DateTime _oldTime;
TimeSpan _oldCPUUsage;

private void CPUUsage()
{
//Find out how much processor has been idle. Usage is 100% -
idle%.
DateTime newTime = DateTime.Now;
TimeSpan newCPUUsage = Process.GetProcessesByName
("Idle")[0].TotalProcessorTime;
double idlePercent =
(newCPUUsage.Subtract(_oldCPUUsage).TotalHours /
newTime.Subtract(_oldTime).TotalHours) * 100;

Debug.WriteLine((100.0 - idlePercent).ToString());
_oldCPUUsage = newCPUUsage;
_oldTime = newTime;
}

It seems to work great some of the time, but not all the time.
Sometimes my CPU usage (100.0 - idlePercent) ends up being negative or
really big (> 100000). So there's got to be something wrong in my
algorithm, but I don't know what. My thinking is that the "idle"
process might not always be an accurate way to get what I think it's
giving me. Any ideas?

Or is there a better way to do what I am trying to do?

thanks

John

Nov 15 '05 #2
> Use the PerformanceCounters, so just like PerfMon, you can get accurate data
and average it out.
-mike
MVP


If I use PerformanceCounters, can it return to me the average CPU
usage over the last hour? Or would I have to do a regular sample
(maybe once every 5 seconds) and then average the samples myself? If I
have to take samples and then average them, it will be somewhat
inaccurate (e.g. the usage could change quite a bit in the middle of
my samples). But with the method I proposed in my original post, it
will be completely accurate even if the function runs only once an
hour. The problem is that it occasionally gets crazy results.

thanks.
Nov 15 '05 #3
Those are some valid points. In the case that you get crazy results, why
not have it dump all the variables so you could see what went on?
-mike
MVP

"john" <jo********@yahoo.com> wrote in message
news:29**************************@posting.google.c om...
Use the PerformanceCounters, so just like PerfMon, you can get accurate data and average it out.
-mike
MVP


If I use PerformanceCounters, can it return to me the average CPU
usage over the last hour? Or would I have to do a regular sample
(maybe once every 5 seconds) and then average the samples myself? If I
have to take samples and then average them, it will be somewhat
inaccurate (e.g. the usage could change quite a bit in the middle of
my samples). But with the method I proposed in my original post, it
will be completely accurate even if the function runs only once an
hour. The problem is that it occasionally gets crazy results.

thanks.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
2
by: Gregory S. Williamson | last post by:
Dear peoples, Periodically we are getting runaway postgres processes on our Linux (2.4.21-0.13 on Dell servers), using 7.4 and GIS (0.8 USE_GEOS=1 USE_PROJ=1 USE_STATS=1). All of the queries...
13
by: royaltiger | last post by:
I am trying to copy the inventory database in Building Access Applications by John L Viescas but when i try to run the database i get an error in the orders form when i click on the allocate...
0
by: fireball | last post by:
I installed Parallels Desktop and Win XP Pro on my iMac for testing purposes. I installed Python 2.5.2, wxPython, and PythonCard. I cannot get the "Open with IDLE" in an Explorer window to work....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.