On Monday there was a need to determine the cluster size of a NTFS volume. Searching the web led to the discovery of a few different techniques but nothing that could be absorbed easily into a program. One technique called for creating a very small file and then looking at the file properties for the space used on disk. The second technique used the FSUTIL tool (built into Windows). There was even a third technique which allowed for capturing the output of FSUTIL into Visual Basic to use the cluster size.
Why worry about cluster size in the first place? Well, normally, you wouldn’t. It is something that is for those of you that like to fine tune your performance and storage space. The quick analysis is that having bigger clusters is more efficient for larger files (less fragmentation and faster load with less overhead) but small files can waste heaps of space. Basically files that do not use the full cluster are going to take up space that other files could have used. It’s a delicate balance of wants. Most likely it would be difficult to prove what the optimal cluster size is. But, before we go to far, it is currently difficult to determine cluster size from a program.
After learning of this problem, the search began for a magic FSCTL Ioctl to the file system to figure this out. It did not look very promising until my co-worker Anil pointed out that maybe the Win32 GetDiskFreeSpace function might do the trick.
To my surprise, GetDiskFreeSpace did exactly what was needed. It does not return explicitly the cluster size but it does return (sectors per cluster) and (bytes per sector). A simple multiplication and the answer is there. The funny thing is that this function is considered deprecated since it cannot support greater than 2GB volumes. However, in this case it was extremely useful and not affected by the limit.
The next step was to build a simple command line tool that would exploit GetDiskFreeSpace. The new tools is called ClusterSize (how creative is that?) and can be run against any volume in the system. The default (no parameters) is to figure out the C: drive cluster size. You can specify any other drive on the command line.
Because it is not possible to post executables from WordPress, here is the source instead. It is fairly easy and should build under Visual Studio without too much trouble.
Here’s an example of the output from trying it against a USB Flash Drive on my system:
Determining cluster size for volume f:
Volume(f:) ClusterSize(32768) SectorsPerCluster(64) BytesPerSector(512)
At first it did not include the ability to report on the sectors and sector size. It seemed kind of dull not to report them after the initial runs.
This certainly is not the most exciting topic but it is fun to share new minor discoveries. This is the first time that Citrixblogger has source using PDF straight from VisualStudio using PDFCreator. It is much more accurate than trying to post straight into the blog directly. It even keeps all the pretty colours as well.