suakii.egloos.com

suakii's log





NS2 두번째 throughput 계산하기 공부

원래 어제 쓰던 글인데 잘 되던 코드를 수정하고 계속 Segmentation Fault오류를 뿜어내는 ns2에게 gg치고 오늘 있을 시험 공부를 하느라 이렇게 다음날 추가로 적게 되었다. 뭐 시험은 예상했던 정도의 수준이 나와주었고 지금은 막 이렇게 점심을 먹고 돌아온길.. 교원대의 가을 풍경은 정말로 아름답다. 시간이 지날수록 떨어지는 낙엽을 보는것이 다만 안타까울 따름이지만 말이야. 몇년전 와서 보왔던 그 낙엽들 때문인지, 애써 아름다움을 느끼려하지 않으려는 나 일지도 모르겠다. 암튼.

아래의 글에 이은 두번째 예제는 Throughput 을 TraceFile에서 직접 산출해보는 것이 목표다. 물론 LossMonitor 나 TCPSink의 bytes_ 속성을 이용해도 되지만 직접 그린 그래프가 왠지 더 이쁜것은 나 만의 느낌인지....

1. Topology
말이 필요없다. 노드 4개 그리고 TCP와 UDP Agent를 사용하고 CBR을 붙여서 0.1초부터 시뮬레이션 시작..


2. Tcl Scripts

# Create a simulator object
set ns [new Simulator]

# Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf

# Open the All trace file
set nt [open out.tr w]
$ns trace-all $nt

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

# Create links between the nodes
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
 
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

# Set queue size of linke n2-n3 to 10
#$ns queue-limit $n2 $n3 10

# Set up a TCP connection
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

# Setup a CBR ovder TCP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $tcp
$cbr0 set packet_size_ 1024
$cbr0 set interval_ 0.01

# Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2

# Setup a CBR over UDP connection
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp
$cbr1 set packet_size_ 1024
$cbr1 set interval_ 0.01

# Schedule events for the CBR agents
$ns at 0.1 "$cbr0 start"
$ns at 0.1 "$cbr1 start"
$ns at 4.5 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"

# Define a 'finish' procedure
proc finish {} {
    global ns nf nt
    $ns flush-trace
   
    #Close the trace file
    close $nf
    close $nt
    exec nam out.nam &
    exit 0
}

$ns run

3. Trace File 분석하기

3.1 일단 n0 와 n1 노드의 패킷들을 보려고 노드 2번에서의 패킷을 분석하는 awk 코드 작성

awk '{if(($3=="0")&&($4=="2")&&($1=="r"))print $2, $6}' out.tr > Temp02
awk '{if(($3=="1")&&($4=="2")&&($1=="r"))print $2, $6}' out.tr > Temp12

위의 코드는 out.tr 트레이스 파일에서 세번째 네번째 필드 즉, from-node, to-node, 첫 번째  event 값을 검사하여 Temp파일로 써주고 있다.

3.2 throughput.awk 파일 작성
BEGIN {
    sum = 0;
}
{
    if ($1>0)
        printf("%f\t%f\n",$1, sum/$1);
    sum += $2;
}
END {
    puts "end"
}

별거 없다. 그냥 시간에 따른 수신된 패킷을 누적시키면서 throughput을 보고 있다.
실행방법은
awk -f throughput.awk Temp01 > throughput02tcp
awk -f throughput.awk Temp12 > throughput12udp

4. xgraph 그리기
xgraph throughput02tcp throughput12udp

아래 결과를 보면 알겠지만 udp의 throughput이 tcp에 비해 얼마나 높게 유지가 되는지 알 수 있다.
좀더 자세히 살표보면 Y축의 값이 Byte이므로 8을 곱해서 읽어보면 0.8Mbps정도로 할당된 대역폭인 1Mbps에 근접함을 알 수 있으나 tcp의 경우에는 0.24Mbps정도의 throughput 을 내는것을 알 수 있다.



4.1 비슷한 방법으로 node3에서 동일한 방법으로 수행한 결과
 최종 목적지인 node3에서의 throughput 역시 위의 결과와 비슷하다.
 udp의 경우 0.76Mbps 정도인데 반해 tcp의 경우는 0.24Mbps 정도의 throughput을 보여주고 있다.


5. 위의 코드를 수정하여 trace file을 직접 조작하지 않고 작업할 수 있는 코드로 변경하여 그래프를 그려보았다.
아래는 수정된 Tcl Scripts
# Create a simulator object
set ns [new Simulator]

$ns color 1 Blue
$ns color 2 Red

# Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf

# Open the All trace file
set nt [open out.tr w]
$ns trace-all $nt

# Open the output files
set f0 [open outtcp.tr w]
set f1 [open outudp.tr w]

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

# Create links between the nodes
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
 
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

# Set queue size of linke n2-n3 to 10
#$ns queue-limit $n2 $n3 10

# Set up a TCP connection
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

# Set the network sink for node tcp n0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
$ns connect $tcp $sink0
$tcp set fid_ 1

# Setup a CBR ovder TCP connection
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $tcp
$cbr0 set packet_size_ 1024
$cbr0 set interval_ 0.01

# Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp

# Set the null sink for node udp t1
set sink1 [new Agent/LossMonitor]
$ns attach-agent $n3 $sink1
$ns connect $udp $sink1
$udp set fid_ 2

# Setup a CBR over UDP connection
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp
$cbr1 set packet_size_ 1024
$cbr1 set interval_ 0.01

# Schedule events for the CBR agents
$ns at 0.0 "sinkNodeRecord"
$ns at 0.1 "$cbr0 start"
$ns at 0.1 "$cbr1 start"
$ns at 4.5 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"

proc sinkNodeRecord {} {
    global sink0 sink1 f0 f1
   
    # Get an instance of the simulator
    set ns [Simulator instance]

    # Set the time after which the procedure should be called again
    set time 0.5

    # How many bytes have been received by the traffic sinks?
    set bw0 [$sink0 set bytes_]
    set bw1 [$sink1 set bytes_]

    # Get the current time
    set now [$ns now]

    # Calculate the bandwidth (in Mbit/s) and write it to the files
    puts $f0 "$now [expr $bw0/$time*8/1000000]"
    puts $f1 "$now [expr $bw1/$time*8/1000000]"

    # Reset the bytes_ values on the traffic sinks
    $sink0 set bytes_ 0
    $sink1 set bytes_ 0

    # Re-schedule the procedure
    $ns at [expr $now+$time] "sinkNodeRecord"

}

# Define a 'finish' procedure
proc finish {} {
    global ns nf nt f0 f1
    $ns flush-trace
   
    #Close the trace file
    close $nf
    close $nt
    close $f0
    close $f1

    #exec nam out.nam &
    exec xgraph outtcp.tr outudp.tr &
   
    exit 0
}

$ns run
5.1  결과 그래프





p.s 작성한 코드와 결과가 이상이 없다면 예상대로 tcp 와 udp의  throughput차이는 결코 작지 않음을 알 수 있다. 물론 두 프로토콜 자체가 적합한 응용이 따로 있으니 거기에 맞게 사용하면 될 일이고 말이야. 현재 이 글은 우분트에서 작성하고 있는데 오픈오피스가 손에 익지 않아서 인지 도형그리기가 조금 힘들었고, 이미지 편집을 위해 사용한 김프도 그렇고, 정말 마음만 먹으면 리눅스나 맥으로 스위칭이 가능할것 같기도 하다. 아직은 나한테는 조금 이르고. 여기까지는 정말 ns2의 반에 반도 아직 온것 같지 않다. 알수록 더 모르는게 많아지는것이 모든 것들인것 같기도 하다. 이제 기존 프로토콜을 수정하거나 추가해보는 일이 남은것 같다. 지난학기의 네트웍 프로그래밍에 이어서 이번엔 그 아래쪽의 부분을 또 조금 더 깊게 알게 되는것 같고, 물론 여기 오기전 했던 라우팅 시뮬레이션 이런것들은 또 그 윗단계이겠지만 말이야. 어렵지만 네트웍은 재미가 있는 분야이긴 하다.

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://suakii.egloos.com/tb/2453534 [도움말]

덧글

  • 짜이 2009/10/21 18:48 # 삭제 답글

    이 긴 글을 보고 있자니, 난 또 이 길을 언제 걸어가나~ 두려움이 앞서고 말이야~ ㅠ.ㅜ
  • 수아기 2009/10/21 21:35 #

    코드빨이에요. 제가 쓴것은 실제로 얼마 되지 않아요.ㅠ.ㅠ
덧글 입력 영역