徐雷鳴書中MFlood部分測試代碼學習筆記

來源:互聯網
上載者:User

使用徐雷鳴書第七章的測試tcl代碼得到的trace檔案,

1、RTR路由層收到之後添加了一個20位元組的IP頭,所以包長度從512變為532

第6、7行,節點0又收到自己發出的包,所以,丟棄。Mflood.cc中使用ch->num_forwards() == 0來判斷經過了幾次轉寄,但是沒有看到這個數遞增啊?

使用shell批量化處理,設定情境、流量、運行ns、awk處理實驗資料

發個

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

tcl代碼為徐雷鳴書中代碼

#Agent/UDP set packetSize_ 6000# ======================================================================# Define options# ======================================================================set val(chan)       Channel/WirelessChannelset val(prop)       Propagation/TwoRayGroundset val(netif)      Phy/WirelessPhyset val(mac)        Mac/802_11set val(ifq)        Queue/DropTail/PriQueueset val(ll)         LLset val(ant)        Antenna/OmniAntennaset val(x)             1200   ;# X dimension of the topographyset val(y)             1200   ;# Y dimension of the topographyset val(ifqlen)         50            ;# max packet in ifqset val(seed)           0.0set val(rp)   MFloodset val(nn)             50             ;# how many nodes are simulatedset 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 Variablesset ns_ [new Simulator]set tracefd [open mflood-scene.tr w]$ns_ trace-all $tracefd# set up topographyset 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 nodeset 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 namfor {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 endsfor {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 $tracefdclose $namtraceexit 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,則定為1000mseq=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.awkBEGIN {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;# 沒看明白怎麼統計的# 上一個Regex只是為了得到開始發送的時刻# 第一個時段肯定為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;elseprintf "Avg B/w = 0.0KB/s\n";printf "# Max B/w = %.3fKB/s\n", max >> outfile;}

Plot.sh代碼:

#!/bin/bashgnuplot -persist<<EOFset terminal gifset output "thrpt.gif"set title "throughput"set xlabel "time"set ylabel "throughput/kbps"#unset keyplot "1-2data" title "1->2" with linespointsEOF

繪圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.