| 1 |
eval 'exec perl -S $0 ${1+"$@"}' # -*-Mode:Perl; perl-indent-level:8-*- |
| 2 |
if 0; |
| 3 |
|
| 4 |
#require 5.003; |
| 5 |
|
| 6 |
# |
| 7 |
# Contributed by Giao Nguyen, http://daedalus.cs.berkeley.edu/~gnguyen |
| 8 |
# |
| 9 |
|
| 10 |
sub Usage |
| 11 |
{ |
| 12 |
$0 =~ s/.*\/([^\/]+)$/$1/; |
| 13 |
print <<EOF; |
| 14 |
Split ns trace file into files for individual connection |
| 15 |
Usage: $0 [options] files |
| 16 |
options: -c comment: comment to print at the end along with total throughput |
| 17 |
-f : force new file for each connection |
| 18 |
-psize size of packet in bytes |
| 19 |
-tt trace type |
| 20 |
-pt packet type (list) |
| 21 |
-fid flow id (list) |
| 22 |
-ctt trace type column |
| 23 |
-cpt packet type column |
| 24 |
-ctime time column |
| 25 |
-csrc source address column |
| 26 |
-cdst destination address column |
| 27 |
EOF |
| 28 |
exit; |
| 29 |
} |
| 30 |
|
| 31 |
sub Getopt |
| 32 |
{ |
| 33 |
local($spec) = @_; |
| 34 |
local($key); |
| 35 |
&Usage if ($#ARGV < 0); |
| 36 |
while ($_ = $ARGV[0], /^-(\w+)/) { |
| 37 |
shift @ARGV; |
| 38 |
$key = $1; |
| 39 |
$opt{$key} = (index($spec, "$key:") >= 0) ? shift @ARGV : 1; |
| 40 |
$_ = "\$$key"; |
| 41 |
if ($key =~ /^(tt|pt|fid)$/) { |
| 42 |
# concat to get list of values for a field in the trace |
| 43 |
eval "$_ = $_ . '$opt{$key} '"; |
| 44 |
} else { |
| 45 |
eval "$_ = $opt{$key}"; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
# default information on the trace |
| 52 |
$psize = 1000; # packet size in bytes |
| 53 |
# Column numbers for each field in the trace |
| 54 |
$ctt = 1; |
| 55 |
$ctime = 2; |
| 56 |
$cpt = 5; |
| 57 |
$cfid = 8; |
| 58 |
$csrc = 9; |
| 59 |
$cdst = 10; |
| 60 |
$cseq = 11; |
| 61 |
# the file descriptor number to started with |
| 62 |
$fdcount = 4; |
| 63 |
|
| 64 |
# Set a couple of defaults if not given at the command line |
| 65 |
&Getopt(":c:psize:tt:ctt:ctime:cpt:cfid:csrc:cdst:cseq:"); |
| 66 |
$force = $opt{'f'}; |
| 67 |
$tt = '-' if (! $tt); |
| 68 |
$pt = 'tcp' if (! $pt); |
| 69 |
$fid = 0 if (! $fid); |
| 70 |
|
| 71 |
|
| 72 |
shift @ARGV while ($#ARGV >= 0 && (! -r $ARGV[0])); |
| 73 |
|
| 74 |
# process the trace file |
| 75 |
while (<>) |
| 76 |
{ |
| 77 |
chop; |
| 78 |
@col = ('', split); |
| 79 |
$count{$col[$ctt]}++; |
| 80 |
next if (index($tt, $col[1]) < 0); |
| 81 |
next if (index($pt, $col[$cpt]) < 0); |
| 82 |
next if (index($fid, $col[$cfid]) < 0); |
| 83 |
|
| 84 |
$src = $col[$csrc]; |
| 85 |
$dst = $col[$cdst]; |
| 86 |
$key = "$col[1]_$col[$cpt]_$src_$dst"; |
| 87 |
$time{$key} = $col[$ctime]; |
| 88 |
$pktcnt{$key}++; |
| 89 |
$seqno{$key} = $col[$cseq]; |
| 90 |
|
| 91 |
if ($force) { |
| 92 |
$fd = $opened{$key}; |
| 93 |
if (! $fd) { |
| 94 |
$opened{$key} = $fd = $fdcount++; |
| 95 |
open($fd, ">${ARGV}_$key"); |
| 96 |
} |
| 97 |
printf $fd "%0.6f\t%d\n", $col[$ctime], $col[$cseq]; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
# print the per-connection information to STDOUT |
| 103 |
@keys = sort (keys %time); |
| 104 |
foreach $key (@keys) { |
| 105 |
$t = $time{$key}; |
| 106 |
$s = $seqno{$key}; |
| 107 |
if ($s > $pktcnt{$key}) { |
| 108 |
$s = $pktcnt{$key}; |
| 109 |
} |
| 110 |
$maxtime = $t if ($t > $maxtime); |
| 111 |
$maxpkt += $s; |
| 112 |
printf("%s\t%0.6f\t%d\t%0.6f\t%0.6f\n", |
| 113 |
$key, $t, $s, $s/$t, 0.008 * $psize * $s/$t); |
| 114 |
} |
| 115 |
|
| 116 |
# print summary information to STDERR |
| 117 |
if ($maxtime > 0) { |
| 118 |
printf STDERR " %d\t%0.6f\t%s\t", |
| 119 |
$#keys+1, 0.008 * $psize * $maxpkt / $maxtime, $opt{'c'}; |
| 120 |
|
| 121 |
@keys = sort (keys %count); |
| 122 |
foreach $key (@keys) { |
| 123 |
print STDERR $key, $count{$key}, " "; |
| 124 |
} |
| 125 |
print STDERR "\n"; |
| 126 |
} |