Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question of throttling CPU usage

Reply
Thread Tools

Question of throttling CPU usage

 
 
Nobody
Guest
Posts: n/a
 
      04-26-2012
On Wed, 25 Apr 2012 15:59:47 -0400, James Kuyper wrote:

> The tricky part of that is "do nothing". The C standard doesn't provide
> any mechanism for doing that; you'll have to find a method specific to
> your operating system for doing it. That's what sleep() is for;
> presumably there's a Windows equivalent - it might even be called
> sleep(), for all I know.


Actually, it's called Sleep() (with an upper-case S); the argument is in
milliseconds (as opposed to seconds for the Unix version).

 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      04-26-2012
On Thu, 26 Apr 2012 01:41:06 -0700, jon.harding wrote:

> OK, and I am sorry to labour the point here, but how can I do this
> outside the application then?
>
> Here are the requirements:
> 1. My program will run hopefully on Windows and Linux.
> 2. My program should run at a set CPU percentage and not exceed it.
> 3. The CPU percentage may be different depending on the time of day;
> ie after 6pm and over a lunch break 12:30 - 1:00pm it will run at 85%
> utilization, but at other times around 10%


You'll probably have to do it within the application, using
platform-specific functionality (e.g. sleep, usleep or nanosleep on Unix,
Sleep on Windows).

Very few real programs can be written using only the functionality
provided by the library. E.g. almost all file handling is
platform-specific: even with <stdio.h>, the standard doesn't specify which
filenames are valid, and the standard makes no mention of directories.

 
Reply With Quote
 
 
 
 
Fred K
Guest
Posts: n/a
 
      04-26-2012
On Thursday, April 26, 2012 6:33:32 AM UTC-7, Nobody wrote:
> On Thu, 26 Apr 2012 01:41:06 -0700, jon.harding wrote:
>
> > OK, and I am sorry to labour the point here, but how can I do this
> > outside the application then?
> >
> > Here are the requirements:
> > 1. My program will run hopefully on Windows and Linux.
> > 2. My program should run at a set CPU percentage and not exceed it.
> > 3. The CPU percentage may be different depending on the time of day;
> > ie after 6pm and over a lunch break 12:30 - 1:00pm it will run at 85%
> > utilization, but at other times around 10%

>
> You'll probably have to do it within the application, using
> platform-specific functionality (e.g. sleep, usleep or nanosleep on Unix,
> Sleep on Windows).
>
> Very few real programs can be written using only the functionality
> provided by the library. E.g. almost all file handling is
> platform-specific: even with <stdio.h>, the standard doesn't specify which
> filenames are valid, and the standard makes no mention of directories.


Regardless of what you use to try slowing the program down, you probably can't cause it to use a specific amount of the CPU time. There are likely to be many other things running at the same time that you have no control over, and they will share CPU time with your progam.

Suppose you specify your program to use 85% of the CPU time. Now run two instances of your program at the same time. Do you really expect each of themto use 85% of the CPU time?
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      04-26-2012
On 04/26/2012 10:44 AM, Fred K wrote:
> On Thursday, April 26, 2012 6:33:32 AM UTC-7, Nobody wrote:
>> On Thu, 26 Apr 2012 01:41:06 -0700, jon.harding wrote:

....
>>> 2. My program should run at a set CPU percentage and not exceed it.

....
> Regardless of what you use to try slowing the program down, you probably can't cause it to use a specific amount of the CPU time. There are likely to be many other things running at the same time that you have no control over, and they will share CPU time with your progam.
>
> Suppose you specify your program to use 85% of the CPU time. Now run two instances of your program at the same time. Do you really expect each of them to use 85% of the CPU time?


It's feasible to mandate that your program must use no more than X% of
the CPU time, while allowing for the possibility of using less than X%.
It's possible to interpret jon.harding's specification above as implying
that: he emphasizes that CPU usage must "not exceed" the specified
percentage.
 
Reply With Quote
 
tom st denis
Guest
Posts: n/a
 
      04-26-2012
On Apr 25, 3:35*pm, jon.hard...@gmx.co.uk wrote:
> On Apr 25, 7:44*pm, James Kuyper <jameskuy...@verizon.net> wrote:
>
>
>
>
>
>
>
>
>
> > On 04/25/2012 02:22 PM, jon.hard...@gmx.co.uk wrote:

>
> > > Hi, can someone help with how I can throttle CPU usage in a c
> > > application.

>
> > > By means of an example, in this trivial application

>
> > > int main(int argc, char *argv[]) {
> > > * * int i;
> > > * * while (1) {
> > > * * * *i++;
> > > * * *}
> > > }

>
> > > how can I limit the CPU usage for the loop to be 10% or 20% or 50% or
> > > whatever integer I pass in on the command line.

>
> > The C standard provides no mechanism for doing this; whether there is
> > any such mechanism, and how to use it, will depend upon your operating
> > system.

>
> > For instance, on unix-like systems:
> > sleep() provides a mechanism for relinquishing control of the CPU for a
> > period of time; To achieve a desired percentage of the time, use calls
> > to clock() to determine when the next time should be for a call to sleep().
> > If all you really want is to reduce your CPU usage, without needing
> > precise control over reduction, call the nice() function to lower the
> > scheduling priority for your process.

>
> Thank you James.
>
> I was thinking of doing something like your suggestion but my
> application will hopefully run on both Windows and Linux so I won't be
> able to set process priority or the nice factor.


You can create threads with various priority settings in Windows. And
you can use nice() in Linux/UNIX.... Both can then call a standard
library function you write to process "work."

All of which is OT...

BTW typically if someone wants to run a daemon that does background
heat generation [er... number crunching for whatever cause] they don't
want it fixed to a max of x% but more so "lowest damn priority
possible."

As per your later posts I don't get what a "upto x%" limit buys you.
Why not just set x to 100 and then run it at idle level? Would
someone really want their computer to be 50% idling when not doing
anything giving your program 50% of the cpu? In terms of energy
efficiency it's actually better to give it 100% (for instance, the
higher power draw of the ARM A15 than the A9 is offset by the fact the
core is busy less doing the same unit of work).

Tom
 
Reply With Quote
 
Robert Miles
Guest
Posts: n/a
 
      05-28-2012
On 4/25/2012 2:35 PM, wrote:
> On Apr 25, 7:44 pm, James Kuyper<jameskuy...@verizon.net> wrote:
>> On 04/25/2012 02:22 PM, jon.hard...@gmx.co.uk wrote:
>>
>>> Hi, can someone help with how I can throttle CPU usage in a c
>>> application.

>>
>>> By means of an example, in this trivial application

>>
>>> int main(int argc, char *argv[]) {
>>> int i;
>>> while (1) {
>>> i++;
>>> }
>>> }

>>
>>> how can I limit the CPU usage for the loop to be 10% or 20% or 50% or
>>> whatever integer I pass in on the command line.

>>
>> The C standard provides no mechanism for doing this; whether there is
>> any such mechanism, and how to use it, will depend upon your operating
>> system.
>>
>> For instance, on unix-like systems:
>> sleep() provides a mechanism for relinquishing control of the CPU for a
>> period of time; To achieve a desired percentage of the time, use calls
>> to clock() to determine when the next time should be for a call to sleep().
>> If all you really want is to reduce your CPU usage, without needing
>> precise control over reduction, call the nice() function to lower the
>> scheduling priority for your process.

>
> Thank you James.
>
> I was thinking of doing something like your suggestion but my
> application will hopefully run on both Windows and Linux so I won't be
> able to set process priority or the nice factor.
>
> Where I got my idea from is from boinc. In boinc you can set the CPU
> usage to be anything from 1-100, and even configure the program to
> suspend itself when the computer is doing something else, so that is
> using idle time and not being antisocial to whatever else the computer
> is doing. I am thinking of something like this. Maybe as you suggest,
> I will just check the time every now and then and for 20% usage, I
> will just run at 100% CPU for short bursts, work out how much time it
> took to do the burst, and then do nothing for 8 * that time, and
> repeat.
> Jon


There's a BOINC add-on called Throttle which provides the function
you want, for Windows. The documentation says it can be applied to
non-BOINC programs, but doesn't make it very clear how.

http://efmer.eu/boinc/

I haven't found whether it was written in C.


 
Reply With Quote
 
Robert Miles
Guest
Posts: n/a
 
      05-28-2012
On 4/26/2012 5:59 AM, James Kuyper wrote:
> On 04/26/2012 04:41 AM, wrote:
>> On Apr 25, 11:37�pm, Ian Collins<ian-n...@hotmail.com> wrote:
>>> On 04/26/12 10:33 AM, jon.hard...@gmx.co.uk wrote:
>>>
>>>>>>> On 04/25/2012 02:22 PM, jon.hard...@gmx.co.uk wrote:
>>>
>>>>>>>> Hi, can someone help with how I can throttle CPU usage in a c
>>>>>>>> application.
>>>
>>>> OK, I understand. I am new to c.
>>>
>>>> Let me put it this way, can I do what I want in c++? I don't know that
>>>> language either, but I may as well learn the one that will let me do
>>>> what I want.
>>>
>>> No. �The same conditions apply.
>>>
>>> If you want to manage CPU utilisation, it has to be done either outside
>>> of the application, or in a system dependent way.
>>>
>>> --
>>> Ian Collins

>>
>> OK, and I am sorry to labour the point here, but how can I do this
>> outside the application then?

>
> I think "system dependent" rather than "outside the application" is your
> best bet, given the following requirements.
>
>> Here are the requirements:
>> 1. My program will run hopefully on Windows and Linux.
>> 2. My program should run at a set CPU percentage and not exceed it.
>> 3. The CPU percentage may be different depending on the time of day;
>> ie after 6pm and over a lunch break 12:30 - 1:00pm it will run at 85%
>> utilization, but at other times around 10%

>
> For the Linux side, we've already discussed how clock() and sleep()
> could be used to manage your CPU percentage. localtime() provides the
> mechanism for making the % vary with time of day.
>
> For Windows, I can't help you - I haven't had to program for that
> environment in nearly two decades. But Robert Wessel seems to have dealt
> with that side.


I've used the Cygwin Linux-emulation under Windows, and it provides
clock(), sleep(), and localtime(). Use the info command for locating
details on how to call each of them.

Robert Miles
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about throttling the number of webservice calls from a .NETwebsite Simon Roust ASP .Net 1 04-16-2010 07:02 PM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:09 PM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:07 PM
question about netflix throttling bbcrock@gmail.com DVD Video 86 07-22-2006 12:06 PM
Speedstep/CPU throttling Nik Schlein Computer Support 3 05-12-2005 06:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57