跳至內容

使用者:Junjie Yuan/正

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書

翻譯至:磁碟碎片,翻譯自File system fragmentation(英文),oldid=832616347。

磁碟碎片及碎片整理的視覺化表示

計算領域,檔案系統碎片(英語:File system fragmentation,或稱磁碟碎片檔案系統老化)是檔案系統將檔案內容非連續排列以允許就地修改其內容的後果,亦是碎片化的一個特例。磁碟碎片會增加磁碟磁頭移動或尋道時間(應用於此處),這會降低磁碟讀寫效能,進而影響作業系統軟體的效能。另外,檔案系統不能承受無限制的碎片。對現有碎片的更正是將檔案和可用空間重新組織為連續區域,這是一個稱為碎片整理的過程。

現代電腦中的固態硬碟(SSD)不是真正的磁碟,也不是「旋轉的」(應該是「固定的」),所以沒有磁碟碎片問題。事實上,對這些驅動器進行碎片整理反而會縮短它們的使用壽命。

原因

分割區首次初始化檔案系統時,它只包含一些小的內部結構,其他區域則是一個連續的空白區塊。[a] 這意味著檔案系統能夠將新建立的檔案放置在分割區的任何位置。在檔案系統被建立後的一段時間內,其中的檔案布局近乎最佳。而當作業系統軟體被安裝或存檔被解包時,單獨的檔案最終會按順序發生,因此相關檔案的位置彼此接近。

當現有檔案被刪除或截斷時,將會新增的可用空間;而當新資料被附加到現有檔案時,通常不會在檔案結束的地方重新開始寫入,因為另一個檔案可能已經被分配在那裡。因此,必須分配新的磁碟空間片段。隨著時間的推移,並且持續存在相同的因素,自由空間以及頻繁附加的檔案往往會碎片化。可用空間變短也意味著檔案系統不再能夠為新檔案分配連續空間,而必須將它們分解成碎片。當檔案系統變滿並且大量連續的可用空間區域不可用時尤其如此。

範例

對自由空間碎片和檔案碎片發生過程的簡單範例

下面的例子是一個簡化表示:

新硬碟有5個檔案,將其分別命名為A、B、C、D、E,並按順序連續儲存。每個檔案使用10個塊空間。(在這個例子中,塊的大小並不重要。)剩餘的磁碟空間則是一個空閒塊。因此,可以在檔案E之後建立並儲存附加檔案。

如果檔案B被刪除, a second region of ten blocks of free space is created, and the disk becomes fragmented. The empty space is simply left there, marked as and available for later use, then used again as needed.[b] The file system could defragment the disk immediately after a deletion, but doing so would incur a severe performance penalty at unpredictable times.

Now, a new file called F, which requires seven blocks of space, can be placed into the first seven blocks of the newly freed space formerly holding the file B, and the three blocks following it will remain available. If another new file called G, which needs only three blocks, is added, it could then occupy the space after F and before C.

If subsequently F needs to be expanded, since the space immediately following it is occupied, there are three options for the file system:

  1. Adding a new block somewhere else and indicating that F has a second extent
  2. Moving files in the way of the expansion elsewhere, to allow F to remain contiguous
  3. Moving file F so it can be one contiguous file of the new, larger size

The second option is probably impractical for performance reasons, as is the third when the file is very large. The third option is impossible when there is no single contiguous free space large enough to hold the new file. Thus the usual practice is simply to create an extent somewhere else and chain the new extent onto the old one.

Material added to the end of file F would be part of the same extent. But if there is so much material that no room is available after the last extent, then another extent would have to be created, and so on. Eventually the file system has free segments in many places and some files may be spread over many extents. Access time for those files (or for all files) may become excessively long.

必要性

Some early file systems were unable to fragment files. One such example was the Acorn DFS file system used on the BBC Micro. Due to its inability to fragment files, the error message can't extend would at times appear, and the user would often be unable to save a file even if the disk had adequate space for it.

DFS used a very simple disk structure and files on disk were located only by their length and starting sector. This meant that all files had to exist as a continuous block of sectors and fragmentation was not possible. Using the example in the table above, the attempt to expand file F in step five would have failed on such a system with the can't extend error message. Regardless of how much free space might remain on the disk in total, it was not available to extend the data file.

Standards of error handling at the time were primitive and in any case programs squeezed into the limited memory of the BBC Micro could rarely afford to waste space attempting to handle errors gracefully. Instead, the user would find themselves dumped back at the command prompt with the Can't extend message and all the data which had yet to be appended to the file would be lost. The resulting frustration would be greater if the user had taken the trouble to check the free space on the disk beforehand and found free space. While free space on the disk may exist, the fact that it was not in the place where it was needed was not apparent without analyzing the numbers presented by the disk catalog and so would escape the user's notice. In addition, DFS users had almost without exception previously been accustomed to cassette file storage, which does not suffer from this error. The upgrade to a floppy disk system was expensive performance upgrade, and it was a shock to make the sudden and unpleasant discovery that the upgrade might without warning cause data loss.[1][2]

類型

磁碟碎片可能發生在多個級別上:

  • Fragmentation within individual files
  • Free space fragmentation
  • The decrease of locality of reference between separate, but related files

檔案碎片

單個檔案碎片發生在單個檔案被分成多個部分(稱為基於副檔名的檔案系統的擴充部分)時。雖然磁碟檔案系統試圖保持單個檔案的連續性,但如果沒有顯著的效能損失,這通常是不可能的。檔案系統檢查和碎片整理工具通常僅將「碎片百分比」統計中的檔案碎片考慮在內。

Individual file fragmentation occurs when a single file has been broken into multiple pieces (called extents on extent-based file systems). While disk file systems attempt to keep individual files contiguous, this is not often possible without significant performance penalties. File system check and defragmentation tools typically only account for file fragmentation in their "fragmentation percentage" statistic.

可用空間碎片化

Free (unallocated) space fragmentation occurs when there are several unused areas of the file system where new files or metadata can be written to. Unwanted free space fragmentation is generally caused by deletion or truncation of files, but file systems may also intentionally insert fragments ("bubbles") of free space in order to facilitate extending nearby files (see preventing fragmentation below).

File scattering

File segmentation, also called related-file fragmentation, or application-level (file) fragmentation, refers to the lack of locality of reference (within the storing medium) between related files (see file sequence for more detail). Unlike the previous two types of fragmentation, file scattering is a much more vague concept, as it heavily depends on the access pattern of specific applications. This also makes objectively measuring or estimating it very difficult. However, arguably, it is the most critical type of fragmentation, as studies have found that the most frequently accessed files tend to be small compared to available disk throughput per second.[3]

To avoid related file fragmentation and improve locality of reference (in this case called file contiguity), assumptions or active observations about the operation of applications have to be made. A very frequent assumption made is that it is worthwhile to keep smaller files within a single directory together, and lay them out in the natural file system order. While it is often a reasonable assumption, it does not always hold. For example, an application might read several different files, perhaps in different directories, in exactly the same order they were written. Thus, a file system that simply orders all writes successively, might work faster for the given application.

負面影響

File system fragmentation is more problematic with consumer-grade hard disk drives because of the increasing disparity between sequential access speed and rotational latency (and to a lesser extent seek time) on which file systems are usually placed.[4] Thus, fragmentation is an important problem in file system research and design. The containment of fragmentation not only depends on the on-disk format of the file system, but also heavily on its implementation.[5] File system fragmentation has less performance impact upon solid-state drives, as there is no mechanical seek time involved.[6] However, the file system needs to store one additional piece of metadata for the corresponding file. Each piece of metadata itself occupies space and requires processing power and processor time. If the maximum fragmentation limit is reached, write requests fail.[6]

In simple file system benchmarks, the fragmentation factor is often omitted, as realistic aging and fragmentation is difficult to model. Rather, for simplicity of comparison, file system benchmarks are often run on empty file systems. Thus, the results may vary heavily from real-life access patterns.[7]

Mitigation

Several techniques have been developed to fight fragmentation. They can usually be classified into two categories: preemptive and retroactive. Due to the difficulty of predicting access patterns these techniques are most often heuristic in nature and may degrade performance under unexpected workloads.

防止碎片

Preemptive techniques attempt to keep fragmentation at a minimum at the time data is being written on the disk. The simplest is appending data to an existing fragment in place where possible, instead of allocating new blocks to a new fragment.

Many of today's file systems attempt to preallocate longer chunks, or chunks from different free space fragments, called extents to files that are actively appended to. This largely avoids file fragmentation when several files are concurrently being appended to, thus avoiding their becoming excessively intertwined.[5]

If the final size of a file subject to modification is known, storage for the entire file may be preallocated. For example, the Microsoft Windows swap file (page file) can be resized dynamically under normal operation, and therefore can become highly fragmented. This can be prevented by specifying a page file with the same minimum and maximum sizes, effectively preallocating the entire file.

BitTorrent and other peer-to-peer filesharing applications limit fragmentation by preallocating the full space needed for a file when initiating downloads.[8]

A relatively recent technique is delayed allocation in XFS, HFS+[9] and ZFS; the same technique is also called allocate-on-flush in reiser4 and ext4. When the file system is being written to, file system blocks are reserved, but the locations of specific files are not laid down yet. Later, when the file system is forced to flush changes as a result of memory pressure or a transaction commit, the allocator will have much better knowledge of the files' characteristics. Most file systems with this approach try to flush files in a single directory contiguously. Assuming that multiple reads from a single directory are common, locality of reference is improved.[10] Reiser4 also orders the layout of files according to the directory hash table, so that when files are being accessed in the natural file system order (as dictated by readdir), they are always read sequentially.[11]

碎片整理

Retroactive techniques attempt to reduce fragmentation, or the negative effects of fragmentation, after it has occurred. Many file systems provide defragmentation tools, which attempt to reorder fragments of files, and sometimes also decrease their scattering (i.e. improve their contiguity, or locality of reference) by keeping either smaller files in directories, or directory trees, or even file sequences close to each other on the disk.

The HFS Plus file system transparently defragments files that are less than 20 MiB in size and are broken into 8 or more fragments, when the file is being opened.[12]

The now obsolete Commodore Amiga Smart File System (SFS) defragmented itself while the filesystem was in use. The defragmentation process is almost completely stateless (apart from the location it is working on), so that it can be stopped and started instantly. During defragmentation data integrity is ensured for both metadata and normal data.

參見

注釋

  1. ^ 一些檔案系統,例如NTFSext2+,可能為了特殊目的預先分配空的連續區域。
  2. ^ The practice of leaving the space occupied by deleted files largely undisturbed is why undelete programs were able to work; they simply recovered the file whose name had been deleted from the directory, but whose contents were still on disk.

參考文獻

  1. ^ http://www.8bs.com/hints/083.txt - Description of the can't extend error
  2. ^ http://8bs.com/mag/1to4/basegd1.txt - Possible data loss caused by the can't extend error
  3. ^ Douceur, John R.; Bolosky, William J. A Large-Scale Study of File-System Contents. ACM SIGMETRICS Performance Evaluation Review (Association for Computing Machinery). June 1999, 27 (1): 59–70. doi:10.1145/301464.301480. 
  4. ^ Kryder, Mark H. Future Storage Technologies: A Look Beyond the Horizon (PDF). Storage Networking World conference. Seagate Technology. 2006-04-03. (原始內容 (PDF)存檔於17 July 2006). 
  5. ^ 5.0 5.1 McVoy, L. W.; Kleiman, S. R. Extent-like Performance from a UNIX File System (PostScript). Proceedings of USENIX winter '91. Dallas, Texas: Sun Microsystems, Inc.: 33–43. Winter 1991 [2006-12-14]. 
  6. ^ 6.0 6.1 Hanselman, Scott. The real and complete story - Does Windows defragment your SSD?. Scott Hanselman's blog. 3 December 2014. 
  7. ^ Smith, Keith Arnold. Workload-Specific File System Benchmarks (PDF). Cambridge, Massachusetts: Harvard University. January 2001 [2006-12-14]. (原始內容 (PDF)存檔於2004-11-17). 
  8. ^ Layton, Jeffrey. From ext3 to ext4: An Interview with Theodore Ts'o. Linux Magazine (QuinStreet). 29 March 2009. 
  9. ^ Singh, Amit. Fragmentation in HFS Plus Volumes. Mac OS X Internals. May 2004. 
  10. ^ Sweeney, Adam; Doucette, Doug; Hu, Wei; Anderson, Curtis; Nishimoto, Mike; Peck, Geoff. Scalability in the XFS File System (PDF). Proceedings of the USENIX 1996 Annual Technical Conference. San Diego, California: Silicon Graphics. January 1996 [2006-12-14]. 
  11. ^ Reiser, Hans. The Reiser4 Filesystem. Google TechTalks. 2006-02-06 [2006-12-14]. (原始內容存檔於19 May 2011). 
  12. ^ Singh, Amit. 12 The HFS Plus File System. Mac OS X Internals: A Systems Approach. Addison Wesley. 2007. ISBN 0321278542. 

延伸閱讀