Trick to low CPU usage?

E

Eternally

Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's the
trick to that?

I noticed that on certain applications like those used to decode video, you
can specify how much CPU to use (high priority, low priority...), how is
that accomplished? How can I bring my applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%, however
telling it to sleep for a full second is too much. A tenth of a second
would be fine, but sleep only takes ints. I then tried using clock() and
making it pause until a certain amount of time had passed like .005 of a
second, but the cpu sticks at 100% with that method (since it's running in a
while loop testing the amount of time passed....i.e. it's not really
sleeping).

Thanks in advance for the help!
 
J

John Carson

Eternally said:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm
running the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with
my application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's
the trick to that?

I noticed that on certain applications like those used to decode
video, you can specify how much CPU to use (high priority, low
priority...), how is that accomplished? How can I bring my
applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%,
however telling it to sleep for a full second is too much. A tenth
of a second would be fine, but sleep only takes ints. I then tried
using clock() and making it pause until a certain amount of time had
passed like .005 of a second, but the cpu sticks at 100% with that
method (since it's running in a while loop testing the amount of time
passed....i.e. it's not really sleeping).

Thanks in advance for the help!


This is platform specific and is better asked in newsgroups for the specific
platforms. I don't know about Linux, but Sleep(n) under Windows puts your
application to sleep for n milliseconds (i.e., n/1000 seconds), not n
seconds.
 
E

Eternally

John Carson said:
This is platform specific and is better asked in newsgroups for the specific
platforms. I don't know about Linux, but Sleep(n) under Windows puts your
application to sleep for n milliseconds (i.e., n/1000 seconds), not n
seconds.

Well, like I said, the application is cross-platform to an extent. I'm not
looking for a platform specific solution, as then it either won't run in
Windows under Cygwin, or it won't run under Linux.

I'm more or less looking for a C++ solution. Does one not exist?

Oh, and the sleep function only takes an int representing seconds, not
milliseconds, so that doesn't work. Thanks for the suggestion though.
 
G

Gianni Mariani

Eternally said:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's the
trick to that?

I noticed that on certain applications like those used to decode video, you
can specify how much CPU to use (high priority, low priority...), how is
that accomplished? How can I bring my applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%, however
telling it to sleep for a full second is too much. A tenth of a second
would be fine, but sleep only takes ints. I then tried using clock() and
making it pause until a certain amount of time had passed like .005 of a
second, but the cpu sticks at 100% with that method (since it's running in a
while loop testing the amount of time passed....i.e. it's not really
sleeping).

Thanks in advance for the help!


What are you doing that you need 97% of the CPU ?

Is your code event driven ? Can it be event driven ?
 
J

John Carson

Eternally said:
Well, like I said, the application is cross-platform to an extent.
I'm not looking for a platform specific solution, as then it either
won't run in Windows under Cygwin, or it won't run under Linux.

I'm more or less looking for a C++ solution. Does one not exist?

Threads are platform specific. Standard C++ knows nothing of them. You may
be able to find a cross-platform threading library, but it will be an
extension to standard C++.
Oh, and the sleep function only takes an int representing seconds, not
milliseconds, so that doesn't work. Thanks for the suggestion though.

I was referring to Sleep with a capital S. I don't know anything about
sleep, which appears to be neither a standard C++ nor a standard Windows
function.
 
R

Rolf Magnus

Eternally said:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm
running the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with
my application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's
the trick to that?

What is your program doing in this time? Idling in a loop or doing
calculations? The former should be changed to something event based. If
the latter, it's not wise to make your program use a fixed maximum
amount of CPU time. If no other process is running that would need the
CPU, your program would only run at 1/10 of its possible maximum speed
and the rest of the CPU time would be spent idling in the OS's idle
process.
I noticed that on certain applications like those used to decode
video, you can specify how much CPU to use (high priority, low
priority...), how is that accomplished?

That's a better approach. If there are other processes with higher
priority, those will be preferred. If not, your program gets the CPU.
So your program doesn't make other things slower, but if there is
nothing else to do (other processes waiting for IO e.g.), it'll still
get the most possible CPU time.
Anyway, all that is highly system specific, since C++ doesn't define
anything about multitasking and process priorities. You will have to
use system specific functions. Since you wrote you're writing for
cygwin and linux, there is a good chance that you find a posix function
that works for both.
 
C

Clem Dickey

There is no mechanism within C++. Even sleep is not part of C++, it
is POSIX.
Since you wrote you're writing for
cygwin and linux, there is a good chance that you find a posix function
that works for both.

To reduce priority, use POSIX "nice." To sleep for less than one
second, "nanosleep".
 
M

Moonlit

Hi,

Wat might work is using 'select'. However do not select anything just use
the timeout.

select is available on most unixes and win32.

int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);

Regards, Ron AF Greve.
 
E

Eternally

Rolf Magnus said:
What is your program doing in this time? Idling in a loop or doing
calculations?

It's a real time packet monitor using OpenGl, so it is doing calculations.
It's the display loop that's running at > 60 fps, but I only really need it
to run at most 10 fps. Since it's real-time, and the graph is constantly
adjusting with time, I don't think there's any even that I could use to
adjust it.
The former should be changed to something event based. If
the latter, it's not wise to make your program use a fixed maximum
amount of CPU time. If no other process is running that would need the
CPU, your program would only run at 1/10 of its possible maximum speed
and the rest of the CPU time would be spent idling in the OS's idle
process.


That's a better approach. If there are other processes with higher
priority, those will be preferred. If not, your program gets the CPU.
So your program doesn't make other things slower, but if there is
nothing else to do (other processes waiting for IO e.g.), it'll still
get the most possible CPU time.
Anyway, all that is highly system specific, since C++ doesn't define
anything about multitasking and process priorities. You will have to
use system specific functions. Since you wrote you're writing for
cygwin and linux, there is a good chance that you find a posix function
that works for both.

Ok, thanks. I'll post to a Unix or Linux programming group. I didn't think
cygwin would be capable of that since while the program thinks it's running
in something similar to Linux, Cygwin is still running in Windows. I guess
Cygwin has conversion functions though and calls the appropriate windows
functions.

THanks for the help!
 
E

Eternally

Gianni Mariani said:
What are you doing that you need 97% of the CPU ?

Is your code event driven ? Can it be event driven ?

It's a real time packet monitor displayed in a graph using OpenGl. It's the
display loop that's running at > 60 fps, but I only really need it to run at
most 10 fps. Since it's real-time, and the graph is constantly adjusting
with time, I don't think there's any event that I could use to adjust it.
 
J

Jakob Bieling

It's a real time packet monitor displayed in a graph using OpenGl. It's the
display loop that's running at > 60 fps, but I only really need it to run at
most 10 fps. Since it's real-time, and the graph is constantly adjusting
with time, I don't think there's any event that I could use to adjust it.

If you sleep for about 80-90ms, then you will get about 10fps with a
real low CPU usage. Actually, you would need 100ms to be at exactly 10fps,
but firstly you cannot sleep for *exactly* 100ms and secondly, you also need
some time for calculations.

<Platform-Specific> On Windows you can use "Sleep" to have your process
sleep for n milliseconds. For other platforms I do not know.
</Platform-Specific>

hth
 
R

Ron Natalie

Eternally said:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

You're going to have to use a system dependent call to give up the CPU.
On UNIXOIDS it's going to be something like setitimer. On windows it's
a bit more involved.
 
G

Gianni Mariani

- now this is off topic for c.l.c++
It's a real time packet monitor displayed in a graph using OpenGl. It's the
display loop that's running at > 60 fps, but I only really need it to run at
most 10 fps. Since it's real-time, and the graph is constantly adjusting
with time, I don't think there's any event that I could use to adjust it.

I thought when using double buffering, the call to switch front and back
buffers would automatically wait for the next frame. It's been a while.
 
J

Jakob Bieling

Gianni Mariani said:
- now this is off topic for c.l.c++
it.

I thought when using double buffering, the call to switch front and back
buffers would automatically wait for the next frame. It's been a while.


If vsync is on, it will wait for the next refresh of the screen.
Otherwise it will not wait at all.

hth
 
M

Mike Smith

Jakob said:
<Platform-Specific> On Windows you can use "Sleep" to have your process
sleep for n milliseconds. For other platforms I do not know.
</Platform-Specific>

<more-platform-specific> Sleep() does not guarantee an exact time. If
no other threads are ready to run, the Sleep()ing thread could
conceivably resume immediately. A timer event might a the better way to
go. Does a corresponding thingy exist in Linux?</m-p-s>
 
J

Jakob Bieling

Mike Smith said:
<more-platform-specific> Sleep() does not guarantee an exact time. If
no other threads are ready to run, the Sleep()ing thread could
conceivably resume immediately

It's right that it is not exact, but when you say 80ms, your thread is
going to sleep for at least 80ms - guaranteed. This is why I suggested
roughly 80-90ms for the timeout; the remaining 10-20ms are for executing
code and for the inaccuracies.

regards
 
F

Frank Schmitt

Mike Smith said:
<more-platform-specific> Sleep() does not guarantee an exact time. If
no other threads are ready to run, the Sleep()ing thread could
conceivably resume immediately. A timer event might a the better way
to go. Does a corresponding thingy exist in Linux?</m-p-s>

man nanosleep
man 3 usleep

HTH & kind regards
frank
 
E

Eternally

Eternally said:
Hey folks,

I'm writing a simple C++ application which runs continuously. I'm running
the program from the command line in Cygwin from XP, and it's
cross-compatible with Linux.

While running the program, I notice that the CPU usage is 100%, with my
application stealing 97% of it.

I'd like to bring that down to at most 10% of the CPU usage. What's the
trick to that?

I noticed that on certain applications like those used to decode video, you
can specify how much CPU to use (high priority, low priority...), how is
that accomplished? How can I bring my applications CPU usage down to 10%?

Note: I tried running sleep(1), and that brought it down below 10%, however
telling it to sleep for a full second is too much. A tenth of a second
would be fine, but sleep only takes ints. I then tried using clock() and
making it pause until a certain amount of time had passed like .005 of a
second, but the cpu sticks at 100% with that method (since it's running in a
while loop testing the amount of time passed....i.e. it's not really
sleeping).

Thanks in advance for the help!

Thanks everyone! usleep did the trick!
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top