Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #19910 > unrolled thread
| Started by | jon.harding@gmx.co.uk |
|---|---|
| First post | 2012-04-25 11:22 -0700 |
| Last post | 2012-04-25 21:39 +0200 |
| Articles | 18 — 10 participants |
Back to article view | Back to comp.lang.c
Question of throttling CPU usage jon.harding@gmx.co.uk - 2012-04-25 11:22 -0700
Re: Question of throttling CPU usage James Kuyper <jameskuyper@verizon.net> - 2012-04-25 14:44 -0400
Re: Question of throttling CPU usage jon.harding@gmx.co.uk - 2012-04-25 12:35 -0700
Re: Question of throttling CPU usage Thomas Richter <thor@math.tu-berlin.de> - 2012-04-25 21:39 +0200
Re: Question of throttling CPU usage James Kuyper <jameskuyper@verizon.net> - 2012-04-25 15:59 -0400
Re: Question of throttling CPU usage jon.harding@gmx.co.uk - 2012-04-25 15:33 -0700
Re: Question of throttling CPU usage Ian Collins <ian-news@hotmail.com> - 2012-04-26 10:37 +1200
Re: Question of throttling CPU usage jon.harding@gmx.co.uk - 2012-04-26 01:41 -0700
Re: Question of throttling CPU usage Robert Wessel <robertwessel2@yahoo.com> - 2012-04-26 04:25 -0500
Re: Question of throttling CPU usage James Kuyper <jameskuyper@verizon.net> - 2012-04-26 06:59 -0400
Re: Question of throttling CPU usage Robert Miles <milesrf@Usenet-News.net> - 2012-05-27 23:35 -0500
Re: Question of throttling CPU usage Nobody <nobody@nowhere.com> - 2012-04-26 14:33 +0100
Re: Question of throttling CPU usage Fred K <fred.l.kleinschmidt@gmail.com> - 2012-04-26 07:44 -0700
Re: Question of throttling CPU usage James Kuyper <jameskuyper@verizon.net> - 2012-04-26 11:45 -0400
Re: Question of throttling CPU usage Nobody <nobody@nowhere.com> - 2012-04-26 14:27 +0100
Re: Question of throttling CPU usage tom st denis <tom@iahu.ca> - 2012-04-26 12:01 -0700
Re: Question of throttling CPU usage Robert Miles <milesrf@Usenet-News.net> - 2012-05-27 23:19 -0500
Re: Question of throttling CPU usage Lanarcam <lanarcam1@yahoo.fr> - 2012-04-25 21:39 +0200
| From | jon.harding@gmx.co.uk |
|---|---|
| Date | 2012-04-25 11:22 -0700 |
| Subject | Question of throttling CPU usage |
| Message-ID | <6e2a5c39-7a60-4ce1-a609-4211213782bc@er9g2000vbb.googlegroups.com> |
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.
Thank you
Jon
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-04-25 14:44 -0400 |
| Message-ID | <4F984620.8000006@verizon.net> |
| In reply to | #19910 |
On 04/25/2012 02:22 PM, jon.harding@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.
[toc] | [prev] | [next] | [standalone]
| From | jon.harding@gmx.co.uk |
|---|---|
| Date | 2012-04-25 12:35 -0700 |
| Message-ID | <4384689a-5010-4a41-94eb-a747dad00f6d@j3g2000vba.googlegroups.com> |
| In reply to | #19911 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas Richter <thor@math.tu-berlin.de> |
|---|---|
| Date | 2012-04-25 21:39 +0200 |
| Message-ID | <jn9jtm$oui$1@news.belwue.de> |
| In reply to | #19912 |
On 25.04.2012 21:35, jon.harding@gmx.co.uk wrote: > 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. But there we have exactly the problem: There is no portable way of "doing nothing" in C. You somehow need to use the operating system for that, or any other library outside of the C standard. Greetings, Thomas
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-04-25 15:59 -0400 |
| Message-ID | <4F9857B3.3080402@verizon.net> |
| In reply to | #19912 |
On 04/25/2012 03:35 PM, jon.harding@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.
>
> 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.
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.
In your example:
int i;
while(1) {
i++;
}
This code has undefined behavior for two separate reasons. First of all,
'i' is uninitialized; that means that it could, among other
possibilities, have a trap representation. Any attempt to read the value
of an object containing a trap representation has undefined behavior. On
a system where 'int' has no trap representations, which is most
real-world systems, that's probably not a problem, but you should get in
the habit of always initializing every variable sometime before the
first time that its value is read.
The second reason is that your program keeps incrementing 'i' until it
reaches INT_MAX, and then executes i++ one more time. As soon as it does
that, the behavior is undefined. In fact, as soon as it becomes
inevitable that this will happen (which is the moment your program
starts) the behavior is undefined. Again, on many systems this will do
something innocuous, such as set i to INT_MIN, but there's no guarantees
that the result will be that painless. You can avoid both problems by using
unsigned u = 0;
while(1) {
u++;
}
The behavior of u++ when u is UINT_MAX is well-defined; it sets u to 0.
Because your code makes no use of the value of i after the end of the
loop, a good optimizing compiler can convert your code into the
equivalent of:
int main(void) { while(1); }
It's been argued that a really good optimizing compiler can execute an
infinite empty loop in a finite amount of time, since infinity*0 has no
well defined meaning. The mathematical part of that argument is suspect,
but the C standard never specifies anything about how long your program
should take to execute, so it is arguably the case that such a compiler
could implement your program as the equivalent of
int main(void) { }
That's pretty much just a joke, but the underlying truth behind that
joke is that with modern optimizing compilers, the only way to make sure
that your loop actually wastes time doing something, is to make sure
that the thing it does can't be optimized away.
Getting back to my main point: if the thing your loop is doing can't be
optimized away, then it doesn't qualify as "do nothing".
[toc] | [prev] | [next] | [standalone]
| From | jon.harding@gmx.co.uk |
|---|---|
| Date | 2012-04-25 15:33 -0700 |
| Message-ID | <4543ab86-aafd-4500-94f3-0fd954c2de51@f5g2000vbt.googlegroups.com> |
| In reply to | #19916 |
On Apr 25, 8:59 pm, James Kuyper <jameskuy...@verizon.net> wrote:
> On 04/25/2012 03: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.
>
> > 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.
>
> 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.
>
> In your example:
>
> int i;
> while(1) {
> i++;
> }
>
> This code has undefined behavior for two separate reasons. First of all,
> 'i' is uninitialized; that means that it could, among other
> possibilities, have a trap representation. Any attempt to read the value
> of an object containing a trap representation has undefined behavior. On
> a system where 'int' has no trap representations, which is most
> real-world systems, that's probably not a problem, but you should get in
> the habit of always initializing every variable sometime before the
> first time that its value is read.
>
> The second reason is that your program keeps incrementing 'i' until it
> reaches INT_MAX, and then executes i++ one more time. As soon as it does
> that, the behavior is undefined. In fact, as soon as it becomes
> inevitable that this will happen (which is the moment your program
> starts) the behavior is undefined. Again, on many systems this will do
> something innocuous, such as set i to INT_MIN, but there's no guarantees
> that the result will be that painless. You can avoid both problems by using
>
> unsigned u = 0;
> while(1) {
> u++;
> }
>
> The behavior of u++ when u is UINT_MAX is well-defined; it sets u to 0.
>
> Because your code makes no use of the value of i after the end of the
> loop, a good optimizing compiler can convert your code into the
> equivalent of:
>
> int main(void) { while(1); }
>
> It's been argued that a really good optimizing compiler can execute an
> infinite empty loop in a finite amount of time, since infinity*0 has no
> well defined meaning. The mathematical part of that argument is suspect,
> but the C standard never specifies anything about how long your program
> should take to execute, so it is arguably the case that such a compiler
> could implement your program as the equivalent of
>
> int main(void) { }
>
> That's pretty much just a joke, but the underlying truth behind that
> joke is that with modern optimizing compilers, the only way to make sure
> that your loop actually wastes time doing something, is to make sure
> that the thing it does can't be optimized away.
>
> Getting back to my main point: if the thing your loop is doing can't be
> optimized away, then it doesn't qualify as "do nothing".
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.
Thank you again
James
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2012-04-26 10:37 +1200 |
| Message-ID | <9vrclmFi68U11@mid.individual.net> |
| In reply to | #19932 |
On 04/26/12 10:33 AM, jon.harding@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
[toc] | [prev] | [next] | [standalone]
| From | jon.harding@gmx.co.uk |
|---|---|
| Date | 2012-04-26 01:41 -0700 |
| Message-ID | <0c88c621-fe73-4530-beda-50b52b0c504f@l3g2000vbv.googlegroups.com> |
| In reply to | #19934 |
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? 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% Ideally something from sourceforge would be ideal to wrap my application but I cannot find anything. Thank you Jon
[toc] | [prev] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2012-04-26 04:25 -0500 |
| Message-ID | <414ip7lbtcgf9djm6aceicbreosniqi8rk@4ax.com> |
| In reply to | #19947 |
On Thu, 26 Apr 2012 01:41:06 -0700 (PDT), jon.harding@gmx.co.uk 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? > >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% > >Ideally something from sourceforge would be ideal to wrap my >application but I cannot find anything. As mentioned, this is all OT for c.l.c: Mostly assuming Windows: There are a number of applications for both Linux and Windows that do something similar to what you're requesting. Process Tamer and Process Lasso are two for Windows, I offer no recommendations, just their names, if you want to look at something in the genre. First, do you actually want to hard limit your CPU utilization to (say) 10%, or do you want to use no more than 10% so long as other applications don't want the CPU (a soft cap)? If you're doing this from within your application, you can watch the amount of CPU time your application is using via the GetSystemTimes() function or the System.Diagnostics stuff. You can then decide what to do with that information, periodically suspending the thread (for hard limits) or raising or lowing its priority (for soft limits), so that utilization stays withing your desired cap. You also have to consider how threads, processes and multiple cores all impact that too. On Windows 7 and WS08R2 (might be in Vista too), there is hard quota support on a per-user basis built in. If you're running one of those you can set that up, and just run the application(s) under a login (use "runas" or similar, as appropriate). http://technet.microsoft.com/en-us/library/ff384148(v=ws.10).aspx Alternatively you could just set the priority of your thread or process to less than normal, and other applications will preempt your program if they're ready to run.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-04-26 06:59 -0400 |
| Message-ID | <jnb9qe$es3$1@dont-email.me> |
| In reply to | #19947 |
On 04/26/2012 04:41 AM, jon.harding@gmx.co.uk 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. -- James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | Robert Miles <milesrf@Usenet-News.net> |
|---|---|
| Date | 2012-05-27 23:35 -0500 |
| Message-ID | <4fc300fb$0$17031$882e7ee2@usenet-news.net> |
| In reply to | #19953 |
On 4/26/2012 5:59 AM, James Kuyper wrote: > On 04/26/2012 04:41 AM, jon.harding@gmx.co.uk 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
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-04-26 14:33 +0100 |
| Message-ID | <pan.2012.04.26.13.33.31.515000@nowhere.com> |
| In reply to | #19947 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Fred K <fred.l.kleinschmidt@gmail.com> |
|---|---|
| Date | 2012-04-26 07:44 -0700 |
| Message-ID | <30980468.232.1335451461360.JavaMail.geo-discussion-forums@pbcrp8> |
| In reply to | #19960 |
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 them to use 85% of the CPU time?
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-04-26 11:45 -0400 |
| Message-ID | <4F996DA1.3020209@verizon.net> |
| In reply to | #19963 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-04-26 14:27 +0100 |
| Message-ID | <pan.2012.04.26.13.27.18.157000@nowhere.com> |
| In reply to | #19916 |
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).
[toc] | [prev] | [next] | [standalone]
| From | tom st denis <tom@iahu.ca> |
|---|---|
| Date | 2012-04-26 12:01 -0700 |
| Message-ID | <90356058-20ec-4751-aaf4-a3003781e8ef@m13g2000yqi.googlegroups.com> |
| In reply to | #19912 |
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
[toc] | [prev] | [next] | [standalone]
| From | Robert Miles <milesrf@Usenet-News.net> |
|---|---|
| Date | 2012-05-27 23:19 -0500 |
| Message-ID | <4fc2fd16$0$22618$882e7ee2@usenet-news.net> |
| In reply to | #19912 |
On 4/25/2012 2:35 PM, jon.harding@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.
>
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Lanarcam <lanarcam1@yahoo.fr> |
|---|---|
| Date | 2012-04-25 21:39 +0200 |
| Message-ID | <4f98530d$0$13185$426a74cc@news.free.fr> |
| In reply to | #19911 |
Le 25/04/2012 20:44, James Kuyper a écrit :
> On 04/25/2012 02:22 PM, jon.harding@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.
This is not only system dependant but also processor dependant. For
instance Cortex M0 processor have instructions for entering sleep
mode.
"Wait for interrupt
The Wait For Interrupt instruction, WFI, causes immediate entry
to sleep mode. When the processor executes a WFI instruction it
stops executing instructions and enters sleep mode. See WFI for
more information.
Wait for event
The Wait For Event instruction, WFE, causes entry to sleep
mode conditional on the value of a one-bit event register.
When the processor executes a WFE instruction, it checks
the value of the event register:
0
The processor stops executing instructions and enters sleep
mode
1
The processor sets the register to zero and continues
executing instructions without entering sleep mode."
Obviously, the OS or RTOS must support those instructions.
<http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0497a/BABHHGEB.html>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web