Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[solved] Disable swap - Good or bad idea ?
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 10:25 am    Post subject: [solved] Disable swap - Good or bad idea ? Reply with quote

Hello,
I know there is no one size fits all solution when swap is involved. However I'd like to check few things with you.

I have 16Gb of RAM and a 1.5Gb swap file. With this setup I'm pretty sure to have a system freeze when under heavy load. By freeze I mean that the GUI (KDE) is unresponsive, music repeating on a 2 second loop and I'm unable to switch to a console (ctrl+alt+f1). All I can do is a hard reset and damn I hate doing that.

This morning I made a mistake that triggered a system freeze again. I had a 4Gb virtual machine running while portage was building Firefox. The system became completely unresponsive. I know, I should have expected that to happen.

I know that the swap file may have a role to play here, but why didn't the kernel kill memory intensive process (oom kill) to "save" the system from this situation ? Killing the qemu process would have saved me a hard reset for sure.

Right now I'm debating over 2 options:

- Disable swap completely
- Reduce the size of the swap file

What do you think ? What would be the best option for me to prevent those system freeze ? I know that a freeze might still happen, but at least have the OOM Kill to happen.


Last edited by MorgothSauron on Tue Nov 10, 2020 7:55 pm; edited 1 time in total
Back to top
View user's profile Send private message
Perfect Gentleman
Veteran
Veteran


Joined: 18 May 2014
Posts: 1252

PostPosted: Tue Nov 10, 2020 10:37 am    Post subject: Reply with quote

There is 3rd option:
- disable swapfile and enable swap on ZRAM
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2727

PostPosted: Tue Nov 10, 2020 11:01 am    Post subject: Reply with quote

Are you sure it's due to swapping? Have you tried to ssh from another machine if possible? You may be able to get some more info from dmesg.

If you were using nvidia-drivers 455+ I'd have a decent guess at what might be happening given it happens during heavy I/O / ram usage and ssh should still work, not that every setups is affected.


Last edited by Ionen on Tue Nov 10, 2020 11:03 am; edited 1 time in total
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 11:01 am    Post subject: Reply with quote

Perfect Gentleman wrote:
There is 3rd option:
- disable swapfile and enable swap on ZRAM


I didn't know about ZRAM or I forgot about it. It does seem to be a good option.

If I understand swap on ZRAM will behave as a swapfile, except it's in RAM and compressed. Correct ?
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 11:06 am    Post subject: Reply with quote

Ionen wrote:
Have you tried to ssh from another machine if possible? Sometime that still works and you may be able to get some more info from dmesg.


I don't have SSH enabled and I don't have a second computer available. I can eventually enable SSH and try to connect from Android ....

Ionen wrote:
If you were using nvidia-drivers 455+ I'd have a decent guess at what might be happening given it happens during heavy I/O / ram usage and ssh should still work, not that every setups is affected.


I have an AMD GPU.
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2727

PostPosted: Tue Nov 10, 2020 11:09 am    Post subject: Reply with quote

I see, nevermind then.

For zram we do have a wiki page: https://wiki.gentoo.org/wiki/Zram

I think it's great on low memory systems, but with 16GB I'd try to manage ram usage better instead. But if you really need more without adding more physical ram, then ram does compress pretty well.

I personally try to just not build firefox when I run my VMs :) Shouldn't happen unless some unlikely timing but I'd hate the idea of risking the VM being killed from OOM too (I don't care if rustc/cc1plus gets killed though). I'm sure there's ways to control this but never really looked into it.
Back to top
View user's profile Send private message
trilithium
n00b
n00b


Joined: 18 Nov 2019
Posts: 43

PostPosted: Tue Nov 10, 2020 11:13 am    Post subject: Reply with quote

The best option is to get enough RAM to run the software you want.

I invested in a generous amount of RAM and disable both swap and overcommit. I see no way to have a reliable system while never letting memory allocations fail and killing large processes according to some heuristic when physical memory runs out. Software that relies on overcommit or can't deal with memory allocation failure in a reasonable manner needs to be fixed or replaced.
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 11:15 am    Post subject: Reply with quote

I know that this morning it was my fault. A 4Gb VM and a Firefox build at the same time was just asking for troubles. I don't usually do that kind of stupid thing, but it happened today.

I'm just looking for an alternate swap or no swap config that could alleviate system freeze when under heavy load.
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 11:22 am    Post subject: Reply with quote

trilithium wrote:
The best option is to get enough RAM to run the software you want..


Now that I look back at it, it was just plain stupid from my part to update firefox while a VM was running. I knew I would have both running, but I didn't think about the memory usage. One thing I can do for sure in the future is to not emerge any package while a VM is running. Or only run both when Firefox isn't involved.

But I think I have nothing to lose in improving my current swap config. In the end it might be a good idea to just double the RAM (I have 2 slots left and I already have 2x8Gb)
Back to top
View user's profile Send private message
Perfect Gentleman
Veteran
Veteran


Joined: 18 May 2014
Posts: 1252

PostPosted: Tue Nov 10, 2020 11:27 am    Post subject: Reply with quote

MorgothSauron wrote:

If I understand swap on ZRAM will behave as a swapfile, except it's in RAM and compressed. Correct ?

Yes.
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2727

PostPosted: Tue Nov 10, 2020 11:29 am    Post subject: Reply with quote

1.5GB swap doesn't really amount to much vs 16GB either way, might as well not be there if it's causing problems.

Personally my system stays responsive during heavy swapping though, at most I'll get some crackling audio but keeps working -- so I like having some decent amount in case (and almost never in use due to vm.swappiness = 1). I do have the swapfile on a nvme ssd though, so I guess that's related.
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 11:30 am    Post subject: Reply with quote

Perfect Gentleman wrote:
MorgothSauron wrote:

If I understand swap on ZRAM will behave as a swapfile, except it's in RAM and compressed. Correct ?

Yes.


Ok, I'll give it a try. I looked at the wiki and it seems quite simple to setup.
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 11:34 am    Post subject: Reply with quote

Ionen wrote:
1.5GB swap doesn't really amount to much vs 16GB either way, might as well not be there if it's causing problems.

Personally my system stays responsive during heavy swapping though, at most I'll get some crackling audio but keeps working -- so I like having some decent amount in case (and almost never in use due to vm.swappiness = 1). I do have the swapfile on a nvme ssd though, so I guess that's related.


Do you mean that my current config I should have a larger swapfile ? 4gb ? 8Gb ? I have plenty of space, so it's not an issue.

I also have the swapfile on a nvme ssd :)
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2727

PostPosted: Tue Nov 10, 2020 11:36 am    Post subject: Reply with quote

I meant that it's easy to go outdo that 1.5GB and start doing OOM kills. I do wonder why your system freezes so badly though :| Not that I'm overly familiar with how swap gets handled.
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 11:38 am    Post subject: Reply with quote

Ionen wrote:
I meant that it's easy to go outdo that 1.5GB and start doing OOM kills. I do wonder why your systems freezes so badly though :|


Yeah, I'd like to know why it happens. This morning I waited a solid 10 minutes for things to settle down. But with the qemu process I already had 4Gb allocated. So 12Gb for the running system and a Firefox compilation in progress probably didn't help.
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2727

PostPosted: Tue Nov 10, 2020 11:40 am    Post subject: Reply with quote

What I/O scheduler are you using? Not [none], right? If all rootfs i/o came to a halt due to swapping I guess it could be something that'd freeze the system pretty well.

Edit: I know some people sometime recommended [none] for nvme ssd like it was immune from being 100% used
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 54317
Location: 56N 3W

PostPosted: Tue Nov 10, 2020 11:46 am    Post subject: Reply with quote

MorgothSauron,

Disabling swap is a bad idea. The kernel has several ways it can swap and disabling swap prevents one of them from working and puts pressure on the others.

Swap, file/partition/zram, no matter how its implemented, is only used for dynamically allocated RAM. That includes the content of tmpfs.
Think of it as RAM content that has no permanent home on disk.

The kernel will (eventually) use all of RAM for disk buffers, ether read buffers or write buffers. Buffers are not dropped until the RAM is needed for other things.
Dropping read buffers is instant. The data is already on disk and can be reread as required.
Dropping dirty write buffers takes a little longer. They must be flushed to disk first. They are never written to swap. That's the wrong place.

Now it gets worse.
Your executing program code is like a read buffer. It can be dropped instantly and reloaded as required. That's a very bad thing if it happens.
Code executes at less than the speed of disk reads.
The program goes for some code, gets a page fault because its not in RAM, the kernel does a context switch to try to let something else run while it fetches the missing code and so on.

swap can delay the onset of 'code swapping' when RAM is marginal, maybe prevent it by providing a home for dynamically allocated RAM that won't be needed for a long time.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 11:49 am    Post subject: Reply with quote

Ionen wrote:
What I/O scheduler are you using? Not [none], right? If all rootfs i/o came to a halt due to swapping I guess it could be something that'd freeze the system pretty well.

Edit: I know some people sometime recommended [none] for nvme ssd like it was immune from being 100% used


I never messed with I/O scheduler ;) But here's the current one for the nvme:
Code:
[root@morgoth]/swap> cat /sys/block/nvme0n1/queue/scheduler
[none] mq-deadline kyber bfq
[root@morgoth]/swap> 
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2727

PostPosted: Tue Nov 10, 2020 11:56 am    Post subject: Reply with quote

I think that may be the related to your freezing issues then, I use [bfq] myself (with its default low latency mode, nice for a desktop).

With [none] if I were to use the i/o 100% of my nvme ssd then I can't even open a man page anymore until that's done. Applications of course wouldn't be able to read libraries, config files, or any other filesystem access and hang trying to read stuff.

If I do the same with [bfq], then the man page opens like it's nobody's business.


Last edited by Ionen on Tue Nov 10, 2020 11:59 am; edited 1 time in total
Back to top
View user's profile Send private message
389292
Guru
Guru


Joined: 26 Mar 2019
Posts: 504

PostPosted: Tue Nov 10, 2020 11:59 am    Post subject: Reply with quote

option 4:
lowering the makeopts
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 12:04 pm    Post subject: Reply with quote

NeddySeagoon wrote:
MorgothSauron,

Disabling swap is a bad idea. The kernel has several ways it can swap and disabling swap prevents one of them from working and puts pressure on the others.

Swap, file/partition/zram, no matter how its implemented, is only used for dynamically allocated RAM. That includes the content of tmpfs.
Think of it as RAM content that has no permanent home on disk.


I hope I understand this correctly: it's best to have a swap in place and it's doesn't really matter how it's implemented (file, partition or zram).

This means one thing: my current swap setup is not adequate for my configuration and system use. As someone pointed out 1.5Gb can be quickly "out done" which implies that the swap is not big enough.

Again, I'm aware there is no one size fits all type of solution. I'm mostly looking at potential solutions to reduce the risk of system freeze when the load increases.
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2727

PostPosted: Tue Nov 10, 2020 12:12 pm    Post subject: Reply with quote

There's differing opinions on best practices regarding this, and indeed not really a one-size-fits-all.

But I do believe your freezing is really just a scheduler issue, you have the swapfile on your nvme ssd (which I guess is your rootfs / ). And when heavy usage kicks in it doesn't let anything else work with a noop [none] ""scheduler"". If can resolve this then "occasional" swap usage wouldn't be so bad.
Back to top
View user's profile Send private message
Anon-E-moose
Watchman
Watchman


Joined: 23 May 2008
Posts: 6103
Location: Dallas area

PostPosted: Tue Nov 10, 2020 12:13 pm    Post subject: Re: Disable swap - Good or bad idea ? Reply with quote

MorgothSauron wrote:
Hello,
I know there is no one size fits all solution when swap is involved. However I'd like to check few things with you.

I have 16Gb of RAM and a 1.5Gb swap file. With this setup I'm pretty sure to have a system freeze when under heavy load. By freeze I mean that the GUI (KDE) is unresponsive, music repeating on a 2 second loop and I'm unable to switch to a console (ctrl+alt+f1). All I can do is a hard reset and damn I hate doing that.


Highly unlikely that it has anything to do with swapping. ETA: but you could disable swap just to check.

How's your entropy when the system is heading towards freezing "cat /proc/sys/kernel/random/entropy_avail"

Edit to add:
Code:
$ cat /sys/block/nvme0n1/queue/scheduler
[mq-deadline] kyber bfq none


I never have pauses/slowdowns/freezes because of the nvme.
_________________
PRIME x570-pro, 3700x, 6.1 zen kernel
gcc 13, profile 17.0 (custom bare multilib), openrc, wayland
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 12:42 pm    Post subject: Reply with quote

Ionen wrote:
There's differing opinions on best practices regarding this, and indeed not really a one-size-fits-all.

But I do believe your freezing is really just a scheduler issue, you have the swapfile on your nvme ssd (which I guess is your rootfs / ). And when heavy usage kicks in it doesn't let anything else work with a noop [none] ""scheduler"". If can resolve this then "occasional" swap usage wouldn't be so bad.


Both the rootfs and the swapfile are on the same nvme drive.

I have a "standby" SSD (crucial mx300) and I noticed that it has a different scheduler
Code:
[root@morgoth]/swap> cat /sys/block/sda/queue/scheduler
[mq-deadline] kyber bfq none
[root@morgoth]/swap>


Ok, I think I have a lot to "play" with right now. I'll try different things and see how it goes for my setup. I just need to be careful to not change too many thing at once. I think I'll try the scheduler first since few people seems to think my problem is mostly related to the scheduler.
Back to top
View user's profile Send private message
MorgothSauron
Tux's lil' helper
Tux's lil' helper


Joined: 24 Sep 2020
Posts: 76

PostPosted: Tue Nov 10, 2020 3:49 pm    Post subject: Re: Disable swap - Good or bad idea ? Reply with quote

Anon-E-moose wrote:

How's your entropy when the system is heading towards freezing "cat /proc/sys/kernel/random/entropy_avail"

Edit to add:
Code:
$ cat /sys/block/nvme0n1/queue/scheduler
[mq-deadline] kyber bfq none


I never have pauses/slowdowns/freezes because of the nvme.


I managed to trigger the problem again while watching the value of /proc/sys/kernel/random/entropy_avail. From start to the freeze the value never dropped below 3500.

I was also watching htop output. As expected the memory usage was very close to the max and the swapfile used up to 75%. I started few more programs to add a bit more stress on the system. The swap usage kept increasing slowly and then the system froze with no way to recover it. This shows that the swap usage in itself is not the problem. The problem seems to appear when I max out both the RAM and swap.

I'll retry doing the same with an I/O scheduler set.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum