Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the CPU usage of process?

I want to limit the CPU usage of process in a GPU Server. I find some ways, "nice" or "cpulimit". But they are not exactly what I want.
cpulimit allow a process to run as fast as it wants until it has exceeded a percentage after which it gets a SIGSTOP, followed by a sleep, and a SIGCONT.
But I don't want the process which exceeded a percentage to sleep. I just hope that it does not exceed a certain percentage. And keep it running in a normal way.

For example, when I run cpulimit -p 1111 -l 30, the terminal will be
[1]+ Stopped
This is not what I want.

like image 817
Swind D.C. Xu Avatar asked Dec 20 '16 09:12

Swind D.C. Xu


People also ask

How do I monitor CPU usage on a specific process?

Right-click on the graph and select "Add Counters". In the "Available counters" list, open the "Process" section by clicking on the down arrow next to it. Select "% Processor Time" (and any other counter you want). In the "Instances of selected object" list, select the process you want to track.

How do I limit CPU usage in Linux?

Limit the CPU Usage of a Process You can add the --background or -b flag with the command to send the command to the background. If the --background option doesn't work, you can add an Ampersand (&) after the command to send it to the background. Use the top command to check if the aforementioned command works.


1 Answers

I don't think this is possible without sending a sleep to your program. But I don't think you should be worried about sleeps, because this is what UNIX does when another thread requests an access to the CPU.

If you see that you program is using 50% of a CPU core, it is 50% of the time, because a CPU core can only do one or two threads at a time (depending of your configuration, lscpu to see on linux).

If you're building a app that consume always 100% of a core and never let a other process enter in, it may be possible, but you never know if your kernel will one time allow some CPU to another emergency program.

So my advice is to consider the fact that your app may be paused, because it may happen, and it is something you need obviously.

Hope I help you :) See ya !

like image 130
Alexis Tacnet Avatar answered Sep 21 '22 06:09

Alexis Tacnet