A Weird Imagination

Limit web browser processor usage

Posted in

The problem#

cpulimit is a useful utility for stopping a program from wasting CPU, but it only limits a single process. As all modern web browsers use process isolation, limiting just a single process doesn't do very much, we actually want to limit all of the browser processes.

The solution#

The following script will limit the CPU usage of all browser processes to $LIMIT percent CPU. Note that the limit is per process not total over all processes, so you may want to set it quite low to actually have an effect.

LIMIT=10 # Hard-code a limit of 10% CPU as an example.

# Kill child processes (stop limiting CPU) on script exit.
for sig in INT QUIT HUP TERM; do
  trap "
    pkill -P $$
    trap - $sig EXIT
    kill -s $sig "'"$$"' "$sig"
done
trap cleanup EXIT

# Find and limit all child processes of all browsers.
for name in firefox firefox-esr chromium chrome
do
    for ppid in $(pgrep "$name")
    do
        cpulimit --pid="$ppid" --limit="$LIMIT" &
        for pid in "$ppid" $(pgrep --parent "$ppid")
        do
            cpulimit --pid="$pid" --limit="$LIMIT" &
        done
    done
done

The details#

Script description#

The script is fairly straight-forward: it has a list of names of browser processes to look for and uses pgrep to find instances of them. In the middle loop, it uses pgrep's --parent option to enumerate all processes whose parent is one of those browser processes. Then it launches a separate cpulimit process to limit each of those. The code at the top is based on my recent post on killing child jobs on script exit to kill all of the cpulimit processes, and therefore stop limiting the CPU of all of the browser processes, when the script is killed.

One notable limitation of this approach is that it only limits processes that exist when the script is run. If you keep using the browser, it will probably spawn new processes which won't be limited. This wasn't relevant to the reason I made the script originally, but I may investigate augmenting the script to cover this case in a future blog post.

--monitor-forks#

cpulimit has an option --monitor-forks which is documented as follows:

watch and throttle child processes of the target process Warning: It is usually a bad idea to use this flag on a shell script. The commands in the script will each spawn a process which will, in turn, spawn more copies of this program to throttle them, bogging down the system.

While it sounds like it should do the right thing... the warning is accurate: its effect is to bog down the CPU with running cpulimit, which is quite counter-productive.

Browser tab/task manager#

It's possible the appropriate course of action isn't to blanket limit CPU for every tab. Firefox's task manager or the equivalent in Chromium/Chrome can be used to view their resource usage by subprocess to see which tabs are using processor power. The solution may be to just close a tab or not use websites that waste CPU power.

Comments

Have something to add? Post a comment by sending an email to comments@aweirdimagination.net. You may use Markdown for formatting.

There are no comments yet.