LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How can I use awk scripts to find the end-to-end delay from the .tr file? (https://www.linuxquestions.org/questions/linux-software-2/how-can-i-use-awk-scripts-to-find-the-end-to-end-delay-from-the-tr-file-4175541892/)

viethd92 05-07-2015 12:18 PM

How can I use awk scripts to find the end-to-end delay from the .tr file?
 
I have finished my .tcl file for the wireless sensor network.
I tried to get the results from the .tr file using the awk scripts, but I did not get anything. Each time it gave an error "division by zero". Please can anyone help?
I have the .tr file and awk script below

.tr file:
https://docs.google.com/file/d/0B3rL...lPVDNFcVk/edit
awk script:
Code:

# ===================================================================

# AWK Script for calculating:

#    => Average End-to-End Delay.

# ===================================================================

 

BEGIN {

    seqno = -1;   

#    droppedPackets = 0;

#    receivedPackets = 0;

    count = 0;

}

{

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

          seqno = $6;

    }
#        else if(($4 == "AGT") && ($1 == "r")) {

#            receivedPackets++;

#    } else if ($1 == "D" && $7 == "tcp" && $8 > 512){

#            droppedPackets++;           

#  }

    #end-to-end delay

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

          start_time[$6] = $2;

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

        end_time[$6] = $2;

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

          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 "GeneratedPackets            = " seqno+1;

#    print "ReceivedPackets            = " receivedPackets;

#    print "Packet Delivery Ratio      = " receivedPackets/(seqno+1)*100
#"%";

#    print "Total Dropped Packets = " droppedPackets;

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

    print "\n";

}

Thank you very much.

bigrigdriver 05-07-2015 02:57 PM

What I see in your code:
1) count set to 0
Code:

count = 0;
2) count is supposed to be incremented here:
Code:

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

          if(end_time[i] > 0) {

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

                  count++;

3) division by value of count here (apparently not incremented over the initial value of zero):
Code:

n_to_n_delay = n_to_n_delay/count;
For whatever reason, step 2 is not being processed. Try stepping though your code to find out why.

knudfl 05-07-2015 03:36 PM

Besides the information from @bigrigdriver :


Not all the old awk scripts are actually usable as is with ns2 / any random trace file.
All "end to end" scripts :
en2end-delay.awk
end-to-end.awk
Endtoenddelay.awk
End-to-End-Delay.awk
delay-e2e.awk
e2e.awk
e2edelay.awk
... The scripts are available from `awk-and-perl_scripts_12.2014.tar.gz'
https://drive.google.com/file/d/0B7S...ew?usp=sharing


-

viethd92 05-08-2015 12:10 PM

thanks for your help, i used file ''perl"
Code:

#!/usr/bin/perl
#
#Program to calculate end to end delay.
#
#1. Program reads the trace file, finds for combination of 's' source event and 'AGT' packet type.
#2. For this combination, the program takes the packet id and timestamp.
#3. Within the same file, the program searches for a combination of 'r' receive event and 'AGT' packet type.
#4. For this combination, the program takes the packet id and compares with previous packet id. If they are same, timestamp is noted.
#5. Difference between step 4 timestamp and step 3 timestamp gives the end to end delay for that packet.
#6. Delays are aggregated and average delay is found.
#

#use strict;
use warnings;

$SIG{INT} = sub {
        my (@stack, $level);
        while(1)
        {
                my ($pkg, $fn, $ln, $sub) = caller($level++);
                if (!($pkg or $fn or $ln or $sub)) {
                        for (my $i = 0; $i < @stack; $i++) {
                                print " " x $i, $stack[$i], "\n";
                        }
                        exit;
                }
                unshift @stack, "-> $pkg: $fn (line $ln) sub $sub";
        }
};

#Input trace file
my($infile) =$ARGV[0];

#Keep track of variables
my($enqueue_time) = 0;
my($receive_time) = 0;
my($packet_id) = 0;
my($delay) = 0;
my($total_receive_count) = 0;
my($sum_of_delay) = 0;
my($average_delay) = 0;
my($simulation_time) = 0;
my($file_position) = 0;
my (@x);

open(DATA,"<","$infile" ) || die "could't open $infile$!";

while(<DATA>)
{
        @x=split(' ');
        if(($x[0] eq 's') && ($x[3] eq 'AGT'))
        {
                $file_position = tell(DATA);
                $enqueue_time = $x[1];
                $packet_id = $x[5];
                while(<DATA>)
                {
          @x=split(' ');
                        if(($x[0] eq 'r') && ($x[3] eq 'AGT'))
                        {
                                if(($x[5] == $packet_id))
                                {
                                        $receive_time = $x[1];
                                        $total_receive_count++;
                                        $delay = $receive_time - $enqueue_time;
                                        $sum_of_delay = $sum_of_delay + $delay;

                                        #Following is for debug.
                                        $delay = $delay * 1000;
                                        print("\nDelay:$delay");

                                        last;
                                }
                        }
                }
                #Continue to search for next 's' event from where the previous 's' was found.
                #So move to the same line where previous 's' event was found.
        seek(DATA,$file_position,SEEK_SET);
        }
        #While(<DATA>) takes care of moving to the next line.
}

$simulation_time = $x[1];

print("\n Simulation Time = $simulation_time seconds");
print("\n Total Receive Count = $total_receive_count");

if($total_receive_count != 0 )
{
        $average_delay = $sum_of_delay / $total_receive_count;
        $average_delay = $average_delay * 1000;
        print("\n Average End to End Delay = $average_delay milliseconds");
}
else
{
        print("\n No packet received.");
}

print("\n");
print("\n");
close DATA;
exit(0);

and i type
Code:

perl avgdelay.pl leach.tr
Is my result correct?
Code:

Simulation Time = 276.70000 seconds
 Total Receive Count = 28152
 Average End to End Delay = 38.5367725412756 milliseconds


viethd92 05-09-2015 09:13 PM

There is not a packet to be dropped in my trace file. Is it normal?

knudfl 05-10-2015 01:44 AM

Re #5.

You can do extra checks with the other available scripts, e.g. :
droppackets.awk
packetloss.pl
packet-loss-ratio.awk
packetloss.sh
totalpacketsreceived.awk
totalpacketssent.awk
PacketDeliveryRatio.awk

? I don't know if your script is meant to show "packet to be dropped in trace .." ?
Or if your trace file shows any packet drops ?


-

viethd92 05-10-2015 04:23 AM

I use command:
Code:

ns ns-2.34/tcl/ex/wireless.tcl -sc ns-2.34/mit/uAMPS/sims/nodescen -rp leach -x 100 -y 100 -nn 101 -stop 600 -eq_energy 1 -init_energy 1 -filename LEACH1 -dirname leach_dir -topo ns-2.34/mit/uAMPS/sims/100nodes.txt -num_clusters 5 -bs_x 50 -bs_y 50 2> leach_dir/leach.err > leach_dir/leach.out
And in my trace file, There is not a 'd' label (in 1st column)
https://docs.google.com/file/d/0B3rL...tkM004S0k/edit
Is correct ?

knudfl 05-10-2015 04:46 AM

Re #7.

Please compare with other examples of "leach.tr".

This one is with a default ./test , i.e. default settings in leach_test
https://drive.google.com/file/d/0B7S...ew?usp=sharing


-

viethd92 05-10-2015 11:41 AM

There is not a difference between my trace file and your trace file in #8. Awk files in #6 don't have a result which i want. Why does it like this? Help me!

knudfl 05-10-2015 01:25 PM

Re #9.

I don't know. Sorry.

viethd92 05-11-2015 12:02 AM

Can u show me the meaning of fields in trace file?
Code:

s 11.007514174 _11_ AGT  --- 968 rca 4016 [0 b000000 0 0] ------- [s 11 59 -1]
r 11.007514175 _35_ AGT  --- 963 rca 304 [0 3b000000 ffff0008 0] ------- [D 59 59 -1]

thanks for your help!

knudfl 05-11-2015 01:45 AM

Re #11.

NS-2 Trace Formats :
http://nsnam.isi.edu/nsnam/index.php/NS-2_Trace_Formats

viethd92 05-12-2015 03:42 AM

Dear knudfl
Can u use awk files in #6 to run with your trace file (#8) and give me your result? Please.

hbenlabbes 12-20-2015 06:40 AM

Energy trace for each node (WSN) in perl or awk
 
any one, have Energy trace for each node (WSN) in perl or awk for trace file tr
Thanks.


All times are GMT -5. The time now is 08:55 AM.