Sunday, June 16, 2013

AWK to analyze trace files

In this post I am going to present analyzing NS-2 trace file with AWK script. For this, let consider the trace file obtained from the post "Creating Broadcast agents in NS-2". AWK script consists of three blocks BEGIN - where global variables are placed, logic - which are enclosed in simple flower braces ({}) for writing the logic and END - to print the results obtained by running logic.

As the output of simulation are placed in the trace file in column wise. We need to read the column to analyse the results.

let name the following file as bcast.awk


BEGIN {
rsent[4] = 0; #array to hold router sent packets
msent[4] = 0; #array to hold mac sent packets
rrecv[4] = 0; #array to hold router receive packets
mrecv[4] = 0; #array to hold mac receive packets
}
{
#column 1 of trace file representing event such as send, receive etc
event = $1;
#column 3 of trace file represent node number
node = $3;
#column 4 of trace file represent level - Router (RTR), Mac (MAC) etc
level = $4;
# increment the packet count if event is send and level is RTR
if(event == "s" && level == "RTR") {
if(node == "_0_")
rsent[0]++;
else if(node == "_1_")
rsent[1]++;
else if(node == "_2_")
rsent[2]++;
else if(node == "_3_")
rsent[3]++;
}
# increment the packet count if event is send and level is MAC
if(event == "s" && level == "MAC") {
if(node == "_0_")
msent[0]++;
else if(node == "_1_")
msent[1]++;
else if(node == "_2_")
msent[2]++;
else if(node == "_3_")
msent[3]++;
}
# increment the packet count if event is receive and level is RTR
if(event == "r" && level == "RTR") {
if(node == "_0_")
rrecv[0]++;
else if(node == "_1_")
rrecv[1]++;
else if(node == "_2_")
rrecv[2]++;
else if(node == "_3_")
rrecv[3]++;
}
# increment the packet count if event is receive and level is MAC
if(event == "r" && level == "MAC") {
if(node == "_0_")
mrecv[0]++;
else if(node == "_1_")
mrecv[1]++;
else if(node == "_2_")
mrecv[2]++;
else if(node == "_3_")
mrecv[3]++;
}
}
END {
# Print the result
for(i=0;i<4 b="" i="">
printf ("Routing Packets Sent by Node %d : %d \n",i,rsent[i]);
}
for(i=0;i<4 b="" i="">
printf ("Routing Packets Received by Node %d : %d \n",i,rrecv[i]);
}
for(i=0;i<4 b="" i="">
printf ("Mac Packets Sent by Node %d : %d \n",i,msent[i]);
}
for(i=0;i<4 b="" i="">
printf ("Mac Packets Received by Node %d : %d \n",i,mrecv[i]);
}
}

Save and close file

run the file as follows

$ awk -f bcast.awk out.tr

and see the result


No comments: