tobixen / thrash-protect

Simple-Stupid user-space program doing "kill -STOP" and "kill -CONT" to protect from thrashing
GNU General Public License v3.0
163 stars 21 forks source link

[Query] Is there a way to skip console apps from being STOPPED #14

Closed operabeats closed 4 years ago

operabeats commented 6 years ago

Hi,

thrash-protect is helping me avoid system freezing due to swapping on my old 4GB ram and HDD based laptop. I have one issue, it keeps freezing "ncdu" (curses based du utility) running in xfce terminal. I have tried "fg" after that, but arrow keys and commands are not working.

I tried white listing it in the script and re-running the service ( I am on manjaro linux), but still it freezes ncdu.

I have also tried running it with "ionice -c3 ncdu -x /home", but still no luck.

Please help

edit: how do I gracefully stop this script / service, when it has few processes STOPPED (can I just kill the script and send SIGCONT signal to processes it has STOPPED)

tobixen commented 6 years ago

Graceful stop and restart, that's something I should consider implementing support for. But yes, it's just to stop or kill the script and then SIGCONT anything that is stopped and shouldn't be stopped.

If the whitelisting doesn't help, It may be that the issue is that the parent process got suspended, or that the process name doesn't match the whitelisted process name. Try whitelisting xfce as well.

tobixen commented 6 years ago

It is a well-known issue that interactive terminal processes run under bash (or other job-controlling shells) may misbehave if they are suspended without the parent process first being suspended. Possibly a proper fix for this is to make sure that parent process are always suspended before the child process is suspended, and that the child process is always resumed before the parent process is resumed. Quite a while ago I did fix so that at least anything running under bash would be handled like this. Are you using some different shell than bash?

Anyway, this trick may help you:

screen ncdu

this way ncdu is run directly under screen and not through a job-controlling shell. Using the trick above, I can at suspend and resume ncdu locally without any issues.

I guess ncdu may indeed spend quite some memory on a big file system as it should keep all the files and file sizes in memory.

operabeats commented 6 years ago

Thank you for a quick reply! I am running it from xfce4-terminal under "bash". I will run it from tmux and monitor from htop. It it gets STOPPED should be able to tell what all has been STOPPED. will update if that happens

operabeats commented 6 years ago

I wanted to test if this script suspends bash parent process before suspending ncdu by just running it directly from xfce terminal. but it seems its just suspending ncdu only. I have a screenshot of htop here:

ncdu on its own is suspended

I will run this with tmux and update you.

version : version = "0.11.6" modified whitelist: cmd_whitelist = cmd_whitelist.split(' ') if cmd_whitelist else ['sshd', 'bash', 'xinit', 'X', 'spectrwm', 'screen', 'SCREEN', 'mutt', 'ssh', 'xterm', 'rxvt', 'urxvt', 'Xorg.bin', 'systemd-journal', 'i3lock', 'xautolock', 'xautolock']

I have tried to unfreeze manually, but it continues in forzen state:

screenshot_2018-02-20_21-38-15

Edit: If I use job control commands from terminal like "fg" it un-freezes, but cursonr movement wont work. I will try with tmux and update

I am also attaching the log file filtered for ncdu process (pid 27684) thrashp-ncdu.log

I also have few queries:

  1. I believe this is meant for desktop system (where foreground apps should take precedence). Correct if I am wrong
  2. Does this freeze X server as well? (I believe it will, as the script may not distinguish between processes except for whiltlist and freeze parameter like OOM score etc)

For now I am loving this script! my laptop is more responsive now with all those chromium & Firefox tabs open.

tobixen commented 6 years ago

Sudo is also a bit tricky. In october sudo was added to the list of things that should be stopped prior to the child processes being stopped, but that's included in the 0.11.6-version.

It seems like master is on 0.11.7, but if I'm not mistaken 0.11.7 was only a version number change, done due to some problems with the arch linux aur packaging.

tobixen commented 6 years ago

bash should be frozen right before ncdu gets frozen, and it should remain frozen until right after ncdu gets thawed, and then there should be no job control issues (no need to type "fg"). It seems like it does not work for you, so it's a bug. I will investigate.

tobixen commented 6 years ago

As for the questions:

1) I started playing with this idea perhaps around year 2001, then I was considering the desktop environment. However, when I implemented the current version it was due to some production server that kept going down (I needed to power cycle it through the management interface). It has been used a lot both on the server side and the desktop side, but there is not a lot of test code verifying that it works exactly as intended.

2) xorg.bin is on the whitelist, so theoretically it shouldn't be frozen. However, the whitelist/blackllist isn't really a binary thing ... it affects weighting, if X is considered to be very heavy on the memory consumption it may still be frozen.

operabeats commented 6 years ago

This query can be closed as issue with freezing & unfreezing ncdu is not related to thrash-protect and also ncdu was not in the whitelist (I just realised xautolock has repeated twice instead of ncdu being there).

Also in my case X process name was "Xorg" I have updated the whitelist accordingly and all works fine. Only thing that bothers me is the size of logfile. It grows very rapidly (if the system is paging lot)

I am on Manjaro linux and this runs as a systemd service. To set the whitelist variable, I did this:

$ sudo systemctl edit thrash-protect
## which creates override file: /etc/systemd/system/thrash-protect.service.d/override.conf
## I have added following to it:
====================
[Service]
Environment="THRASH_PROTECT_CMD_WHITELIST=sshd bash -bash sudo xinit X SCREEN ssh xterm xfce4-terminal Xorg xfwm4 systemd-journal journalctl i3lock xautolock ncdu vim Thunar keepassxc"
Environment="THRASH_PROTECT_LOG_USER_DATA_ON_FREEZE=1"
====================