Currently I've a programme taking up too much of CPU.
How could I limit the amount of CPU it occupies?
OS is Linux, Fedora.
I can't modify the source code of that programme.
What I need is a Bash command.
Currently I've a programme taking up too much of CPU.
How could I limit the amount of CPU it occupies?
OS is Linux, Fedora.
I can't modify the source code of that programme.
What I need is a Bash command.
You can try limiting your program by lowering the priority with nice. No programming involved there.
Running at "50%" CPU isn't that meaningful. You want the program to use every resource possible when it's available. If the CPU isn't doing anything else, that program might as well make full use of it. If you wanted the program to really do nothing at all, you'd have to modify the source code and put in pauses/sleeps where possible.
What you want is to have everything else have higher priority. See the manpage for the nice command, run it at nice 19
Another and possibly more effective way of limiting resources is to install the schedutils package, and run the program using the SCHED_BATCH process scheduler.
Setrlimit and co ...
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
int main ()
{
struct rlimit rl;
/* Obtain the current limits. */
getrlimit (RLIMIT_CPU, &rl);
/* Set a CPU limit of 1 second. */
rl.rlim_cur = 1;
setrlimit (RLIMIT_CPU, &rl);
/* Do busy work. */
while (1);
return 0;
}
From here
You're looking for something simple and fast? Try the cpulimit program. Just run:
cpulimit name-of-program
and voila, it's limited.
If you don't want to modify the program another option to consider is virtualisation.
If you want to limit a process's cpu, based on the concept of percentage, consider cpulimit.
You can manually do time-slicing within the application by using a high-performance timer and measuring how much time each iteration of a top level loop is taking, then put in appropriate sleeps (or nanosleep's) in that loop. This wont correlate directly to a percentage of CPU, especially across machines, but it will limit the cpu resources that the program takes.
Take a look at Control Groups.
LWN has an article about them.
Redhat, Fedora, CentOS have an RPM package named libcgroup that has several handy command-line tools, a system daemon and some config files to manage control groups.
This is based on libcg hosted on SourceForge.