public
Description: Small examples, or proof of concepts
Homepage:
Clone URL: git://github.com/melo/perl-examples.git
perl-examples / nfsd_report_bench / fast_stats.pl
100755 193 lines (156 sloc) 4.558 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env perl
 
use strict;
use warnings;
use Time::HiRes qw( gettimeofday tv_interval );
 
my $mask = shift || 18; ## report every 256k lines
my $size = shift || 2**20; ## 1Mb default
$mask = 2**$mask - 1;
 
my $lines = my $ll = 0;
 
$SIG{INT} = sub {
  report(1);
  exit;
};
 
system("ps l | egrep 'PID|$$' | grep -v egrep");
 
print "\n\nParsing nfsdstats records, performance report every "
  . ($mask + 1)
  . " lines\n";
print "\nPress Ctrl-C to stop with a final report (pid $$)\n\n";
 
my $buf = '';
my $offset = 0;
my ($ts, $type, $op, $bytes);
my $t0 = my $tl = [gettimeofday];
my $rr = my $rrl = 0;
 
### Original script counters
my (
  $header1, %read_ops, %write_ops, %read_bytes,
  %write_bytes, @req_ops, @res_ops, $date,
  $h, $d, $m, $y
);
my ($read, $write, $nops) = (0, 0, 0);
my @opnames = (
  'null', 'getattr', 'setattr', 'lookup', 'access', 'ireadlink',
  'read', 'write', 'create', 'mkdir', 'symlink', 'mkdir',
  'remove', 'rmdir', 'rename', 'link', 'readdir', 'readdir+',
  'fsstat', 'fsinfo', 'pathconf', 'commit'
);
 
## discard first two lines
my $n;
do {
  $n = sysread(\*STDIN, $buf, $size);
  die "No lines, " unless $n > 0;
} until ($buf =~ s/^.+\n.+\n//);
 
 
while () {
  $n = sysread(\*STDIN, $buf, $size, length($buf));
 
  while ($buf =~ /(.+)\n/gc) {
    $lines++;
    $_ = $1;
    /^(\d+)/gc; $ts = $1;
    /(\w)\sV/gc; $type = $1;
    /\s(\d+)\s\w/gc; $op = $1;
    /\w\s(\d+)\s/gc; $bytes = $1;
 
    print "!!! Unrecognized line >>$_<<\n", next
      unless $ts && $type && $op && $bytes;
 
    ### do original bookkeeping
    if ($type eq 't') {
      $req_ops[$op]++;
      if ($op == 7) {
        $write += $bytes;
 
        $ts -= $ts % 3600; # Normalize by the hour.
 
        $write_ops{$ts}++;
        $write_bytes{$ts} += $bytes;
      }
    }
    elsif ($type eq 'e') {
      $res_ops[$op]++;
      if ($op == 6) {
        $read += $bytes;
 
        $ts -= $ts % 3600; # Normalize by the hour.
 
        $read_ops{$ts}++;
        $read_bytes{$ts} += $bytes;
      }
    }
 
    report() unless $lines & $mask;
  }
  last unless $n > 0;
 
  $buf = substr($buf, pos($buf));
  $rr += $n;
}
 
report(1);
 
sub report {
  my ($final) = @_;
 
  my $now = [gettimeofday];
  my $d = tv_interval(($final ? $t0 : $tl), $now);
  $tl = $now;
 
  my $l = $lines;
  $l -= $ll unless $final;
  $ll = $lines;
 
  my $r = $rr;
  $r -= $rrl unless $final;
  $rrl = $rr;
  $r >>= 20;
 
  my $l_rate = $d ? int($l / $d) : 'na';
  my $r_rate = $d ? $r / $d : 'na';
 
  if ($final) {
    original_report();
    print "\n\nFINAL PERFORMANCE REPORT:\n\n";
  }
 
  print "elapsed $d, lines $lines, rate $l_rate lines/sec (read "
    . ($rr >> 20)
    . " MB, rate $r_rate MB/sec)\n";
 
  if ($final) {
    print "\n\n";
    system("ps l | egrep 'PID|$$' | grep -v egrep");
  }
}
 
 
### Rest of original script follows
sub get_date {
  my $ts = shift;
  my ($h, $d, $m, $y) = (localtime($ts))[2 .. 5];
  my $date =
      ($y + 1900)
    . ($m + 1 > 9 ? $m + 1 : '0' . ($m + 1))
    . ($d > 9 ? $d : '0' . $d)
    . ($h > 9 ? $h : '0' . $h);
  return $date;
}
 
sub original_report {
  print "\n\nNumber of NFSOps REQUESTS:\n";
 
  # 0..21 -> 22 opnames
  for (0 .. 21) {
    if ($req_ops[$_]) {
      print $opnames[$_], ": \t", $req_ops[$_], "\n";
      $nops += $req_ops[$_];
    }
  }
  print "\nNumber of NFSOps RESPONSES:\n";
  foreach (0 .. 21) {
    print $opnames[$_], ": \t", $res_ops[$_], "\n" if ($res_ops[$_]);
  }
  print "Total # of NFSOPs: \t", $nops, "\n";
  print "Data Read (Gb): \t", sprintf("%.2f", ($read >> 20) / 1024),
    " Gb\n";
  print "Data Written (Gb):\t", sprintf("%.2f", ($write >> 20) / 1024),
    " Gb\n";
  print "Read/Write bytes ratio:\t", sprintf("%.2f", $read / $write), "x\n";
  print "Read/Write ops ratio:\t", sprintf("%.2f", $req_ops[6] / $req_ops[7]),
    "x\n\n\n";
 
  print "Number of Read Operations:\n";
 
  foreach my $key (keys %read_ops) {
 
    print $date, ", ", $read_ops{$key}, "\n";
  }
  print "\nNumber of Read Mbytes:\n";
  foreach my $key (keys %read_bytes) {
    print get_date($key), ", ",
      sprintf("%.2f", ($read_bytes{$key} >> 10) / 1024), "\n";
  }
  print "\nNumber of Write Operations:\n";
  foreach my $key (keys %write_ops) {
    print get_date($key), ", ", $write_ops{$key}, "\n";
  }
  print "\nNumber of Write Mbytes:\n";
  foreach my $key (keys %write_bytes) {
    print get_date($key), ", ",
      sprintf("%.2f", ($write_bytes{$key} >> 10) / 1024), "\n";
  }
  print "\n";
}