generic/019: don't dump cores when fio/fsstress hit io errors
[xfstests-dev.git] / src / fill2fs
1 #!/usr/bin/perl -w
2 #
3 #  Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # fill2fs:
6 #   Fill a filesystem, or write a specified number of bits.
7 #   Script runs deterministically given a seed for the random number
8 #   generator. Will generate the same set of directories and files,
9 #   with the only source of variation the precise point at which the
10 #   filesystem fills up. When used with XFS filesystems, and when 
11 #   the filesize is small, XFS pre-allocation may cause the filesystem
12 #   to "fill up" before the actual disk space is gone. Using this
13 #   script repeatedly, with a delay between invocations, to account
14 #   for extent flushing, will allow more of the filesystem to be filled.
15 #
16 #   All generated files are checksummed and this sum is stored in the
17 #   filename (files are named: <sequence number>.<checksum>.data.
18 #   Normally script is invoked using the --list option, which causes all
19 #   data files to be written to standard output (--list=-) or a file
20 #   (--list=filename). This list can be fed into fill2fs_check, which
21 #   recomputes the checksums and flags any errors.
22 #
23 #   The --stddev=0 forces all files to be precisely --filesize bytes in
24 #   length, some other value (--stddev=val) produces filesizes with a 
25 #   normal distribution with standard deviation val. If not specified
26 #   most file sizes are set to lie within 30% of the requested file size.
27 #
28 #   fill2fs is not guaranteed to write the requested number of bytes, or
29 #   fill the filesystem completely. However, it and fill2 are both very
30 #   careful about establishing when write operations fail. When the 
31 #   --verbose option is used the number of bytes "actually written"
32 #   is guaranteed to be the number of bytes on disk.
33 #
34 #   $Id$
35 #
36
37 use Getopt::Long;
38 use File::Basename;
39
40 #
41 # fsinfo: get filesystem info put it into the global namespace, initialises:
42 #       $dev, $type, $blocks, $used, $avail, $cap, $mnt, $mnt_options
43 #       $fsblocks, $fsblocksize, $agblocks, $agcount, $imax_pct, $logblocks
44 #       $logstart, $internal
45 #
46 # if $verbose is set then output fs info to STDERR
47 #
48
49 sub fsinfo {
50   my($file) = $_[0];
51
52   # filesystem space and mount point
53   
54   $cmd = "df -P -T --block-size=512";
55   chomp($_ = `$cmd $file | tail -1`);
56   $n = ($dev, $type, $blocks, $used, $avail, $cap, $mnt) = split(/ +/);
57   die("df failed") if ($n != 7);
58
59   if ($fscheck && $type ne "xfs") {
60     die("Error: $progname: filesystem is not xfs")
61   }
62
63   # how was this filesystem mounted
64   $_ = `grep $dev /etc/mtab`;
65   @mtab = split(/ +/);
66   $mnt_options = $mtab[3];
67
68   # if we're running as root run xfs_db on the filesystem
69   if ($> == 0) {
70     # xfs_db: read only, use the default superblock, print everything
71     die("Error: $progname: can't read device: \"$dev\"\n") if (! -r $dev);
72     $_=`xfs_db -r -c sb -c p $dev`;
73     # multiline matching ^$ refers to individual lines...
74     ($fsblocks) = /^dblocks = (\d+)$/m;
75     ($fsblocksize) = /^blocksize = (\d+)$/m;
76     ($agblocks) = /^agblocks = (\d+)$/m;
77     ($agcount) = /^agcount = (\d+)$/m;
78     ($imax_pct) = /^imax_pct = (\d+)$/m;
79     ($logblocks) = /^logblocks = (\d+)$/m;
80     ($logstart) = /^logstart = (\d+)$/m;
81     $internal = $logstart > 0 ? " (internal)" : "";
82
83     $verbose && print STDERR <<"EOF"
84 Filesystem information:
85   type=$type; device=$dev
86   mount point=$mnt; mount options=$mnt_options
87   percent full=$cap; size (512 byte blocks)=$blocks; used=$used; avail=$avail
88   total filesystem size (fs blocks)=$fsblocks; fs block size=$fsblocksize; imax_pct=$imax_pct
89   agcount=$agcount; agblocks=$agblocks; logblocks=$logblocks; logstart=$logstart$internal
90 EOF
91   }
92 }
93
94
95 # returns numbers with a normal distribution
96 sub normal {
97   my($mean) = $_[0];
98   my($stddev) = $_[1];
99
100   $x = -6.0;
101   for ($i = 0; $i < 12; $i++) {
102     $x += rand;
103   }
104
105   $x = $mean + $stddev * $x;
106   return $x;
107 }
108
109 #
110 # determine script location and find fill2
111 #
112
113 chomp($cwd = `pwd`);
114 chomp($_ = `which fill2 2>&1 | head -1`);
115 if (-x $_) {
116   # look in the path
117   $fill2 = fill2;
118 }
119 else {
120   # in the same directory - get absolute path
121   chomp($dirname = dirname $0);
122   if ($dirname =~ m!^/.*!) {
123     $fill2 = $dirname . "/fill2";
124   }
125   else {
126     # relative
127     $fill2 = $cwd . "/" . $dirname . "/fill2";
128   }    
129   if (! -x $fill2) {
130     die("Error: $progname: can't find fill2, tried \"$fill2\"\n");
131   }
132 }
133
134 #
135 # process/check args
136 #
137
138 $progname=$0;
139 GetOptions("bytes=f" => \$bytes,
140            "dir=s" => \$root,
141            "filesize=i" => \$filesize,
142            "force!" => \$force,
143            "help!" => \$help,
144            "list=s" => \$list,
145            "fscheck!" => \$fscheck,
146            "percent=f" => \$percentage,
147            "seed=i" => \$seed,
148            "stddev=i" => \$stddev,
149            "sync=i" => \$sync_bytes,
150            "verbose!" => \$verbose);
151
152
153 # check/remove output directory, get filesystem info
154 if (defined $help
155     || (! defined $root && @ARGV != 1)
156     || (defined $root && @ARGV == 1))
157 {
158   # newline at end of die message suppresses line number
159   print STDERR <<"EOF";
160 Usage: $progname [options] root_dir
161 Options:
162   --bytes=num       total number of bytes to write
163   --dir=name        where to write files
164   --filesize=num    set all files to num bytes in size
165   --force           overwrite any existing files
166   --help            print this help message
167   --list=filename   store created files to filename (- for stdout)
168   --percent=num     percentage of filesystem to fill
169   --seed=num        seed for random number generator
170   --stddev          set file size standard deviation
171   --sync=num        sync every num bytes written
172   --verbose         verbose output
173 EOF
174   exit(1) unless defined $help;
175   # otherwise...
176   exit(0);
177     
178 }
179
180
181 #
182 # lots of boring argument checking
183 #
184
185 # root directory and filesystem info
186 $root = $ARGV[0] if (@ARGV == 1);
187 if (-e $root) {
188   if (! $force) {
189     die("Error: $progname: \"$root\" already exists\n");
190   }
191   else {
192     $verbose && print STDERR "Removing \"$root\"... ";
193     system("rm -rf $root");
194     $verbose && print STDERR "done\n";
195   }
196 }
197 chomp($root_dir = dirname $root);
198 fsinfo $root_dir;
199
200 # $list can be "-" for stdout, perl open ">-" opens stdout
201 open LIST, ">$list" if (defined $list);
202
203 # how many bytes should we write
204 if (defined $bytes && defined $percentage) {
205   die("Error: $progname: can't specify --bytes and --percent\n");
206 }
207 # check percentage
208 if (defined $percentage && ($percentage < 0 || $percentage > 100)) {
209   die("Error: $progname: invalid percentage\n");
210 }
211 if (! defined $bytes && ! defined $percentage) {
212   $bytes = $avail * 512;
213   $verbose && print STDERR <<"EOF";
214 Neither --bytes nor --percent specified: filling filesystem ($bytes bytes)
215 EOF
216 }
217 elsif (! defined $bytes) {
218   $bytes = int($blocks * 512 * $percentage / 100.0);
219 }
220 if (($bytes > $blocks * 512) || (! $force && $bytes > $avail * 512))
221 {
222   die("Error: $progname: not enough free disk space, disk is $cap full\n");
223 }
224
225
226
227 #
228 # To get fix sized files set stddev to 0. The default is to make most files
229 # within 30% of the requested filesize (or 4k if filesize is not specified).
230 # Set the standard deviation to be half of the required percentage: ~95% of
231 # samples lie within 2 standard deviations of the mean.
232 #
233
234 $filesize = 4096 if (! defined $filesize);
235 die("Error: $progname: --filesize must be >= 1 byte") if ($filesize < 1);
236 $stddev = 0.5 * 0.3 * $filesize if (! defined $stddev);
237 $seed = time ^ $$ if (! defined $seed);
238 srand $seed;
239 umask 0000;
240 mkdir $root, 0777;
241 chdir $root;
242 $total = 0;
243 $files = 0;
244 $dirs = 0;
245 $d = 0;
246 $sync_cnt = 1;
247
248 #
249 # fill filesystem
250 #
251
252 $verbose && print STDERR "Writing $bytes bytes (seed is $seed)... ";
253 while ($total < $bytes) {
254   $r = rand(3.0);
255
256   if (($d == 0 && $r < 0.5) || ($d > 0 && $r >= 0.0 && $r < 2.4)) {
257     # create a new data file
258     $n = sprintf("%04d", $names[$d]++);
259     if ($stddev == 0) {
260       $size = $filesize;
261     }
262     else {
263       $size = int(normal($filesize, $stddev));
264     }
265     $left = $bytes - $total;
266     $size = 0 if ($size < 0);
267     $size = $left if ($size > $left);
268
269     # fill2 will fail if the filesystem is full - not an error!
270     $cmd = "$fill2 -d nbytes=$size,linelength=72,seed=$n -b 4k $n";
271     $cmd .= " > /dev/null 2>&1" if (! $verbose);
272     if (system($cmd) != 0) {
273       if ($verbose) {
274         warn("can't create a file - assuming filesystem is full\n");
275       }
276       if (-e $n && unlink($n) != 1) {
277         warn("couldn't delete \"$n\"");
278       }
279       last;
280     }
281     $_ = `sum -r $n`;
282     ($sum) = split(/ +/);
283     $name = "$n.$sum.data";
284     $cmd = "mv $n $name";       # perl rename failed earlier than using mv
285     $cmd .= " > /dev/null 2>&1" if (! $verbose);
286     if (system($cmd) != 0) {
287       if ($verbose) {
288         warn("can't rename a file - assuming filesystem is full\n");
289       }
290       if (-e $name && unlink($name) != 1) {
291         warn("couldn't delete \"$name\"");
292       }
293       last;
294     }
295     if (defined $list) {
296       chomp($_ = `pwd`);
297       printf LIST ("%s/%s\n", $_, $name);
298     }
299     $total += $size;
300     $files++;
301
302     if (defined $sync_bytes && int($total / $sync_bytes) > $sync_cnt) {
303       $sync_cnt++;
304       system("sync");
305     }
306   }
307   # note that if d==0 create directories more frequently than files
308   elsif (($d == 0 && $r >= 0.5) || ($d > 0 && $r >= 2.4 && $r < 2.7)) {
309     # create a new directory and descend
310     $name = sprintf("%04d.d", $names[$d]++);
311     if (! mkdir($name, 0777)) {
312       warn("can't make a directory - assuming filesystem is full\n");
313       last;
314     }
315     chdir($name) or die();
316     $d++;
317     $dirs++;
318   }
319   elsif ($r >= 2.7 && $r < 3.0) {
320     # pop up to the parent directory same probability as descend
321     die("Error: $progname: panic: shouldn't be here!") if ($d == 0);
322     chdir("..") or die();
323     $d--;
324   }
325 }
326 # make sure we return to the original working directory
327 chdir($cwd) or die();
328 $verbose && print STDERR "done\n";
329 $verbose && print STDERR "$total bytes (in $files files and $dirs directories) were actually written\n";
330 close LIST;
331 exit(0) if ($total = $bytes);
332 exit(1) if ($total == 0);
333 exit(2) if ($total > 0 && $total < $bytes);
334
335 # - to sum all generated data:
336 #   find /home/fill/ -name \*data | xargs ls -al | awk '{total = total + $5; } END { printf("total = %d bytes\n", total); }'
337 # - to find any files not of the required size
338 #   find . -name \*data -a \! -size 4096c
339 # - count new files
340 #   find ./fill -name \*.data | wc
341 # - count new directories
342 #   find ./fill -name \*.d | wc