`

NS by Example 笔记(6)Trace Analysis Example

阅读更多

Trace Analysis Example

 

 

#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 out.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

 

 

代码演示了如何打开一个追踪文件并写入追踪信息。 代码建立的模拟场景可以参考Figure 4在"Simple Simulation Example"小节中。



 

Example 4. Trace Enabled Simple NS Simulation Script (modified from Example 3)

 

运行上面的脚本会生成一个NAM trace文件被NAM工具用来输入信息和一个名叫"out.tr"的trace 文件用于模拟分析。 Figure 13 说明了trace信息的格式和"out.tr"中trace数据的样例。




 
Figure 13. Trace Format Example

 

每行trace信息以事件描述符(+, -, d, r)开头、 紧接着是事件的模拟时间(秒为单位)、和起始与目的节点,这些用来标明事件是在那个连接上发生的。 "Network Components"小节中的Figure 9说明了在一个连接的哪个地方每个类型的事件被追踪。 flags项之前 (表示为"------")是数据包类型和大小(in Bytes)。 目前, NS只执行Explicit Congestion Notification (ECN 明显拥挤通告位) bit, 并没用到其余的位(remaining bits are not used)。 IPv6的flow id (fid) 可以设置给每个流flow在输入OTcl脚本。即使fid field在模拟中没有用到, 也可以把它用作分析。fid field还在为NAM的显示图像指定stream color时使用。source和destination地址项形式上是"node.port". 下一项表示网络层协议的数据包的序列号。即使UDP不使用seq, NS也保持了对UDP packet sequence number的追踪作为分析目的。最后一项表示数据包unique id。

 

有了模拟追踪的数据后,需要做的就剩下转换感兴趣的数据为容易理解的信息并对他进行分析。 下面是一个数据转换的例子。它使用一个用perl编写的命令叫"column",这个命令把几列数据从输入数据中选出来。 运行这个例子需要下载"column"并使它可执行(i.e. "chmod 755 column")。 然后是tunneled shell command加上awk, 它将计算CBR traffic jitter在接收节点(n3) 使用"out.tr"中的数据, 然后保存结果到"jitter.txt".

 

 

cat out.tr | grep " 2 3 cbr " | grep ^r | column 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

 

 

($1带入了Figure 13 format中的time field的值,$2带入seq field的值,$1带指前面的column 1,$2带指前面的column 10。根据Figure 13的格式从0-11个项,则每行中第1个列column 1是time field,第10个列是seq field)

这个shell command 选中"CBR packet receive"类型为CBR的包在节点n3上, 选中次数(column 1)和sequence number序号 (column 10), 然后为每个序号计算和上一个数据包在接收时间的不同,然后除以2个包在seq上的不同。 下面是相应的振动波图(jitter graph)用 gnuplot 生成。X 轴表示数据包的sequence number,Y 轴表示模拟时间(in seconds.)。




 
Figure 14. CBR Jitter at The Receiving Node (n3)

 

Example Utilities小节将介绍更多的工具。

 

本节演示了在NS中如何生成追踪数据、如何解析数据、并如何得到可用的信息从追踪数据中。这个例子中, 在模拟后post simulation处理在命令行中进行。 下一节将会讲到如何将这些处理也可以被写在OTcl脚本中input。 

 

 

 

 

 

 

 

  • 大小: 3.3 KB
  • 大小: 5.2 KB
  • 大小: 8.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics