Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_init / ns_tsplit.pl
1 #!/usr/bin/perl -w\r
2 #\r
3 # Copyright (c) 2002-2003\r
4 #      The President and Fellows of Harvard College.\r
5 #\r
6 # Redistribution and use in source and binary forms, with or without\r
7 # modification, are permitted provided that the following conditions\r
8 # are met:\r
9 # 1. Redistributions of source code must retain the above copyright\r
10 #    notice, this list of conditions and the following disclaimer.\r
11 # 2. Redistributions in binary form must reproduce the above copyright\r
12 #    notice, this list of conditions and the following disclaimer in the\r
13 #    documentation and/or other materials provided with the distribution.\r
14 # 3. Neither the name of the University nor the names of its contributors\r
15 #    may be used to endorse or promote products derived from this software\r
16 #    without specific prior written permission.\r
17 #\r
18 # THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND\r
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
21 # ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE\r
22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
24 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
25 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
27 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
28 # SUCH DAMAGE.\r
29 #\r
30 # $Id: ns_tsplit,v 1.4 2003/07/26 20:52:04 ellard Exp $\r
31 \r
32 use Getopt::Std;\r
33 use Time::Local;\r
34 \r
35 $ProgDir = $0;\r
36 $ProgDir =~ /(^.*)\//;\r
37 $ProgDir = $1;\r
38 if (!$ProgDir) {\r
39         $ProgDir = ".";\r
40 }\r
41 \r
42 # Set the default start and end times.\r
43 \r
44 $DEF_BEGIN_TIME = "20000101:00:00:00";\r
45 $DEF_END_TIME   = "20360101:00:00:00";\r
46 \r
47 # The range includes everything from the start time up to and less\r
48 # than the end time (so if you want one day, it is correct to go\r
49 # midnight to midnight, instead of going until 23:59:59).\r
50 \r
51 $Usage =<< ".";\r
52 \r
53 Usage: $0 [options] rangeSpec... - [table.ns [table.ns...]]\r
54 \r
55 Command line options:\r
56 \r
57 -h              Print usage message and exit.\r
58 \r
59 Any number of time range specifier can be provided.  The special\r
60 symbol "-" is used to separated the time specifiers from the input\r
61 file names.  If input is taken from stdin, then the "-" may be\r
62 omitted.\r
63 \r
64 The basic time range specification format is:  StartTime-EndTime where\r
65 StartTime and EndTime have the form YYMMDD[:HH[:MM[:SS]]].  All\r
66 records from the input that have a timestamp greater than or equal to\r
67 StartTime and less than EndTime are printed to stdout.\r
68 \r
69 If StartTime is omitted, $DEF_BEGIN_TIME is used.\r
70 If EndTime is omitted, $DEF_END_TIME is used.\r
71 \r
72 Note that omitting both the StartTime and EndTime results in a rangeSpec\r
73 of "-", which is the special symbol that marks the end of the rangeSpecs.\r
74 .\r
75 \r
76 $cmdline = "$0 " . join (' ', @ARGV);\r
77 $Options = "h";\r
78 if (! getopts ($Options)) {\r
79         print STDERR "$0: Incorrect usage.\n";\r
80         print STDERR $Usage;\r
81         exit (1);\r
82 }\r
83 if (defined $opt_h) {\r
84         print $Usage;\r
85         exit (0);\r
86 }\r
87 \r
88 @StartTimes     = ();\r
89 @EndTimes       = ();\r
90 \r
91 while ($ts = shift @ARGV) {\r
92         last if ($ts eq '-');\r
93 \r
94         my ($ts_s, $ts_e) = split (/-/, $ts);\r
95         my $s;\r
96 \r
97         if ($ts_s eq '') {\r
98                 $ts_s = $DEF_START_TIME;\r
99         }\r
100         $s = &ts2secs ($ts_s);\r
101         if (! defined $s) {\r
102                 print STDERR "Failed to translate ($ts_s)\n";\r
103                 exit (1);\r
104         }\r
105         push @StartTimes, $s;\r
106 \r
107         if ($ts_e eq '') {\r
108                 $ts_e = $DEF_END_TIME;\r
109         }\r
110         $s = &ts2secs ($ts_e);\r
111         if (! defined $s) {\r
112                 print STDERR "Failed to translate ($ts_e)\n";\r
113                 exit (1);\r
114         }\r
115 \r
116         push @EndTimes, $s;\r
117 }\r
118 \r
119 print "#cmdline $cmdline\n";\r
120 \r
121 while ($l = <>) {\r
122         if ($l =~ /^#/) {\r
123                 print $l;\r
124                 next;\r
125         }\r
126 \r
127         my ($type, $time, $client, $fh, $euid, $egid, @vals) = split (' ', $l);\r
128 \r
129         next unless ($type eq 'C');\r
130 \r
131         # Something wrong with the input?\r
132 \r
133         next if (@vals == 0);\r
134 \r
135         if (saveRecord ($time)) {\r
136                 print $l;\r
137         }\r
138 }\r
139 \r
140 exit 0;\r
141 \r
142 sub saveRecord {\r
143         my ($t) = @_;\r
144 \r
145         for (my $i = 0; $i < @StartTimes ; $i++) {\r
146                 if (($StartTimes[$i] <= $t) && ($t < $EndTimes[$i])) {\r
147                         return (1);\r
148                 }\r
149         }\r
150 \r
151         return (0);\r
152 }\r
153 \r
154 # This is ugly!\r
155 #\r
156 # The basic time specification format is:  YYMMDD[:HH[:MM[:SS]]].\r
157 #\r
158 # To make a time range, join two time specs with a -.\r
159 #\r
160 # The data spec is Y2.01K compliant...  I'm assuming that all the\r
161 # traces processed by this tool will be gathered before 2010.  If\r
162 # anyone is still gathering NFSv3 traces in 2010, I'm sorry.\r
163 \r
164 sub ts2secs {\r
165         my ($ts) = @_;\r
166         my ($hour, $min, $sec) = (0, 0, 0);\r
167 \r
168         my (@d) = split (/:/, $ts);\r
169 \r
170         if ($d[0] =~ /([0-9][0-9])([0-2][0-9])([0-3][0-9])/) {\r
171                 $year = $1 + 100;\r
172                 $mon = $2 - 1;\r
173                 $day = $3;\r
174         }\r
175         else {\r
176                 return undef;\r
177         }\r
178 \r
179         if (@d > 1) {\r
180                 if ($d[1] =~ /([0-2][0-9])/) {\r
181                         $hour = $1;\r
182                 }\r
183                 else {\r
184                         return undef;\r
185                 }\r
186         }\r
187 \r
188         if (@d > 2) {\r
189                 if ($d[2] =~ /([0-5][0-9])/) {\r
190                         $min = $1;\r
191                 }\r
192                 else {\r
193                         return undef;\r
194                 }\r
195         }\r
196 \r
197         if (@d > 3) {\r
198                 if ($d[3] =~ /([0-5][0-9])/) {\r
199                         $sec = $1;\r
200                 }\r
201                 else {\r
202                         return undef;\r
203                 }\r
204         }\r
205 \r
206         my $s = timelocal ($sec, $min, $hour, $day, $mon, $year,\r
207                         undef, undef, undef);\r
208 \r
209         my ($s2, $m2, $h2, $md2, $mon2, $y2, $isdst) = localtime ($s);\r
210         if (($s2 != $sec) || $min != $m2 || $hour != $h2 || $day != $md2) {\r
211                 print "failed 1!\n";\r
212         }\r
213         if (($mon != $mon2) || $year != $y2) {\r
214                 print "failed 2!\n";\r
215         }\r
216 \r
217         return ($s);\r
218 }\r
219 \r