Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[fs/lustre-release.git] / lustre / tests / create.pl
1 #!/usr/bin/perl -w
2 use strict;
3 $|++;
4
5 $ENV{PATH}="/bin:/usr/bin";
6 $ENV{ENV}="";
7 $ENV{BASH_ENV}="";
8 use POSIX ":sys_wait_h";
9
10 use diagnostics;
11 use Getopt::Long;
12
13 use vars qw(
14             $MAX_THREADS
15             );
16
17 # Don't try to run more than this many threads concurrently.
18 $MAX_THREADS = 16;
19
20 # Initialize variables
21 my $silent = 0;
22 my $use_mcreate = 1; # should we use mcreate or open?
23 my $num_files = 5;   # number of files to create
24 my $iterations = 1;
25 my $num_threads = 1;
26 my $mountpt;
27 my $num_mounts = -1;
28
29 # Get options from the command line.
30 GetOptions("silent!" => \$silent,
31            "use_mcreate=i" => \$use_mcreate,
32            "num_files=i" => \$num_files,
33            "mountpt=s" => \$mountpt,
34            "num_mounts=i" => \$num_mounts,
35            "iterations=i" => \$iterations,
36            "num_threads=i" => \$num_threads,
37            ) || die &usage;
38
39 # Check for mandatory args.
40 if (!$mountpt || 
41     !$num_mounts) {
42     die &usage;
43 }
44
45 if ($num_threads > $MAX_THREADS) {
46     print "\nMAX_THREADS is currently set to $MAX_THREADS.\n\n";
47     print "You will have to change this in the source\n";
48     print "if you really want to run with $num_threads threads.\n\n";
49     exit 1;
50 }
51
52 # Initialize rand() function.
53 srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
54
55 #########################################################################
56 ### MAIN
57
58 for (my $i=1; $i<=$num_threads; $i++) {
59     my $status = &fork_and_create($i);
60     last if ($status != 0);
61 }
62
63 # Wait for all our threads to finish.
64 my $child = 0;
65 do {
66     $child = waitpid(-1, WNOHANG);
67 } until $child > 0;
68 sleep 1;
69
70 exit 0;
71
72 #########################################################################
73 ### SUBROUTINES
74
75 sub usage () {
76     print "\nUsage: $0 [--silent] [--use_mcreate=n] [--num_files=n] [--iterations=n] [--num_threads=n] --mountpt=/path/to/lustre/mount --num_mounts=n\n\n";
77     print "\t--silent\tminimal output\n";
78     print "\t--use_mcreate=n\tuse mcreate to create files, default=1 (yes)\n";
79     print "\t--num_files=n\tnumber of files to create per iteration, default=5\n";
80     print "\t--iterations=n\tnumber of iterations to perform, default=1\n";
81     print "\t--num_threads=n\tnumber of thread to run, default=1\n";
82     print "\t--mountpt\tlocation of lustre mount\n";
83     print "\t--num_mounts=n\tnumber of lustre mounts to test across, default=-1 (single mount point without numeric suffix)\n\n";
84     print "example: $0 --mountpt=/mnt/lustre --num_mounts=2 --iterations=50\n";
85     print "         will perform 50 interations in /mnt/lustre1 and /mnt/lustre2\n";
86     print "         $0 --mountpt=/mnt/lustre --num_mounts=-1 --iterations=50\n";
87     print "         will perform 50 iterations in /mnt/lustre only\n\n";
88     exit;
89 }
90
91 #########################################################################
92 sub fork_and_create ($) {
93     my ($thread_num) = @_;
94     
95   FORK: {
96       if (my $pid = fork) {
97           # parent here
98           # child process pid is available in $pid
99           return 0;
100       } elsif (defined $pid) { # $pid is zero here if defined
101           my $current_iteration=1;
102           while ($current_iteration <= $iterations) {
103               for (my $i=1; $i<=$num_files; $i++) {
104                   my $which = "";
105                   if ($num_mounts > 0) {
106                       $which = int(rand() * $num_mounts) + 1;
107                   }
108                   my $d = int(rand() * $num_files);
109                   do_open("${mountpt}${which}/thread${thread_num}.${d}");
110                   
111                   if ($num_mounts > 0) {
112                       $which = int(rand() * $num_mounts) + 1;
113                   }
114                   $d = int(rand() * $num_files);
115                   my $path = "${mountpt}${which}/thread${thread_num}.${d}";
116                   print  "Thread $thread_num: Unlink $path start [" . $$."]...\n" if !$silent;
117                   if (unlink($path)) {
118                       print "Thread $thread_num: Unlink done [$$] $path: Success\n" if !$silent;
119                   } else {
120                       print "Thread $thread_num: Unlink done [$$] $path: $!\n"if !$silent;
121                   }
122               }
123               if (($current_iteration) % 100 == 0) {
124                   print STDERR "Thread $thread_num: " . $current_iteration . " operations [" . $$ . "]\n";
125               }
126               $current_iteration++;
127           }
128           
129           my $which = "";
130           if ($num_mounts > 0) {
131               $which = int(rand() * $num_mounts) + 1;
132           }
133           for (my $d = 0; $d < $num_files; $d++) {
134               my $path = "${mountpt}${which}/thread${thread_num}.${d}";
135               unlink("$path") if (-e $path);
136           }
137           
138           print "Thread $thread_num: Done.\n";
139           
140           exit 0;
141
142       } elsif ($! =~ /No more process/) {
143           # EAGAIN, supposedly recoverable fork error
144           sleep 5;
145           redo FORK;
146       } else {
147           # weird fork error
148           die "Can't fork: $!\n";
149       }
150   }
151
152 }
153
154 #########################################################################
155
156 sub do_open ($) {
157     my ($path) = @_;;
158
159     if ($use_mcreate) {
160         my $tmp = `./mcreate $path`;
161         if ($tmp) {
162             print  "Creating $path [" . $$."]...\n" if !$silent;
163             $tmp =~ /.*error: (.*)\n/;
164             print  "Create done [$$] $path: $!\n" if !$silent;
165         } else {
166             print  "Create done [$$] $path: Success\n"if !$silent;
167         }
168     } else {
169         print  "Opening $path [" . $$."]...\n"if !$silent;
170         open(FH, ">$path") || die "open($path: $!";
171         print  "Open done [$$] $path: Success\n"if !$silent;
172         close(FH) || die;
173     }
174 }
175