本篇中的代码主要来自[1],计算接收端数据包的时延抖动jitter,作为学习过程,记录一下。
ns2中tracer文件中数据各行的含义如图:
simple_tracer.tcl
#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Open the Trace file
set tf [open jitter.tr w]
$ns trace-all $tf
#Define a 'finish' procedure
proc finish {} {
global ns nf tf
$ns flush-trace
#Close the NAM trace file
close $nf
#Close the Trace file
close $tf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns run
执行命令仿真,./ns simple_tracer.tcl ,数据输出文件jitter.tr。
执行脚本,检出数据./jitter.sh
#! /bin/sh -f
cat jitter.tr | grep " 2 3 cbr " | grep ^r | ./column.perl 1 10 | awk '{dif = $2 - old2; if(dif==0) dif = 1; if(dif > 0) {printf("%d\t%f\n", $2, ($1 - old1) / dif); old1 = $1; old2 = $2}}'> jitter.txt
其中,数据分析脚本column.perl,从数据输出文件中检出第一行(时间)与第十行(数据包序号)。
#!/usr/bin/perl
# Mark Claypool
# Last significantly modified: December, 2001
# This program prints out fields of an indicated column.
# The columns are numbered 0, 1, 2, 3 ...
require 'getopts.pl';
&ParseCommandLine;
$line = <STDIN>;
while ($line) {
$line =~ s/^\s+//; # remove initial white-space
$line =~ s/\s+/ /g; # turn double-space into single space
@word = split($split,$line); # columns will then be $1, $2, $3 ...
$i =0;
while ($i <= $#col) {
print "@word[@col[$i]]\t";
$i += 1;
}
print "\n";
$line = <STDIN>;
}
exit;
#######################################################################
# ParseCommandLine
# check for the right number of command line arguments
# print usage message and quit if there is an error
# global variables are @col
sub ParseCommandLine {
# get token to split on
&Getopts('t:');
if ($opt_t) {
$split = $opt_t;
} else {
$split = '\s+';
}
# get columns
while ($#ARGV >= 0) {
$arg = shift(@ARGV);
if ($arg =~ /^(\d+)/) {
push(@col, $1);
} else {
&usage;
}
}
if ($#col < 0) {
&usage;
}
}
##########################################################################
# usage
# print a usage maessage and quit
sub usage
{
print STDERR "column: print fields from an indicated column\n";
print STDERR "Usage: column <flags>, where flags are:\n";
print STDERR " [-t str]\ttoken to use as a separator (default is white space)\n";
print STDERR " {# [#...]}\tcolumn(s) to print, numbered 0,1,2...\n";
exit;
}
遇见错误,则给予相应文件的执行权限,chmod 775 column.perl。最终的输出数据在jitter.txt。
采用gnuplot画图。命令如下;
gnuplot
set yrange [0:0.02]
set xlabel "seq"
set ylabel "jitter"
plot "jitter.txt" using 1:2 with lines
set term jpeg
set output "jitter.jpeg"
replot
最终的图如下: