News for hackers. Stuff that matters.
Re:Swap, swap, swap, and bad places to work by KaiserPro on Friday August 09, 2019 @03:15AM (#20651982)
Swap, the sub religion.

I've wobbled back and forward on swap. In the early days I used to be annoyed by how much disk space it'd take. (an 8 gig disk with 1 gig for swap is too much)

I've run an 8 core machine with only 2 gigs of ram, and tried to compile something with boost in it. swap allowed me to kill it, and recover the system.

I've run VMs with no swap, some swap and loads.

However, what I've never done is actually benchmarked the same workload on machines with no, some and loads of swap. However, I generally defer to Rachel, because Rachel has been there and been bitten by that before.

On this point: http://rachelbythebay.com/w/2018/04/28/meta/ which everyone should read and digest, this remark jumped out:

>"This is so easy to test for"

If I ever say this and never qualify it, it should read: "haha, yeah, I made that same mistake, that's why I test for it now."

The only reason why I am "better"[I hope] than my younger self, is because I've made a bucket load of mistakes before. Some of them are technical, but to be honest a load of them are societal. (as in, bleating like Cassandra and not being able to affect change.)

If we, as "engineers" are to grow as a class of people, we have to actually learn from other people's mistakes, not just use them as bias confirmation. This is why I like blog posts where they lay out the problem, outfall, cause, workaround and eventual solution.

Re:Swap, swap, swap, and bad places to work by slacka on Friday August 09, 2019 @10:37AM (#20654740)
It's not always about performance. Years ago while traveling I installed Linux to a flash drive to carry my entire OS, apps, and data with me. While I did backup often, I was well aware of the danger that excessive write wear posed in killing my stick, so swap was disabled. To this very day and for the same reason, my Pi also has swap disabled.

As a result, I've encountered this flaw the Linux kernel design multiple times. And it is a flaw, as you only have to do the same test on Windows to see how an OS should behave when you run out of RAM: a hiccup followed by your offending tab being terminated by the OOM killer. Locking up the system for 10 min. is just not acceptable.

Re:Swap, swap, swap, and bad places to work by falsedan on Friday August 09, 2019 @06:14AM (#20652902)
> Some of them are technical, but to be honest a load of them are societal. (as in, bleating like Cassandra and not being able to affect change.)

This is where I am now, where I'm comfortable making technical mistakes and know enough to avoid the most grevious pitfalls, but struggle to expand the sphere of things affected by my changes beyond a few people or single team/product. Some people get into management for this, but I've seen that management is mostly planning, budgeting, hiring/firing, and non-technical communications.

Re:Swap, swap, swap, and bad places to work by healsjnr1 on Friday August 09, 2019 @07:30AM (#20653308)
This may seem like dumb wordplay, but while management may seem to be about hiring / firing, budgets, etc, _leadership_ is about _influence_.

Influence and leadership are force multipliers. If you can lead people well and influence decisions (be it as a people leader, or a technical leader, or both), your experience can be learnt from by more and more people.

There is no doubt that leadership involves doing 'management' more often than not. But it's just one aspect, it's not the end in itself.

Re:Swap, swap, swap, and bad places to work by collyw on Friday August 09, 2019 @02:36PM (#20657133)
Depends on the company.
Re:Swap, swap, swap, and bad places to work by fit2rule on Friday August 09, 2019 @05:40AM (#20652748)
>benchmarked

This is what this story is really about. People not instrumenting their systems, no benchmarks, no real clue how things will perform under load.

Swap or not swap: it doesn't matter what you do if you haven't benchmarked the system before you set it up - in this case, its your customers who will tell you, eventually, how your software behaves ...

Re:Swap, swap, swap, and bad places to work by VectorLock on Friday August 09, 2019 @10:07AM (#20654416)
>However, what I've never done is actually benchmarked the same workload on machines with no, some and loads of swap. However, I generally defer to Rachel, because Rachel has been there and been bitten by that before.

I have. A well utilized machine is going to absolutely tank once it hits swap. Do you want to engineer your application to be able to cope with two radically different performance regimes, or do you simply want to ensure that your working set stays bounded?

Re:Swap, swap, swap, and bad places to work by chc on Friday August 09, 2019 @02:02PM (#20656835)
If you ensure your working set stays bounded, having swap enabled (say, with swappiness 1) is not a big deal. If you're worried about the performance characteristics of swap, you've already conceded that you aren't confident in the bounds of your working set. So it doesn't seem like the relevant question is "Do we want to have well-bounded memory or use swap?" so much as "Can you tolerate some temporary slowness more easily than a server crash?"
Re:Swap, swap, swap, and bad places to work by zaphar on Friday August 09, 2019 @10:17AM (#20654529)
I have too. I've also been in the opposite scenario where not having swap caused run away service kill and restart scenarios. Reading between the lines it looks like Rachel has too. Which is why she advises a "not too much" swap approach. As is usual the answer is not one or the other extreme it's much more nuanced than that.
Re:Swap, swap, swap, and bad places to work by VectorLock on Friday August 09, 2019 @12:38PM (#20655942)
>I've also been in the opposite scenario where not having swap caused run away service kill and restart scenarios.

Seems like a good time to just kill it and move on. "Cattle not pets."

Re:Swap, swap, swap, and bad places to work by zaphar on Saturday August 10, 2019 @02:42PM (#20663804)
The funny thing about cattle is that they stampede. One host goes down the load increases on the others. They go down too. That cascades until they are all in a kill/restart cycle. I've seen lack of swap cause this. I'm betting Rachel has too.
Re:Swap, swap, swap, and bad places to work by andyjpb on Friday August 09, 2019 @03:06PM (#20657385)
It's still not as simple as that.

Swap is there to relieve memory allocation pressure. Memory allocation pressure is an incredibly dense concept but it basically means "if lots of people are asking for fresh pages, how quickly can I service them"?

There are also other types of memory pressure. One is "if lots of people are reading and writing to pages, how quickly can I service them?"

This depends on the state of the mapping for the virtual page being read or written. If that virtual page has an associated physical page then the answer is "not too slowly". If the physical page is in one of the caches then the answer is "more quickly". If the relevant part of the page is in a register then the answer is "in one clock cycle".

On the other hand, if the read or write is associated with a page that needs to be mapped in, either from a disk file or a swap file, then the answer is "slowly".

These types of pressure need to be balanced with each other. The idea isn't just to keep as much data in physical RAM as possible. The idea is to use the RAM as effectively as possible (perhaps by keeping as much relevant data in RAM as possible) whilst also being able to respond effectively to requests for fresh pages.

In general, under pressure and load, the Linux kernel tries to keep a few MiB of ready-to-allocate pages. When it runs out of these it raids the page cache. It's reasonably quick to get (non-dirty) pages from the page cache because they can just be zero'd and handed out. The most difficult pages to reclaim are those that must be copied to swap before they are zero'd. So the kernel tries to minimise that.

How does it do that? Well, it has some tricks:

When processes load and run they allocate a bunch of pages. Sometimes, and not infrequently, they allocate and write to pages which are never accessed again!

I see this on my laptop all the time. I never use even close to the physical amount of RAM I have in the machine. Right now I'm using 4,959MiB of 15,799MiB and it rarely surpasses 6 or 7 GiB. However, if I leave it running for any length of time (a number of days or weeks) then I start to see a little bit of swap getting allocated. I've currently been up about 98 days and right now I'm using 317MiB of swap.

What's happened here is that the kernel has swapped out pages that it thinks are never going to be used again. That way, if memory allocation pressure suddenly increases it's got those physical pages ready to service those requests.

If there was no swap, those pages would be unnecessarily pinned into physical memory even tho' they would never be used.

Another commenter asked a question like "What's the difference between a machine with some memory and some swap and a machine with more memory?" Well, there are a few subtle differences, but this is one of them. The machine with swap will have a higher percentage of physical memory available and will be able to respond faster to larger allocations.

Another difference is price. RAM is still expensive compared to disk. If you have a 16GiB box with a bit of swap then you have a 16GiB box that you can use for data that's actively being read and written. If you don't have the swap then you're paying for some of that RAM that gets written and never used. This doesn't matter so much in the small scale but when you have a few machines you want to be getting the most out of them (and what that really means is the subject of another post!).

Swap space gets you more bang for your buck.

So swap space is really about giving the kernel a mechanism to manage the different types of memory pressure without taking too many compromises on the different trade-offs. Even if you're never using all your RAM, swap will still get used so that the machine can respond optimally to as many types of memory activity as possible.

One thing I'd really like to know is whether the number `free` gives me for "Swap used" is the amount of data that's in swap and nowhere else or whether it's the amount of swap space that's used even if the pages are also still in physical memory.

Re:Swap, swap, swap, and bad places to work by VectorLock on Friday August 09, 2019 @04:47PM (#20658134)
You make a good point about swapping out unused pages, and I'd love to discover how to balance between maintaining the "fail fast" of swap-off and the ability to swap out never used paged with swap-some. While the latter would be nice to have that extra RAM its not worth the risk, in my world at least.
Re:Swap, swap, swap, and bad places to work by zzzcpan on Friday August 09, 2019 @05:08PM (#20658271)
> how to balance between maintaining the "fail fast" of swap-off and the ability to swap out never used paged with swap-some

A monitoring process, like earlyoom, can do this for you. Also features like cgroups, setrlimit(), prlimit() can help. Last one can even be used to dynamically adjust memory limits per each process and avoid kernel's weird OOM behavior. This is all in accordance with fail fast, which requires monitoring processes that actually deal with all the failings.