… | |
… | |
3 | # inspired by treescan by Jamie Lokier <jamie@imbolc.ucc.ie> |
3 | # inspired by treescan by Jamie Lokier <jamie@imbolc.ucc.ie> |
4 | # about 40% faster than the original version (on my fs and raid :) |
4 | # about 40% faster than the original version (on my fs and raid :) |
5 | |
5 | |
6 | use strict; |
6 | use strict; |
7 | use Getopt::Long; |
7 | use Getopt::Long; |
|
|
8 | use Time::HiRes (); |
8 | use IO::AIO; |
9 | use IO::AIO; |
9 | |
10 | |
10 | our $VERSION = $IO::AIO::VERSION; |
11 | our $VERSION = $IO::AIO::VERSION; |
11 | |
12 | |
12 | Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order", "auto_help", "auto_version"); |
13 | Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order", "auto_help", "auto_version"); |
13 | |
14 | |
14 | my ($opt_silent, $opt_print0, $opt_stat, $opt_nodirs, $opt_nofiles, $opt_grep); |
15 | my ($opt_silent, $opt_print0, $opt_stat, $opt_nodirs, |
|
|
16 | $opt_nofiles, $opt_grep, $opt_progress); |
15 | |
17 | |
16 | GetOptions |
18 | GetOptions |
17 | "quiet|q" => \$opt_silent, |
19 | "quiet|q" => \$opt_silent, |
18 | "print0|0" => \$opt_print0, |
20 | "print0|0" => \$opt_print0, |
19 | "stat|s" => \$opt_stat, |
21 | "stat|s" => \$opt_stat, |
20 | "dirs|d" => \$opt_nofiles, |
22 | "dirs|d" => \$opt_nofiles, |
21 | "files|f" => \$opt_nodirs, |
23 | "files|f" => \$opt_nodirs, |
22 | "grep|g=s" => \$opt_grep, |
24 | "grep|g=s" => \$opt_grep, |
|
|
25 | "progress|p" => \$opt_progress, |
23 | or die "Usage: try $0 --help"; |
26 | or die "Usage: try $0 --help"; |
24 | |
27 | |
25 | @ARGV = "." unless @ARGV; |
28 | @ARGV = "." unless @ARGV; |
26 | |
29 | |
27 | $opt_grep &&= qr{$opt_grep}s; |
30 | $opt_grep &&= qr{$opt_grep}s; |
|
|
31 | |
|
|
32 | my ($n_dirs, $n_files, $n_stats) = (0, 0, 0); |
|
|
33 | my $n_last; |
|
|
34 | my $n_start = Time::HiRes::time; |
28 | |
35 | |
29 | sub printfn { |
36 | sub printfn { |
30 | my ($prefix, $files, $suffix) = @_; |
37 | my ($prefix, $files, $suffix) = @_; |
31 | |
38 | |
32 | if ($opt_grep) { |
39 | if ($opt_grep) { |
… | |
… | |
45 | |
52 | |
46 | $path .= "/"; |
53 | $path .= "/"; |
47 | |
54 | |
48 | IO::AIO::poll_cb; |
55 | IO::AIO::poll_cb; |
49 | |
56 | |
|
|
57 | if ($opt_progress and $n_last + 1 < Time::HiRes::time) { |
|
|
58 | $n_last = Time::HiRes::time; |
|
|
59 | printf STDERR "%7d dirs %7d files %7d stats %g stats/s \r", $n_dirs, $n_files, $n_stats, $n_stats / ($n_last - $n_start) |
|
|
60 | if $opt_progress; |
|
|
61 | } |
|
|
62 | |
50 | aioreq_pri -1; |
63 | aioreq_pri -1; |
|
|
64 | ++$n_dirs; |
51 | aio_scandir $path, 8, sub { |
65 | aio_scandir $path, 8, sub { |
52 | my ($dirs, $files) = @_ |
66 | my ($dirs, $files) = @_ |
53 | or warn "$path: $!\n"; |
67 | or warn "$path: $!\n"; |
54 | |
68 | |
55 | printfn "", [$path] unless $opt_nodirs; |
69 | printfn "", [$path] unless $opt_nodirs; |
56 | printfn $path, $files unless $opt_nofiles; |
70 | printfn $path, $files unless $opt_nofiles; |
57 | |
71 | |
|
|
72 | $n_files += @$files; |
|
|
73 | |
58 | if ($opt_stat) { |
74 | if ($opt_stat) { |
59 | aio_wd $path, sub { |
75 | aio_wd $path, sub { |
60 | my $wd = shift; |
76 | my $wd = shift; |
61 | |
77 | |
62 | aio_lstat [$wd, $_] for @$files; |
78 | aio_lstat [$wd, $_] for @$files; |
|
|
79 | $n_stats += @$files; |
63 | }; |
80 | }; |
64 | } |
81 | } |
65 | |
82 | |
66 | &scan ("$path$_") for @$dirs; |
83 | &scan ("$path$_") for @$dirs; |
67 | }; |
84 | }; |
… | |
… | |
70 | IO::AIO::max_outstanding 100; # two fds per directory, so limit accordingly |
87 | IO::AIO::max_outstanding 100; # two fds per directory, so limit accordingly |
71 | IO::AIO::min_parallel 20; |
88 | IO::AIO::min_parallel 20; |
72 | |
89 | |
73 | for my $seed (@ARGV) { |
90 | for my $seed (@ARGV) { |
74 | $seed =~ s/\/+$//; |
91 | $seed =~ s/\/+$//; |
|
|
92 | ++$n_stats; |
75 | aio_lstat "$seed/.", sub { |
93 | aio_lstat "$seed/.", sub { |
76 | if ($_[0]) { |
94 | if ($_[0]) { |
77 | print STDERR "$seed: $!\n"; |
95 | print STDERR "$seed: $!\n"; |
78 | } elsif (-d _) { |
96 | } elsif (-d _) { |
79 | scan $seed; |
97 | scan $seed; |