You might look into a technique called checkpoint/restore. This will allow you to take a running process and save its state to a set of files, then restore it at a later time.
To use it, start by installing the criu [git,wiki] program (yum install criu
or apt install criu
).
To checkpoint a running process, create an empty directory to hold its files and cd into that directory.
mkdir /var/tmp/checkpoint
cd /var/tmp/checkpoint
Now checkpoint the running process. In this case I'm using the --shell-job since I have my process running in a shell with an associated tty.
criu dump -t 404 --shell-job
404 is the pid of the process I want to checkpoint. When I do this I see my running process get killed and my /var/tmp/checkpoint directory get populated with a set of files needed to restore it.
To restore the process, I make sure I'm in the directory with the checkpoint files and do a restore.
cd /var/tmp/checkpoint
criu restore --shell-job
The process will pick up where it left off in the terminal where this was run. If I kill this running process and run criu restore --shell-job
again, the process will revert back to the checkpoint and start up again.
Hope this helps.
1What is the intention behind this? Do you want to ensure that the process resumes more quickly? Or do you want to prevent sensitive data to be written to disk? Or something else? If we know the intention, we might be able to give better answers. – oliver – 2016-11-14T18:13:41.633
13The OS will do this automatically. There's really no reason to do anything specific. – David Schwartz – 2016-11-14T19:39:27.093
@oliver I am creating a batch scheduler (https://github.com/brutusin/wava). The current implementation offers non-preemptive scheduling but I want to move to a preemptive one (being able of stop running jobs) to graceful avoid some deadlock situations when all running jobs depends on queued jobs. I need exactly the behavior asked, continuing stopped processes (not creating new ones from a checkpoint)
– idelvall – 2016-11-14T22:43:33.5401@DavidSchwartz that is a risky assertion – idelvall – 2016-11-14T22:44:54.610
@idelvall Then it sounds like you don't want to do anything special to memory. – David Schwartz – 2016-11-14T22:48:39.933
I didn't feel necessary to give more details on my actual problem, furthermore I didn't want the question to sound like i wanted to promote my project but given your comments I can tell you that my scheduler main concern is to guarantee that running jobs memory doesn't never exceed physical memory limits in order to avoid paging and swapping. Stopping a running process without freeing its RSS memory would not make room for other queued processes to start – idelvall – 2016-11-14T22:58:44.067
@DavidSchwartz I forgot to mention you in my last comment. this is my real issue: https://github.com/brutusin/wava/issues/13
– idelvall – 2016-11-15T09:07:36.737@idelvall Your question still makes no sense. You are asking how to force the paging out of memory while claiming that your objective is to avoid paging! Do you want to make sure to page everything or do you want to avoid paging things? – David Schwartz – 2016-11-15T10:13:46.647
yes I expressed wrong, what i want is to avoid paging performed by the OS, especially for processes unmanaged by the scheduler. – idelvall – 2016-11-15T10:46:14.803
Does
– Peter Cordes – 2016-11-15T21:27:22.307posix_fadvise(POSIX_FADV_DONTNEED)
work on/proc/<PID>/mem
?vmtouch
might have a command-line option to do that, so you might not need to write code yourself to experiment. (I'd suggest usingstrace
to make surevmtouch
is making the system calls you expect.) Doesmadvise(MADV_DONTNEED)
work to trigger page-out of anonymous pages (easiest to try from within a running process, since madvise works on memory addresses, not file positions)?Near-duplicate of http://superuser.com/questions/128385/how-to-tell-linux-to-explicitly-swap-out-main-memory-of-a-suspended-process, but that doesn't have much of an answer. It just suggests raising
– Peter Cordes – 2016-11-15T21:37:16.537/proc/sys/vm/swappiness
to encourage the kernel to page out more aggressively.I would look into the top answer on here: http://unix.stackexchange.com/questions/87908/how-do-you-empty-the-buffers-and-cache-on-a-linux-system
– pycvalade – 2016-11-17T18:12:53.267