Managing A Programs’ CPU and Memory Utilization With cgroups

There are many days, especially days I have off from work, that I want to do a little ripping/encoding of my movie collection (still have a ways to go).  While MakeMKV will not tax my i5 2600k Intel processor, handbrake will, of course.  If you wish to still work on various average tasks on your system without a large amount of slowdown, you will want to setup cgroups to handle either handbrake or MakeMKV’s CPU/Memory constraints.  I will also go through an alternative universal method.  Read on for more

Using cgroups via systemd

Thankfully, systemd makes process control amazingly easy.  I will be going through an example here with a group I created for video encoding/decoding.  Your utilization numbers of what I will set myself may be different, and can be modified to your liking.  The full set of wiki steps can be found on the Arch Wiki here.

First, we will need to install the appropriate utility from the AUR (libcgroups).  Please be sure to modify the wget target if it changes:


wget https://aur.archlinux.org/packages/li/libcgroup/libcgroup.tar.gz
tar -xvzf libcgroup.tar.gz
cd libcgroup.tar.gz
make -csi

After libcgroups is installed, we can move on to the next set of steps.  First, let’s create my “encoding” cgroup.  Replace $USER with your desired username.

cgcreate -a $USER -g memory,cpu:encoding

Easy right?  Next, we will modify both the cpu and memory contraints for the processes that will make use of this group.  The memory constraint must be written in bytes (i.e. 10MB being 10000000).  The CPU constraint will be handled in what cgroups calls “shares.”  There is a total of 1024 shares, with 100 shares roughly being ~10 percent of all CPU utilization.

I will be setting the cgroup “encoding” with a limit of 8 GB of memory, and 700 “shares” of CPU processing.  With that understood, here are my example settings.

echo 8589934592 > /sys/fs/cgroup/memory/encoding/memory.limit_in_bytes
echo 700 > /sys/fs/cgroup/cpu/encoding/cpu.shares

That’s all you have to do for a basic setup.  To run handbrake under this group, perform the following command in the Terminal for handbrake:

cgexec -g memory,cpu:handbrake ghb &

A simple way to handle this is to create yourself a shortcut anywhere on your desktop or system to launch this command via bash script:

vi handbrake_cgroup.sh

The contents are as follows:

!/bin/bash
cgexec -g memory,cpu:encoding ghb &

Alternatively,To make this script executable without asking for the root password, edit access using the ‘visudo command’ adding the line:


YOU_USER ALL = (ALL) NOPASSWD: /home/YOU_USER/handbrake_cgroup.sh

Now you can add a shortcut anywhere in the system and launch the process using cgroup with no passwod.  From a security standpoint this is not recommended, but for some home users that are in control of their systems, it may.

Setting up your group to run on startup

If you wish to set the cgroup persistently for when you boot you machine, you can modify “/etc/cgconfig” as such:

group groupname {
  perm {
# who can manage limits
    admin {
      uid = $USER;
    }
# who can add tasks to this group
    task {
      uid = $USER;
    }
  }
# create this group in cpu and memory controllers
  cpu { }
  memory { }
}

group groupname/foo {
  cpu {
    cpu.shares = 100;
  }
  memory {
    memory.limit_in_bytes = 10000000;
  }
}

An alternative method

Also, you can manage cpu utilization with ‘cpulmit‘   Archlinux contains this package under the default repository set.  As an example, here is a command to limit Firefox:

cpulimit -e firefox -l 30

This command will limit the firefox process to 30% of the CPU’s utilization.  If you wish to use the PID of the process, use the ‘-p’ option instead of the ‘-e’ option.  If is recommended to run cpulmit as a root user for better control, but it is not completely necessary.

Using cpulimit with SMP (symetric multi-core processor) Systems

The project’s page specifically notes:

"If your machine has one processor you can limit the percentage from 0% to 100%,
which means that if you set for example 50%, your process cannot use more than 
500 ms of cpu time for each second. But if your machine has four processors, 
percentage may vary from 0% to 400%, so setting the limit to 200% means to use 
no more than half of the available power. In any case, the percentage is the 
same of what you see when you run top."

Final Thoughts

With that in mind, you should be all set using either cgroups or cpulimit (only handling CPU limits of course).  I highly suggest you use cgroups if possible.  it is far* more capable and customizable.  If you should have any questions of comments, be sure to leave them below, or for personalized support, see the “Linux Support” link at the to of the site

-mikeyd

Advertisements

About professorkaos64

www.libregeek.org

Posted on 20131227, in How-To and tagged , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s