大网数据广告活动
服务器运维问答

Linux: The difference between "paging on major page fault" and "swapping enabled manually"

· 52CE 我爱测

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, th…

问题原文

原帖链接 · John the Traveler · CC BY-SA 4.0

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.

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!

已采纳解答

原帖链接 · John Mahowald · CC BY-SA 4.0

Modern operating systems typically implement their virtual memory in terms of small chunks called pages, including swapping out to disk. This is an improvement over needing to swap out entire programs, as was done in early UNIX System V.

Several have emphasized that paging is distinct from the old swapping, including in Understanding the Linux Virtual Memory Manager. But notice that the swapping term survives.

Confusingly, swapping is only a subset of paging. Executables or memory mapped files are examples where page faults can occur independently of swap space. These file maps are backed by permanent storage already. In contrast, swap space moves around anonymous pages.

So sar paging statistics is a different metric than vmstat swap in / swap out.

Without swap space, there is no means to reclaim anonymous pages. Workloads still need their memory, so the pressure is more intense on file caches. Adding some swap space helps move anon around as well. It isn't helpful as "emergency memory", aggressive reclaim to slow swap space is terrible for performance.

An analogy: consider physically moving house. The "schedule" of memory allocations may force a time critical rush for the kernel "movers" to pick up and move everything. Add an external storage locker, and things not immediately needed can be stored there ahead of time in less disruptive smaller loads. That is swap space used effectively.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-03-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。