ns2仿真学习(二)-tcp拥塞窗口的跟踪

本文主要处理[1]的输出结果。

仿真脚本 basic1.tcl

# basic1.tcl simulation: A---R---B

#Create a simulator object
set ns [new Simulator]

#Open the nam file basic1.nam and the variable-trace file basic1.tr
set namfile [open basic1.nam w]
$ns namtrace-all $namfile
set tracefile [open basic1.tr w]
$ns trace-all $tracefile

#Define a 'finish' procedure
proc finish {} {
        global ns namfile tracefile
        $ns flush-trace
        close $namfile
        close $tracefile
        exit 0
}

#Create the network nodes
set A [$ns node]
set R [$ns node]
set B [$ns node]

#Create a duplex link between the nodes

$ns duplex-link $A $R 10Mb 10ms DropTail
$ns duplex-link $R $B 800Kb 50ms DropTail

# The queue size at $R is to be 7, including the packet being sent
$ns queue-limit $R $B 7

# some hints for nam
# color packets of flow 0 red
$ns color 0 Red
$ns duplex-link-op $A $R orient right
$ns duplex-link-op $R $B orient right
$ns duplex-link-op $R $B queuePos 0.5

# Create a TCP sending agent and attach it to A
set tcp0 [new Agent/TCP/Reno]
# We make our one-and-only flow be flow 0
$tcp0 set class_ 0
$tcp0 set window_ 100
$tcp0 set packetSize_ 960
$ns attach-agent $A $tcp0

# Let's trace some variables
$tcp0 attach $tracefile
$tcp0 tracevar cwnd_
$tcp0 tracevar ssthresh_
$tcp0 tracevar ack_
$tcp0 tracevar maxseq_

#Create a TCP receive agent (a traffic sink) and attach it to B
set end0 [new Agent/TCPSink]
$ns attach-agent $B $end0

#Connect the traffic source with the traffic sink
$ns connect $tcp0 $end0

#Schedule the connection data flow; start sending data at T=0, stop at T=10.0
set myftp [new Application/FTP]
$myftp attach-agent $tcp0
$ns at 0.0 "$myftp start"
$ns at 10.0 "finish"

#Run the simulation
$ns run
仿真运行  ./ns  basic1.tcl

数据输出,basic1.tr,数据处理脚本,检出cwnd的值。

cwnd.awk

BEGIN {
     cwnd_counter = 0;
}
{
time =$1;
key_type=$6; #cwnd_
value=$7;
if(key_type=="cwnd_")
{
	time_array[cwnd_counter]=time;
	cwnd[cwnd_counter]=value;
	cwnd_counter++;
}

}

END{
for(cwnd_index=0;cwnd_index<cwnd_counter;cwnd_index++)
{
printf("%f %f\n",time_array[cwnd_index],cwnd[cwnd_index]);
}

}
运行,awk -f cwnd.awk basic.tr>cwnd.txt。

采用gnuplot绘图:

gnuplot

set xlabel  "time"

set ylabel  “cwnd”

set yrange [0:30]

set term  png  

set output "cwnd.png"

plot "cwnd.txt" using 1:2  with lines

最后输出的图形:


找了个计算速率的脚本[2]:throughput.pl

# Usage: 
#
#   perl throughput.pl <trfile> <destNode> <srcNode.port#> <destNode.port#> 
#                   <granularity> 
#
# Example: plot the throughput of connection 2.0 - 1.0 every 0.5 sec
#
#	perl throughput.pl  out.tr  1 2.0 1.0  0.5
#
# cs558000 example:
#
#    Tracefile:
#       r 0.351867 3 4 tcp 40 ------- 0 0.0 4.0 0 0
#                    ^                  ^^^ ^^^
#    Use:
#	perl throughput.pl  out.tr  4 0.0 4.0  0.5

# #############################################################
# Get input file (first arg)
   $infile=$ARGV[0];

# #############################################################
# Get node that receives packets (second arg)
   $destnode=$ARGV[1];
   $fromport=$ARGV[2];
   $toport=$ARGV[3];

# #############################################################
# Get time granularity (time interval width
   $granularity=$ARGV[4];

# #########################################################################
# We compute how many bytes were transmitted during time interval specified
# by granularity parameter in seconds

   $sum=0;
   $grantsum=0;
   $clock=0;

# #########################################################################
# Open input file
   open (DATA,"<$infile") || die "Can't open $infile $!";
  
   while (<DATA>) 
   {

# Tokenize line using space as separator
      @x = split(' ');

# column 1 is time 
      if ($x[1]-$clock <= $granularity)
      {

# checking if the event (column 0) corresponds to a reception 
         if ($x[0] eq 'r') 
         { 

# checking if the destination (column 3) corresponds to node in arg 1
            if ($x[3] eq $destnode && $x[8] eq $fromport && $x[9] eq $toport) 
            { 
#checking if the packet type is TCP
               if ($x[4] eq 'tcp') 
               {
                  $sum=$sum+$x[5];
		  $grantsum=$grantsum+$x[5];
               }
            }
         }
      }
      else
# One interval has passed, compute throughput
      {   
         $throughput=0.000008*$sum/$granularity;
         print STDOUT "$clock $throughput\n";

         $clock=$clock+$granularity;

	 if ($x[0] eq 'r' && $x[3] eq $destnode && $x[8] eq $fromport 
	     && $x[9] eq $toport && $x[4] eq 'tcp')
         {
	    $sum=$x[5];
	    $grantsum=$grantsum+$x[5];
	 }
	 else
         {
	    $sum=0;
	 }

         while ($x[1]-$clock > $granularity)
         {
            print STDOUT "$clock 0.0\n";
            $clock=$clock+$granularity;
         }
      }   
   }

   $throughput=0.000008*$sum/$granularity;
   print STDOUT "$clock $throughput\n";
   $clock=$clock+$granularity;

   print STDERR "Avg throughput $fromport - $toport = ",
	         0.000008*$grantsum/$clock,"MBytes/sec \n";

   close DATA;

   exit(0);
 

使用方法,perl  throughput.pl   trfile  destNode  srcNode.port#  destNode.port time-granularity ,具体本例子,perl throughput.pl  basic1.tr 2 0.0 2.0 0.5 >out.txt

最后画出的TCP的速率如下;


[1]An Introduction to Computer Networks

[2]Studying TCP's Throughput and Goodput using NS


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值