Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_init / rfs.stub.pl
diff --git a/TBBT/trace_init/rfs.stub.pl b/TBBT/trace_init/rfs.stub.pl
new file mode 100755 (executable)
index 0000000..a0de62a
--- /dev/null
@@ -0,0 +1,213 @@
+#!/usr/bin/perl -w\r
+\r
+$Usage =<< ".";\r
+\r
+This is the rfs program\r
+\r
+After run nfsscan, you will get a file-list file in the output\r
+\r
+This perl program do post processing based on this file\r
+\r
+\r
+.\r
+\r
+$writebuf = "R" x 8193;\r
+$writeBlockSize = 8192;\r
+\r
+main ();\r
+\r
+sub rfsCreateSymlink()\r
+{\r
+        my ($path, $pathcnt, $sizeHexStr) = @_;\r
+        $sizeHexStr = "0x".$sizeHexStr;\r
+\r
+        my $dir = $path;\r
+        my $name = '';\r
+        my $cmdbuf;\r
+\r
+        $dir =~ /(^.*)\/(.*)/;\r
+        $dir = $1;\r
+        $name = $2;\r
+        if (!$dir) {$dir = ".";}\r
+        die "name is empty\n" if (!$name);\r
+        #print "path($path) dir($dir) name($name)\n";\r
+\r
+        if (! -e $dir) {\r
+                $cmdbuf = "mkdir -p $dir";\r
+                system $cmdbuf;\r
+                print "RFS: Warning: the directory should be created already: $path\n";\r
+        } else {\r
+                die "warning: directory name exist but not a directory: $path\n" if (!(-d $dir));\r
+        }\r
+\r
+        my $size = hex($sizeHexStr);\r
+        #print "size($sizeHexStr) lp($lp) rem($remSize)\n";\r
+\r
+        my $pathnamebuf = $sizeHexStr;\r
+        symlink ($pathnamebuf, $path);\r
+        #if (! symlink ($pathnamebuf, $path) ) {\r
+        #      print STDERR "JIAWU: WARNING: failed to create symlink: $path -> $pathnamebuf\n";\r
+        #}\r
+}\r
+\r
+\r
+sub  rfsCreateFile()\r
+{\r
+        my ($path, $pathcnt, $sizeHexStr) = @_;\r
+        $sizeHexStr = "0x".$sizeHexStr;\r
+\r
+        my $dir = $path;\r
+        my $name = '';\r
+        my $cmdbuf;\r
+\r
+        $dir =~ /(^.*)\/(.*)/;\r
+        $dir = $1;\r
+        $name = $2;\r
+        if (!$dir) {$dir = ".";}\r
+        die "name is empty\n" if (!$name);\r
+        #print "path($path) dir($dir) name($name)\n";\r
+\r
+        if (! -e $dir) {\r
+                $cmdbuf = "mkdir -p $dir";\r
+                system $cmdbuf;\r
+                print "RFS: Warning: the directory should be created already: $path\n";\r
+        } else {\r
+                die "warning: directory name exist but not a directory: $path\n" if (!(-d $dir));\r
+        }\r
+\r
+        my $size = hex($sizeHexStr);\r
+        my $remSize = $size % $writeBlockSize;\r
+        my $lp = ($size - $remSize) / $writeBlockSize;\r
+        #print "size($sizeHexStr) lp($lp) rem($remSize)\n";\r
+\r
+        open RFSTMPWRITE, ">$path" || die "RFS: can not open file for write";\r
+\r
+       # the eight lines below should not be commented out \r
+        #my $i = 0;\r
+        #for ($i = 0; $i < $lp; $i++) {\r
+        #        syswrite(RFSTMPWRITE, $writebuf, $writeBlockSize);\r
+        #}\r
+        #if ($remSize) {\r
+        #        syswrite(RFSTMPWRITE, $writebuf, $remSize);\r
+        #        #print "write ($remSize) byte\n";\r
+        #}\r
+        close RFSTMPWRITE;\r
+}\r
+\r
+# Useful commands:\r
+# sort -n -k a,b -c -u \r
+# \r
+# *** WARNING *** The locale specified by the  environment  affects  sort\r
+#      order.  Set LC_ALL=C or LC_ALL=POSIX to get the traditional sort order that uses native\r
+#      byte values.\r
+\r
+sub main {\r
+\r
+       my $cmdbuf;\r
+\r
+       # format of lines in test.fil:\r
+       # "F(1) $type(2) $state(3) $fh(4) $path(5) $pathcnt(6) $attrOrig(7-14) $attr()\r
+       # attrOrig: ($size(7) $mode(8) $op(9) $atime(10) $mtime(11) $ctime(12) $nlink(13) $ts(14))\r
+       # attrLast: ($size(15) $mode(16) $op(17) $atime(18) $mtime(19) $ctime(20) $nlink(21) $ts(22))\r
+\r
+       # skip comment lines\r
+       # if (path_count ($6) == 0 or original_op($9) == "create" or "mkdir" or "symlink") skip the file \r
+\r
+       $cmdbuf = 'gawk \' !/^[#]/ { if ($6 != "0") print $0 }\'  test.fil > all.fil';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+       \r
+       # sort the test.fil according to path name\r
+       $cmdbuf = 'export LC_ALL=C; sort -k 5,5 all.fil > all.fil.order; echo $LC_ALL';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+\r
+       # keep the interested field only\r
+       # 2(D/F) 4(FH) 5(path) 6(count) 15(size_last) 21(nlink_last) 14(ts_s) 22(ts_e)\r
+       $cmdbuf = 'gawk \' { print $14, $22, $4, $5, $15, $21}\'  all.fil.order > fh-path-map-all';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+       \r
+       # skip comment lines\r
+       # if (path_count ($6) == 0 or original_op($9) == "create" or "mkdir" or "symlink") skip the file \r
+\r
+       $cmdbuf = 'gawk \' !/^[#]/ { if ($6 != "0" && $9 != "create" && $9 != "mkdir" && $9 != "symlink") print $0 }\'  test.fil > active.fil';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+       \r
+       # sort the active.fil according to path name\r
+       $cmdbuf = 'export LC_ALL=C; sort -k 5,5 active.fil > active.fil.order; echo $LC_ALL';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+\r
+       # keep the interested field only\r
+       # 2(D/F) 4(FH) 5(path) 6(count) 7(size) 8(mode) 13(nlink)\r
+       $cmdbuf = 'gawk \' { print $2, $4, $5, $6, $7, $8 }\'  active.fil.order > active';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+       $cmdbuf = 'gawk \' { print $4, $5, $7, $13}\'  active.fil.order > fh-path-map';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+\r
+       # format output files\r
+       $cmdbuf = 'export LC_ALL=C; mv noattrdirdiscard noattrdirdiscard-tmp; sort -u -k 1,1 noattrdirdiscard-tmp > noattrdirdiscard; echo $LC_ALL; rm -f noattrdirdiscard-tmp';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+\r
+       $cmdbuf = 'export LC_ALL=C; mv noattrdir-root noattrdir-root-tmp; sort -u -k 1,1 noattrdir-root-tmp > noattrdir-root; echo $LC_ALL; rm -f noattrdir-root-tmp';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+\r
+       #$cmdbuf = 'mv noattrdirdiscard noattrdirdiscard-tmp; cat  noattrdirdiscard-tmp missdiscardfh >noattrdirdiscard; rm -f noattrdirdiscard-tmp missdiscardfh';\r
+       $cmdbuf = 'mv noattrdirdiscard noattrdirdiscard-tmp; cat  noattrdirdiscard-tmp missdiscardfh >noattrdirdiscard; rm -f noattrdirdiscard-tmp';\r
+       print "$cmdbuf\n";\r
+       system $cmdbuf;\r
+       \r
+       #exit(0);\r
+       \r
+       # so far, you got the following information\r
+       # in active: all files/dirs active \r
+       # in noattrdir-root: a set of fhs pointing to RFSNN0\r
+       # in rfsinfo: a set of dir fhs refer to RFSNNxxx(>0)\r
+       #                a set of file fhs should be discard due to short of information\r
+\r
+       # create the active fs\r
+       # 1. BASEDIR/RFSNN0\r
+       # 2. BASEDIR/active\r
+       $cmdbuf = "mkdir -p RFSFS/RFSNN0";\r
+       system $cmdbuf;\r
+       open RFS_ACTIVE, "active" || die "open active failed\n";\r
+       while (<RFS_ACTIVE>) {\r
+               chop;\r
+               my ($type, $fh, $path, $pathcnt, $sizeHexStr, $mode) = split (' ', $_, 7);\r
+               if ($type==2) {\r
+                       $cmdbuf = "mkdir -p RFSFS$path";\r
+                       system $cmdbuf;\r
+               }elsif ($type==1){\r
+                       &rfsCreateFile("RFSFS$path", $pathcnt, $sizeHexStr);\r
+               }elsif ($type==5){\r
+                       #print STDERR "SYMLINK: $type fh: $fh path: $path\n";\r
+                       &rfsCreateSymlink("RFSFS$path", $pathcnt, $sizeHexStr);\r
+               }else {\r
+                       print STDERR "special file: $type fh: $fh path: $path\n";\r
+               }\r
+\r
+               my $line_num=0;\r
+               $line_num++;\r
+               if ( ($line_num %100)==0 ) {\r
+                       print STDERR "$line_num\n";\r
+               }\r
+               \r
+       }\r
+\r
+       # create map table: key (fh), value (path/fn)\r
+\r
+       # check whether there is fh that is not mapped to any path/fn in the trace\r
+\r
+       # simulate a replay of trace\r
+\r
+       return;\r
+       \r
+}\r
+\r
+1;\r