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
  8. M 2.00000 0 (0.00, 0.00, 0.00), (10.00, 10.00), 100.00
    M 2.00000 1 (0.00, 0.00, 0.00), (150.00, 100.00), 150.00
    s 10.000000000 0 10.00 10.00 0 0 0 AGT --- 0 cbr 210 [0 0 0 0] ------- [0:0 1:0 32 0] [0] 0 0
    r 10.000000000 0 10.00 10.00 0 0 0 RTR --- 0 cbr 210 [0 0 0 0] ------- [0:0 1:0 32 0] [0] 0 0
    s 10.000000000 0 10.00 10.00 0 0 0 RTR --- 0 AODV 48 [0 0 0 0] ------- [0:255 -1:255 30 0] [0x2 1 1 [1 0] [0 4]] (REQUEST)
    s 10.000535000 0 10.00 10.00 0 0 0 MAC --- 0 AODV 106 [0 ffffffff 0 800] ------- [0:255 -1:255 30 0] [0x2 1 1 [1 0] [0 4]] (REQUEST)
    r 10.001383555 1 150.00 100.00 0 0 0 MAC --- 0 AODV 48 [0 ffffffff 0 800] ------- [0:255 -1:255 30 0] [0x2 1 1 [1 0] [0 4]] (REQUEST)
    r 10.001408555 1 150.00 100.00 0 0 0 RTR --- 0 AODV 48 [0 ffffffff 0 800] ------- [0:255 -1:255 30 0] [0x2 1 1 [1 0] [0 4]] (REQUEST)
    s 10.001408555 1 150.00 100.00 0 0 0 RTR --- 0 AODV 44 [0 0 0 0] ------- [1:255 0:255 30 0] [0x4 1 [1 4] 10.000000] (REPLY)
    s 10.002023555 1 150.00 100.00 0 0 0 MAC --- 0 ARP 86 [0 ffffffff 1 806] ------- [REQUEST 1/1 0/0]
    r 10.002712110 0 10.00 10.00 0 0 0 MAC --- 0 ARP 28 [0 ffffffff 1 806] ------- [REQUEST 1/1 0/0]
    s 10.003087110 0 10.00 10.00 0 0 0 MAC --- 0 RTS 44 [36b 1 0 0]
    r 10.003439664 1 150.00 100.00 0 0 0 MAC --- 0 RTS 44 [36b 1 0 0]
    s 10.003449664 1 150.00 100.00 0 0 0 MAC --- 0 CTS 38 [231 0 0 0]
    s 10.003750000 0 10.00 10.00 0 0 0 AGT --- 1 cbr 210 [0 0 0 0] ------- [0:0 1:0 32 0] [1] 0 0
    r 10.003750000 0 10.00 10.00 0 0 0 RTR --- 1 cbr 210 [0 0 0 0] ------- [0:0 1:0 32 0] [1] 0 0
    r 10.003754219 0 10.00 10.00 0 0 0 MAC --- 0 CTS 38 [231 0 0 0]


    in above old trace format

    After Node ID column and before LEVEL column " 10.00 10.00 0 0 0 " fields added.

    I do not have any idea how to deal with this.

    ReplyDelete

Post a Comment

Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools 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 gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libclang-dev llvm-

Installation of NS2 (ns-2.35) in Ubuntu 20.04

Installation of NS2 (ns-2.35) in Ubuntu 20.04 LTS Step 1: Install the basic libraries like      $] sudo apt install build-essential autoconf automake libxmu-dev Step 2: install gcc-4.8 and g++-4.8 open the file using sudo mode $] sudo nano /etc/apt/sources.list Include the following line deb http://in.archive.ubuntu.com/ubuntu bionic main universe $] sudo apt update $] sudo apt install gcc-4.8 g++-4.8 Step 3:  Unzip the ns2 packages to home folder $] tar zxvf ns-allinone-2.35.tar.gz $] cd ns-allinone-2.35/ns-2.35 Modify the following make files. ~ns-2.35/Makefile.in Change @CC@ to gcc-4.8 change @CXX@ to g++-4.8 ~nam-1.15/Makefile.in ~xgraph-12.2/Makefile.in ~otcl-1.14/Makefile.in Change in all places  @CC@ to gcc-4.8 @CPP@ or @CXX@ to g++-4.8 open the file: ~ns-2.35/linkstate/ls.h Change at the Line no 137  void eraseAll() { erase(baseMap::begin(), baseMap::end()); } to This void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); } All changes made Step 4: Open a new termi

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb http://in.archive.ubuntu.com/ubuntu/ bionic main universe $ sudo apt update Since, it&#