Sunday 12 June 2022

Re: systemd-oomd issues on desktop

On Thu, Jun 9, 2022 at 4:05 PM Steve Langasek <steve.langasek@ubuntu.com> wrote:
>
> On Thu, Jun 09, 2022 at 03:19:36PM -0400, Dan Streetman wrote:
> > > 4. Increase swap on Ubuntu. I am adding this for completeness, but I
> > > doubt this is a viable option.
>
> > Personally, I think this is the correct option. 1GB is not a good
> > default swap size.
>
> Could you elaborate why? This default was arrived at some time ago with
> input from the kernel team regarding the fact that the kernel behaves best
> when it has some swap space but shouldn't necessarily have a lot.

Because swap isn't only used when there is pressure on anonymous
pages, and over time, anonymous pages from applications that don't
access all their memory often can wind up swapped out. The amount of
'useful' swap on a system is absolutely a function of the specific
system's workload as well as overall memory, so coming up with a
default that is 'best' for all systems is no easy task; however IMHO
using an absolute value (e.g. 1GB) is definitely not the 'best'
default; a percentage of total memory, or stepped values based on
system memory ranges (similar to how kdump calculates the amount of
memory to reserve for the kdump kernel) is almost certainly a better
approach.

For some background that might help, when the system experiences
'memory pressure' (in the context of performing swap), it only means
the number of free pages has fallen below the 'high' watermark. At
this point, kswapd is engaged to (in the background) start finding
ways to increase the number of free pages. It (mostly) does this is by
looking through *both* the pagecache as well as anonymous pages, and
attempts to find pages that it can free. The value of 'swappiness'
controls how much it attempts to find pages of each type; a value of
100 means kswapd will look equally at anonymous and pagecache pages,
while by default swappiness is 60, meaing kswapd will try harder to
evict pagecache pages than swapping anonymous pages.

What that means is that swap can be used up over time, even on a
system where there is never any memory pressure from anonymous pages.
As a simple example, setup a system with 1GB swap, and 16GB of ram,
then run:
$ stress-ng -m 1 --vm-bytes 12G --vm-hang 3600 &
( wait 20-30 seconds for the pages to be allocated )
$ sudo dd if=/dev/zero of=/randomfile bs=1024k count=12k
$ for i in {1..10}; do cat /randomfile > /dev/null ; free -h ; done

That will 1) allocate 12G of anonymous pages that the application
isn't using, 2) create a file larger than the remaining free memory,
and 3) cause memory pressure on the pagecache, which (over time)
causes the anonymous pages to get swapped out. Note that at no time
was there any anonymous page memory pressure - meaning, the amount of
anonymous pages was always multiple G lower than the total amount of
memory.

A quick test for me shows I'm able to fill up the entire 1GB of swap
in just a few runs:
total used free shared buff/cache available
Mem: 15Gi 12Gi 170Mi 2.0Mi 3.3Gi 3.1Gi
Swap: 1.0Gi 35Mi 988Mi
total used free shared buff/cache available
Mem: 15Gi 12Gi 161Mi 2.0Mi 3.3Gi 3.1Gi
Swap: 1.0Gi 39Mi 984Mi
total used free shared buff/cache available
Mem: 15Gi 11Gi 147Mi 0.0Ki 3.8Gi 3.6Gi
Swap: 1.0Gi 519Mi 504Mi
total used free shared buff/cache available
Mem: 15Gi 11Gi 158Mi 0.0Ki 4.2Gi 4.1Gi
Swap: 1.0Gi 1.0Gi 0B

Also note this *in no way* should be taken as a suggestion that
'swappiness' should be reduced; that is *absolutely* not what I'm
saying and would be a mistake to lower it (by default).

Also I think the discussion of default swap size is (almost)
completely separate from the discussion of systemd-oomd; I think the
current default is set so low that it it's aggravating systemd-oomd's
behavior, but I don't think increasing the default swap size is a
singular 'fix' for any/all issues with systemd-oomd.

>
> If we think 1GB is a wrong value, we should change it from 22.04.1
> forward... but don't have a good way to automatically change the allocation
> for existing installs. Fortunately, our use of swap files mean it's
> possible for the end user to non-destructively increase their swap space,
> but I wouldn't be comfortable with us doing this automatically as part of a
> release upgrade or in an SRU!
>
> --
> Steve Langasek Give me a lever long enough and a Free OS
> Debian Developer to set it on, and I can move the world.
> Ubuntu Developer https://www.debian.org/
> slangasek@ubuntu.com vorlon@debian.org

--
ubuntu-devel mailing list
ubuntu-devel@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-devel