Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a swapped-out zsh process to swap in?

Tags:

linux

zsh

I usually have many long-running zsh processes (in spread out over various screen sessions) on my computer, but shells that haven't been used for a while tend to be swapped out. When I switch to such a swapped-out shell and press enter (at an empty prompt) it takes many seconds before the shell responds and a new prompt is displayed (but after that it resumes working perfectly).

To lessen my annoyance I'd like to have a command (e.g. unswap PID) that I can invoke (from another, already responding shell) to force a shell swap in.

Any ideas on how to achieve this?

P.S. I started off by looking around for a command to ask the system to swap in any process – but I have found no such thing, making me believe that this is not possible. I've here focused my question to zsh, since this is the case that really interests me, and I'm hoping there might be a solution for that particular case. If you have a more general method that would work for any process (and not just zsh) that would be quite welcome too.

P.P.S. At first I thought of using a trap function inside my shell, which then did some activity (anything) enough to cause the shell to swap in. Having tried this with the WINCH signal however, I've found that it doesn't work. (Maybe the signal is not delivered to a swapped out process? Are any signals?)


EDIT: In my typical case I have a bunch of zsh processes running, and then I've left my browser open (with multiple Gmail, Facebook etc tabs) for a bit too long, causing the browser to hoard all of the available RAM and squeezing everything else out into swap. The desktop goes unresponsive for a while, but that clears up kinda quickly. However, when reattaching to a screen session each shell have to be waken up manually (going through them and pressing enter in each shell).

like image 835
zrajm Avatar asked Aug 03 '13 06:08

zrajm


1 Answers

As mentioned in this answer one can use gdb to dump the memory of a process in order to get it unswapped.

I also found out here that there is a gcore script to do this directly from the command line (instead of first having to start gdb and then execute additional commands inside of it), so invoking:

gcore -o /tmp/SOMETHING PID1 PID2 PID3

will cause programs with PID1, PID2 and PID3 to become unswapped, and dump their contents to files /tmp/SOMETHING.PID1, /tmp/SOMETHING.PID2 etc.

There is also a Python script in this answer which does the same thing. Though, admittedly – as a response to this question – in a much nicer fashion, since this scripts output the dump on STDOUT which we can pipe straight to hell (uhm, I mean /dev/null, of course) since we're only interested in the side effect of unswapping anyway.

like image 92
zrajm Avatar answered Oct 14 '22 16:10

zrajm