Monday 23 February 2015

awk scripts for analysis of trace files in ns2

awk scripts for analysis of trace files in ns2


Packet delivery ratio

awk script

BEGIN {
        sendLine = 0;
        recvLine = 0;
        fowardLine = 0;
}
 
$0 ~/^s.* AGT/ {
        sendLine ++ ;
}
 
$0 ~/^r.* AGT/ {
        recvLine ++ ;
}
 
$0 ~/^f.* RTR/ {
        fowardLine ++ ;
}
 
END {
        printf "s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine, (recvLine/sendLine),fowardLine;
}


End to end delay
awk script 

BEGIN {

    seqno = -1;    


    count = 0;

}

{

    if($4 == "AGT" && $1 == "s" && seqno < $6) {

          seqno = $6;

    } 

    #end-to-end delay

    if($4 == "AGT" && $1 == "s") {

          start_time[$6] = $2;

    } else if(($7 == "tcp"||$7=="cbr") && ($1 == "r")) {

        end_time[$6] = $2;

    } else if($1 == "D" && ($7 == "tcp"$7=="cbr") {

          end_time[$6] = -1;

    } 

}

 
END {        
  
    for(i=0; i<=seqno; i++) {

          if(end_time[i] > 0) {

              delay[i] = end_time[i] - start_time[i];

                  count++;

        }

            else

            {

                  delay[i] = -1;

            }

    }

    for(i=0; i<=seqno; i++) {

          if(delay[i] > 0) {

              n_to_n_delay = n_to_n_delay + delay[i];

        }         

    }

   n_to_n_delay = n_to_n_delay/count;

 

    print "\n";

    print "Average End-to-End Delay    = " n_to_n_delay * 1000 " ms";

    print "\n";

}

Control Overhead(Normalized routing overhead)
Note: Add control packets in the (routing packet)conditions as per your protocol 
awk script
BEGIN{
recvd = 0;#################### to calculate total number of data packets received
rt_pkts = 0;################## to calculate total number of routing packets received
}

{
##### Check if it is a data packet
if (( $1 == "r") && ( $7 == "cbr" || $7 =="tcp" ) && ( $4=="AGT" )) recvd++;

##### Check if it is a routing packet
if (($1 == "s" || $1 == "f") && $4 == "RTR" && ($7 =="udp" || $7 == "AODV" || $7 =="message" || $7 =="AOMDV" || $7 =="OLSR")) rt_pkts++;

}


END{
printf("##################################################################################\n");
printf("\n");
printf("total no of data packets\t%d\n",recvd);
printf("\ntotal no of routing packets\t%d\n",rt_pkts);
printf("                       Normalized Routing Load = %.3f\n", rt_pkts/recvd);
printf("\n");
printf("##################################################################################\n");
}

Throughput
Note: Change packet size as per mentioned in your tcl script 
awk script

BEGIN {
       recvdSize = 0
       startTime = 400
       stopTime = 0
  }
   
  {
             event = $1
             time = $2
             node_id = $3
             pkt_size = $8
             level = $4
   
  # Store start time
  if (level == "AGT" && event == "s" && pkt_size >= 512) {
    if (time < startTime) {
             startTime = time
             }
       }
   
  # Update total received packets' size and store packets arrival time
  if (level == "AGT" && event == "r" && pkt_size >= 512) {
       if (time > stopTime) {
             stopTime = time
             }
            recvdSize += pkt_size
       }
  }
   
  END {
       printf("Average Throughput[kbps] = %.2f\t\t StartTime=%.2f\tStopTime=%.2f\n",(recvdSize/(stopTime-startTime))*(8/1000),startTime,stopTime)
  }

How to apply awk?
awk -f pdr.awk out.tr