Whamcloud - gitweb
new upstream libsysio snapshot (20041101)
[fs/lustre-release.git] / libsysio / tests / populator.pl
1 #!/usr/bin/perl -w
2
3 use IPC::Open2;
4
5 use strict;
6 use helper;
7
8 sub usage
9 {
10   print "Usage: ./populator.pl <-seed seed>     :\n";
11   print "                      <-file filename> :\n";
12   print "                      <-bytes bytes>   : Create a file, filename, that\n";
13   print "                                       : is bytes long and populate with\n";
14   print "                                       : random numbers using the given\n";
15   print "                                       : seed.  Will use defaults if args\n";
16   print "                                       : not given\n";
17   exit(-1);
18 }
19
20 sub get_buf
21 {
22   my $MAX_SIZE = 2147483648;
23
24   my $str;
25   my $num;
26   my $len = 0;
27
28   while ($len < 512) {
29     $num = rand $MAX_SIZE;
30     my $tmpstr = sprintf("%d", $num);
31     $str .= $tmpstr;
32     $len += length $tmpstr;
33   }
34
35   return ($len, $str);
36 }
37
38 sub write_file
39 {
40   my ($cmdfh, $outfh, $filename, $bytes) = @_;
41
42
43   # Allocate the read buffer
44   my $cmd = '$buf = ALLOC 1024'."\n";
45   helper::send_cmd($cmdfh, $outfh, "alloc", $cmd);  
46   
47   # Open (create) the new file
48   $cmd = '$fd = CALL open '."$filename O_RDWR|O_CREAT S_IRWXU\n";
49   helper::send_cmd($cmdfh, $outfh, "open", $cmd);  
50
51   # Verify the system call's output
52   helper::verify_cmd($cmdfh, $outfh, "open");  
53
54   my $left_bytes = $bytes;
55   while ($left_bytes > 0) {
56     # Get a buffer filled with random numbers
57     # Buffer will be no less than 512 bytes
58     my ($len, $buf) = get_buf;
59     if ($len > $left_bytes) {
60       $len = $left_bytes;
61     }
62
63     # Need to fill $buf with the buffer 
64     $cmd = "CALL fill $buf STR $len 0 ".'$buf'."\n";
65     helper::send_cmd($cmdfh, $outfh, "fill", $cmd);
66
67     # Write out $len bytes to $filename
68     $cmd = 'CALL write $fd $buf '."$len\n";
69    
70     helper::send_cmd($cmdfh, $outfh, "write", $cmd);
71
72     my $written_bytes = helper::verify_cmd($cmdfh, $outfh, "write");
73     $written_bytes = oct($written_bytes);
74     if ($written_bytes != $len) {
75        helper::print_and_exit($cmdfh, $outfh, 1, 
76                            "ERROR! Meant to print out $len but only printed $written_bytes\n");
77      }
78
79     $left_bytes -= $len;
80   }
81 }
82
83 sub populate_file
84 {
85   my ($filename, $bytes, $is_alpha) = @_;
86   
87   eval {
88       if ($is_alpha == 0) {
89           open2(\*OUTFILE, \*CMDFILE, "./test_driver --np");
90       } else {
91           open2(\*OUTFILE, \*CMDFILE, 
92                 "yod -batch -quiet -sz 1 ./test_driver --np");
93       }
94   };
95
96   if ($@) {
97     if ($@ =~ /^open2/) {
98       warn "open2 failed: $!\n$@\n";
99       return;
100     }
101     die;
102
103   }
104
105   my $outfh = \*OUTFILE;
106   my $cmdfh = \*CMDFILE;
107
108   if ($is_alpha == 0) {
109     helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n");
110   }
111
112   # Now write the file
113   write_file($cmdfh, $outfh, $filename, $bytes);
114
115   # Close the file
116   my $cmd = 'CALL close $fd'."\n";
117   helper::send_cmd($cmdfh, $outfh, "close", $cmd);
118
119   helper::verify_cmd($cmdfh, $outfh, "close");
120
121   # All done
122   helper::print_and_exit($cmdfh, $outfh, 0, "File $filename successfully created\n");
123 }
124
125
126 my $is_alpha = 0;
127 my $seed = time;
128 my $filename = "randfile.$seed.$$";
129 my $bytes = 1024;
130 for (my $i = 0; $i < @ARGV; $i++) 
131 {
132   if ($ARGV[$i] eq "-file") {
133     $i++;
134     $filename = $ARGV[$i];
135   } elsif ($ARGV[$i] eq "-seed") {
136     $i++;
137     $seed = $ARGV[$i];
138   } elsif ($ARGV[$i] eq "-alpha") {
139     $is_alpha = 1;
140   } elsif ($ARGV[$i] eq "-bytes") {
141     $i++;
142     $bytes = $ARGV[$i];
143   }
144 }
145
146 # seed the randome number generator
147 srand $seed;
148
149 populate_file($filename, $bytes, $is_alpha);
150
151 exit $seed;
152
153
154
155