9

I'd like to limit the CPU usage of a process to, say, 10%.

One way this could be done is running the process for 6 continuous seconds every minute and pausing it the rest of the time.

I'm not looking to change the process priority with nice.

0

3 Answers 3

6

cpulimit (newer github link here) seems to be the tool for the job - it lets you control the processor usage of a process by PID, process name or absolute location, and set its processor usage in terms of percentage of total.

From the docs on the sourceforge page -

Examples of use

Limit the process 'bigloop' by executable name to 40% CPU:

cpulimit --exe bigloop --limit 40
cpulimit --exe /usr/local/bin/bigloop --limit 40 

Limit a process by PID to 55% CPU:

cpulimit --pid 2960 --limit 55 

Launch a process by command line and limit it to 40% (in development version only!):

cpulimit --limit 40 /etc/rc.d/rc.boinc start 

Cyberciti.biz has a howto that also indicates you maybe able to use

-p : Process PID.
-e : Process name.
-l : percentage of CPU allowed from 0 to 100.
-P: absolute path name of the executable program file.

Instead of --exe and so on.

And that for multi processors, you should set the percentage based off 100 * number of cores - so a dual system can allow percentages up to 150, and a quad core can allow up to 200

3

As you didn't specified your operational system, I will just put here an option for Windows and another one for Linux:

For Linux:

Use Stress:

stress is a deliberately simple workload generator for POSIX systems. It imposes a configurable amount of CPU, memory, I/O, and disk stress on the system. It is written in C, and is free software licensed under the GPLv2.

Here is an example invocation: a load average of four is imposed on the system by specifying two CPU-bound processes, one I/O-bound process, and one memory allocator process.

   $ stress --cpu 2 --io 1 --vm 1 --vm-bytes 128M --timeout 10s --verbose
   stress: info: [9372] dispatching hogs: 2 cpu, 1 io, 1 vm, 0 hdd
   stress: dbug: [9372] (243) using backoff sleep of 12000us
   stress: dbug: [9372] (262) setting timeout to 10s
   stress: dbug: [9372] (285) --> hogcpu worker 9373 forked
   stress: dbug: [9372] (305) --> hogio worker 9374 forked
   stress: dbug: [9372] (325) --> hogvm worker 9375 forked
   stress: dbug: [9372] (243) using backoff sleep of 3000us
   stress: dbug: [9372] (262) setting timeout to 10s
   stress: dbug: [9372] (285) --> hogcpu worker 9376 forked
   stress: dbug: [9375] (466) hogvm worker malloced 134217728 bytes
   stress: dbug: [9372] (382) <-- worker 9374 signalled normally
   stress: dbug: [9372] (382) <-- worker 9373 signalled normally
   stress: dbug: [9372] (382) <-- worker 9375 signalled normally
   stress: dbug: [9372] (382) <-- worker 9376 signalled normally
   stress: info: [9372] successful run completed in 10s

For Windows:

Use BES – Battle Encoder Shirase 1.4.5 / 1.5.2:

Free software that controls per-process CPU usage. “Active” software CPU cooler. BES is a small tool that throttles the CPU usage for the process you “target”: for instance, you can limit the CPU usage of a process which would use CPU 100%, down to 50% (or any percentage you’d like). With this, you can use other programs comfortably while doing something CPU-intensive in the background.

By limiting the CPU load, you can also cool down your CPU immediately when it happens to get too hot. It’s an “active” software CPU cooler. Conventional soft-coolers save CPU energy by making CPU sleep when it is idle. They passively wait until CPU gets idle. BES is more aggressive: it cools CPU by making the “heating” process slow down, i.e. periodically forcing CPU to be idle for a short time. (But you should install a better hardware cooler if you often need to use BES for this purpose. Actually, the first thing you should try is to remove dust around your heat sink (See below). Active soft-cooling might be an interesting hack, but it's just a workaround after all. On the other hand, if your application uses CPU 100% meaninglessly, let BES do the job!)

enter image description here

You could also automate it using Windows command-line:

The following command line tells BES to watch target.exe, and—when target.exe starts running—throttle its CPU usage so that target.exe will be only allowed to use CPU (100 − percentage) % at most:

"C:\path\to\bes.exe" "D:\path to\target.exe" [percentage] [--minimize]

Optionally, you can use an integer between 1 and 99 as percentage. If percentage is omitted or invalid, the last known valid percentage for target.exe is automatically used (if this is the first time to target target.exe, the default reduction 33% will be applied, unless percentage is specified). Note that, when started from command line, BES will Watch/Limit the target, not just Limit it.

2
  • Battle Encoder Shirase is the coolest name for a software ever
    – Journeyman Geek
    Jun 29, 2012 at 13:01
  • 9
    What does stress have to do with this question? It adds new processes, which consume a fixed amount of processor time, but the question wanted to limit an existing application.
    – Ben Voigt
    Jun 29, 2012 at 13:17
0

For Windows: (This is just an expansion to @Diogo 's answer.)

As a Windows user wanting to just click on a single shortcut to have the desired target program run at the desired CPU throttle automatically, I found that it required a bit more steps involving a batch file and some clarification needed with regards to how exactly to implement the [percentage] and [--minimize] portions within the suggested command line:

"C:\path\to\bes.exe" "D:\path to\target.exe" [percentage] [--minimize]

The method I ended up using is as follows. Make a Windows batch file, create a file with a text editor and name it as "[name].bat" (e.g.: something.bat). Within the file have the following typed out:

@echo off 
start "" "C:\path\to\bes.exe" "D:\path to\target.exe" 90 -m
start "" "D:\path to\target.exe"

where:

  • start "", allows both the BES and target program to run at the same time.
  • "C:\path\to\bes.exe" is the exact path to the BES program within the quotes,
  • "D:\path to\target.exe" is the exact path to the program you want to throttle CPU with. within the quotes,
  • 90 is a percentage of reduction, in this case means reducing the CPU limit to 100-90 = 10%
  • -m is to start up the BES program minimized (as per BES instructions: "The --minimize option, or -m for short, tells BES to start as minimized (sent to the system tray)."

Then double-clicking the resulting .bat file will run the target program while automatically be throttled to the desired CPU level with BES.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .