banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Tip Top Tips: Is Your Handbrake Too Hot to Handle? Try cpulimit


Editor's Note: Tip Top Tips is a semi-monthly column in The PCLinuxOS Magazine. Periodically, we will feature – and possibly even expand upon – one tip from the PCLinuxOS forum. The magazine will not accept independent tip submissions specifically intended for inclusion in the Tip Top Tips column. Rather, if you have a tip, share it in the PCLinuxOS forum's “Tips & Tricks” section. Occasionally, we may run a “tip” posted elsewhere in the PCLinuxOS forum. Either way, share your tip in the forum, and it just may be selected for publication in The PCLinuxOS Magazine.


This month's tip comes from horusfalcon – KG5WLH.


CPU Limiter
Image by talha khalil from Pixabay

Let's start with a statement of the problem. I use the Handbrake media transcription program (okay… it's a disk ripper) more than anyone really should. I back up my DVDs to Matroska video files and store them on my Media Center, an old Dell Precision T3500 (haven't cracked the code for Blu-Ray yet, but I do have a USB 3.2 BD-R drive).

Well, I tend to actually use Handbrake on my laptop, since it can rip a 90-minute movie in under 30 minutes (to a 720p .MKV with no restorative presets or filtering). The problem was that it would cause my CPU to approach/exceed 200 deg. F (just a fuzz over 93 deg. C).

In troubleshooting, I went through a whole bunch of changes: heatsink paste and pads, a new heatsink/fan assembly, you know, the usual suspects. Somewhere in my observations with Handbrake running, I noticed CPU Zero was the sensor reading highest, with all others in more normal temperature ranges. This made me think that Handbrake was not using all the CPUs, and only CPU Zero was “toting the load”. In hindsight, I can tell you this is not the case. Load is definitely not equal, but ALL the CPUs were (and still are) being utilized.

I started casting about for a solution, and nothing seemed promising until I tried cpulimit. For those unfamiliar, here's the description of the cpulimit package in Synaptic:

CPU Usage Limiter
cpulimit is a simple program that attempts to limit the cpu usage of a process (expressed in percentage, not in cpu time). This is useful to control batch jobs, when you don't want they eat too much cpu. It does not act on the nice value or other scheduling priority stuff, but on the real cpu usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.


/usr/share/doc/cpulimit/README has more info.


I did some reading and research about this, and it's just what it says on the tin. What it doesn't say (but should be obvious) is that cpulimit is a utility for use from the Linux shell prompt or in scripts.

Rather than remember all the switches and so on, I edited the Launcher on my Xfce desktop for Handbrake after I got through testing. It's easy enough on Plasma, and on Xfce — right-click on the Launcher (aka: “shortcut”) and select Edit Launcher. A small dialog opens up, showing in one field the Command being run. Mine, after editing, looked like this:

cpulimit -l 580 -b ghb

The -l 580 switch sets the maximum CPU load made available to the program it will call. (That is not -I. It's a “minus lowercase ell”.)

The -b switch puts the target program (ghb is Handbrake) in the background.

(I had previously appended %f at the end of this command string for some reason. parnote was kind enough to point out that %f is an Xfce parameter that means the launcher is expecting a source file name. Since Handbrake needs to scan the source, usually a DVD, and has a special mechanism for this, it's not really needed.)

A note about cpulimit's -l switch values. Each CPU is limited over a scale from 0 (don't use) to 100 (gimme all you've got). My machine is a quad-core Xeon with Hyperthreading enabled, so it presents as 4 real and 4 virtual cores (threads). That's a total of 8 cores as far as cpulimit is concerned.

Eight times 100 yields 800, which represents the total work output of all “cores”. So the value 580 means that cpulimit throttles Handbrake to 580/800 or 72.5% of the Xeon's total output. I determined this value by trial and error, after testing and observation, as the best compromise between thermal control and good performance. You, if you are going to use cpulimit, should get to know your hardware so you can set up reasonable values for the -l switch.

With this setup there is one drawback: If you do an “open with Handbrake” without using the Launcher shortcut, all your limits are no longer in play, Handbrake will be the greedy little CPU hog it is once more, and things may get hotter than you'd like.

{Mauro was kind enough to mention that making the ghb command that starts Handbrake into an alias can resolve this problem. I should have realized this: I've used aliases in the distant past. Thanks, man.}

To create the alias, I edited the file ~/.bashrc, adding this line and a carriage return to the end of the existing file, then saving:

alias ghb='cpulimit -l 580 -b ghb'

I encountered high temperatures this afternoon (06 Sep 2024) on a ripping job, and looking at what happened, I decided to undo this alias. Here's why:

The original alias line added to my ~/.bashrc was:

alias ghb='cpulimit -l 580 -b ghb'


What I believe may be taking place is a circular reference: the alias ghb refers to the program ghb. Until I can figure out what I'm doing incorrectly here, I have retreated to using the command:

cpulimit -l 580 -b ghb

from my Handbrake Launcher.

{For more on using aliases, see: Linux Alias Command . Note that the author of this article refers to single quotes as “inverted commas”, and may not be a native speaker of English, but the info there is good.}

How does cpulimit do all this? I could tell you what little I know (basically in the package description above), but that is not likely to be useful. I'm usually more interested in the results, and it does work. The best I can tell from what I've observed using it like this for about a month is that cpulimit acts in some way to make the load on the entire CPU a bit more balanced. CPU Zero is still the hottest of the lot, but is much better behaved, with peak temperatures running somewhere from 160 F to about 180 F with occasional spikes to 187 F [86 C] (highest observed so far). That little Xeon is tough — she can handle it.

The trade-off is that the ninety-minute movie rip I mentioned earlier that took about 30 minutes got about 4 to 6 minutes longer. Small tradeoff for extended system life, at least to me.

Here's hoping cpulimit works for you. Its manpage is not in the PCLinuxOS man-pages package. It may be found online here.



Previous Page              Top              Next Page