6

On a Linux machine, we can enable swap by commands like the following

sudo fallocate -l 500M /data/swapfile sudo chmod 600 /data/swapfile sudo mkswap /data/swapfile sudo swapon /data/swapfile 

But even when this is not enabled, the kernel still does paging when a page is not in memory.

We can verify this by running the sar -B 1 30 command on a machine without setting any swap file.

03:08:40 AM pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff 03:08:41 AM 0.00 0.00 3.00 0.00 44.00 0.00 0.00 0.00 0.00 03:08:42 AM 0.00 0.00 19.00 0.00 30.00 0.00 0.00 0.00 0.00 03:08:43 AM 0.00 0.00 0.00 0.00 3.00 0.00 0.00 0.00 0.00 03:08:44 AM 24.00 0.00 2.00 1.00 7.00 0.00 0.00 0.00 0.00 03:08:45 AM 364.00 60.00 18.00 3.00 4.00 0.00 0.00 0.00 0.00 03:08:46 AM 140.00 0.00 392.00 2.00 243.00 0.00 0.00 0.00 0.00 

There is still majflt which will trigger paging out data to the disk paging in data from the disk.

My Questions is:

  • Can we say there are two types of swapping on the OS?
  • How do the two mechanisms work differently?
  • If there is always a paging mechanism working, why is there still a need to enable swap manually?

I know some people said:

Swapping refers to copying the entire process address space, or at any rate, the non-shareable-text data segment, out to the swap device, or back, in one go (typically disk).

Whereas paging refers to copying in/out one or more pages of the address space. In particular, this is at a much finer grain. For example, there are ~250,000 4 KB pages in a 1 GB RAM address space.

However, in the book Understanding the Linux Virtual Memory Manager,it doesn't seem to be this way in Linux.

Strictly speaking, Linux does not swap as “swapping” refers to coping an entire process address space to disk and “paging” to copying out individual pages. Linux actually implements paging as modern hardware supports it, but traditionally has called it swapping in discussions and documentation. To be consistent with the Linux usage of the word, we too will refer to it as swapping.

Could someone shed some light on this? Thanks!

9
  • You seem to have done your homework, so kudos on that. Are you sure you're reading the output from sar correctly? Since programs are loaded into memory as "page faults" mapped to the executable on disk, you'll always have MAJFLTs. Commented Mar 27, 2020 at 6:04
  • 1
    By the way, I've never found sar -B to be helpful. I suggest vmstat 1 -SM for a live view of your swapfile activity. Just look at the si/so (swap in / swap out) columns. Commented Mar 27, 2020 at 6:18
  • Oh, and to answer your question about why the need for a swapfile if we have paging: Paging occurs when we access a memory mapped file or (as I mentioned) running an executable. A swapfile is what is needed when you run out of RAM as it provides a place to page out parts of a running process. Commented Mar 27, 2020 at 6:21
  • 1
    Hi! @hackerb9 Yes, I know the si/so in vmstat shows the activity in swap file. So based on what you said, can I say there are 2 swapping mechanisms? And could you give me more detail on how the two mechanisms works? Or some good references are welcomed :) Commented Mar 27, 2020 at 6:29
  • 1
    @JohntheTraveler please don't copy and paste the same question everywhere serverfault.com/questions/1008629/… Commented Apr 2, 2020 at 18:33

2 Answers 2

5

There is still majflt which will trigger paging out data to the disk.

It’s the other way round: major fault are page faults which can only be addressed by paging in data from a disk.

Can we say there are two types of swapping on the OS?

Not quite; the distinction here is that paging out (copying the content of memory “somewhere else” so pages can be discarded) can use different backing stores. Pages in memory have different targets for paging out: mapped files are typically their own backing store, most other pages need a swap file or partition as backing store. Swapping refers to the latter. When the kernel needs to free a page of memory, if it wants to free an unmodified page from a file it can simply discard it, knowing that the page can be restored from the file whenever it’s needed. When it needs to discard a modified page from a file, if that file is mapped read/write with no sharing, the page will be written to the file before being discarded; again, the kernel then knows it can restore the page from the file. Anything else needs some other form of storage, or it can’t be discarded.

How do the two mechanisms work differently?

See above.

If there is always a paging mechanism working, why is there still a need to enable swap manually?

See above, mostly. The point of swap is to provide a backing store for pages which don’t have their own backing store already.

See also Why does Linux need swap space in a VM?

2
  • Hi @Stephen Could I ask one more follow-up question? Do these swapping operations backed with different stores have names for us to refer to? Commented Mar 27, 2020 at 9:17
  • 1
    Not really. My answer isn’t quite accurate, I’ll rephrase it; the general term used when copying the content of memory to “somewhere else” is paging out, and swapping is used specifically when paging out to a swap file or partition. Commented Mar 27, 2020 at 9:32
0

Your two citations have the historical/original meaning of swap. wikipedia Paging:

In the 1960s, swapping was an early virtual memory technique. An entire program would be "swapped out" (or "rolled out") from RAM to disk, and another one would be swapped in (or rolled in).

The Intel Manual is explicit:

When paging is implemented properly in the operating-system or executive, the swapping of pages between physical memory and the disk is transparent to the correct execution of a program.

(It goes on explaining it transparently works also for 16-bit virtual 8086 mode...)

AMD has also very readable parts:

Finally, system software can use the paging mechanism to map multiple, large virtual-address spaces into a much smaller amount of physical memory. Each application can use the entire 32-bit or 64-bit virtual-address space. System software actively maps the most-frequently-used virtual-memory pages into the available pool of physical-memory pages. The least-frequently-used virtual-memory pages are swapped out to the hard drive. This process is known as demand-paged virtual memory.

It does not consider a setup without swapping out to hard drive. In that case the LFUs would have to be written back to the file (if any) or dropped (if clean).


A little bit related is this new 64 instruction:

The SWAPGS instruction provides a fast method for system software to load a pointer to system data structures. SWAPGS can be used upon entering system-software routines as a result of a SYSCALL instruction or as a result of an interrupt or exception.

...

This exchange allows the system-kernel software to quickly access kernel data structures by using the GS segment-override prefix during memory references.

It always goes back to this segmentation:

Although segmentation provides a great deal of flexibility in relocating and protecting software and data, it is often more efficient to handle memory isolation and relocation with a combination of software and hardware paging support. For this reason, most modern system software bypasses the segmentation features. However, segmentation cannot be completely disabled, and an understanding of the segmentation mechanism is important to implementing long-mode system software.


So you can say there is this page-wise swapping out of RAM, and the context-switch side of swapping, reflected by the 60s implementation, but also the modern SWAPGS instruction.

7
  • Thanks for sharing the history. So in the modern system, are the two terms "paging" "swapping" somewhat interchangeable? Commented Mar 27, 2020 at 9:13
  • 1
    SWAPGS has absolutely nothing to do with swapping as discussed here. Commented Mar 27, 2020 at 9:29
  • @JohntheTraveler They are very related in Linux; but I wouldn't call that interchangeable -- maybe "paging in/out" comes closer, depending on the context. Commented Mar 27, 2020 at 9:55
  • "program swapping" (also called "roll-in/roll-out") actually, the original SO citation is very precise. Just like the CPU Manuals from Intel and AMD. Commented Mar 27, 2020 at 10:07
  • So in the modern system: - When we talk about -"swapping", it mostly means "swapping backed by a swap file for memory allocations and anonymous mmaps". Not the "program roll-in/roll-out", right? - And when we say "paging", it means the writing to/reading from the disk in the VM architecture. Does my understanding sound okay? Commented Mar 27, 2020 at 12:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.