zoukankan      html  css  js  c++  java
  • NS 2.35 柯志亨书实验9笔记队列管理机制

    当时记得笔记:

    目前,实现了RED的实时和平均队列长度的显示,但是显示的图形与wpi.edu中的走势有点区别???

     但是柯志亨用的是myfifo,应该是自己写的队列

    当改为droptail时,与tr文件关联出现错误???不知道如何解决

     RED文献中比较了RED和droptail下平均队列长度和吞吐量之间的关系,想做一个类似的图

    Tcl代码如下:

    # Kezhiheng, exper 9, test DropTail and RED
    
    if {$argc != 2} {
        puts "Usage: ns queue.tcl queuetype_ noflow_"
        puts "Example: ns queue.tcl DropTail 10"
        puts "queuetype_: DropTail or RED"
        exit
    }
    
    set par1 [lindex $argv 0]
    set par2 [lindex $argv 1]
    
    #Create a simulator object
    set ns [new Simulator]
    
    set tracefd [open zout$par1-$par2.tr w]
    $ns trace-all $tracefd
    set namtracefd [open out.nam w]
    $ns namtrace-all $namtracefd
    
    proc finish {} {
        global ns tracefd namtracefd par2 tcp startT
        $ns flush-trace
        close $tracefd
        close $namtracefd
        
        set time [$ns now]
        set sum_thrpt 0
    
        #throughput=number of ack recv * pktsize / time
        #number of ack recv = number of packet sent
        for {set i 1} {$i<=$par2} {incr i} {
            # ack_是tcp的一个变量、参数,表示highest ACK received,默认是0,ns manual p194
            set ackno_($i) [$tcp($i) set ack_]
            # tcp默认的包大小是packetSize_ 1000
            set thrpt($i) [expr $ackno_($i)*1000*8/($time-$startT($i))]
            puts $thrpt($i)
            set sum_thrpt [expr $sum_thrpt+$thrpt($i)]
        }
    
        set avethrpt [expr $sum_thrpt/$par2]
        puts "average throughput: $avethrpt (bps)"
    
        #exec nam out.nam &
        exit 0
    }
    
    # Set router nodes
    set r1 [$ns node]
    set r2 [$ns node]
    $ns duplex-link $r1 $r2 56kb 10ms $par1
    $ns queue-limit $r1 $r2 50
    
    # Set TCP src, dest, link
    for {set i 1} {$i<=$par2} {incr i} {
        set s($i) [$ns node]
        set d($i) [$ns node]
        
        $ns duplex-link $s($i) $r1 10Mb 1ms DropTail
        $ns duplex-link $r2 $d($i) 10Mb 1ms DropTail
    }
    
    #把队列长度记录下来
    set queuefd [open q-$par1-$par2.tr w]
    set q_ [[$ns link $r1 $r2] queue]
    $q_ trace curq_
    $q_ trace ave_
    $q_ attach $queuefd
    
    # DropTail没有任何参数,RED有许多参数,ns manual p74
    if {$par1 == "RED"} {
        #使用packet mode, not byte mode
        $q_ set bytes_ false
        $q_ set queue_in_bytes_ false
    }
    
    # Create tcp and ftp flow
    for {set i 1} {$i<=$par2} {incr i} {
        set tcp($i) [$ns create-connection TCP/Reno $s($i) TCPSink $d($i) 0]
        $tcp($i) set fid_ $i
        
        set ftp($i) [$tcp($i) attach-app FTP]
    }
    
    # Random start ftp flow
    set rng [new RNG]
    $rng seed 1
    
    set rvStart [new RandomVariable/Uniform]
    $rvStart use-rng $rng
    $rvStart set min_ 0.0
    $rvStart set max_ 1.0
    
    for {set i 1} {$i<=$par2} {incr i} {
        set startT($i) [expr [$rvStart value]]
        puts "startT($i) $startT($i) sec"
        $ns at $startT($i) "$ftp($i) start"
    }
    
    $ns at 200.0 "finish"
    
    $ns run
    View Code
    #把队列长度记录下来
    set q_ [[$ns link $r1 $r2] queue]
    [$ns link $r1 $r2],返回$link
    [[$ns link $r1 $r2] queue]相当于[$link queue],返回link中的queue
    $q_ trace curq_,让$queue trace当前队列的长度和平均队列长度
    $q_ trace ave_

    这是queue trace文件的内容:

    a 0.0515932 0.0288164
    Q 0.0515932 1
    a 0.0524252 0.0856189
    Q 0.0524252 2
    a 0.0891547 0.169601
    Q 0.0891547 3
    a 0.217742 0.251163
    a 0.218574 0.359191
    Q 0.218574 4

    其中a表示平均队列长度, Q表示当前队列长度,可以看到有时候a有多个,而Q只有一个,可能因为平均长度一直在计算,而瞬时长度没有改变,因为瞬时长度没有两个相同的长度连着。这对于后面提取出的 时间 长度 数据绘图时没有影响,gnuplot会自动按x轴的时间绘图。

    这是提取a和Q的awk代码,用于从queue trace文件中读取内容,分别把a和Q写入不同的文件

    BEGIN{
        # program initialize
    
    }
    
    {
    
        class = $1;
        time = $2;
        value = $3;
    
        #
        if(class=="a")
        {
            print time, value >> "tempa"
        }
        else if(class=="Q")
        {
            print time, value >> "tempq"
        }
    
    }
    
    END {
    # When read over, start to calculate
    
    }
    View Code

    Gnuplot的代码:

    #!/bin/bash
    
    gnuplot -persist<<EOF
    
    set terminal gif
    set output "RED.gif"
    set title "RED average and current queue size"
    set xlabel "time"
    set ylabel "queue size"
    #unset key
    
    plot "tempa" with linespoints, "tempq" with linespoints
    
    EOF
    View Code

    绘制出的图形:

    图形走势不像wpi.edu中的图形:

    似乎指数加权的记忆因子比较大,使平均队列长度不会随当前队列长度的变换而剧烈变化。

  • 相关阅读:
    Golang mysql数据库
    C++ list结构体变量排序
    VS2013 ERROR MSB8020
    error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation
    error C2664: “FILE *fopen(const char *,const char *)”: 无法将参数 1 从“LPCTSTR”转换为“const char *”
    error C4430: missing type specifier
    虚拟地址转物理地址
    vs2013 x64 编译汇编代码
    fs寄存器相关,PEB,TEB
    boost 1.57 vs2013 编译
  • 原文地址:https://www.cnblogs.com/yanhc/p/3082865.html
Copyright © 2011-2022 走看看