使用 zram 的主要缺點是LRU反轉:
較舊的頁面進入優先順序較高的 zram 并快速填充它,而較新的頁面則從較慢的交換區換入和換出 [...]
這zswap 文檔說 zswap 不會受到這個問題的影響:
Zswap 透過 Frontswap API 接收用於壓縮的頁面,並且能夠以 LRU 為基礎從自己的壓縮池中逐出頁面,並在壓縮池已滿的情況下將它們寫回後備交換設備。
max_pool_percent
透過設定為 ,我可以享受 zram 和完全壓縮 RAM 的所有好處嗎100
?
Zswap seeks to be simple in its policies. Sysfs attributes allow for one user controlled policy: * max_pool_percent - The maximum percentage of memory that the compressed pool can occupy.
這裡沒有max_pool_percent
指定預設值,但是拱門維基頁面說是的20
。
除了解壓縮的性能影響之外,設定max_pool_percent
為是否有任何危險/缺點100
?
它會像使用改進的交換支援的 zram 一樣運作嗎?
答案1
為了回答你的問題,我先進行了一系列實驗。最終答案以粗體顯示在最後。
進行的實驗:
1) swap file, zswap disabled
2) swap file, zswap enabled, max_pool_percent = 20
3) swap file, zswap enabled, max_pool_percent = 70
4) swap file, zswap enabled, max_pool_percent = 100
5) zram swap, zswap disabled
6) zram swap, zswap enabled, max_pool_percent = 20
7) no swap
8) swap file, zswap enabled, max_pool_percent = 1
9) swap file (300 M), zswap enabled, max_pool_percent = 100
實驗前設定:
- 虛擬盒 5.1.30
- Fedora 27,xfce spin
- 512 MB RAM、16 MB 視訊 RAM、2 個 CPU
- Linux核心4.13.13-300.fc27.x86_64
- 預設
swappiness
值 (60) - 建立了一個空的 512 MB 交換檔案(實驗 9 中為 300 MB),以便在某些實驗期間可能使用(使用) ,
dd
但尚未swapon
- 停用所有 dnf* systemd 服務,運行
watch "killall -9 dnf"
以更確定 dnf 不會在實驗期間嘗試自動更新或其他操作,從而使結果偏離太多
實驗前說明:
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 280 72 8 132 153
Swap: 511 0 511
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 74624 8648 127180 0 0 1377 526 275 428 3 2 94 1 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 102430 688 3593850 67603 3351 8000 1373336 17275 0 26
sr0 0 0 0 0 0 0 0 0 0 0
隨後的交換操作等導致實驗期間的不同設置,導致這些值的差異在大約 2% 以內。
實驗操作包括:
- 首次運行 Firefox
- 等待約 40 秒或直到網路和磁碟活動停止(以較長者為準)
- 記錄實驗後的以下狀態(firefox保持運行,除了實驗7和實驗9,firefox崩潰了)
實驗後狀態:
1) 交換文件,zswap 禁用
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 287 5 63 192 97
Swap: 511 249 262
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 255488 5904 1892 195428 63 237 1729 743 335 492 3 2 93 2 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 134680 10706 4848594 95687 5127 91447 2084176 26205 0 38
sr0 0 0 0 0 0 0 0 0 0 0
2) 交換文件,啟用 zswap,max_pool_percent = 20
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 330 6 33 148 73
Swap: 511 317 194
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 325376 7436 756 151144 3 110 1793 609 344 477 3 2 93 2 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 136046 1320 5150874 117469 10024 41988 1749440 53395 0 40
sr0 0 0 0 0 0 0 0 0 0 0
3) 交換文件,啟用 zswap,max_pool_percent = 70
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 342 8 32 134 58
Swap: 511 393 118
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 403208 8116 1088 137180 4 8 3538 474 467 538 3 3 91 3 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 224321 1414 10910442 220138 7535 9571 1461088 42931 0 60
sr0 0 0 0 0 0 0 0 0 0 0
4) 交換文件,啟用 zswap,max_pool_percent = 100
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 345 10 32 129 56
Swap: 511 410 101
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 420712 10916 2316 130520 1 11 3660 492 478 549 3 4 91 2 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 221920 1214 10922082 169369 8445 9570 1468552 28488 0 56
sr0 0 0 0 0 0 0 0 0 0 0
5)zram交換,zswap禁用
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 333 4 34 147 72
Swap: 499 314 185
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
5 0 324128 7256 1192 149444 153 365 1658 471 326 457 3 2 93 2 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 130703 884 5047298 112889 4197 9517 1433832 21037 0 37
sr0 0 0 0 0 0 0 0 0 0 0
zram0 58673 0 469384 271 138745 0 1109960 927 0 1
6) zram 交換,啟用 zswap,max_pool_percent = 20
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 338 5 32 141 65
Swap: 499 355 144
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 364984 7584 904 143572 33 166 2052 437 354 457 3 3 93 2 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 166168 998 6751610 120911 4383 9543 1436080 18916 0 42
sr0 0 0 0 0 0 0 0 0 0 0
zram0 13819 0 110552 78 68164 0 545312 398 0 0
7)無交換
請注意,在記錄這些統計數據時,本實驗中 Firefox 並未運行。
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 289 68 8 127 143
Swap: 0 0 0
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 70108 10660 119976 0 0 13503 286 607 618 2 5 88 5 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 748978 3511 66775042 595064 4263 9334 1413728 23421 0 164
sr0 0 0 0 0 0 0 0 0 0 0
8) 交換文件,啟用 zswap,max_pool_percent = 1
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 292 7 63 186 90
Swap: 511 249 262
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 255488 7088 2156 188688 43 182 1417 606 298 432 3 2 94 2 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 132222 9573 4796802 114450 10171 77607 2050032 137961 0 41
sr0 0 0 0 0 0 0 0 0 0 0
9) 交換檔案 (300 M),啟用 zswap,max_pool_percent = 100
Firefox 卡住了,系統仍然瘋狂地從磁碟讀取資料。由於寫入了新的交換文件,因此實驗的基線有所不同:
total used free shared buff/cache available
Mem: 485 280 8 8 196 153
Swap: 299 0 299
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 8948 3400 198064 0 0 1186 653 249 388 2 2 95 1 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 103099 688 3610794 68253 3837 8084 1988936 20306 0 27
sr0 0 0 0 0 0 0 0 0 0 0
具體來說,由於此更改,額外寫入了 649384 個扇區。
實驗後狀態:
[root@user-vm user]# free -m ; vmstat ; vmstat -d
total used free shared buff/cache available
Mem: 485 335 32 47 118 53
Swap: 299 277 22
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
7 1 283540 22912 2712 129132 0 0 83166 414 2387 1951 2 23 62 13 0
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 3416602 26605 406297938 4710584 4670 9025 2022272 33805 0 521
sr0 0 0 0 0 0 0 0 0 0 0
從 2022272 中減去額外的 649384 個寫入磁區,結果為 1372888。 這小於 1433000(見下文),這可能是因為 Firefox 未完全載入。
我還運行了一些低值swappiness
(10 和 1)的實驗,它們都因過多的磁碟讀取而陷入凍結狀態,導致我無法記錄最終的記憶體統計資料。
觀察結果:
- 主觀上,高
max_pool_percent
價值導致遲緩。 - 主觀上,實驗 9 的系統速度太慢,以致於無法使用。
- 高
max_pool_percent
值會導致最少的寫入量,而非常低的值max_pool_percent
會導致最多的寫入量。 - 實驗 5 和 6(zram 交換)顯示 Firefox 寫入的資料導致大約 62000 個磁區寫入磁碟。任何高於 1433000 的磁區都是由於交換而寫入的。參見下表。
- 如果我們假設實驗中讀取扇區的最低數量作為基線,我們可以根據交換造成的額外讀取扇區數量來比較實驗。
交換直接導致的書面扇區(大約):
650000 1) swap file, zswap disabled
320000 2) swap file, zswap enabled, max_pool_percent = 20
30000 3) swap file, zswap enabled, max_pool_percent = 70
40000 4) swap file, zswap enabled, max_pool_percent = 100
0 5) zram swap, zswap disabled
0 6) zram swap, zswap enabled, max_pool_percent = 20
-20000 7) no swap (firefox crashed)
620000 8) swap file, zswap enabled, max_pool_percent = 1
-60000 9) swap file (300 M), zswap enabled, max_pool_percent = 100 (firefox crashed)
交換直接導致的額外讀取扇區(大約):
51792 1) swap file, zswap disabled
354072 2) swap file, zswap enabled, max_pool_percent = 20
6113640 3) swap file, zswap enabled, max_pool_percent = 70
6125280 4) swap file, zswap enabled, max_pool_percent = 100
250496 5) zram swap, zswap disabled
1954808 6) zram swap, zswap enabled, max_pool_percent = 20
61978240 7) no swap
0 (baseline) 8) swap file, zswap enabled, max_pool_percent = 1
401501136 9) swap file (300 M), zswap enabled, max_pool_percent = 100
結果解讀:
- 這是主觀的,並且特定於當前的用例;在其他用例中行為會有所不同。
- Zswap 的頁面池佔用了RAM 中原本可以由系統頁面快取(用於檔案支援的頁面)使用的空間,這意味著系統會重複丟棄檔案支援的頁面並在需要時再次讀取它們,從而導致過多的讀取。
- 實驗 7 中的大量讀取是由相同的問題引起的 - 系統的匿名頁面佔用了大部分 RAM,並且必須從磁碟重複讀取文件支援的頁面。
- 在某些情況下,寫入交換磁碟的資料量可以最小化到接近零,
zswap
但它顯然不適合此任務。 - 不可能有“完全壓縮的RAM「因為系統需要一定數量的非交換頁駐留在 RAM 中進行操作。
個人觀點和軼事:
- zswap 在磁碟寫入方面的主要改進不是它壓縮頁面,而是它有自己的緩衝和快取系統,可以減少頁面快取並有效地將更多匿名頁面(以壓縮形式)保留在 RAM 中。 (但是,根據我日常使用 Linux 時的主觀經驗,具有 swap 且
zswap
預設值為swappiness
和的系統max_pool_percent
總是比任何swappiness
值、沒有zswap
或zswap
具有較高值的系統表現得更好max_pool_percent
。) - 較低的
swappiness
值似乎會使系統表現得更好,直到剩餘的頁面快取量非常小,導致系統因過多的磁碟讀取而無法使用。與太高類似max_pool_percent
。 - 要么僅使用
zram
交換並限制需要在內存中保存的匿名頁面的數量,要么使用磁碟支援的交換,並且和 的zswap
值大約為預設值。swappiness
max_pool_percent
編輯:未來回答您問題的細節的可能工作是找出您的特定用例zsmalloc
中使用的分配器如何在zram
壓縮方面與zbud
中使用的分配器進行比較zswap
。不過,我不會這樣做,只是指出要在文件/網路上搜尋的內容。
編輯2:
echo "zsmalloc" > /sys/module/zswap/parameters/zpool
將 zswap 的分配器從 切換zbud
為zsmalloc
.繼續我上述的實驗的測試裝置並與zram
+zswap
進行比較zsmalloc
,似乎只要所需的交換記憶體與zram
swap 或 aszswap
相同max_pool_percent
,兩者之間的磁碟讀寫量就非常相似。基於事實,我個人認為,只要zram
我需要的交換量小於zram
我在RAM中實際能夠保留的交換量,那麼最好單獨使用zram
;一旦我需要的交換量超出了我實際可以保留在內存中的數量,最好改變我的工作負載以避免它,或者禁用zram
交換並使用zswap
withzsmalloc
並將其設置為與zram 之前在內存中佔用的內容相同的值( *壓縮比max_pool_percent
的大小)zram