爲什麼 Linux 和 macOS 不需要碎片整理

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲什麼這麼設計(Why’s THE Design)是一系列關於計算機領域中程序設計決策的文章,我們在這個系列的每一篇文章中都會提出一個具體的問題並從不同的角度討論這種設計的優缺點、對具體實現造成的影響。如果你有想要了解的問題,可以在文章下面留言。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相信今天很多的軟件工程師使用的都是 Linux 或者 macOS 系統,與 Windows 不同,我們很難看到磁盤碎片整理這一概念,從個人的經驗來看,作者在過去七八年沒有在 macOS 中整理過磁盤的碎片,你在今天的磁盤工具中也找不到相關的操作,只能通過 diskutil 命令設置某一塊磁盤是否開啓或者關閉碎片整理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/73\/73c2f645ab3f4f29514a730801ce8298.png","alt":"macos-disk-utility","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"圖 1 - macOS 磁盤工具"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們在 "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/","title":null,"type":null},"content":[{"type":"text","text":"前一篇文章"}]},{"type":"text","text":" 中曾經分析過爲什麼早期 Windows 操作系統每隔一段時間可能需要整理磁盤上的碎片"},{"type":"sup","content":[{"type":"text","text":"1"}]},{"type":"text","text":",該問題背後有兩個原因,其一是 Windows 使用的 FAT 是很簡單的文件系統,該文件系統的設計決定了同一份文件可能會散落在磁盤的不同位置,其二是固態硬盤在上古時代沒有普及,機械硬盤的隨機讀寫性能很差。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Linux 和 macOS 系統不需要碎片整理的原因與 Windows 需要碎片整理的原因正好相反:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Linux 和 macOS 使用的文件系統或者降低了碎片發生的概率或者實現自動整理碎片的特性;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"固態硬盤與機械硬盤具有不同的特性,碎片整理可能不僅對提高讀寫性能沒有顯著的幫助,還不利於硬件的使用壽命;"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"文件系統"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Linux 一般都使用 Ext2、Ext3 和 Ext4 文件系統,今天的大多數 Linux 發行版都選擇了 Ext4。與 Windows 將多個文件連續存儲的方式不同,Linux 會把文件散落到磁盤的不同地方存儲,同時在文件之間留下一些空間,保證文件在修改或者更新時不會造成碎片。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/9a\/9af9d4eec8cc9372a161a293a2a1266b.png","alt":"linux-filesystem-allocaiton","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"圖 2 - Linux 文件系統"},{"type":"sup","content":[{"type":"text","text":"2"}],"marks":[{"type":"strong"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今天的 macOS 多數都使用 APFS 文件系統"},{"type":"sup","content":[{"type":"text","text":"3"}]},{"type":"text","text":",它是蘋果專門爲固態硬盤等設備優化的文件系統。更早的 HFS 和 HFS+ 都是用了基於區塊(Extent)的設計,每個區塊都包含序號和一段連續的存儲空間,這種分配方式會在文件系統中查找幾個連續的區塊來提供所需的空間。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/36\/369091cf579829a02ebfacb2714df129.webp","alt":"macos-format","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"圖 3 - macOS 文件系統"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"無論是 Linux 還是 macOS,它們的文件系統都是基於區塊設計的,而磁盤的空間分配也相對比較合理,所以不會出現 Windows 系統上碎片化的磁盤。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了文件系統在設計就避免了碎片的出現之外,Linux 和 macOS 也都是引入了延遲分配空間的策略,它們會通過緩衝區儘可能延遲磁盤寫入的時間,這樣不僅能夠降低刷盤的概率,還能增加文件寫入相鄰區塊的概率,然而這種機制並不是沒有副作用,在系統斷電或者崩潰時可能會丟失更多的數據。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果磁盤上確實出現了碎片,那麼 Linux 和 macOS 的文件系統也會嘗試移動出現碎片的文件,不需要額外的碎片整理工具,這種設計帶來的用戶體驗會比手動觸發耗時較長的碎片整理好很多。macOS 上的 HFS+ 系統還支持實時的去碎片化,當滿足以下條件時會觸發碎片整理"},{"type":"sup","content":[{"type":"text","text":"4"}]},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件小於 20 MB;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件存儲在 8 個以上的區塊上;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件在過去一分鐘沒有被更新;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"系統已經啓動了三分鐘;"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在多數情況下,這些操作系統中的磁盤碎片比例都非常低,只有在磁盤空間不足時纔會開始出現碎片,所以在這時我們其實需要的是一個更大的磁盤或者更新的電腦,而不是整理磁盤上的碎片。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"固態硬盤"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"固態硬盤其實已經是有着三十年曆史的存儲介質了,但是由於固態硬盤的價格在過去一直都十分昂貴,所以沒有在數據中心和個人電腦中普及開來。哪怕是在今天,機械磁盤的價格與固態硬盤相比也有比較明顯的優勢。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/09\/09157c629619f2b2e1bd67f9156ecf4d.png","alt":"SSD-vs-HDD-price-erosion","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"圖 4 - 固態硬盤和機械硬盤價格對比"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"新型的存儲介質帶來了全新的特性和性能,我們在前一篇文章中曾經介紹過,因爲機械硬盤的機械結構,所以它的隨機 I\/O 與順序的 I\/O 性能可能相差幾百倍,碎片整理可以將散落在磁盤上的數據合併到一處,隨機 I\/O 的次數減少自然也會提高讀寫文件的性能。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"固態硬盤的順序 I\/O 和隨機 I\/O 在性能上雖然也有差異,但是差距可能在十幾倍到幾十倍之間,而固態硬盤的隨機 I\/O 延遲也比機械磁盤好幾十倍甚至上千倍,到現在來看整理固態硬盤上的碎片雖然有收益,但是也比較有限。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/ce\/cefaf2d1fdd24d1ba792fe978b3640cc.png","alt":"nand-flash-evolution","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"圖 5 - NAND 閃存的演進"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作爲電子元件的固態硬盤雖然有着較好的性能表現,但是固態硬盤都有循環擦寫的次數限制,也被稱作 P\/E。它的壽命與機械硬盤相比卻比較有限。如果一個 512 GB 的固態硬盤的擦寫數目是 1000 次,每次寫滿數據都會消耗一次壽命,等擦寫次數達到 1000 次之後硬盤就會報廢,碎片整理其實就是主動移動硬盤上的數據,自然會影響硬件的壽命。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在軟件工程中有一個非常有趣的現象,做硬件和基礎架構的工程師都在拼命優化系統的性能,然而應用層的工程師很多時候並不在乎性能上的微小差異,而這也是工作職責上的差異帶來的結果,不同的位置決定了不同的關注點。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"硬件的演進和革新深深地影響着上層軟件的設計,想要設計出通用的系統是異常困難的,在設計文件系統時如果不考慮底層硬件的特性,也就無法充分利用硬件提供的性能並得到期望的結果。這裏簡單總結一下 Linux 和 macOS 不需要碎片整理的兩個原因:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件系統基於區塊分配的設計使得磁盤上出現碎片的概率很低,延遲分配和自動的整理策略解放了操作系統的使用者,在多數情況下不需要考慮磁盤的碎片化;"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"固態硬盤的隨機讀寫性能遠遠好於機械硬盤,隨機讀寫和順序讀寫雖然也有性能差異,但是沒有機械硬盤的差異巨大,而頻繁的碎片整理也會影響固態硬盤的使用壽命;"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到最後,我們還是來看一些比較開放的相關問題,有興趣的讀者可以仔細思考一下下面的問題,與前面的問題不同,作者會在後面的文章中解答這兩個問題:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲什麼固態硬盤的擦寫次數有上限?"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"機械硬盤在哪些情況下更容易損壞?"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果對文章中的內容有疑問或者想要了解更多軟件工程上一些設計決策背後的原因,可以在博客下面留言,作者會及時回覆本文相關的疑問並選擇其中合適的主題作爲後續的內容。"}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"推薦閱讀"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"032 "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-cpu-and-disk\/","title":null,"type":null},"content":[{"type":"text","text":"爲什麼 CPU 訪問硬盤很慢"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"034 "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-linux-hugepages","title":null,"type":null},"content":[{"type":"text","text":"爲什麼 HugePages 可以提升數據庫性能"}]}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"參考資料"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"爲什麼早期的 Windows 需要整理碎片 "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-linux-macos-fragmentation\/#fnref:1","title":null,"type":null},"content":[{"type":"text","text":"↩︎"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"Is there a tool to visualize a filesystem allocation map on Linux? "},{"type":"link","attrs":{"href":"https:\/\/unix.stackexchange.com\/questions\/30743\/is-there-a-tool-to-visualize-a-filesystem-allocation-map-on-linux","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/unix.stackexchange.com\/questions\/30743\/is-there-a-tool-to-visualize-a-filesystem-allocation-map-on-linux"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-linux-macos-fragmentation\/#fnref:2","title":null,"type":null},"content":[{"type":"text","text":"↩︎"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"Wikipedia: Apple File System "},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Apple_File_System","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/en.wikipedia.org\/wiki\/Apple_File_System"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-linux-macos-fragmentation\/#fnref:3","title":null,"type":null},"content":[{"type":"text","text":"↩︎"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"HFS+ and File System Fragmentation "},{"type":"link","attrs":{"href":"https:\/\/developercoach.com\/file-system-fragmentation\/","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/developercoach.com\/file-system-fragmentation\/"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-linux-macos-fragmentation\/#fnref:4","title":null,"type":null},"content":[{"type":"text","text":"↩︎"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"本文轉載自:"},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/","title":"xxx","type":null},"content":[{"type":"text","text":"面向信仰編程"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"原文鏈接:"},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-linux-macos-fragmentation\/","title":"xxx","type":null},"content":[{"type":"text","text":"爲什麼 Linux 和 macOS 不需要碎片整理"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章