爲什麼早期的 Windows 需要整理碎片

{"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":"記得十幾年前還在用早期 Windows 系統的時候,每用一段時間系統都會變得很卡頓,這時候需要打開系統提供的下面的磁盤碎片整理程序,當碎片整理完成後會感覺到系統變得稍微流暢了一些。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/40\/40b28edf9bb6b7a3dbffced81e10a216.jpeg","alt":"windows-defragmentation","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 - Windows 磁盤碎片整理程序"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在文件系統中,碎片整理(Defragmentation)是減少文件系統中碎片的過程"},{"type":"sup","content":[{"type":"text","text":"1"}]},{"type":"text","text":",該過程會將磁盤上相同文件的的內容按照順序重新排列並利用壓縮算法去除文件之間的空隙,有點類似垃圾回收中的標記壓縮算法"},{"type":"sup","content":[{"type":"text","text":"2"}]},{"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":"作者已經很多年都不使用 Windows 操作系統了,上大學之後就一直在用 macOS 到今年也有七八年的時間了,最近在研究文件系統時突然想到小時候經常見到的磁盤碎片整理程序彷彿已經消失了。不知道今天的 Windows 是否還需要磁盤整理,但是無論是 Linux 還是 macOS 上都沒有類似的工具"},{"type":"sup","content":[{"type":"text","text":"3"}]},{"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":"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":"上古時代的 Windows 使用了非常簡單的文件系統 - 文件分配表(File Allocation Table、FAT)"},{"type":"sup","content":[{"type":"text","text":"4"}]},{"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":"FAT 是 1977 年最開始爲軟盤設計的文件系統,軟盤是一種非常古老的存儲介質,今天的電腦基本上也都移除了軟盤的驅動,當時的軟盤存儲空間很小,每次寫入文件時都會從頭開始查找可用空間並且不會檢查可用空間的大小,當前可用空間不足時,會寫到後面的寫入空間直到文件全部寫入。"}]},{"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":"在該文件系統被使用後不久,隨着機械硬盤(Hard Disk Driver、HDD)的價格開始變得逐漸低廉並被廣泛使用,微軟選擇在 DOS 和 Windows 9x 系列擴展 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":"雖然每次寫入數據都可能造成碎片,不過因爲軟盤的存儲空間比較小,所以這在當時也是可以接受的,但是隨着存儲介質的空間變得越來越大,我們需要引入隨機寫入提高效率,支持隨機寫入的 FAT 也是很簡單的文件系統"},{"type":"sup","content":[{"type":"text","text":"5"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/22\/22ff931e83b76f609494a6f2ea6b4fd1.png","alt":"fat-file-system","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 - FAT 的寫入"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上圖所示,如果我們要向一塊新的硬盤寫入多個文件 A、B 和 C,這些文件會在 FAT 文件系統中按照順序存儲,文件之間不存在任何碎片。然而如果在這時我們決定刪除其中 B 文件並向文件系統中寫入更大的文件 D,會出現比較有趣的情況。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/48\/481ecb34164e1a016534d379d2ec9379.png","alt":"fat-and-fragmentation","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 - FAT 的碎片"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FAT 文件系統在磁盤上會先找到刪除 B 後留下的兩塊空閒位置並在其中寫入 D 文件的一部分,隨後又會在 C 文件後找到另一塊空閒位置並將 D 文件的剩餘內容全部寫到該位置。這樣造成的結果是 D 文件會分散在硬盤上,當用戶讀取 D 文件時需要觸發多次隨機讀取。"}]},{"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":"FAT 是一種非常簡單、原始的文件系統,它的設計和實現從今天的角度來看都很糟糕,每次寫入文件時不僅不會檢查空閒空間的大小、造成文件碎片,還不包含碎片管理功能,使用時間過長還需要用戶手動觸發磁盤的碎片整理,這其實是很糟糕的設計和用戶體驗。"}]},{"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":"機械硬盤(Hard Disk Drive、HDD)是一種基於電子的、非易失的機械數據存儲設備,它使用磁性存儲器存儲並查找磁盤上的數據,在讀取和寫入數據的過程中,硬盤機械臂連接的磁頭會讀寫磁盤表面的位"},{"type":"sup","content":[{"type":"text","text":"6"}]},{"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":"正是因爲磁盤具有比較複雜的機械結構,所以磁盤的讀取和寫入都要花費很多時間,數據庫的讀寫性能也基本都依賴於磁盤的性能,如果我們在使用機械硬盤的數據庫中隨機查詢一條數據,這可能會觸發磁盤的隨機 I\/O,然而將數據從磁盤讀取到內存中所需要的成本是非常大的,普通磁盤(非 SSD)加載數據需要經過隊列、尋道、旋轉以及傳輸的這些過程,大概要花費 10ms 左右的時間"},{"type":"sup","content":[{"type":"text","text":"7"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/f3\/f30290ceaf126a094242eb98bc997a74.png","alt":"hdd-random-io","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 - 磁盤的隨機 I\/O"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我們在磁盤中讀取文件時,如果文件的內容散落到了磁盤上的不同位置,它可能需要執行多次隨機 I\/O 才能夠獲取該文件的全部內容,這對於機械結構的磁盤來說是很大的額外開銷。如果文件的內容會存儲在相同的位置,那麼讀取文件時僅需要執行一次隨機 I\/O,後續的讀取都可以使用速度約爲 40 MB\/s 的順序 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 讓磁盤將大多數的時間都花在尋道和旋轉上,導致其無法全力工作。與機械硬盤相比,具有電子結構的固態硬盤能夠更好地耐受碎片化的文件系統,而整理碎片反而會影響它的使用壽命。"}]},{"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":"相信很多工程師在進入這一行業之前都會使用 Windows 系統,早期的 Windows 是桌面系統近乎唯一的選擇,作者對今天的這個題目有比較特殊的感情,在研究操作系統的文件系統之前一直都沒有想過這個問題,直到碎片化的文件系統一詞讓自己想到了十多年前的疑問,這種豁然開朗的感覺在今天還是很少能夠體會到的。我們簡單總結一下早期的 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":"早期的 Windows 系統使用簡單的 FAT 文件系統,該文件系統經過頻繁的寫入刪除操作會導致大文件散落在磁盤的各處;"}]}]},{"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":"爲什麼 macOS 的文件系統不需要整理碎片?"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲什麼 Linux 的文件系統不需要整理碎片?"}]}]}]},{"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":"Wikipedia: File system fragmentation "},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/File_system_fragmentation","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/en.wikipedia.org\/wiki\/File_system_fragmentation"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/#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":"內存管理設計精要 "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/system-design-memory-management\/","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/draveness.me\/system-design-memory-management\/"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/#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":"Do Macs need to be defragmented? "},{"type":"link","attrs":{"href":"https:\/\/apple.stackexchange.com\/questions\/829\/do-macs-need-to-be-defragmented","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/apple.stackexchange.com\/questions\/829\/do-macs-need-to-be-defragmented"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/#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":"Wikipedia: Design of the FAT file system "},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Design_of_the_FAT_file_system","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/en.wikipedia.org\/wiki\/Design_of_the_FAT_file_system"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/#fnref:4","title":null,"type":null},"content":[{"type":"text","text":"↩︎"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"Why is it less necessary (comparing to FAT) to do disk defragmentation for NTFS hard drive? "},{"type":"link","attrs":{"href":"https:\/\/www.quora.com\/Why-is-it-less-necessary-comparing-to-FAT-to-do-disk-defragmentation-for-NTFS-hard-drive?top_ans=69674612","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/www.quora.com\/Why-is-it-less-necessary-comparing-to-FAT-to-do-disk-defragmentation-for-NTFS-hard-drive?top_ans=69674612"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/#fnref:5","title":null,"type":null},"content":[{"type":"text","text":"↩︎"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"Wikipedia: Hard disk drive "},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Hard_disk_drive","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/en.wikipedia.org\/wiki\/Hard_disk_drive"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/#fnref:6","title":null,"type":null},"content":[{"type":"text","text":"↩︎"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"爲什麼 CPU 訪問硬盤很慢 "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-cpu-and-disk\/","title":null,"type":null},"content":[{"type":"text","text":"https:\/\/draveness.me\/whys-the-design-cpu-and-disk\/"}]},{"type":"text","text":" "},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/#fnref:7","title":null,"type":null},"content":[{"type":"text","text":"↩︎"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":8,"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":8,"align":null,"origin":null},"content":[{"type":"text","text":"原文鏈接:"},{"type":"link","attrs":{"href":"https:\/\/draveness.me\/whys-the-design-windows-defragmentation\/","title":"xxx","type":null},"content":[{"type":"text","text":"爲什麼早期的 Windows 需要整理碎片"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章