Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_init / ns_tsplit.pl
diff --git a/TBBT/trace_init/ns_tsplit.pl b/TBBT/trace_init/ns_tsplit.pl
new file mode 100755 (executable)
index 0000000..e3dd420
--- /dev/null
@@ -0,0 +1,219 @@
+#!/usr/bin/perl -w\r
+#\r
+# Copyright (c) 2002-2003\r
+#      The President and Fellows of Harvard College.\r
+#\r
+# Redistribution and use in source and binary forms, with or without\r
+# modification, are permitted provided that the following conditions\r
+# are met:\r
+# 1. Redistributions of source code must retain the above copyright\r
+#    notice, this list of conditions and the following disclaimer.\r
+# 2. Redistributions in binary form must reproduce the above copyright\r
+#    notice, this list of conditions and the following disclaimer in the\r
+#    documentation and/or other materials provided with the distribution.\r
+# 3. Neither the name of the University nor the names of its contributors\r
+#    may be used to endorse or promote products derived from this software\r
+#    without specific prior written permission.\r
+#\r
+# THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND\r
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE\r
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
+# SUCH DAMAGE.\r
+#\r
+# $Id: ns_tsplit,v 1.4 2003/07/26 20:52:04 ellard Exp $\r
+\r
+use Getopt::Std;\r
+use Time::Local;\r
+\r
+$ProgDir = $0;\r
+$ProgDir =~ /(^.*)\//;\r
+$ProgDir = $1;\r
+if (!$ProgDir) {\r
+       $ProgDir = ".";\r
+}\r
+\r
+# Set the default start and end times.\r
+\r
+$DEF_BEGIN_TIME        = "20000101:00:00:00";\r
+$DEF_END_TIME  = "20360101:00:00:00";\r
+\r
+# The range includes everything from the start time up to and less\r
+# than the end time (so if you want one day, it is correct to go\r
+# midnight to midnight, instead of going until 23:59:59).\r
+\r
+$Usage =<< ".";\r
+\r
+Usage: $0 [options] rangeSpec... - [table.ns [table.ns...]]\r
+\r
+Command line options:\r
+\r
+-h             Print usage message and exit.\r
+\r
+Any number of time range specifier can be provided.  The special\r
+symbol "-" is used to separated the time specifiers from the input\r
+file names.  If input is taken from stdin, then the "-" may be\r
+omitted.\r
+\r
+The basic time range specification format is:  StartTime-EndTime where\r
+StartTime and EndTime have the form YYMMDD[:HH[:MM[:SS]]].  All\r
+records from the input that have a timestamp greater than or equal to\r
+StartTime and less than EndTime are printed to stdout.\r
+\r
+If StartTime is omitted, $DEF_BEGIN_TIME is used.\r
+If EndTime is omitted, $DEF_END_TIME is used.\r
+\r
+Note that omitting both the StartTime and EndTime results in a rangeSpec\r
+of "-", which is the special symbol that marks the end of the rangeSpecs.\r
+.\r
+\r
+$cmdline = "$0 " . join (' ', @ARGV);\r
+$Options = "h";\r
+if (! getopts ($Options)) {\r
+       print STDERR "$0: Incorrect usage.\n";\r
+       print STDERR $Usage;\r
+       exit (1);\r
+}\r
+if (defined $opt_h) {\r
+       print $Usage;\r
+       exit (0);\r
+}\r
+\r
+@StartTimes    = ();\r
+@EndTimes      = ();\r
+\r
+while ($ts = shift @ARGV) {\r
+       last if ($ts eq '-');\r
+\r
+       my ($ts_s, $ts_e) = split (/-/, $ts);\r
+       my $s;\r
+\r
+       if ($ts_s eq '') {\r
+               $ts_s = $DEF_START_TIME;\r
+       }\r
+       $s = &ts2secs ($ts_s);\r
+       if (! defined $s) {\r
+               print STDERR "Failed to translate ($ts_s)\n";\r
+               exit (1);\r
+       }\r
+       push @StartTimes, $s;\r
+\r
+       if ($ts_e eq '') {\r
+               $ts_e = $DEF_END_TIME;\r
+       }\r
+       $s = &ts2secs ($ts_e);\r
+       if (! defined $s) {\r
+               print STDERR "Failed to translate ($ts_e)\n";\r
+               exit (1);\r
+       }\r
+\r
+       push @EndTimes, $s;\r
+}\r
+\r
+print "#cmdline $cmdline\n";\r
+\r
+while ($l = <>) {\r
+       if ($l =~ /^#/) {\r
+               print $l;\r
+               next;\r
+       }\r
+\r
+       my ($type, $time, $client, $fh, $euid, $egid, @vals) = split (' ', $l);\r
+\r
+       next unless ($type eq 'C');\r
+\r
+       # Something wrong with the input?\r
+\r
+       next if (@vals == 0);\r
+\r
+       if (saveRecord ($time)) {\r
+               print $l;\r
+       }\r
+}\r
+\r
+exit 0;\r
+\r
+sub saveRecord {\r
+       my ($t) = @_;\r
+\r
+       for (my $i = 0; $i < @StartTimes ; $i++) {\r
+               if (($StartTimes[$i] <= $t) && ($t < $EndTimes[$i])) {\r
+                       return (1);\r
+               }\r
+       }\r
+\r
+       return (0);\r
+}\r
+\r
+# This is ugly!\r
+#\r
+# The basic time specification format is:  YYMMDD[:HH[:MM[:SS]]].\r
+#\r
+# To make a time range, join two time specs with a -.\r
+#\r
+# The data spec is Y2.01K compliant...  I'm assuming that all the\r
+# traces processed by this tool will be gathered before 2010.  If\r
+# anyone is still gathering NFSv3 traces in 2010, I'm sorry.\r
+\r
+sub ts2secs {\r
+       my ($ts) = @_;\r
+       my ($hour, $min, $sec) = (0, 0, 0);\r
+\r
+       my (@d) = split (/:/, $ts);\r
+\r
+       if ($d[0] =~ /([0-9][0-9])([0-2][0-9])([0-3][0-9])/) {\r
+               $year = $1 + 100;\r
+               $mon = $2 - 1;\r
+               $day = $3;\r
+       }\r
+       else {\r
+               return undef;\r
+       }\r
+\r
+       if (@d > 1) {\r
+               if ($d[1] =~ /([0-2][0-9])/) {\r
+                       $hour = $1;\r
+               }\r
+               else {\r
+                       return undef;\r
+               }\r
+       }\r
+\r
+       if (@d > 2) {\r
+               if ($d[2] =~ /([0-5][0-9])/) {\r
+                       $min = $1;\r
+               }\r
+               else {\r
+                       return undef;\r
+               }\r
+       }\r
+\r
+       if (@d > 3) {\r
+               if ($d[3] =~ /([0-5][0-9])/) {\r
+                       $sec = $1;\r
+               }\r
+               else {\r
+                       return undef;\r
+               }\r
+       }\r
+\r
+       my $s = timelocal ($sec, $min, $hour, $day, $mon, $year,\r
+                       undef, undef, undef);\r
+\r
+       my ($s2, $m2, $h2, $md2, $mon2, $y2, $isdst) = localtime ($s);\r
+       if (($s2 != $sec) || $min != $m2 || $hour != $h2 || $day != $md2) {\r
+               print "failed 1!\n";\r
+       }\r
+       if (($mon != $mon2) || $year != $y2) {\r
+               print "failed 2!\n";\r
+       }\r
+\r
+       return ($s);\r
+}\r
+\r