Skip to main content

AWK Scripts for NS2 to process data from Trace Files

AWK Scripts are very good in processing the data from the log (trace files) which we get from NS2. If you want to process the trace file manually, here is the detail

Here is a sample of trace file from NS2 (However ns2 supports a new type of trace file also), but this post will make you understand the old trace format only.

r 0.030085562 _0_ MAC  --- 0 message 32 [0 ffffffff 1 800] ------- [1:255 -1:255 32 0]

r 0.030110562 _0_ RTR  --- 0 message 32 [0 ffffffff 1 800] ------- [1:255 -1:255 32 0]

s 1.119926192 _0_ RTR  --- 1 message 32 [0 0 0 0] ------- [0:255 -1:255 32 0]
AWK Scripts are very good in processing the data column wise. For example
the first column in the above trace file represents r, s which indicates receive, sent respectively. If we want to trace the entire r and s alone from this trace file we can represent it as $1
So
$1 represents ACTION
$2 Time
$3 Node ID
$4 Layer
$5 Flags
$6 seqno
$7 type
$8 Size
$14 Energy (if the network nodes includes EnergyModel)

To run the awk script in Linux,

gawk –f filename.awk filename.tr

So, it is necessary for the researchers to know the basics of awk scripts before they are used.
Here this post will let you know some of the scripts that were used to process the data (NB: all these codes were taken from various websites and you can refer those websites for further information).

To find the throughput of the Network

   1: BEGIN {
   2:        recvdSize = 0
   3:        startTime = 400
   4:        stopTime = 0
   5:   }
   6:    
   7:   {
   8:              event = $1
   9:              time = $2
  10:              node_id = $3
  11:              pkt_size = $8
  12:              level = $4
  13:    
  14:   # Store start time
  15:   if (level == "AGT" &;& event == "s" && pkt_size >= 512) {
  16:     if (time <; startTime) {
  17:              startTime = time
  18:              }
  19:        }
  20:    
  21:   # Update total received packets' size and store packets arrival time
  22:   if (level == "AGT" &;& event == "r" && pkt_size >= 512) {
  23:        if (time >; stopTime) {
  24:              stopTime = time
  25:              }
  26:        # Rip off the header
  27:        hdr_size = pkt_size % 512
  28:        pkt_size -= hdr_size
  29:        # Store received packet's size
  30:        recvdSize += pkt_size
  31:        }
  32:   }
  33:    
  34:   END {
  35:        printf("Average Throughput[kbps] = %.2f\t\t StartTime=%.2f\tStopTime=%.2f\n",(recvdSize/(stopTime-startTime))*(8/1000),startTime,stopTime)
  36:   }

To print the Congestion window size



   1: BEGIN {
   2:  
   3: }
   4: {
   5: if($6=="cwnd_") {
   6:     printf("%f\t%f\n",$1,$7);
   7: }
   8: }
   9: END {
  10:  
  11: }

To print packet Delivery ratio



   1: BEGIN {
   2:         sendLine = 0;
   3:         recvLine = 0;
   4:         fowardLine = 0;
   5: }
   6:  
   7: $0 ~/^s.* AGT/ {
   8:         sendLine ++ ;
   9: }
  10:  
  11: $0 ~/^r.* AGT/ {
  12:         recvLine ++ ;
  13: }
  14:  
  15: $0 ~/^f.* RTR/ {
  16:         fowardLine ++ ;
  17: }
  18:  
  19: END {
  20:         printf "cbr s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine, (recvLine/sendLine),fowardLine;
  21: }
  22:  

AWK Script for calculating the Send, Received, Dropped Packets, Received Packets, Packet Delivery Ratio and Average end to End Delay



   1: BEGIN {
   2: seqno = -1; 
   3: droppedPackets = 0;
   4: receivedPackets = 0;
   5: count = 0;
   6: }
   7: {
   8: #packet delivery ratio
   9: if($4 == "AGT" &;& $1 == "s" && seqno < $6) {
  10: seqno = $6;
  11: } else if(($4 == "AGT") && ($1 == "r")) {
  12: receivedPackets++;
  13: } else if ($1 == "D" && $7 == "tcp" && $8 > 512){
  14: droppedPackets++; 
  15: }
  16: #end-to-end delay
  17: if($4 == "AGT" &;& $1 == "s") {
  18: start_time[$6] = $2;
  19: } else if(($7 == "tcp") && ($1 == "r")) {
  20: end_time[$6] = $2;
  21: } else if($1 == "D" && $7 == "tcp") {
  22: end_time[$6] = -1;
  23: }
  24: }
  25:  
  26: END { 
  27: for(i=0; i<=seqno; i++) {
  28: if(end_time[i] >; 0) {
  29: delay[i] = end_time[i] - start_time[i];
  30: count++;
  31: }
  32: else
  33: {
  34: delay[i] = -1;
  35: }
  36: }
  37: for(i=0; i<count; i++) {
  38: if(delay[i] >; 0) {
  39: n_to_n_delay = n_to_n_delay + delay[i];
  40: } 
  41: }
  42: n_to_n_delay = n_to_n_delay/count;
  43: print "\n";
  44: print "GeneratedPackets = " seqno+1;
  45: print "ReceivedPackets = " receivedPackets;
  46: print "Packet Delivery Ratio = " receivedPackets/(seqno+1)*100
  47: "%";
  48: print "Total Dropped Packets = " droppedPackets;
  49: print "Average End-to-End Delay = " n_to_n_delay * 1000 " ms";
  50: print "\n";
  51: }

Example: The following is a wireless network code, name it as a .tcl file and run it using “ns wireless.tcl” (without quotes), a trace file called wireless_mitf.tr will be created.



   1: set val(chan)           Channel/WirelessChannel    ;#Channel Type
   2: set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
   3: set val(netif)          Phy/WirelessPhy            ;# network interface type
   4: set val(mac)            Mac/802_11                 ;# MAC type
   5: set val(ifq)            Queue/DropTail/PriQueue    ;# interface queue type
   6: set val(ll)             LL                         ;# link layer type
   7: set val(ant)            Antenna/OmniAntenna        ;# antenna model
   8: set val(ifqlen)         50                         ;# max packet in ifq
   9: set val(nn)             2                          ;# number of mobilenodes
  10: set val(rp)             DSDV                       ;# routing protocol
  11: #set val(rp)             DSR                       ;# routing protocol
  12: set val(x)        500
  13: set val(y)        500
  14:  
  15: # Initialize Global Variables
  16: set ns_        [new Simulator]
  17: set tracefd     [open wireless_mitf.tr w]
  18: $ns_ trace-all $tracefd
  19:  
  20: set namtrace [open wireless_mitf.nam w]
  21: $ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
  22:  
  23: # set up topography object
  24: set topo       [new Topography]
  25:  
  26: $topo load_flatgrid $val(x) $val(y)
  27:  
  28: # Create God
  29: create-god $val(nn)
  30:  
  31: # New API to config node: 
  32: # 1. Create channel (or multiple-channels);
  33: # 2. Specify channel in node-config (instead of channelType);
  34: # 3. Create nodes for simulations.
  35:  
  36: # Create channel #1 and #2
  37: set chan_1_ [new $val(chan)]
  38: set chan_2_ [new $val(chan)]
  39:  
  40: # Create node(0) "attached" to channel #1
  41:  
  42: # configure node, please note the change below.
  43: $ns_ node-config -adhocRouting $val(rp) \
  44:         -llType $val(ll) \
  45:         -macType $val(mac) \
  46:         -ifqType $val(ifq) \
  47:         -ifqLen $val(ifqlen) \
  48:         -antType $val(ant) \
  49:         -propType $val(prop) \
  50:         -phyType $val(netif) \
  51:         -topoInstance $topo \
  52:         -agentTrace ON \
  53:         -routerTrace ON \
  54:         -macTrace ON \
  55:         -movementTrace OFF \
  56:         -channel $chan_1_ 
  57:  
  58: set node_(0) [$ns_ node]
  59:  
  60: # node_(1) can also be created with the same configuration, or with a different
  61: # channel specified.
  62: # Uncomment below two lines will create node_(1) with a different channel.
  63: #  $ns_ node-config \
  64: #         -channel $chan_2_ 
  65: set node_(1) [$ns_ node]
  66:  
  67: $node_(0) random-motion 0
  68: $node_(1) random-motion 0
  69:  
  70: for {set i 0} {$i <; $val(nn)} {incr i} {
  71:     $ns_ initial_node_pos $node_($i) 20
  72: }
  73:  
  74: #
  75: # Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
  76: #
  77: $node_(0) set X_ 5.0
  78: $node_(0) set Y_ 2.0
  79: $node_(0) set Z_ 0.0
  80:  
  81: $node_(1) set X_ 8.0
  82: $node_(1) set Y_ 5.0
  83: $node_(1) set Z_ 0.0
  84:  
  85: #
  86: # Now produce some simple node movements
  87: # Node_(1) starts to move towards node_(0)
  88: #
  89: $ns_ at 3.0 "$node_(1) setdest 50.0 40.0 25.0"
  90: $ns_ at 3.0 "$node_(0) setdest 48.0 38.0 5.0"
  91:  
  92: # Node_(1) then starts to move away from node_(0)
  93: $ns_ at 20.0 "$node_(1) setdest 490.0 480.0 30.0" 
  94:  
  95: # Setup traffic flow between nodes
  96: # TCP connections between node_(0) and node_(1)
  97:  
  98: set tcp [new Agent/TCP]
  99: $tcp set class_ 2
 100: set sink [new Agent/TCPSink]
 101: $ns_ attach-agent $node_(0) $tcp
 102: $ns_ attach-agent $node_(1) $sink
 103: $ns_ connect $tcp $sink
 104: set ftp [new Application/FTP]
 105: $ftp attach-agent $tcp
 106: $ns_ at 3.0 "$ftp start" 
 107:  
 108: #
 109: # Tell nodes when the simulation ends
 110: #
 111: for {set i 0} {$i <; $val(nn) } {incr i} {
 112:     $ns_ at 30.0 "$node_($i) reset";
 113: }
 114: $ns_ at 30.0 "stop"
 115: $ns_ at 30.01 "puts \"NS EXITING...\" ; $ns_ halt"
 116: proc stop {} {
 117:     global ns_ tracefd
 118:     $ns_ flush-trace
 119:     close $tracefd
 120: }
 121:  
 122: puts "Starting Simulation..."
 123: $ns_ run

Run the tracefile (wireless_mitf.tr, which will be created when the above TCL program runs) as given in the screenshot.


Screenshot from 2013-03-24 22^%04^%58



gawk –f pdf.awk wireless_mitf.tr


The above scripts were taken from http://ns2ultimate.com and http://elmurod.net and through search engines. But if you need any doubt about the scripts, just comment on the section below.

Comments

  1. error while loading shared libraries: libmwsgl.so: cannot open shared object file: No such file or directory
    i am facing this error at installation time. in the solution they said check the file path but i already give the path location still its not working .. please help me and if there is an other method of using trace file please tell em

    ReplyDelete
    Replies
    1. Did u set the LD_LIBRARY_PATH, you have to set it in the .bashrc file (ubuntu and fedora) and in linux mint (.profile) file.
      you have set the path like this
      export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/pradeepkumar/tracegraph202/bin/glnx86
      Change the username of your machine (my username is pradeepkumar), even you can give the above path inside the shell also.

      Delete
  2. sir could you help me with awk script for hop count calculation.

    ReplyDelete
  3. sir how can we induce selfishness in nodes

    ReplyDelete
  4. I am using the above file for calculation of throughput , but when i am running this for .tr files generated by AODV and LAR . Start time of lar is different than AODV , this is due to packet size parameter. if i remove this from awk file then throughput is different. even i am giving same simulation time in both the tcl files .
    Could you please help me in this regard


    could you please help me and tell me which file is to be used .

    ReplyDelete
  5. can anyone please post the awk script for calculating the goodput?

    ReplyDelete
  6. Respected Sir,
    please help me regarding the calculation of overhead
    Link of the trace file is given
    Trace format- new trace file
    https://drive.google.com/file/d/1kz8kMFey5Q6mI6cbSYhwdx0Om8k7
    Also, I have tried to generate control packets by using commands
    cat out.tr |grep "^RTR." | wc -l
    but it gives result 0
    Please help

    ReplyDelete
  7. simulate a network consisting of tcp and udp traffic to calculate throughput using awk script with five nodes

    ReplyDelete

Post a Comment

Popular posts from this blog

Routing in VANETs using ns3

Part 1
WAVE - Wireless Access for Vehicular environments. vanet-routing-compare.cc It might take more than an hour. The readers are requested to be patient. SUMO, VANETs, routing comparison 1. Explain the full source code (1550 lines of code) 2. Creating a real scenario using osm (Open Street Map Web Wizard) 3. Performance analysis for various vanet protocols. https://www.nsnam.com Location of the source code /home/pradeepkumar/ns-allinone-3.27/ns-3.27/src/wave/examples
Move this file to the scratch/ for inclusion of all modules. Step 1: Explanation of source code. Copy the file to scratch folder. This is just Part 1 of the VANET comparison

Part 2 Please watch the First Part before watching this video   https://www.youtube.com/watch?v=IJYeIpUqjQI&t=850s
Part 2 - Analysis of the results.
Please go through the first video (Part 1) and then watch this video (PArt 2)
#VANETs #NS3 #Routing
1. SUMO for web traffic (osmWebWizard.py) 2. Convert this into mobility.tcl file and t…

ns3 installation in Ubuntu 16.04

This post serves the installation instructions of ns3 in ubuntu 16.04 version. Some of my students are working in ns3, this post will benefit them in installing ns3.

OS Used: Ubuntu 16.04.4
ns3 version: ns3 version 3.27

The same procedure will be applied for OS like Debian, Linux Mint.

Fresh installation of Ubuntu 16.04 Let you try the fresh installation of ubuntu in your hard disk along with windows.
Installation of ns3 dependencies ns3 needs so many dependencies, developmental libraries, drivers, etc. so install all those
$] sudo apt update  $] sudo apt upgrade
$] sudo apt-get install build-essential autoconf automake libxmu-dev python-pygoocanvas python-pygraphviz cvs mercurial bzr git cmake p7zip-full python-matplotlib python-tk python-dev python-kiwi python-gnome2 python-gnome2-desktop-dev python-rsvg qt4-dev-tools qt4-qmake qt4-qmake qt4-default gnuplot-x11 wireshark
The above command make take some time to download, compile and install it, Be Patient. 
Installing ns3 Download the…

TORA Protocol in NS-2.35 (NS2)

This post tells you how to enable the TORA (Temporally ordered routing Algorithm) protocol in Network Simulator 2 (ns-2.35)

TORA is a protocol in wireless adhoc networks that works with timing parameters. NS-2.35 comes with the TORA protocol by default but it has to be tweaked manually to make it run.
This post will help you to do that.

You can watch this video for detailed instructions:


Step 1: Generate a Scenario for TORA protoco using NS2 Scenario Generator NSG Software.
We have created a tcl file using NSG2.1.jar

$] java -jar NSG2.1.jar

Three files have to be modified
~ns-2.35/tora/tora.cc~ns-2.35/tora/tora.h~ns-2.35/imep/imep.cc There are various websites that tells you how to configure TORA by making changes to the above three files.  Change 1: tora.h In the tora.h file, go to the end of the File before the agent completes, include these two lines

#include <classifier/classifier-port.h>
protected: PortClassifier *dmux_;

Change 2: tora.cc Open the tora.cc and include the follow…