469,631 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

How to tell when CPU usage is too high

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

Nov 15 '05 #1
7 2160
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.
"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:db*******************@news20.bellglobal.com.. .
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


Nov 15 '05 #2
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();
}

"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:db*******************@news20.bellglobal.com.. .
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


Nov 15 '05 #3
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
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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.
"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:db*******************@news20.bellglobal.com.. .
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



Nov 15 '05 #4
Thanks,

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

Best regards,

Marco
"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:db*******************@news20.bellglobal.com.. .
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


Nov 15 '05 #5
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.
"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:l2*******************@news20.bellglobal.com.. .
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
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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.
"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:db*******************@news20.bellglobal.com.. .
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




Nov 15 '05 #6
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

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:uo**************@tk2msftngp13.phx.gbl... 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.
"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:l2*******************@news20.bellglobal.com.. .
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
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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.
"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:db*******************@news20.bellglobal.com.. .
> 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
>
>
>
>
>



Nov 15 '05 #7
Sure I can, but [1] does it much better ;-)
Read it carefully I guess it will help you solve your issue.

[1]<http://msdn.microsoft.com/msdnmag/is...ing/default.as
px>

Willy.

"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:hp********************@news20.bellglobal.com. ..
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

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...
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.
"Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
news:l2*******************@news20.bellglobal.com.. .
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
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
> 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.
>
>
> "Marco Martin" <ma**********@sympatico.ca.antispam> wrote in message
> news:db*******************@news20.bellglobal.com.. .
> > 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
> >
> >
> >
> >
> >
>
>



Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by christopher diggins | last post: by
10 posts views Thread by rdemyan via AccessMonster.com | last post: by
9 posts views Thread by Adam Right | last post: by
reply views Thread by Dinesh | last post: by
reply views Thread by Learning.Net | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.