徐雷鸣书中MFlood部分测试代码学习笔记

使用徐雷鸣书第七章的测试tcl代码得到的trace文件,

1、RTR路由层收到之后添加了一个20字节的IP头,所以包长度从512变为532

第6、7行,节点0又收到自己发出的包,所以,丢弃。Mflood.cc中使用ch->num_forwards() == 0来判断经过了几次转发,但是没有看到这个数递增啊?

徐雷鸣书中MFlood部分测试代码学习笔记_第1张图片

在第8行可以看到packet直接到了AGT层,一开始觉得有点奇怪,在node 0的时候从AGT到RTR层,在node2的时候是不是应该从RTR层到AGT层呢,这样才符合分层。但后来才想到,node的结构中的address classifier,这个会先判断address是否是本地节点的地址,如果是,就直接递交给AGT了可能,如果不是本地节点地址,才会将其递交给默认的255的RTR。


使用shell批量化处理,设置场景、流量、运行ns、awk处理实验数据

发个

#!/bin/bash
##############################################
# Shell grammar: 
#	test expr is logic expression
#	lt means lower than
##############################################
i=1
while (test $i -lt 2)
do
	scn/setdest -v 1 -n 50 -p 0 -M 5 -t 100 -x 1000 -y 1000 > scn/scene-50n-0p-5s-100t-1000-1000
	ns scn/cbrgen.tcl -type cbr -nn 50 -seed $i -mc 30 -rate 1.0 > scn/cbr-50n-30c-1p
	
	ns mflood-scene.tcl
	awk -f getRatio.awk mflood-scene.tr
    let i=i+1
done

tcl代码为徐雷鸣书中代码

#Agent/UDP set packetSize_ 6000

# ======================================================================
# Define options
# ======================================================================
set val(chan)       Channel/WirelessChannel
set val(prop)       Propagation/TwoRayGround
set val(netif)      Phy/WirelessPhy
set val(mac)        Mac/802_11
set val(ifq)        Queue/DropTail/PriQueue
set val(ll)         LL
set val(ant)        Antenna/OmniAntenna

set val(x)             	1200   ;# X dimension of the topography
set val(y)             	1200   ;# Y dimension of the topography
set val(ifqlen)         50            ;# max packet in ifq
set val(seed)           0.0
set val(rp)   			MFlood
set val(nn)             50             ;# how many nodes are simulated
set val(cp)             "scn/cbr-50n-30c-1p"
set val(sc)             "scn/scene-50n-0p-5s-100t-1000-1000"
set val(stop)	    	100

# ======================================================================
# Main Program
# ======================================================================

#ns-random 0

# Initialize Global Variables
set ns_ [new Simulator]
set tracefd [open mflood-scene.tr w]
$ns_ trace-all $tracefd

# set up topography
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

set namtrace    [open mflood-scene.nam w]
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)

#
# Create God
#
set god_ [create-god $val(nn)]

# Create the specified number of mobilenodes [$val(nn)] and "attach" them
# to the channel. 
# configure node
set channel [new Channel/WirelessChannel]
$channel set errorProbability_ 0.0

        $ns_ node-config -adhocRouting $val(rp) \
			 -llType $val(ll) \
			 -macType $val(mac) \
			 -ifqType $val(ifq) \
			 -ifqLen $val(ifqlen) \
			 -antType $val(ant) \
			 -propType $val(prop) \
			 -phyType $val(netif) \
			 -channel $channel \
			 -topoInstance $topo \
			 -agentTrace ON \
			 -routerTrace ON\
			 -macTrace OFF \
			 -movementTrace OFF			
			 
	for {set i 0} {$i < $val(nn) } {incr i} {
		set node_($i) [$ns_ node]	
		$node_($i) random-motion 0;	
	}

#
# Define node movement model
#
puts "Loading connection pattern..."
source $val(cp)

#
# Define traffic model
#
puts "Loading scenario file..."
source $val(sc)


# Define node initial position in nam

for {set i 0} {$i < $val(nn)} {incr i} {

    # 20 defines the node size in nam, must adjust it according to your scenario
    # The function must be called after mobility model is defined

    $ns_ initial_node_pos $node_($i) 20
}

# Tell nodes when the simulation ends
for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ at $val(stop).0 "$node_($i) reset";
}

$ns_ at $val(stop).0 "stop"
$ns_ at $val(stop).01 "puts \"NS EXITING...\" ; $ns_ halt"

proc stop {} {
    global ns_ tracefd namtrace
    $ns_ flush-trace
    close $tracefd
	close $namtrace
	exit 0
}

puts "Starting Simulation..."
$ns_ run

getRatio.awk文件为徐雷鸣书中代码,添加了部分注释

##########################################################
# awk grammar: exp ~ /regexp/ 如果exp符合regexp,结果为真
# awk grammar: exp !~ /regexp/ 如果exp不符合regexp,结果为真
# regular expression: 
# 		^脱字符,只能匹配位于行首的字符串
#		.与任意字符匹配
#		*与任意0或n个字符匹配
#		.*与任意数量字符串匹配
##########################################################

BEGIN {
    sendLine = 0;
    recvLine = 0;
	fowardLine = 0;
	if(mseq==0)		# 如果没有指定最大seq,则定为1000
		mseq=10000;
	for(i=0; i<mseq; i++) {		# 初始化重复包判断的缓存数组
		sseq[i]=-1;
		rseq[i]=-1;
	}
}

$0 ~/^s.* AGT/ {
	if(sseq[$6]==-1) {
    	sendLine ++ ;
		sseq[$6]=$6;
	}
}

$0 ~/^r.* AGT/ {
	if(rseq[$6]==-1) {
    	recvLine ++ ;
		rseq[$6]=$6;
	}
}

$0 ~/^f.* RTR/ {
    fowardLine ++ ;
}

END {
    printf "cbr s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine, (recvLine/sendLine),fowardLine;
}

输出结果:

cbr s:1169 r:809, r/sRatio:0.6920, f:38242

上面是分析整个的吞吐量,下面分析某对源节点和目的节点:

awk -v src=1 -v dst=2 -voutfile=1-2data -f getNodeRecv.awk mflood-scene.tr

 getNodeRecv.awk文件:

# getNodeRecv.awk

BEGIN {
	if(step ==0)
		step = 10;
	base = 0;
	start = 0;
	bytes = 0;
	total_bytes = 0;
	max = 0;
	calc = 0;

}

$0 ~/^s.* AGT/ {
	if (base == 0 && $3 == ("_" src "_")) {
		base = $2;
		start = $2;
		calc = 1;
	}
}

$0 ~/^r.* AGT/ && calc == 1{ 
	time = $2;
	# 没看明白怎么统计的
	# 上一个正则表达式只是为了得到开始发送的时刻
	# 第一个时段肯定为0,因为byte还没有加
	# 以后的时段,将base定为当前加step,
	# 然后,time一点一点加,同时统计dst收到多少src的包
	# 当time超过step之后,计算上一时段的吞吐率
	# time --> --> --> -->
	# base		+	 step |
	if (time > base) {
		bw = bytes/(step * 1000.0);
        if (max < bw)
			max = bw;
        printf "%.9f %.9f\n", base, bw >> outfile;
        base += step;
        bytes = 0;
    }
	# dst收到多少src发出的包
	# match [31:0 .与任意字符匹配
	if ($3 == ("_" dst "_") && match($14,"." src ":") >=0 ) {
		total_bytes += $8;
		bytes += $8;
	}
}

END {							
	if (total_bytes)
		printf "# Avg B/w = %.3fKB/s\n", ((total_bytes/1000.0)/(time-start)) >> outfile;
	else	
		printf "Avg B/w = 0.0KB/s\n";
	printf "# Max B/w = %.3fKB/s\n", max >> outfile;		
}
Plot.sh代码:


#!/bin/bash

gnuplot -persist<<EOF

set terminal gif
set output "thrpt.gif"
set title "throughput"
set xlabel "time"
set ylabel "throughput/kbps"
#unset key

plot "1-2data" title "1->2" with linespoints

EOF

绘图:



你可能感兴趣的:(徐雷鸣书中MFlood部分测试代码学习笔记)