10

Is there a tool that would show me for a specific file on disk, how fragmented it is? (How many seeks does physical disk need to make if I were to read that file in a linear fashion)

Ghostrider
  • 7,545
  • 7
  • 30
  • 44

5 Answers5

17

The Sysinternals tool contig with parameter -a can do this for a file or all files in a folder and its subfolders.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • @SmitJohnth, wow, was this still useful? I thought that with the SSD:s of today fragmentation was hardly an issue anymore. – Albin Sunnanbo Jul 16 '16 at 19:54
  • SSD are still too expensive to store all crap on it even after prices for HDD have hardly fallen in the last years. I produced a big text file, tried to search in it and then noticed that it was too slow. I measured reading speen and then started to suspect something... Now it's fixed. – Smit Johnth Jul 17 '16 at 18:22
10

You can use DeviceIoControl with FSCTL_GET_VOLUME_BITMAP, FSCTL_GET_RETRIEVAL_POINTERS and FSCTL_MOVE_FILE, see Defragmenting Files.

You can also find different code examples if you search for FSCTL_MOVE_FILE.
Here is one in C and another in .NET.

Axalo
  • 2,953
  • 4
  • 25
  • 39
Oleg
  • 220,925
  • 34
  • 403
  • 798
7

filefrag is the tool you're looking for, if you're using Linux. Use -v parameter with filename to get detailed list of fragmentation. http://linux.die.net/man/8/filefrag

Sami Lehtinen
  • 881
  • 10
  • 16
3

fsutil file queryallocranges offset=<o> length=<l> <file> will show you the file's extents you will need admin rights.

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
Dominik Weber
  • 711
  • 5
  • 13
3

And, of course, "fragmentation" is suspect:

  1. The file may be in pieces in the same cylinder. No seek overhead, just rotational latency. Or not as the pieces may be an optimal order (chances are near zero for this one).
  2. The file may be "contiguous" but across several cylinders. Even reading sequentially will result in seeks.
  3. The file may be on a stripe set and you have no idea where the boundaries are. You may skip to another controller, another spindle, or another partition on the same drive.

Be careful about what conclusions you draw.

MJZ
  • 1,074
  • 6
  • 12