Reiserfs is one fs which has no tools to measure fragmentation.
I asked myself how can we measure fragmentation on a reiserfs partition. I wrote a script that is capable to measure this fragmentation.
This script requires e2fsprogs package (for filefrag command), but this package is installed on almost all gentoo. It is written in perl.
Since filefrag works on various fs type (including reiserfs), my script should work too on various fs type.
The script :
Code: Select all
#!/usr/bin/perl -w
#this script search for frag on a fs
use strict;
#number of files
my $files = 0;
#number of fragment
my $fragments = 0;
#number of fragmented files
my $fragfiles = 0;
#search fs for all file
open (FILES, "find " . $ARGV[0] . " -xdev -type f |");
while (defined (my $file = <FILES>)) {
#quote some chars in filename
$file =~ s/!/\\!/g;
$file =~ s/#/\\#/g;
$file =~ s/&/\\&/g;
$file =~ s/>/\\>/g;
$file =~ s/</\\</g;
$file =~ s/\$/\\\$/g;
$file =~ s/\(/\\\(/g;
$file =~ s/\)/\\\)/g;
$file =~ s/\|/\\\|/g;
$file =~ s/'/\\'/g;
$file =~ s/ /\\ /g;
#nb of fragment for the file
open (FRAG, "filefrag $file |");
my $res = <FRAG>;
if ($res =~ m/.*:\s+(\d+) extents? found/) {
my $fragment = $1;
$fragments+=$fragment;
if ($fragment > 1) {
$fragfiles++;
}
$files++;
} else {
print ("$res : not understand for $file.\n");
}
close (FRAG);
}
close (FILES);
print ( $fragfiles / $files * 100 . "% non contiguous files, " . $fragments / $files . " average fragments.\n");Code: Select all
chmod u+x /root/fragck.plYou must be root to execute the script. It takes one argument : the mount point of the fs to analyze. It will report the percentage of fragmented files and the average number of fragment (see explanation below).
It has to scan all regular files of the examinated partition, so this script is quite slow (depends on the number of files). It takes about 5 mins on my root partition /.
Some examples from my personnal computer:
Code: Select all
/root/fragck.pl /
5.10010105507148% non contiguous files, 1.11087084031179 average fragments.
/root/fragck.pl /divers
79.5620437956204% non contiguous files, 7060.22627737226 average fragments.
I hope this script will help some people
It is possible that you will have problem with file that contains special characters. Please report it, I will try to correct the problem.
Enjoy !
PS : the initial thread where I post the script is http://forums.gentoo.org/viewtopic-t-42 ... art-0.html.
PPS : English is not my motherlanguage, feel free to correct me...
PPPS : I put no licence on the code, you are free to do what you want with it. But I'm interested in any enhancement







