Whamcloud - gitweb
b=11512
[fs/lustre-release.git] / libsysio / tests / test_stats.pl
1 #!/usr/bin/perl -w
2
3 #
4 # stats test: Verifies that the set of stat calls (stat, fstat, fstatvfs, and 
5 #             statvfs) return the same items and that the calls return the
6 #             same items as Perl's stat call (which would use a native library
7 #             and not libsysio)
8 #
9 #
10
11 use IPC::Open2;
12
13 use strict;
14 use FindBin;
15 use lib "$FindBin::Bin";
16 use helper;
17
18 sub usage
19 {
20   print "Usage ./test_stats.pl file : Verifies that the set of stat calls (stat, \n";
21   print "                           : fstat, fstatvfs, statvfs) return the same set\n";
22   print "                           : of stats for file and that the calls return \n";
23   print "                           : the same items as Perl's stat call (which \n";
24   print "                           : would use a native library and not libsysio)\n";
25   exit(-1);
26 }
27
28 # Compares the output of Perl's stat function with the output
29 # from libsysio's stat
30 sub cmp_stats
31 {
32
33     my ( $cmdfh, $outfh, $is_alpha, $bits, @stats) = @_;
34
35
36     my ($iodev, $ioino, $iomode, $ionlink, $iouid, $iogid, $iordev, 
37         $iosize, $ioblksize, $ioblks, $ioatime, $iomtime, $ioctime, @pstats) =
38             @stats;
39
40     if ($is_alpha == 1) {
41         ($iodev, $ioino, $iomode, $ionlink, $iouid, $iogid, $iordev, 
42             $iosize, $ioatime, $iomtime, $ioctime, $ioblks, $ioblksize, @pstats) =
43                 @stats;
44     }
45     if ($bits == 64) {
46         ($iodev, $ioino, $ionlink, $iomode,  $iouid, $iogid, $iordev, 
47             $iosize, $ioblksize, $ioblks, $ioatime, $iomtime, $ioctime,@pstats) =
48                 @stats;
49     }
50     my ($pdev, $pino, $pmode, $pnlink, $puid, $pgid, $prdev,
51         $psize, $patime, $pmtime, $pctime, $pblksize, $pblks) = @pstats;
52
53 #  helper::cmp_nums($cmdfh, $outfh, $iodev, $pdev, "device numbers");
54   helper::cmp_nums($cmdfh, $outfh, $ioino, $pino, "inode numbers");
55   helper::cmp_nums($cmdfh, $outfh, $iomode, $pmode, "file modes");
56   helper::cmp_nums($cmdfh, $outfh, $ionlink, $pnlink, "number of links");
57   helper::cmp_nums($cmdfh, $outfh, $iouid, $puid, "user ids");
58   helper::cmp_nums($cmdfh, $outfh, $iogid, $pgid, "group ids");
59   helper::cmp_nums($cmdfh, $outfh, $iordev, $prdev, "device ids");
60   helper::cmp_nums($cmdfh, $outfh, $iosize, $psize, "file sizes");
61   helper::cmp_nums($cmdfh, $outfh, $ioatime, $patime, "access times");
62   helper::cmp_nums($cmdfh, $outfh, $iomtime, $pmtime, "modification times");
63   helper::cmp_nums($cmdfh, $outfh, $ioctime, $pctime, "inode change times");
64   helper::cmp_nums($cmdfh, $outfh, $ioblksize, $pblksize, "block sizes");
65   helper::cmp_nums($cmdfh, $outfh, $ioblks, $pblks, "blocks allocated");
66 }
67
68   
69 # Prints out the stat buffer and verifies that it matches
70 # Perl's output
71 sub verify_stat
72 {
73   my ($cmdfh, $outfh, $cmd, $is_alpha, $bits, @stats) = @_;
74   my $i=0;
75
76   my $cmdstr;
77   # Print out the stat buffer
78   if ($is_alpha == 1) {
79       $cmdstr = 'PRINT $buf 0 16 LONG 16 16 INT 32 8 LONG 40 4 INT 48 40 LONG'."\n";
80   } elsif ($bits == 32) {
81       $cmdstr = 'PRINT $buf 0 8 LONG 12 24 INT 44 8 LONG 48 8 INT 56 24 LONG'."\n";
82   } else {
83       $cmdstr = 'PRINT $buf 0 24 LONG 24 16 INT 48 32 LONG 88 8 LONG 104 8 LONG'."\n";     
84   }
85  
86   helper::send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);
87
88   my $res = <$outfh>;
89   chop($res);
90
91   my @iostats = split(' ', $res);
92   foreach my $iostat (@iostats) {
93     $iostats[$i] = oct($iostat);
94     $i++;
95   }
96
97   cmp_stats($cmdfh, $outfh, $is_alpha, $bits, @iostats, @stats);
98  
99 }
100
101 sub process_cmd
102 {
103   my ($file, $use_system, $is_alpha) = @_;
104   
105 # Get tests directory
106   my $testdir = $FindBin::Bin;
107
108   eval {
109       if ($is_alpha == 0) {
110           open2(\*OUTFILE, \*CMDFILE, "$testdir/test_driver --np");
111       } else {
112           open2(\*OUTFILE, \*CMDFILE, 
113                 "yod -batch -quiet -sz 1 $testdir/test_driver --np");
114       }
115   };
116
117   if ($@) {
118     if ($@ =~ /^open2/) {
119       warn "open2 failed: $!\n$@\n";
120       return;
121     }
122     die;
123
124   }
125
126   my $outfh = \*OUTFILE;
127   my $cmdfh = \*CMDFILE;
128
129   if ($is_alpha == 0) {
130     helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n");
131   }
132
133   my @stats;
134   if ($use_system == 1) {
135       # Get stats for file
136       @stats = stat($file);
137   } 
138  
139   # Allocate the buffer
140   my $cmdstr = '$buf = ALLOC ( $size = CALL sizeof stat )'."\n";
141   helper::send_cmd($cmdfh, $outfh, "alloc", $cmdstr);  
142
143
144   # Issue the stat command
145   $cmdstr = 'CALL stat '."$file ".'$buf'."\n";
146   helper::send_cmd($cmdfh, $outfh, "stat", $cmdstr);  
147   helper::verify_cmd($cmdfh, $outfh, "stat");
148
149   # Attempt to determine type
150   $cmdstr = 'PRINT $size'."\n";
151   helper::send_cmd($cmdfh, $outfh, "print", $cmdstr);  
152   my $statsize = <$outfh>;
153   chop($statsize);
154   $statsize = oct($statsize);
155   my $bits = 32;
156   if ($statsize == 144) {
157       $bits = 64;
158   }
159   
160   if ($use_system == 1) {
161       # Now print the buffer out and verify that it matches
162       # what Perl has
163       verify_stat($cmdfh, $outfh, "stat", $is_alpha, $bits, @stats);
164   }
165
166   # Open the file
167   $cmdstr = '$fd = CALL open '."$file O_RDONLY\n";
168   helper::send_cmd($cmdfh, $outfh, "open", $cmdstr);  
169   helper::verify_cmd($cmdfh, $outfh, "open");
170
171
172   # Now issue an fstat call
173   $cmdstr = 'CALL fstat $fd $buf'."\n";
174   helper::send_cmd($cmdfh, $outfh, "fstat", $cmdstr);  
175   helper::verify_cmd($cmdfh, $outfh, "fstat");
176
177   if ($use_system == 1) {
178       verify_stat($cmdfh, $outfh, "fstat", $is_alpha, $bits, @stats);
179   }
180
181   # Test lstat
182   if ($use_system == 1) {
183       @stats = lstat($file);
184   }
185   
186   $cmdstr = 'CALL lstat '."$file ".'$buf'."\n";
187   helper::send_cmd($cmdfh, $outfh, "lstat", $cmdstr);  
188   helper::verify_cmd($cmdfh, $outfh, "lstat");
189
190   if ($use_system == 1) {
191       verify_stat($cmdfh, $outfh, "lstat", $is_alpha, $bits, @stats);
192   }
193
194   if (0) {
195       # Now do statvfs functions
196       $cmdstr = '$buf2 = ALLOC ( $size2 = CALL sizeof statvfs )'."\n";
197       helper::send_cmd($cmdfh, $outfh, "alloc", $cmdstr);
198       
199       # Clear out the buffer
200       $cmdstr = 'CALL clear $buf2'."\n";
201       helper::send_cmd($cmdfh, $outfh, "CLEAR", $cmdstr);
202       
203       $cmdstr = 'CALL statvfs '."$file ".'$buf2'."\n";
204       helper::send_cmd($cmdfh, $outfh, "statvfs", $cmdstr);  
205       helper::verify_cmd($cmdfh, $outfh, "statvfs");
206                 
207       # Print out the statvfs buffer
208       $cmdstr = 'PRINT $buf2 0 16 LONG 16 32 INT 48 16 LONG'."\n";
209       helper::send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);
210       
211       my $res = <$outfh>;
212       chop($res);
213       my @vfsstats1 = split(' ', $res);
214                 
215       # Clear out the buffer
216       $cmdstr = 'CALL clear $buf2'."\n";
217       helper::send_cmd($cmdfh, $outfh, "CLEAR", $cmdstr);
218                 
219       # Now do fstatvfs
220       $cmdstr = 'CALL fstatvfs $fd $buf2'."\n";
221       helper::send_cmd($cmdfh, $outfh, "fstatvfs", $cmdstr);  
222       helper::verify_cmd($cmdfh, $outfh, "fstatvfs");
223       
224       # Print out the statvfs buffer
225       $cmdstr = 'PRINT $buf2 0 16 LONG 16 32 INT 48 16 LONG'."\n";
226       helper::send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);
227       
228       $res = <$outfh>;
229       chop($res);
230       my @vfsstats2 = split(' ', $res);
231   
232       # Verify the two vfsstats arrays match
233       if (@vfsstats1 != @vfsstats2) {
234           helper::print_and_exit($cmdfh, $outfh, 1, "Two vfsstat arrays unequal lengths\n");
235         }
236       
237       my $i=0;
238
239       foreach my $stat1 (@vfsstats1) {
240           if ($stat1 ne $vfsstats2[$i++]) {
241               my $str = sprintf("vfsstats field %d are not equal (%s != %s)\n",
242                                 $i-1, $stat1, $vfsstats2[$i-1]);
243               helper::print_and_exit($cmdfh, $outfh, 1, $str);
244       }
245       }
246   }
247   
248   helper::print_and_exit($cmdfh, $outfh, 0, "stat test successful\n");
249 }
250
251
252
253
254 my $currarg = 0;
255 my $is_alpha = 0;
256 if (@ARGV < 2) {
257   usage;
258 } elsif (@ARGV > 2) {
259   if ($ARGV[$currarg++] eq "-alpha") {
260     $is_alpha = 1;
261   }
262 }
263
264 my $use_system= $ARGV[$currarg++];
265 my $file = $ARGV[$currarg];
266
267 process_cmd($file, $use_system, $is_alpha);
268