Tookmund / Swapspace

A fork of Jeroen T. Vermeulen's excellent dynamic swap space manager
GNU General Public License v2.0
128 stars 12 forks source link

Question about the first sentence in the readme (suspend and resume) #31

Open anohren opened 2 years ago

anohren commented 2 years ago

Hello,

The first thing that I read after the repo title is:

N.B. If you want suspend and resume to work you will still need a static swapfile!

This gives rise to a few questions for me:

  1. Can I use Swapspace in conjunction with a "static swapfile" to enable suspend and resume, or does this sentence mean "you can't use Swapspace if you want suspend and resume to work"?
  2. a. If the latter, why doesn't suspend and resume work when using Swapspace? b. Is it hard to support it? Why?
Tookmund commented 2 years ago

The fundamental problem is that Swapspace tries to dynamically allocate the bare minimum amount of swap required to keep your system running.

However, suspend and hibernate require enough swap to fit the entire contents of your RAM. Thus, you need to make sure you have that much space, and the easiest way is to have another swapfile with enough space which is what that warning is trying to convey. Rereading it now it’s not particularly clear I should probably rephrase it.

You certainly can use Swapspace (the program) with another swapfile as well it will take that into account when deciding whether to allocate new swapfiles.

It would not be super difficult to support but it would kinda defeat the purpose of this program since most people don’t typically need that much swap. If you’ve got a swapfile that large you probably don’t need Swapspace the program at all.

anohren commented 2 years ago

Thank you for your reply. It's interesting to hear your perspective on this, and it's nice to hear that I can try Swapspace out while preserving suspend and resume.

So just to make sure I understood you correctly, you mean that suspend and hibernation only needs as much swap space as total physical RAM size, and not as much swap space as currently used swap + total physical RAM size?

I find it particularly interesting when you mention the purpose of this program, which I gather can be summed up by this part of the readme:

you don't want to sacrifice too much disk space. And it's not always easy, or safe, to change its size

In other words "making it easy to save as much disk space as possible while still having swap".

I don't want to suggest there was a different purpose for having written Swapspace, but (without having tried it yet) here's the real value proposition that I see for myself, at least for a desktop system:

1. Most of the time the machine just works within the limits of RAM. Everything's fine. 2. Regularly (e.g. once a day on a low spec'd machine) swap is needed, and your swapfile is being used. Old pages get swapped to disk, but you have an SSD and everything still works well enough to stay in control of the system. The user experience is still more than acceptable. 3. Sometimes (e.g. every other day) all swap space gets consumed. Cursor locks up, all control is ripped from your hands and you receive instant punishment because you let your guard down for one second (perhaps when you opened one too many web apps disguised as a native app). Who knows when, or if, you'll ever get a workable system back? How long are you prepared to wait?

While I can only speak for myself, I far more value not having to experience # 3 than saving a few GB of my terabyte sized storage. And I believe most users of a desktop system would. In other words, the big value of Swapspace is that you don't have to predict what amout of swap will be necessary to avoid # 3 at any given time. Avoiding # 3 is also the experience I feel I have while using modern Windows and macOS.

What do you think? Have I missed something important?

Tookmund commented 2 years ago

On Wed, Jul 14, 2021, at 12:34 PM, anohren wrote:

So just to make sure I understood you correctly, you mean that suspend and hibernation only needs as much swap space as total physical RAM size, and not as much swap space as currently used swap + total physical RAM size?

This was my understanding for a long time, as I hadn't really looked into how hibernation worked, and was simply following the general wisdom. As you point out, it doesn't really make sense that it would need less than currently used + physical RAM.

Some research (https://www.kernel.org/doc/Documentation/power/interface.txt ) indicates this whole situation is more complicated than it might seem, and you might actually need even less swap than the total amount of physical ram, with the kernel aiming to make a hibernation image that is no more than 2/5ths of physical ram if possible. A quick glance at the code indicates it does a lot of work to drop caches as needed to get at or below this number, but it's only a best effort.

If you're curious see https://elixir.bootlin.com/linux/latest/source/kernel/power/hibernate.c#L277 as well as https://elixir.bootlin.com/linux/latest/source/kernel/power/snapshot.c#L1718

In summary, it looks like it needs less memory than I thought, though if you've got storage in the terabytes, having at least as much swap as you have ram is probably a good idea for hibernation still.

I find it particularly interesting when you mention the purpose of this program, which I gather can be summed up by this part of the readme:

you don't want to sacrifice too much disk space. And it's not always easy, or safe, to change its size

In other words "making it easy to save as much disk space as possible while still having swap".

I don't want to suggest there was a different purpose for having written Swapspace, but (without having tried it yet) here's the real value proposition that I see for myself, at least for a desktop system:

1. Most of the time the machine just works within the limits of RAM. Everything's fine. 2. Regularly (e.g. once a day on a low spec'd machine) swap is needed, and your swapfile is being used. Old pages get swapped to disk, but you have an SSD and everything still works well enough to stay in control of the system. The user experience is still more than acceptable. 3. Sometimes (e.g. every other day) all swap space gets consumed. Cursor locks up, all control is ripped from your hands and you receive instant punishment because you let your guard down for one second (perhaps when you opened one too many web apps disguised as a native app). Who knows when, or if, you'll ever get a workable system back? How long are you prepared to wait?

While I can only speak for myself, I far more value not having to experience # 3 than saving a few GB of my terabyte sized storage. And I believe most users of a desktop system would. In other words, the big value of Swapspace is that you don't have to predict what amout of swap will be necessary to avoid # 3 at any given time. Avoiding # 3 is also the experience I feel I have while using modern Windows and macOS.

What do you think? Have I missed something important?

Swapspace was, to the best of my knowledge, written for that purpose, but also for the purpose of saving disk space when possible. It was originally written by @jtv in 2005, when disk space was more of a problem for lower-spec'd machines. Hibernate was only implemented in 2008 in Linux 2.6.25 (https://kernelnewbies.org/Linux_2_6_25 ). Swapspace simply does not account for it because it didn't exist when it was written.

There is, I think, a good argument that with all the new features of linux that have been added around swap (zram, zswap, pressure stall information, and probably more I'm forgetting right now) and having to account for hibernate, it would be a good time for a rewrite. I unfortunately don't see myself having the time anytime soon, but it is something I've been thinking about.

In summary, my recommendation to have an extra swapfile for hibernation support is simply a workaround for swapspace's current limitations. Adding hibernation support is not entirely out of the question, but likely has quite a few edge cases that will need to be carefully considered if implemented, since it seems difficult to guarantee that some minimum amount of swap will be enough for hibernation.

jtv commented 2 years ago

I did try to balance disc usage versus swap to some extent, but Swapspace was always a lot more careful about the latter than about the former — simply because we normally have a lot more of the former than we need of the latter. Throw in too many variables and anything will get complicated.

One thing you can still do, I suppose, is just configure a sufficient minimum swap space, and Swapspace will automatically ensure that you get it.

ghost commented 1 year ago

Can it be possible to hold suspension with sleep timer after user interaction, then put into hibernation as soon as the required amount swap generated ?