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 https://ns2ultimate.com and https://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. Hello sir, I got this error please

      gawk: cmd. line:1: –f
      gawk: cmd. line:1: ^ invalid char '�' in expression

      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

Installing ns-3.34 in Ubuntu 20.04

This post shows how to install ns 3.34 in Ubuntu 20.04 LTS Prerequisites: Fresh installation of Ubuntu Version 20.04 LTS  ns3.34 can be downloaded from here Follow the video link for complete step by step instructions on the installation.  This version fixes the compilation issues of vanet-routing-compare.cc (bug in ns3.33)  Issue the following commands after opening a terminal  $ sudo apt update $ sudo apt install g++ python3 python3-dev python-dev pkg-config sqlite3 python3-setuptools git qt5-default gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar openmpi-bin openmpi-common openmpi-doc libopenmpi-dev tcpdump wireshark libxml2 libxml2-dev Unzip or untar the ns-allinone-3.34.tar.bz2 in the home folder (in my case its /home/pradeepkumar) $ cd ns-allinone-3.34/ $ ./build.py --enable-examples --enable-tests  Once the installation is completed, you may get an output show

Installation of ns3 in Windows 10 and Windows 11 OS using WSL (Windows Subsystem for Linux)

This post shows how to install ns-3.33 in Windows 10 through WSL (Windows Subsystem for Linux) This posts works for Windows 11 also (I have tested it on a Windows 11 ISO and it works the Same way as mentioned in the following post.) This post will work for ns-3.3x version. Prerequisites : Install Windows Subsystem for Linux with GUI: Please refer the following video  System Information: OS used: Windows 10 and WSL (Ubuntu 20.04) GUI: XServer for Windows NS3 Version: ns-3.33 See the following complete video on how to install ns3 in Windows 10 Step 0 : Open XLaunch Step 1 :  Open WSL using PowerShell and open it as Administrator Command:/  wsl $ xfce4-session The GUI of Ubuntu Opens within Windows 10 OS. Step 2 : Download ns3 from nsnam.org website through Mozilla Firefox browser Step 3: Open a Terminal  $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-q

Installing NS-3.32 in Ubuntu 20.04

This is about installing ns version 3.32 in Ubuntu 20.04 LTS. #ns3 #ns3 .32 #networksimulation The commands used in the video are given here. $] sudo apt update $] sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-qmake qt5-default gnuplot-x11 wireshark Download the ns-allinone-3.32.tar.bz2 package from nsnam.org and copy it to /home/ folder See the full video for detailed instructions Extract it either in GUI or using command $] tar jxvf ns-allinone-3.32.tar.bz2 $] cd ns-allinone-3.32/ $] ./build.py --enable-examples --enable-tests The above command will take some time to install all the packages  You can see the output as shown below ns3 To check whether ns3 installed successfully, use the following commands. $] cd ns-3.32/ $] ./waf --run hello-simulator You should get the output as Hello Simulator $] ./waf --run first This is the example from the ns-3.32/exa