cpulimit icon indicating copy to clipboard operation
cpulimit copied to clipboard

Does it works?

Open Profforgr opened this issue 10 years ago • 9 comments

Hi.

My simple question is - does it really works? I've tried to use cpulimit many times, few years ago, and now. Just playing on virtual machines (KVM virtualization). Always i see the same issue - it does not work. CPU used by target process stay the same or become more, also the application itself become hell slow - it should be OK with working limit, but the CPU usage don't changed... Also there are new cpulimit process, which also uses some cpu. So i have slow down on the target app/process and increased CPU usage.

I'm testing on Debian 7 x86. I've tried cpulimit version from apt (from 2012) and current git version, results are very similar.

cpulimit --limit=1 -v -p 16446
4 cpu detected
Process 16446 found
Priority changed to -10
Members in the process group owned by 16446: 1

%CPU    work quantum    sleep quantum   active rate
14.78%       0 us        99999 us       0.00%
9.58%        0 us       100000 us       0.00%
21.65%       0 us       100000 us       0.00%
11.95%       0 us       100000 us       0.00%
28.41%       0 us       100000 us       0.00%
24.07%       0 us       100000 us       0.00%
29.12%       0 us       100000 us       0.00%
31.57%       0 us       100000 us       0.00%
17.59%       0 us       100000 us       0.00%
22.53%       0 us       100000 us       0.00%
20.43%       0 us       100000 us       0.00%
36.58%       0 us       100000 us       0.00%
29.90%       0 us       100000 us       0.00%
24.63%       0 us       100000 us       0.00%
126.17%      0 us       100000 us       0.00%
57.77%       0 us       100000 us       0.00%
27.19%       0 us       100000 us       0.00%
25.82%       0 us       100000 us       0.00%
12.16%       0 us       100000 us       0.00%
22.74%       0 us       100000 us       0.00%

I expect "%CPU" here to be 1.00% or not more than 10.00%. But it's always high. I also, sure, monitor CPU usage by other tools, like top. And result is same - high usage.

16446 root      20   0 1523m 191m 4632 T  41,2 19,0   3860:03 mysqld

41,2% - cpu load here.

What did i miss?

Profforgr avatar Jun 11 '14 01:06 Profforgr

As you, I don't think that it really works. I always get overload with process that I have tryed to limit with cpulimit

humbertorodrigues avatar Sep 09 '14 11:09 humbertorodrigues

I got some VPS here and there and i have make tests on 2 of them. Always tested with -v -i flags. Tried -l with 5, 10, 50 and 90 variations tests.

  • On a free VPS i got with CentOS 6 64bit under OpenVZ get FAIL. Seems to work but did't limit it on any way.
  • On a paid VPS i got with CentOS 6 64bit under OpenVZ SUCCEED.

Doing this test on these similar specs i guess this is OpenVZ container configuration/limitation between providers. I test that to know if OpenVZ was a limitation by default/design, wich has proven to not be.

Related to that question: YES IT WORKS in general terms.

erm3nda avatar Jan 21 '15 01:01 erm3nda

@Profforgr try use -i flag, wich limits the childrens too. Try ´cpulimit` without more flags to see help instructions.

If your target app use threads (wich is very likely, like PHP binary) you will limit nothing because the 1st process is just the "task manager" and your 100% cpu usage job is under a thread.

You can limit a resource by typing just the name of the binary instead the PID.

As example you can limit whole PHP resources by using cpulimit -e php -l 90 -i -v (updated). This will include php tasks from webserver and cli version of PHP.

I found that very nice to use with php, dumps and gameservers.

erm3nda avatar Jan 21 '15 02:01 erm3nda

Poor precision is not the only gotcha you can get when not used to this software due to the strategy of limiting by continuously checking proc stats and sending signals. If you use it in many ways here and there sometimes funny things happen, like slow down music in some games.

For the same reasons it is odd to try to debug and limit at the same time, or to try to do a permanent stop of a process handled by cpulimit.

However it has its situations and cases, and I would tell it has to be more or less well coded. I remembered having cross compiled it in the past for ARM devices and used it bundled within an Android application. I have not seen it limiting so badly in any other system, but at least did something.

ghost avatar Feb 01 '15 22:02 ghost

try use -i flag, wich limits the childrens too.

There are no such flag, where did you find that??

I also doubt that this way of run is suitable. I think -l 90 should be before -e ? And how will you run programm with parameters Like

cpulimit -v -i -l 10 -e php *some parameters for php executable*

Profforgr avatar Feb 02 '15 18:02 Profforgr

Read the --help to find the documented order of arguments. See what you are wanting is not that shown as possible. By the way I have tried sometimes but never got -e or -P to work as this reads.

0:3:1422912899:user@host:~$ cpulimit --help
CPUlimit version 1.7
Usage: cpulimit TARGET [OPTIONS...]
  TARGET must be exactly one of these:
      -p, --pid=N        pid of the process
      -e, --exe=FILE     name of the executable program file
                        The -e option only works when
                        cpulimit is run with admin rights.
      -P, --path=PATH    absolute path name of the
                        executable program file
  OPTIONS
      -b  --background   run in background
      -c  --cpu=N        override the detection of CPUs on the machine.
      -l, --limit=N      percentage of cpu allowed from 1 up.
                        Usually 1 - 200, but can be higher
                        on multi-core CPUs (mandatory)
      -v, --verbose      show control statistics
      -z, --lazy         exit if there is no suitable target process,
                        or if it dies
      -h, --help         display this help and exit
1:4:1422912901:user@host:~$ _

uprego avatar Feb 02 '15 21:02 uprego

@Profforgr There are no such flag, where did you find that??

Nice question. Run cpulimit --help. I've dumped to a commands.txt for my convenience. I do that with all CLI software before use it. In my case i get no version. Just used from here on Github, latest version i guess.

About @Profforgr cpulimit -v -i -l 10 -e php *some parameters for php executable*, yes, i did not wrote commands in the needed order (I think this is the problem about issue 39 guy). I edited my comment with the good one.

About run PHP with some params. None of the -peP rules can do that, but, the pid can select just ONE. There are some approachs.

  • You can use PHP to call the cpulimit itself using getmypid() PHP's function. If you mix the shell_exec() with the getmypid() you got the control of each script you add that.

$pid = getmypid(); passthru("/sbin/cpulimit -l 50 -p " . $pid . " -v -i > /dev/null 2>&1 &");

  • If you need to control anybody on the server, not just your scripts, you can create a php daemon like script, wich constanly checks for PID's searching for QUERY_STRING in FULL ps output (ps auxwwfe). I've develop that as example and tested on my own server. A better way would be use a bash daemon instead a php process, but i enjoy coding that anyway (Watch on Gist).
<?php
set_time_limit(5);

exec("curl -L http://someurl/file.php?param=1 > /dev/null 2>$1 &"); // put a bench as example

$process = shell_exec("ps auxwwfe | grep QUERY_STRING | grep -v grep"); // get all pids that matchs a QUERY_STRING (http request)
preg_match_all("/.*\n/", $process, $matches); // group by new lines
$matches['data'] = $matches[0]; // regroup values
unset($matches[0]); // free last array
foreach ($matches['data'] as $m) { // process all our results

    if (preg_match("/.*cpulimit.*/", $m) != true) {
            $first_token  = strtok($m, ' '); // get the 1st word on a string
            $matches['pid'][] = strtok(' '); // get next word on a previous set string ($first_token)
    } else {
            $first_token  = strtok($m, ' '); // get the 1st word on a string
            $matches['skiped'][] = strtok(' ');
    }
}

foreach($matches['pid'] as $pid) { // limit those scripts by cpulimit
        $close = shell_exec("/sbin/cpulimit -l 10 -p" . $pid . " -v > /dev/null 2>&1 &");
        $matches['closed'][] = $pid;
    }

echo "<pre>";
var_dump($matches); // watch it out
echo "</pre>";
echo "<meta http-equiv='refresh' content='5'>";

A special remark to that file, do not test with something like for i in {1..200}; do nohup curl -L domain.com/script/?123; done because you will eat your CPU by the process handler. I did my tests running that PHP Bench Script.

Regards.

erm3nda avatar Feb 07 '15 17:02 erm3nda

Ey. Did u finally manage to run it? I feel like i just dump my brain here for no reason. At least, tell me if you finally make it work on your systems. I'm very interested on this software and i didn't found anything similar.

On the Windows SO i used the Battle Encoder Shirase (BES), and it WORKS. (http://mion.faireal.net/BES/)

Best regards.

erm3nda avatar Apr 05 '15 23:04 erm3nda

@Profforgr Because I can't/won't compile cpulimit every time I use it on some computers, I use the one I've compiled first on CentOS7, and i found it works on almost "any" other distros, but, having a different set of options, is not just missing 1 command between versions.

Seems you have Ubuntu or another not Debian nor CentOS, you also have some options that I don't have. Im sure you can see why in the source ;-)

Same binary, my Laptop:
m3nda@localhost:~$ uname -a & cpulimit --help
[1] 3814
Usage: cpulimit [OPTIONS...] TARGET
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u5 (2015-10-09) x86_64 GNU/Linux
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 400 (required)
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

Report bugs to <[email protected]>.
Same binary, CentOS7, modified distro from vps provider:
root@googlebot:~# uname -a && cpulimit --help
Linux googlebot 2.6.32-042stab112.15 #1 SMP Thu Jan 29 12:58:41 MSK 2015 x86_64 x86_64 x86_64 GNU/Linux
Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 100 (required)
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

Report bugs to <[email protected]>.
A Debian 8 vps
Linux vultr.guest 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09) x86_64 GNU/Linux
Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 200 (required)
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

Report bugs to <[email protected]>.

erm3nda avatar Jan 04 '16 13:01 erm3nda