Question regarding cpulimit

Hi all, :wave:

I´ve got a question about the correct usage of cpulimit (CPU Usage Limiter for Linux download | SourceForge.net ).

With the help of CPU Usage Limiter for Linux you can limit the maximum percentage of CPU time that a process is allowed to consume.

I also found a pretty good description here: cpulimit › Wiki › ubuntuusers.de (although in German).

To make sure I understand it correctly:

Suppose a process or program called “foo” has started and is using 100% CPU.
With the following command the maximum CPU time can be limited to 50%:
cpulimit -e foo -l 50 # -e: name of the process

[… or:]

The following command can be used to limit the maximum CPU time consumed by the process with PID “1234” to 20%:
cpulimit -p 1234 -l 20 # -p: PID of the process

(translated from German; source: cpulimit › Wiki › ubuntuusers.de )

But let´s say the process “foo” (process ID: 1234) takes up 30% of the RAM (without invoking cpulimit) and I´ll try to limit it thus:

cpulimit -p 1234 -l 50

Then it should have no effect, right :question:
The 50% limitation refers to 100% of the CPU time to be used, and the 30% usage is already below 50%.

Hopefully I got right. :thinking:

If I wanted to make the process use 50% of what it´s using right now (30%) my command should be:

cpulimit -p 1234 -l 15

Can anyone confirm I understood it correctly :question:

Many thanks in advance …
… and many greetings

Rosika :slightly_smiling_face:

Aren’t you mixing up RAM (memory) usage with CPU usage?

So, it’s like this: if process foo is using 30% of one CPU, and you want to cut this number in half, you

issue: cpulimit -e foo -l 15

Usually you wouldn’t use a number so low. Keep in mind that, if you have a computer with, say 4, CPUs and your program is doing some heavy number crunching, then you would use e.g. cpulimit -e foo -l 200, thus assigning the computing powers of two cores to foo and leaving you with the other too.

RAM usage doesn’t come into play here. It also doesn’t seem to make any sense to me to limit a program’s ability to access memory because in that case, excess data would be put into the swap space on disk, making the program extremely slow.

Now, to the concept itself. For me, putting a hard limit on CPU usage only makes sense, if you have a very long running program, and you don’t want your electricity bill to go too high.

If you just want to continue working whilst your program is running, it makes more sense to lower its priority with the nice and renice commands.

1 Like

Hi Mina, :wave:

be thanked a lot for your kind reply. :heart:

Uh, I hope not. I really want to deal with limiting CPU usage here. :blush:

Thanks. I guess that confirms what I was thinking.
I think it shouldn´t matter whether I use the “-e” parameter (name of the process) or the “-p” parameter (PID of the process).

Yes, you´re certainly right there. My example was just hypothetical.

I see.

Well, the background of my question is this:

I often help a friend of mine who lives pretty far away with her PC-related issues by connectiong to her laptop via anydesk.
This programme (anydesk) is not installed on my main PC but it´s rather running in my Debian virtual machine.

When I start anydesk on Debian CPU usage is still pretty low (in Debian as well as on my host).
But as soon as the remote connection is established and the remote desktop is “transmitted” to my system CPU usage goes up.

Alas I forgot to look up the exact values in Debian (VM) but noticed it on my host.

So I installed cpulimit on Debian and my intention was to limit the CPU usage of anydesk within the virtual machine.

Or do you think nice or renice would be better suited for my purposes :grey_question: :thinking:

Many thanks again.
Greetings from Rosika :slightly_smiling_face:

That depends: If you want to limit the overall CPU usage (for whatever reason), then cpulimit is the way to go. If you want to keep your other programs responsive on the possible expense of anydesk,

renice -n N -p P is the way to go. P is the process ID, N a number between -20 and 20, (e.g. 15). Lower number = higher priority.

1 Like

@Rosika ,
If you set a nice value for a shell, eg

nevj@trinity:~/common$ nice bash

then all commands you issue from that shell will inherit its niceness.
That could save a lot of nice commands
Regards
Neville

2 Likes

Very cool!

I didn’t know that.

1 Like

Hi again to all of you, :wave:

thanks a lot for your answers. :heart:

@Mina:

O.K., that sounds fine.

So if I understand it correctly now (hopefully), it should be like this:

I use cpulimit whenever I want to limit the CPU usage of a certain process under all circumstances - no matter what other (resource-hungry or not) processes are running at the same time.

Whereas when I use nice or renice CPU usage limitation may not even kick in IF no other resource-hungry processes are running at the same time :question: :question: :thinking:

I hope I got it right now…

Thanks so much. :slightly_smiling_face:

@nevj:

Hi Neville,

Wow, that´s really good to know. That may very well come in handy.

Thanks a lot for pointing that out.

Many greetings to all of you.
Rosika :slightly_smiling_face:

2 Likes

Basically yes. Only detail is, nice doesn’t put a limit. It just assigns places in the queue, if resources are limited. I think, this is what you want.

Have a lovely Sunday, BTW!

2 Likes

Thanks so much, Mina, for explaining the matter even further. :heart:

I see. Great. :+1:

Well, I think it´s best to try out both and see which works better…

The thing is:

When running Debian in a VM for using anydesk with my far-away friend the anydesk process is the only resource-hungry process running there. All other processes are lightweight compared to an active anydesk-session.

I think I´ll try out cpulimit and nice by installing the mathomatic-primes package as suggested here: Restricting process CPU usage using nice, cpulimit, and cgroups | Scout APM Blog .

The matho-primes command should generate high CPU usage (almost 100%)

Now run top and you will see that the matho-primes process is using all the available CPU.

…and this should set up the scenario for further cpulimit (…) testing. :smirk:

Thanks also for the good wishes, Mina. :heart:

Have a lovely and nice Sunday as well, of course. :smiling_face_with_three_hearts:

Many greetings and stay safe.
Rosika :slightly_smiling_face:

1 Like

Hi Mina, :wave:

so sorry to bother you again. :slightly_frowning_face:

Just wanted to let you know that (after installing the mathomatic-primes package on Debian) I tried it out.

cpulimit works just fine:
The command

cpulimit -l 50 matho-primes 0 9999999999 > /dev/null

produces a CPU usage of something between 52 % and 55 % (instead of 100 %) which is great. So it works as desired. :+1:

Yet nice doesn´t seem to work as it should:

… following the instructions on Restricting process CPU usage using nice, cpulimit, and cgroups | Scout APM Blog :

The commands

nice matho-primes 0 9999999999 > /dev/null &
matho-primes 0 9999999999 > /dev/null &

should start two processes of matho-primes , one with nice and one without.

The 1st command starts the process with a niceness of 10 (the default) but the two processes use the same amount uf CPU it seems (100 % vs 99.7 %).
And that´s despite the fact that a niceness of 10 is assigned to the first process. :thinking:
(see screenshot)

Well, I just wanted to let you know…

Many greetings from Rosika :slightly_smiling_face:

Hang on, I think I got it now.

I didn´t take into consideration the fact that even Debian has got 2 virtual cores assigned to the CPU in the VM.
Surely my observations must have something to do with that.

To (hopefully) prove it I started 3 processes of the kind I described above:

matho-primes 0 9999999999 > /dev/null &
matho-primes 0 9999999999 > /dev/null &
nice matho-primes 0 9999999999 > /dev/null

… and now the desired effect kicks in :smiley:

see:

Two processes consuming 100 % and 90 % respectively but the third one just 9.7 %.

I guess that out of the three processes two are sharing the same core and then nice takes effect.
So it seems nice works as per core… :thinking: :question:

Hopefully I´ve got it right now …

Many greetings.
Rosika :slightly_smiling_face:

2 Likes

@Rosika and @Mina
Great discussion , thanks to both of you
We should note that both nice and cpulimit only affect cpu time usage.
They do not limit memory usage. That seems to be managed by ulimit.
Neville

1 Like

Hi Neville, :wave:

You´re welcome.
I have to point out that @Mina surely deserves most of the praise as she knows so much more than I do. :smiley:

Thanks a lot for the hint. :+1:

As a matter of fact I didn´t know about that one and was pretty much intrigued.
As there are no man-pages for that command on my system I found out:

type ulimit
ulimit is a shell builtin

Curiously enough I didn´t find any documentation about that in the ubuntuusers.de wiki (which otherwise offers an abundance of information about any command [in German] , although there seem to be some discussions of ulimit in the respective forum). Perhaps I wasn´t looking thoroughly enough … :blush:

But I stumbled upon How to Use the ulimit Linux Command {With Examples}
from phoenixnap.com .
There seem to be be some good explanations and examples for the correct usage on that page.

Thanks so much for your input.
Many greetings from Rosika :slightly_smiling_face:

2 Likes

Hi @Rosika
Try man bash under COMMANDS and look for ulimit
Also man - k ulimit and man 3 ulimit
I dont really need to tell you. You have an ability to search around for answers.
I sometimes have to increase ulimit for programs that use a lot of ram for large matrix calculations.
I did not know about type so thank you for that.
Neville

1 Like

It’s not that nice works per core. The thing is: The matho-primes program is probably a single threaded app, so it will never use more than one core.

With two cores and two instances, each one gets executed on one core. They are not competing for resources.

In the second example, with three instances on two cores, at least two instances have to share a single core. The one with the higher priority (lower nicevalue) has priority in grabbing CPU time.

1 Like

Hi again to all, :wave:

thanks a lot for your latest replies. They´re so much appreciated. :heart:

@nevj:

Thanks, Neville.
Although I found out that ulimit is a shell builtin it didn´t occur to me to look it up under man bash. Shame on me. :slightly_frowning_face:
How good of you to point that out. :+1:

gave me

No manual entry for -
No manual entry for k

but the terminal nevertheless still ended up in displaying what I was looking for :smiley: :


ULIMIT(3)                        Linux Programmer's Manual                       ULIMIT(3)

NAME
       ulimit - get and set user limits

SYNOPSIS
       #include <ulimit.h>
[...]

Thanks for the compliment. :blush:

As for type:
man-pages say:

type - indicate how a command would be interpreted

But I´m sure you looked that up already. :smiley:

Once again: thank you so much for your help and the additional info.

@Mina:

Hi Mina,

I see. Well it seems I jumped to conclusions there.
How good of you to set things right. :heart:

For the rest of your explanation:

That´s exactly how I would have seen it.

Yes, that was my train of thought as well.
Therefore I started a third instance of that process and - as can be seen in my 2nd screenshot - nice had its desired effect on the matho-primes process.
I´m ever so glad when things work out in such a nice way. :wink:

Thanks a lot, Mina, for your help. :heart:

P.S.:

a bit off-topic, but anyway:

For my screeshots of my Debian VM I had the i3-tiling-window manager running and I have to say: I simply love it. At least for use in virtual machines.
That way it was simple to have a look at what processes I started for demonstation purposes and what effect they had by displaying top -i … all in one picture.

For anyone interested:

1 Like

@Rosika ,
My mistake
man -k ulimit
no space before k
Neville
i3-tiling-window-manager … I want to try that

1 Like

Hi Neville, :wave:

thanks for helping me once again. :heart:

O.K., now I get:


rosika2@debian ~> man -k ulimit
cpulimit (1)         - limits the CPU usage of a process
lock (2)             - unimplemented system calls
ulimit (3)           - get and set user limits
rosika2@debian ~> man 3 ulimit
ULIMIT(3)                   Linux Programmer's Manual                  ULIMIT(3)

NAME
       ulimit - get and set user limits

SYNOPSIS
       #include <ulimit.h>
[...]

I guess that´s how you meant me to use the right commands.
Works perfectly now. :+1:
Thanks a lot.

I´m glad I could provide some suggestion for a change. :blush:

Many greetings from Rosika :slightly_smiling_face:

@Rosika ,
I should have explained
man 3 ... is for C callable library routines
You probably do not want that
Commands are man 1 ... or just man ...

I am thinking of trying i3 in a laptop. That would minimize use of the trackpad which is not as convenient as a mouse.

Regards
Neville

1 Like

Hi Neville, :wave:

O.K., this works well enough:

man ulimit
man 1 ulimit

I get the respective man-pages now. Thanks. :+1:

Ugh, I have a (vague) idea of what libraries are and I (hopefully) know C, C# and C++ are programming languages but alas I´m still not sure what callable exactly means in that context… :slightly_frowning_face:

On MicroZed Chronicles: SDSoC & C Callable Libraries | by Adam Taylor | Medium I found some reference to that (in a special context though):

In this case, we can use C Callable Libraries to call up our existing HDL cores into the design.

The only one thing I may deduce from that is that C Callable Libraries seem to be used to call something up…

But thanks anyway for the additional info.

How wonderful. Seems like a plan. :smiley:
I hope everything turns out as you hope.

Many greetings from Rosika :slightly_smiling_face: