CPUTool - Limit and Control CPU Utilization of Any Process in Linux


CPU utilization is an important factor that determines the performance of a system. When there are multiple processes running on a system, the CPU can become overloaded, which can lead to slow system performance or even crashes. To avoid this, it is essential to control and limit the CPU utilization of individual processes.

CPUTool is a command-line tool that enables you to limit and control CPU utilization of any process running on a Linux system. It works by setting the CPU affinity and priority of the process, allowing you to allocate a specific percentage of the CPU's processing power to a particular process.

In this article, we will discuss how to install and use CPUTool to limit and control CPU utilization of any process in Linux.

Installation of CPUTool

CPUTool is available in the repositories of most Linux distributions. You can install CPUTool using the package manager of your distribution. For instance, if you are using Ubuntu or Debian, you can install CPUTool by running the following command in the terminal −

sudo apt-get install cpuset

For CentOS and other RedHat-based distributions, you can install CPUTool by running the following command −

sudo yum install cpuset

Basic Usage of CPUTool

Once you have installed CPUTool, you can start using it to control the CPU utilization of any process running on your system. The basic syntax for using CPUTool is as follows −

sudo cpuset -p PROCESS_ID -l CPU_LIST -u CPU_LIST

Here, PROCESS_ID is the ID of the process you want to control, CPU_LIST is the list of CPU cores you want to allocate to the process, and -u is the CPU share that you want to allocate to the process. By default, each process has a CPU share of 1024, which means that it can utilize 100% of the CPU's processing power.

To limit the CPU utilization of a process, you need to reduce its CPU share. For example, if you want to limit the CPU utilization of a process with ID 1234 to 50%, you can run the following command −

sudo cpuset -p 1234 -u 512

This will allocate 50% of the CPU's processing power to the process with ID 1234. You can also set the CPU affinity of the process by specifying the CPU cores that you want to allocate to the process. For instance, if you want to allocate only CPU core 0 and 1 to the process with ID 1234, you can run the following command −

sudo cpuset -p 1234 -l 0,1

This will allocate only CPU core 0 and 1 to the process with ID 1234.

Advanced Usage of CPUTool

CPUTool also provides advanced features that enable you to control the CPU utilization of multiple processes simultaneously. You can group processes into a "cpuset" and allocate a specific CPU share to the cpuset.

To create a cpuset, you need to create a directory in the /sys/fs/cgroup/cpuset/ directory with the name of the cpuset. For instance, if you want to create a cpuset named "mycpuset", you can run the following command

sudo mkdir /sys/fs/cgroup/cpuset/mycpuset

You can then move processes into the cpuset by writing their process IDs to the tasks file of the cpuset. For instance, if you want to move a process with ID 1234 to the "mycpuset" cpuset, you can run the following command −

echo 1234 | sudo tee /sys/fs/cgroup/cpuset/mycpuset/tasks

You can limit the CPU utilization of the cpuset by writing a value to the cpu.shares file of the cpuset. The value represents the relative share of CPU time that the cpuset can use compared to other cpusets. For instance, if you want to limit the CPU utilization of the "mycpuset" cpuset to 20%, you can run the following command −

echo 20 | sudo tee /sys/fs/cgroup/cpuset/mycpuset/cpu.shares

This will limit the CPU utilization of all processes in the "mycpuset" cpuset to 20% of the total CPU time.

You can also limit the CPU utilization of all processes in the system using CPUTool. To do this, you need to set the CPU utilization limit for the "system" cpuset. You can do this by running the following command −

sudo cpuset -c system -l 50

This will limit the CPU utilization of all processes in the system to 50%. You can adjust the value to suit your requirements.

Setting CPU limits for a specific user

CPUTool enables setting CPU limits for specific users or groups without affecting the entire system. It's useful in situations where you want to restrict the CPU utilization of a particular user or group of users. Follow these steps to set CPU limits for a user −

  • Create a cpuset for the user −

    Create a new directory in the /sys/fs/cgroup/cpuset/ directory with the name of the user. For instance, if you want to create a cpuset for a user named "user_name," run the following command −

sudo mkdir /sys/fs/cgroup/cpuset/user_name
  • Set the CPU shares for the cpuset −

    Allocate CPU shares to the user's cpuset by writing a value to the cpu.shares file. For instance, to allocate 100 CPU shares to the user_name cpuset, run the following command −

sudo echo "100" > /sys/fs/cgroup/cpu/user_name/cpu.shares
  • Move the user's processes to the cpuset −

    Move all the processes of the user to the newly created cpuset by writing their process IDs to the tasks file of the cpuset. For instance, if you want to move all the processes of the user_name to the cpuset, run the following command −

sudo pgrep -u user_name | sudo tee /sys/fs/cgroup/cpuset/user_name/tasks > /dev/null

This will move all the processes of the user_name to the cpuset.

Limiting CPU Utilization with CPUTool and Systemd

To limit the CPU utilization of a process or a group of processes in Linux, you can use CPUTool in combination with Systemd. The first step is to create a Systemd service file for the process you want to limit by opening the file with a text editor like nano using the command sudo nano /etc/systemd/system/myprocess.service. In this file, you should add the process's unit description and service details, including enabling CPU accounting and the path to the executable file of the process.

After creating the Systemd service file, you should reload the Systemd configuration using the command sudo systemctl daemon-reload and start the service using the command sudo systemctl start myprocess. You can verify that the service is running correctly by checking its status using the command sudo systemctl status myprocess.

Next, create a cpuset for the process by creating a directory with the process name in the /sys/fs/cgroup/cpuset/ directory using the command sudo mkdir /sys/fs/cgroup/cpuset/myprocess. Set the CPU shares for the cpuset using the command sudo echo "100" > /sys/fs/cgroup/cpu/myprocess/cpu.shares. The CPU shares determine the proportion of the CPU time allocated to the cpuset.

Finally, move the process to the cpuset using the command sudo pgrep myprocess | sudo tee /sys/fs/cgroup/cpuset/myprocess/tasks > /dev/null. This will move the process to the cpuset and limit its CPU utilization according to the CPU shares assigned to the cpuset.

By following these steps, you can effectively limit the CPU utilization of a process or a group of processes in Linux using CPUTool and Systemd.

Conclusion

CPUTool is a powerful tool that enables you to control the CPU utilization of any process in Linux. It provides a simple and intuitive command-line interface that allows you to limit the CPU utilization of processes, monitor the CPU utilization of processes, and perform other advanced tasks such as creating cpusets and controlling the CPU utilization of multiple processes simultaneously. With CPUTool, you can optimize the performance of your system, prevent overheating, and improve the overall stability of your Linux system.

Updated on: 26-Jun-2023

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements