1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/* frag.c - simple fragmentation checker */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#define FIBMAP 1
typedef struct StackElem {
struct StackElem *backref, *next;
char name[NAME_MAX];
char dir_seen;
} StackElem;
StackElem *top = NULL;
static inline int abs (int n)
{
return (n>=0) ? n : -n;
}
void discard( void )
{
StackElem *se = top;
if( se == NULL )
return ;
top = se->next;
free(se);
}
void push( StackElem * se )
{
se -> next = top;
top = se;
}
char *p2s( StackElem *se, char *path )
{
char *s;
if( se->backref!=NULL ) {
path = p2s( se->backref, path );
if( path[-1]!='/' )
*path++ = '/';
}
s = se->name;
while( *s )
*path++ = *s++;
return path;
}
char *path2str( StackElem *se, char *path )
{
*(p2s( se, path ))=0;
return path;
}
void *xmalloc( size_t size )
{
void *p;
if( (p=malloc(size))==NULL ) {
fprintf(stderr,"\nvirtual memory exhausted.\n");
exit(1);
}
return p;
}
int main(int argc,char **argv)
{
int fd,last,frags,blocks,block, this_frag, frag_left, largest_frag,
used_blocks, frag_start;
long sum_blocks=0, sum_frag_blocks=0, sum_files=0,
sum_frag_files=0, sum_frags=0,
n_gaps=0, sum_gaps=0;
long sum_used_blocks=0;
struct stat st;
StackElem *se, *se1;
char path[PATH_MAX], *p;
DIR *dir;
struct dirent *de;
char silent_flag=0;
int long_listing = 0;
if (argc < 2) {
fprintf(stderr,"usage: %s [-l] [-s [-s]] filename ...\n",argv[0]);
exit(1);
}
argc--; argv++;
while (argc>0) {
p = *argv;
if( *p=='-' )
while( *++p )
switch( *p ){
case 's':
silent_flag++; /* may be 1 or 2 */
break;
case 'l':
long_listing++;
break;
default:
fprintf(stderr,"\nunknown flag %c\n", *p );
exit(1);
}
else {
se = xmalloc( sizeof(StackElem) );
se->backref=NULL; se->dir_seen=0;
strcpy( se->name, p );
push(se);
}
argc--; argv++;
}
while ( top != NULL) {
se = top;
if( se->dir_seen )
discard();
else {
path2str( se, path );
if (lstat( path,&st) < 0) {
perror( path );
discard();
} else {
if( S_ISREG(st.st_mode)) {
if ( (fd = open( path ,O_RDONLY)) < 0 ) {
perror( path );
discard();
} else {
last = -1; block = -1; frag_start = -1;
frags = 0; used_blocks = 0; frag_left = 0;
this_frag=0; largest_frag=-1;
for (blocks = 0; blocks < ((st.st_size+1023) >> 10);
blocks++) {
last = block;
block = blocks;
if (ioctl(fd,FIBMAP,&block) < 0) {
perror(path);
break;
}
if (block) {
this_frag++;
used_blocks++;
frag_left--;
if (last != block-1 && last != block+1 &&
frag_left<0) {
if( largest_frag<this_frag )
largest_frag=this_frag;
this_frag=1;
frag_left = (last == -1) ? 12 : 256;
if (long_listing && frags > 0)
printf (" (file %s, fragment #%d "
"begins at block %d, "
"ends at block %d)\n",
path, frags, frag_start, last);
if (frags > 0) {
n_gaps++;
sum_gaps += abs(block - last);
}
frags++;
frag_start = block;
}
}
}
if( largest_frag<this_frag )
largest_frag=this_frag;
if (long_listing)
printf (" (file %s, fragment #%d begins at "
"block %d, ends at block %d)\n",
path, frags, frag_start, block);
if( !silent_flag )
printf(" %3d%% %s (%d block(s), %d used, "
"%d fragment(s), largest %d)\n",
frags < 2 ? 0 : frags*100/used_blocks,
path,blocks,used_blocks,frags,largest_frag);
sum_blocks+=blocks;
sum_files++;
sum_used_blocks+=used_blocks;
if( frags>1 ) {
sum_frag_blocks+=used_blocks-largest_frag;
sum_frag_files++;
sum_frags += frags - 1;
}
discard();
close(fd);
}
} else if( S_ISDIR( st.st_mode ) ) { /* push dir contents */
if( (dir=opendir( path ))==NULL ) {
perror(path);
discard();
} else {
if( silent_flag<2 )
printf("reading %s\n", path);
while( (de=readdir(dir))!=NULL ) {
if( (strcmp(de->d_name,".")!=0)
&& (strcmp(de->d_name,"..")!=0) ) {
se1 = xmalloc( sizeof(StackElem) );
se1->backref=se; se1->dir_seen=0;
strcpy( se1->name, de->d_name );
push(se1);
}
}
closedir( dir );
se->dir_seen=1;
}
} else /* if( S_ISREG(st.st_mode)) */
discard();
}
} /* if( se->dir_seen ) */
} /* while ( top != NULL) */
printf ("\nsummary:\n");
printf (" %3ld%% file fragmentation "
"(%ld of %ld files contain fragments)\n",
sum_files < 1 ? 0L : sum_frag_files * 100 / sum_files,
sum_frag_files, sum_files);
printf (" %3ld%% block fragmentation "
"(%ld of %ld blocks are in fragments)\n",
sum_used_blocks < 1 ? 0L : sum_frag_blocks * 100/ sum_used_blocks,
sum_frag_blocks, sum_used_blocks);
printf (" %3ld%% overall fragmentation "
"(%ld fragments out of %ld blocks)\n",
sum_used_blocks < 1 ? 0L : sum_frags / sum_used_blocks,
sum_frags, sum_used_blocks);
printf (" Average inter-fragment gap length = %ld\n",
n_gaps < 1 ? 0L : sum_gaps / n_gaps);
exit(0);
}
|