Sunteți pe pagina 1din 27

Introduction to NS-2

Part 4. Queuing Simulation

Min Chen
School of Computer Science and Engineering
Seoul National University

1
Outline

 The Queuing System


 The Simulation Design
 Implementation in NS-2
 OTcl
 AWK script
 GNUplot script
 Parameters from command line
 Batch Script
 Random Seed and Distribution in NS-2
 An Exponential Traffic Example
2
The Queuing System

Rate: λ/h Rate: μ/h

Events Queuing
Server
Generator System

… ...
3
Simulation Design

 There should be 3 scenarios to verify the


impact of the 3 kind of queuing systems
1)Event Rate <= Service Rate
2)Event Rate > Service Rate & Queuing Length is finite
3)Event Rate > Service Rate & Queuing Length is infinite
 End to End Delay can represents the network
capacity in this simulation

4
Queuing Simulation in ns-2

cbr

udp null

0 1

5
Review of Otcl Script

#========================================
# Beginning of the simulation
#========================================
#initiate a simulator
set ns [new Simulator]

set nf [open out.nam w]


$ns namtrace-all $nf

set nd [open out.tr w]


$ns trace-all $nd

#========================================
# nodes configuration
#========================================
set n0 [$ns node]
set n1 [$ns node]
6
Queuing Simulation in ns-2

0 1

7
Review of Otcl Script (2)

#======================================== Service
# Configure the Link and the queue length ServiceRate
Rate
#========================================
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns queue-limit $n0 $n1 10
Queuing
QueuingLength
Length
#========================================
# Setup UDP connection
#========================================
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

set null0 [new Agent/Null]


$ns attach-agent $n1 $null0

$ns connect $udp0 $null0

8
Queuing Simulation in ns-2

udp null

0 1

9
Review of Otcl Script (3)

#========================================
# Setup Exponential Traffic
#========================================
set traffic0 [new Application/Traffic/CBR]
$traffic0 set packetSize_ 1000
$traffic0 set rate_ 1Mb
Event
EventRate
Rate
$traffic0 attach-agent $udp0

#========================================
# For the Animation
#========================================
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n0 $n1 queuePos 0.5

$udp0 set fid_ 1


$ns color 1 Red

10
Queuing Simulation in ns-2

cbr

udp null

0 1

11
Review of Otcl Script (4)

#========================================
# End of the simulation
#========================================
proc finish { } {
global ns nf nd
$ns flush-trace
close $nf
close $nd

exec nam out.nam &


exit 0
}

$ns at 0.5 "$traffic0 start"


$ns at 4.5 "$traffic0 stop"
$ns at 5.0 "finish"

$ns run 12
Review of AWK : Delay Calculation
BEGIN { highest_packet_id=0; }
{ Action = $1; time = $2;
From = $3; to = $4;
type = $5; pktsize = $6;
flow_id = $8; src = $9;
dst = $10; seq_no = $11;
packet_id = $12;

if ( packet_id > highest_packet_id )


highest_packet_id = packet_id;
if ( start_time[packet_id] == 0 )
start_time[packet_id] = time;
if( action == "r" )
{ end_time[packet_id] = time; }
else
{ end_time[packet_id] = -1;}
}
END{
for(packet_id=0; packet_id < highest_packet_id; packet_id++)
{
start = start_time[packet_id];
end = end_time[packet_id];
packet_duration = end-start;
if( start < end )
printf("%f %f\n",start, packet_duration);
}
}
13
Simulation Work

➢ ns 4-cbr-queuing.tcl
➢ To run the OTcl Script
➢ awk -f delay.awk out.tr > cbr-delay-10-1.txt
➢ To calculate real-time delay
➢ Change the data rate and the queuing length
➢ Queuing Length: 10, Data Rate: 1Mb
➢ cbr-delay-10-1.txt
➢ Queuing Length: 10, Data Rate: 1.5Mb
➢ cbr-delay-10-1.5.txt
➢ Queuing Length: 10000, Data Rate: 1.5Mb
➢ cbr-delay-10000-1.5.txt
14
Animations for Simulation
Queuing
QueuingLength:
Length:10
10
Date Rate: 1Mb
Date Rate: 1Mb

Queuing
QueuingLength:
Length:10
10
Data Rate: 1.5 Mb
Data Rate: 1.5 Mb
Queuing
QueuingLength:
Length:10000
10000
Data Rate: 1.5Mb
Data Rate: 1.5Mb

15
Review of GNUplot

gpl script ”cbr-compare.gpl”:


set title "CBR Delay Comparison"
set xlabel "Simulation Time (s)"
set ylabel "Delay (s)"

set terminal png


set output "cbr_delay.png"

plot "cbr-delay-10-1.txt" with lines 1, "cbr-delay-10-1.5.txt" with


lines 2, "cbr-delay-10000-1.5.txt" with lines 3

~$ gnuplot cbr-compare.gpl

16
To make simluation efficiency

 Introduction of parameters from command line


 Add to OTcl Script:
#========================================
# Parameters: Queue Length and Event Rate
#========================================
set QueueLength [lindex $argv 0]
set Rate [lindex $argv 1]
 Modify the two statements:
$ns queue-limit $n0 $n1 $QueueLength
$traffic0 set rate_ [expr $Rate*1.0]Mb

17
Pass values of the parameters

 When you run simulations, put the value of the


parameters by the end of the command, divided
by space
 Examples:
 We need Queuing Length=10, Data Rate=1.5Mb
➔ ~$ ns 4-cbr-queuing.tcl 10 1
 We need Queuing Length=10, Data Rate=1.5Mb
➔ ~$ ns 4-cbr-queuing.tcl 10 1.5
 We need Queuing Length=10000, Data Rate=1.5Mb
➔ ~$ ns 4-cbr-queuing.tcl 10000 1.5

18
More Efficiency?

 To use batch processing to make everything in


one statement!
 An example of Batch Script (cbr-compare.bat):
ns 4-cbr-queuing.tcl 10 1.0
awk -f delay.awk out.tr > cbr-delay-10-1.txt
ns 4-cbr-queuing.tcl 10 1.5
awk -f delay.awk out.tr > cbr-delay-10-1.5.txt
ns 4-cbr-queuing.tcl 10000 1.5
awk -f delay.awk out.tr > cbr-delay-10000-1.5.txt
gnuplot cbr-compare.gpl
 Run it in the terminal
 ~$ sh cbr-compare.bat 19
Result

20
Random Seed and Distribution

 Random Seed a seed to produce random


numbers in a predict interval
 Make the random number different in each
simulation
 Random Numbers
 Random everytime
 Follows a certain behavior in a long term view
 Distribution
 The behavior in statistical view
 Famous Distributions: Uniform, Exponential,
Poisson
21
Exponential Random Numbers

Set rng [new RNG]


$rng seed 1

Set random0 [new RandomVariable/Exponential]


$random0 use-rng $rng
$random0 avg_ 10.0

For {set i 1} {$i <= 100} {incr i}


{
Puts [$random0 value]
}
22
Specific Traffic Flows: Exponential
#========================================
# Parameters: Queue Length and Event Rate
#========================================
set QueueLength [lindex $argv 0]
set Rate [lindex $argv 1]

#========================================
# Beginning of the simulation
#========================================
#initiate a simulator
set ns [new Simulator]
set nf [open out.nam w]
$ns namtrace-all $nf
set nd [open out.tr w]
$ns trace-all $nd

#========================================
# nodes configuration
#========================================
set n0 [$ns node]
set n1 [$ns node] 23
Specific Traffic Flows: Exponential(2)
#========================================
# Configure the Link and the queue length
#========================================
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns queue-limit $n0 $n1 $QueueLength

#========================================
# Setup UDP connection
#========================================
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

set null0 [new Agent/Null]


$ns attach-agent $n1 $null0

$ns connect $udp0 $null0

24
Specific Traffic Flows: Exponential(3)
#========================================
# Setup Exponential Traffic
#========================================
set traffic0 [new Application/Traffic/Exponential]
$traffic0 set packetSize_ 1000
$traffic0 set burst_time_ 0.5
$traffic0 set idle_time_ 0
$traffic0 set rate_ [expr $Rate*1.0]Mb
$traffic0 attach-agent $udp0

#========================================
# For the Animation
#========================================
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n0 $n1 queuePos 0.5

$udp0 set fid_ 1


$ns color 1 Red

25
Burst Time and Idle Time

 In exponential traffic, the system assume that in


some periods the application is sending
packets while in some other periods it stay idle
 Thus in simulations, we have specify the burst
time and idle time

26
Specific Traffic Flows: Exponential(4)
#========================================
# End of the simulation
#========================================
proc finish { } {
global ns nf nd
$ns flush-trace
close $nf
close $nd

exec nam out.nam &


exit 0
}

$ns at 0.5 "$traffic0 start"


$ns at 4.5 "$traffic0 stop"
$ns at 5.0 "finish"

$ns run

27

S-ar putea să vă placă și