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!
sar
correctly? Since programs are loaded into memory as "page faults" mapped to the executable on disk, you'll always have MAJFLTs. – hackerb9 Mar 27 '20 at 06:04sar -B
to be helpful. I suggestvmstat 1 -SM
for a live view of your swapfile activity. Just look at the si/so (swap in / swap out) columns. – hackerb9 Mar 27 '20 at 06:18So 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 :)
– John the Traveler Mar 27 '20 at 06:29