1

I have some binary I want to run in terminal, but if I run it, it quickly starts to use 100% of both CPUs, 100% of memory and the whole Linux freeze in a few seconds. Also I don't have source (in order to calm it down right from the inside). I googled a lot and found and tried two possible solutions:

  1. nice - does literally nothing, the binary still uses 100% of both CPUs and 100% of memory quickly after I run it
  2. ulimit -v makes binary and even some other commands (like git) crash and throw some error

Is there any easy solution to limit CPU usage or memory by this binary and let it use e.g. only one CPU or 50% of both CPUs or only some small amount memory? I imagine something like this:

user@linux:~$ magical_command --limitmemory 1M --command mybinary

or:

user@linux:~$ magical_command --cpu both --limitcpu 50% --command mybinary

or:

user@linux:~$ magical_command --cpu one --limitcpu 100% --command mybinary

Update: From the comments below it looks like it is caused by memory rather than CPU overload, therefore I edited the question, but I still recommend @dbkeys' answer for those looking for CPU limiting magical command.

Update 2: As requested in the comments below: if I restart my notebook and only run terminal and that binary from terminal, nothing else, my notebook is significantly slowed down, mouse and keyboard works with delays or jerkily. But the binary is working for hours (most of time downloading and compiling dependencies), therefore I open Chrome and do some other stuff. If I open Chrome and let the binary run in the background, the whole linux freezes at all and nothing, even not mouse responds. I have to push a switch-off buton for 5 seconds to hard shutdown. But I can't just run it and go away, I want to work on some other stuff, therefore I want to limit somehow maybe both, that binary and Chrome.

aleskva
  • 1,647
  • 3
  • 13
  • 22
  • Are you sure the problem is the CPU usage and not the memory usage? – terdon Oct 24 '16 at 12:38
  • What CPU and how much RAM do you have? – negusp Oct 24 '16 at 12:41
  • You can use cgroups to limit memory usage and restrict a program to a set of cores. See, for example: http://askubuntu.com/a/510936/158442 – muru Oct 24 '16 at 12:43
  • @terdon maybe, I don't have any time to find out. What I only managed to do was that I run System monitoring and looked how my PC freezes real-time: usual usage (Google Chrome and clean terminal without any command running): cca 10% CPU1; cca 10% CPU2; cca 75% memory... Google Chrome and terminal running that binary: 100% CPU1; 100% CPU2; 100% memory – aleskva Oct 24 '16 at 12:57
  • @PatrickNegus CPU Intel Core 2 duo (Celeron) T3000 1.8 GHz x 2; 1.9 GB memory – aleskva Oct 24 '16 at 13:06
  • 1
    The problem then is almost certainly the memory usage, not the CPU. nice would solve the CPU. It sounds like a buggy program. You could also try allocating more swap to see if that helps. – terdon Oct 24 '16 at 13:06
  • @terdon ok, thank you, I'll edit question then. During that experiment swap usage is still cca 1 GB (of 4GB available) – aleskva Oct 24 '16 at 13:08
  • Ah, but if it's swapping 1GB of data, that will slow your machine down significantly. Not a full freeze if there's still swap available, but very slow. Could you [edit] your question and explain what the symptoms of the freeze are in more detail? Do you have to reboot the machine? Does it come back by itself? Is everything frozen, including the mouse? – terdon Oct 24 '16 at 13:13
  • @terdon if I restart my notebook and only run terminal and that binary from terminal, nothing else, my notebook is significantly slowed down, mouse and keyboard works with delays or jerkily. But the binary is working for hours (most of time downloading and compiling dependencies), therefore I open Chrome and do some other stuff. If I open Chrome and let the binary run in the background, the whole linux freezes at all and nothing, even not mouse responds. I have to push a switch-off butoon for 5 seconds to hard shutdown – aleskva Oct 24 '16 at 13:23
  • Please [edit] your question to add this information. It's easy to miss in the comments and comments can be delete without warning. What you describe is typical of a lack of RAM, the CPU usage is irrelevant. Chrome uses a ridiculous amount of RAM, you won't be able to run it while your binary is running. – terdon Oct 24 '16 at 13:25
  • @terdon I know but I haven't found anything better than Chrome yet. This lack of RAM happenned to me once, it was causing Flash in Chrome cca 40 on Ubuntu 14.04 some time ago, but devs solved it then. I'll add it to the question – aleskva Oct 24 '16 at 13:30

1 Answers1

2

taskset, might be the 'magical_command' you are looking for. According to its man page:

"...taskset is used to set or retrieve the CPU affinity of a running process given its pid, or to launch a new command with a given CPU affinity. CPU affinity is a scheduler property that "bonds" a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. "

This would limit mybinary to run on the first two cores:

taskset --cpu-list 0,1 mybinary

Unfortunately, I don't see a parameter in taskset that would also limit what % of the CPU's time you want to allot it to run mybinary.

dbkeys
  • 161
  • Well, this is very relevant to what OP asked but he is actually RAM constrained. Good answer though. – negusp Oct 24 '16 at 13:20
  • @dbkeys I edited the question, but still tried to lead people looking for CPU overload to your great answer – aleskva Oct 24 '16 at 13:25