//
you're reading...
basic

Trace Analysis Example


This section shows a trace analysis example. Example 4 is the same OTcl script as the one in the “Simple Simulation Example” section with a few lines added to open a trace file and write traces to it. For the network topology it generates and the simulation scenario, refer to Figure 4 in the “Simple Simulation Example” section. To run this script download “ns-simple-trace.tcl” and type “ns ns-simple-trace.tcl” at your shell prompt.

#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

Example 4. Trace Enabled Simple NS Simulation Script [ns-simple-trace.tcl] (modified from Example 3)

Running the above script generates a NAM trace file that is going to be used as an input to NAM and a trace file called “out.tr” that will be used for our simulation analysis. Figure 13 shows the trace format and example trace data from “out.tr”.

Trace Format Example

Trace Format Example

Figure 13. Trace Format Example

Each trace line starts with an event (+, -, d, r) descriptor followed by the simulation time (in seconds) of that event, and from and to node, which identify the link on which the event occurred. Look at Figure 9 in the “Network Components” section to see where in a link each type of event is traced. The next information in the line before flags (appeared as “——” since no flag is set) is packet type and size (in Bytes). Currently, NS implements only the Explicit Congestion Notification (ECN) bit, and the remaining bits are not used. The next field is flow id (fid) of IPv6 that a user can set for each flow at the input OTcl script. Even though fid field may not used in a simulation, users can use this field for analysis purposes. The fid field is also used when specifying stream color for the NAM display. The next two fields are source and destination address in forms of “node.port”. The next field shows the network layer protocol’s packet sequence number. Note that even though UDP implementations do not use sequence number, NS keeps track of UDP packet sequence number for analysis purposes. The last field shows the unique id of the packet.

Having simulation trace data at hand, all one has to do is to transform a subset of the data of interest into a comprehensible information and analyze it. Down below is a small data transformation example. This example uses a command written in perl called “column” that selects columns of given input. To make the example work on your machine, you should download “column” and make it executable (i.e. “chmod 755 column”). Following is a tunneled shell command combined with awk, which calculates CBR traffic jitter at receiver node (n3) using data in “out.tr”, and stores the resulting data in “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

This shell command selects the “CBR packet receive” event at n3, selects time (column 1) and sequence number (column 10), and calculates the difference from last packet receive time divided by difference in sequence number (for loss packets) for each sequence number. The following is the corresponding jitter graph that is generated using gnuplot. The X axis show the packet sequence number and the Y axis shows simulation time in seconds.

CBR Jitter at The Receiving Node (n3)

CBR Jitter at The Receiving Node (n3)

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

You might also check for more utilities in the Example Utilities section.

This section showed an example of how to generate traces in NS, how to interpret them, and how to get useful information out from the traces. In this example, the post simulation processes are done in a shell prompt after the simulation. However, these processes can be included in the input OTcl script, which is shown in the next section.


Source: http://nile.wpi.edu/NS/

Discussion

No comments yet.

Leave a comment