memstop icon indicating copy to clipboard operation
memstop copied to clipboard

extension: hook brk(2) and mmap(2)

Open surban opened this issue 6 months ago • 2 comments

So far memstop only checks available memory on process start.

We can do better than that, by hooking the brk and mmap system calls and pausing the program if it tries allocating memory when memory is low.

However, this is not straightforward, since it can result in a deadlock when all processes become paused and no memory is freed.

It would require coordination between all memstop-enabled processes on a machine.

surban avatar Jul 04 '25 11:07 surban

some randomness could be added to avoid such deadlocks.

very much like RED that drops packets randomly and early enough.

as we come close to the threshold, small delay is added

steveschnepp avatar Jul 09 '25 21:07 steveschnepp

Scenario:

I am compiling with -j 16 on 32 GB of RAM. Each .cpp file is a template instantiation of the same template (with different template params, OFC), which will end up using about 4+ GB each. If there are 8 processes blocked and memory goes under threshold, suddenly all 8 processes get a green light and start running, resulting in OOM.

Either solution mentioned in this issue would solve the problem, I think, randomness being clunkier and easier.

Another may to send a SIGSTOP to the memstop process using the most (least?) memory if some threshold is surpassed, so it would stop temporarily growing more and would let the other processes finish, then SIGCONT after the threshold is cleared. This would probably require some kind of IPC.

I may try randomness first.

lanwatch avatar Sep 01 '25 19:09 lanwatch