6 Replies - 3376 Views - Last Post: 03 June 2008 - 10:06 PM Rate Topic: -----

#1 Godzilla  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-May 08

Any way to limit CPU use?

Posted 03 June 2008 - 12:01 AM

Is there any way to limit/cap CPU usage by a program/script in C++?

Note that this is on Linux, and using 'nice', which lowers priority, doesn't seem to have much effect.

Also, is there any way in C++ to know how much of the CPU (e.g. percent, raw or number of cycles) a program is currently using?
Is This A Good Question/Topic? 0
  • +

Replies To: Any way to limit CPU use?

#2 geolej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 02-June 08

Re: Any way to limit CPU use?

Posted 03 June 2008 - 12:05 AM

well depends how i know in a while loop for exapmle you can decrease 100% cpu usage to almost 0% by doing this
while()
}
sleep(10);


i might have messed up there somewhere but it is effective, on a noob level
noob level=me
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Any way to limit CPU use?

Posted 03 June 2008 - 12:14 AM

geolej, what he is asking about is partitioning out the cpu.

Godzilla, you can get the cpu usage from the top command. But I'm not sure how to change it's processing. Are you asking about a per user, or per program basis? Also, are you asking about a single or multi cpu machine? From what I have heard, the Korn shell is supposed to have advanced multi processing capabilities, like fireing off subshells & such, but I don't have any experience with it at that level.
Was This Post Helpful? 0
  • +
  • -

#4 Godzilla  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-May 08

Re: Any way to limit CPU use?

Posted 03 June 2008 - 12:35 AM

no2pencil is right, I am talking about partitioning the CPU. Good terminology :) I don't think sleep would work all that well for my purposes.

I do use top, but I meant finding usage somehow within the program itself via some function. I don't know if a function or library for that exists or not.

I'm speaking in a per program basis, not per user.

It's a multi-processor machine, but I'm not sure how much that matters; it's a web server that has lots apache processes running all the machine. I believe the server uses the Bourne shell, not Korn, which I'm not familiar with.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Any way to limit CPU use?

Posted 03 June 2008 - 12:37 AM

Since apache runs as a daemon, it would be beyond the scope of any advantages that the shell would have to offer.

I was asking about per user, because I saw a presentation on an AS400 a while back, & they partitioned off everything. If you could do it on a per user basis, you could offer a percentage of the CPU to the specified user that apache launches under.

Sorry I couldn't offer much more help.

** update **

I did find this on the net. I've never used, nor heard of this, so you are on your own.
Was This Post Helpful? 0
  • +
  • -

#6 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Any way to limit CPU use?

Posted 03 June 2008 - 07:43 PM

This is old, but it may be of interest:

http://freshmeat.net...jects/cpupatch/

Godzilla said:

Note that this is on Linux, and using 'nice', which lowers priority, doesn't seem to have much effect.


On what? CPU time reported by top, or on performance? If nothing else is in the running state, it will still get most of the CPU time (meaning, everything else is blocked, giving up their respective timeslices). Where the priority makes a difference is when processes of a higher priority are in a run state (not waiting on I/O). In those cases, the higher priority processes will be allotted more time than the lower ones in a schedule cycle. CPU time that is not used is wasted.

If you have two processes of equal priority in the run state, they will get about 50% of the slice each. If one is higher than the other, then that one will get a higher percentage than the other. If only one is running (and is in the run state, not blocked on I/O), it will get 100% as almost nothing else is actually needing it.


Quote

while()
}
sleep(10);


I believe even sleep(0); should have a dramatic impact, as I believe this causes the process to relinquish its timeslice. I know it does on Windows, but I don't know if this is true of Linux.

This post has been edited by perfectly.insane: 03 June 2008 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Any way to limit CPU use?

Posted 03 June 2008 - 10:06 PM

It is possible to limit your process' use of the CPU. That is to say it is possible to ask the OS to limit a thread you control. It may even be possible to do more but these functions would really be very low level in the OS.

This means that you need to track it down at an operating system level. What works on windows with not work on linux will not work on a mac etc. Chances are at that level you may even find that your solution is very kernel specific.

So in short: Can it be done? Sure but it would be a nightmare to support and maintain.

Why would you want to?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1