How to tell when CPU usage is too high

M

Marco Martin

Hi Group,

I've been working on an application that reads data from a comm port and 1)
writes the data to file, 2) displays this data in three different graphs in
real time.The port is receiving data at 38400 baud, the plan is that it will
go up to 115k baud. My app works fine on my development pc (P4 2.6G), But
when the application runs on a pc that is slower (Like a p3 laptop that is
used when on the road) it freezes. The laptop cant handle all of the
calculations used in the Graphing objects. I now this because if I disable
one of my graphs (a multi channel line graph), cpu usage goes from 75% down
to 34%(as per task manager). So I know *which* object is giving me
problems.

My boss wants me to be able to monitor when the pc starts to lagg. If it
laggs, I'm to continue logging the data to file, but not graph every line
that comes in. I should only graph every second or third line...

So to make my story short, how can I go about knowing (through code) if my
computer is starting to lagg, or if my cpu usage is too high or something
like that. I'm not even sure where to start looking...

I've only got a few days to come up with a solution so *any* help
what-so-ever would be greatly appreciated.

Thanks and best regards,

Marco
 
W

Willy Denoyette [MVP]

What exactly do you mean with "it freezes"?

If the CPU usage is only 75%, it's certainly not the cause of the lagg.
I would suggest:
- you profile your application and watch excessive function execution times.
- if it's a multithreaded application (which I hope it is) , watch for lock
contention.

Willy.
 
P

Philip Rieck

Use the System.Diagnostics.PerformanceCounter:

PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor
Time", "_Total");
....
....
pc.NextValue() // gets the cpu % . sometimes the first call will be 0,
other calls will be correct. be aware
....

So (strictly psuedocode)

PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor
Time", "_Total");
single cpu = pc.NextValue();
while (gettingData())
{
if(cpu < 75)
{ renderLine(); }
cpu = pc.NextValue();
}
 
M

Marco Martin

Willy,
Thanks for your reply. The app is multi-threaded.

Basically, when start button is clicked, I create a thread that reads lines
from the comm port inside an infinite loop(which is in an unsafe class
instance). When a line is read, it fires a "CommEvent" in main thread.
When the comm event is fired I do a bunch of UI updates and then start a new
thread that updates all of the graphs.
I had decided to update the graphs in their own thread because of the
intensive calculations needed in order to plot properly.

What I mean by freezes is that the UI starts to get gittery and that
eventualy, nothing happens any more. At this point, task manager shows me
100% CPU usage.

regards,

Marco
 
M

Marco Martin

Thanks,

With the PerformanceCounter, I am able to check if it's possible to update
the graph.

Best regards,

Marco
 
W

Willy Denoyette [MVP]

Marco,

When the "graphs" are UI elements created on the main UI thread, you should
NEVER EVER update these objects from any other thread as the main UI thread
(the creator of the UI elements).

Willy.
 
M

Marco Martin

thanks Willy,
When the "graphs" are UI elements created on the main UI thread, you should
NEVER EVER update these objects from any other thread as the main UI
thread

Can I ask you why?

Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top