51
1 Part III: Extending ns

1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

Embed Size (px)

Citation preview

Page 1: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

11

Part III: Extending ns

Page 2: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

22USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Outline

Extending ns In OTcl In C++

Debugging

Page 3: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

33USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

ns Directory Structure

TK8.0 OTcl tclclTcl8.0 ns-2 nam-1

tcl

ex test lib

...

...

examples validation tests

C++ code

OTcl code

ns-allinone

mcast

Page 4: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

44USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Extending ns in OTcl

If you don’t want to compile source your changes in your sim

scripts

Otherwise Modifying code; recompile Adding new files

Change Makefile (NS_TCL_LIB), tcl/lib/ns-lib.tcl

Recompile

Page 5: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

55USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Example: Agent/Message

n0 n1

n4

n5

n2

n3

128Kb, 50ms

10Mb, 1ms 10Mb, 1ms

C Ccrosstraffic

S R

msg agent

Page 6: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

66USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message

A UDP agent (without UDP header)Up to 64 bytes user messageGood for fast prototyping a simple ideaUsage requires extending ns functionality

SS RR

pkt: 64 bytesof arbitrary string

Receiver-sideprocessing

Page 7: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

77USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 1

Define senderclass Sender –superclass Agent/Messageclass Sender –superclass Agent/Message

# Message format: “Addr Op SeqNo”# Message format: “Addr Op SeqNo”

Sender instproc send-next {} {Sender instproc send-next {} {

$self instvar seq_ agent_addr_$self instvar seq_ agent_addr_

$self send “$agent_addr_ send $seq_”$self send “$agent_addr_ send $seq_”

incr seq_incr seq_

global nsglobal ns

$ns at [expr [$ns now]+0.1] "$self send-next"$ns at [expr [$ns now]+0.1] "$self send-next"

}}

Page 8: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

88USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 2

Define sender packet processing

Sender instproc Sender instproc recvrecv msg { msg {

$self instvar agent_addr_$self instvar agent_addr_

set sdr [lindex $msg 0]set sdr [lindex $msg 0]

set seq [lindex $msg 2]set seq [lindex $msg 2]

puts "Sender gets ack $seq from $sdr"puts "Sender gets ack $seq from $sdr"

}}

Page 9: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

99USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 3

Define receiver packet processing

Class Receiver –superclass Agent/MessageClass Receiver –superclass Agent/Message

Receiver instproc Receiver instproc recvrecv msg { msg {

$self instvar agent_addr_$self instvar agent_addr_

set sdr [lindex $msg 0]set sdr [lindex $msg 0]

set seq [lindex $msg 2]set seq [lindex $msg 2]

puts “Receiver gets seq $seq from $sdr”puts “Receiver gets seq $seq from $sdr”

$self send “$addr_ ack $seq”$self send “$addr_ ack $seq”

}}

Page 10: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1010USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 4

Scheduler and tracing

# Create scheduler# Create scheduler

set ns [new Simulator]set ns [new Simulator]

# Turn on Tracing# Turn on Tracing

set fd [new “message.nam” w]set fd [new “message.nam” w]

$ns namtrace-all $fd$ns namtrace-all $fd

Page 11: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1111USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 5

Topologyfor {set i 0} {$i < 6} {incr i} {for {set i 0} {$i < 6} {incr i} {

set n($i) [$ns node]set n($i) [$ns node]}}$ns duplex-link $n(0) $n(1) 128kb 50ms DropTail$ns duplex-link $n(0) $n(1) 128kb 50ms DropTail$ns duplex-link $n(1) $n(4) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(4) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(5) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(5) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(2) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(2) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(3) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(3) 10Mb 1ms DropTail

$ns queue-limit $n(0) $n(1) 5$ns queue-limit $n(0) $n(1) 5$ns queue-limit $n(1) $n(0) 5$ns queue-limit $n(1) $n(0) 5

Page 12: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1212USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 6

Routing

# Packet loss produced by queueing# Packet loss produced by queueing

# Routing protocol: let’s run distance vector# Routing protocol: let’s run distance vector

$ns rtproto DV$ns rtproto DV

Page 13: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1313USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 7

Cross trafficset udp0 [new Agent/UDP]set udp0 [new Agent/UDP]$ns attach-agent $n(2) $udp0$ns attach-agent $n(2) $udp0set null0 [new Agent/NULL]set null0 [new Agent/NULL]$ns attach-agent $n(4) $null0$ns attach-agent $n(4) $null0$ns connect $udp0 $null0$ns connect $udp0 $null0

set exp0 [new Application/Traffic/Exponential]set exp0 [new Application/Traffic/Exponential]$exp0 set rate_ 128k$exp0 set rate_ 128k$exp0 attach-agent $udp0$exp0 attach-agent $udp0$ns at 1.0 “$exp0 start”$ns at 1.0 “$exp0 start”

Page 14: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1414USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 8

Message agentsset sdr [new Sender]set sdr [new Sender]$sdr set packetSize_ 1000$sdr set packetSize_ 1000

set rcvr [new Receiver]set rcvr [new Receiver]$rcvr set packetSize_ 40$rcvr set packetSize_ 40

$ns attach $n(3) $sdr$ns attach $n(3) $sdr$ns attach $n(5) $rcvr$ns attach $n(5) $rcvr$ns connect $sdr $rcvr$ns connect $sdr $rcvr$ns connect $rcvr $sdr$ns connect $rcvr $sdr$ns at 1.1 “$sdr send-next”$ns at 1.1 “$sdr send-next”

Page 15: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1515USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Step 9

End-of-simulation wrapper (as usual)

$ns at 2.0 finish$ns at 2.0 finish

proc finish {} {proc finish {} {

global ns fdglobal ns fd

$ns flush-trace$ns flush-trace

close $fdclose $fd

exit 0exit 0

}}

Page 16: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1616USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message: Result

Example output> ./ns msg.tcl> ./ns msg.tclReceiver gets seq 0 from 0Receiver gets seq 0 from 0Sender gets ack 0 from 1Sender gets ack 0 from 1Receiver gets seq 1 from 0Receiver gets seq 1 from 0Sender gets ack 1 from 1Sender gets ack 1 from 1Receiver gets seq 2 from 0Receiver gets seq 2 from 0Sender gets ack 2 from 1Sender gets ack 2 from 1Receiver gets seq 3 from 0Receiver gets seq 3 from 0Sender gets ack 3 from 1Sender gets ack 3 from 1Receiver gets seq 4 from 0Receiver gets seq 4 from 0Sender gets ack 4 from 1Sender gets ack 4 from 1Receiver gets seq 5 from 0Receiver gets seq 5 from 0

Page 17: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1717USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Add Your Changes into ns

TK8.0 OTcl tclclTcl8.0 ns-2 nam-1

tcl

ex test lib

...

...

examples validation tests

C++ code

OTcl code

ns-allinone

mcastmysrc

msg.tcl

Page 18: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1818USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Add Your Change into ns

tcl/lib/ns-lib.tclClass SimulatorClass Simulator……source ../mysrc/msg.tclsource ../mysrc/msg.tcl

MakefileNS_TCL_LIB = \NS_TCL_LIB = \tcl/mysrc/msg.tcl \tcl/mysrc/msg.tcl \……

Or: change Makefile.in, make distcleanmake distclean, then ./configure --enable-debug./configure --enable-debug

Page 19: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

1919USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Outline

Extending ns In OTcl In C++

New components

Page 20: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2020USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Extending ns in C++

Modifying code make depend Recompile

Adding code in new files Change Makefile make depend recompile

Page 21: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2121USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Creating New Components

GuidelinesTwo styles New agent based on existing packet

headers Add new packet header

Page 22: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2222USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Guidelines

Decide position in class hierarchy I.e., which class to derive from?

Create new packet header (if necessary)Create C++ class, fill in methodsDefine OTcl linkage (if any)Write OTcl code (if any)Build (and debug)

Page 23: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2323USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

New Agent, Old Header

TCP jump start Wide-open transmission window at

the beginning From cwnd_ += 1cwnd_ += 1 To cwnd_ = MAXWIN_cwnd_ = MAXWIN_

Page 24: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2424USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

TCP Jump Start – Step 1TclObject

NsObject

Connector Classifier

Delay AddrClassifierAgent McastClasifierQueue Trace

DropTail RED TCP Enq Deq Drop

Reno SACK JS

Page 25: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2525USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

TCP Jump Start – Step 2

New file: tcp-js.h

class JSTCPAgent : public TcpAgent {class JSTCPAgent : public TcpAgent {

public:public:

virtual void set_initial_window() {virtual void set_initial_window() {

cwnd_ = MAXWIN_;cwnd_ = MAXWIN_;

}}

private:private:

int MAXWIN_;int MAXWIN_;

};};

Page 26: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2626USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

TCP Jump Start – Step 3

New file: tcp-js.ccstatic JSTcpClass : public TclClass {public:JSTcpClass() : TclClass("Agent/TCP/JS") {}TclObject* create(int, const char*const*) {return (new JSTcpAgent());}};JSTcpAgent::JSTcpAgent() {

bind(“MAXWIN_”, MAXWIN_);}

Page 27: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2727USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

New Packet Header

Create new header structureEnable tracing support of new headerCreate static class for OTcl linkage (packet.h)Enable new header in OTcl (tcl/lib/ns-packet.tcl)This does not apply when you add a new field into an existing header!

Page 28: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2828USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

How Packet Header WorksPacket

next_

hdrlen_

bits_ size determinedat compile time

size determinedat compile time

size determinedat compile time

……

hdr_cmn

hdr_ip

hdr_tcp

size determinedat simulatorstartup time

(PacketHeaderManager)

PacketHeader/Common

PacketHeader/IP

PacketHeader/TCP

Page 29: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

2929USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Example: Agent/Message

New packet header for 64-byte messageNew transport agent to process this new header

Page 30: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3030USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

New Packet Header – Step 1

Create header structurestruct hdr_msg {struct hdr_msg {

char msg_[64];char msg_[64];static int offset_;static int offset_;inline static int& offset() { return offset_; }inline static int& offset() { return offset_; }inline static hdr_msg* access(Packet* p) {inline static hdr_msg* access(Packet* p) {

return (hdr_msg*) p->access(offset_);return (hdr_msg*) p->access(offset_);}}/* per-field member functions *//* per-field member functions */char* msg() { return (msg_); }char* msg() { return (msg_); }int maxmsg() { return (sizeof(msg_)); }int maxmsg() { return (sizeof(msg_)); }

};};

Page 31: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3131USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

New Packet Header – Step 2

PacketHeader/Messagestatic class MessageHeaderClass : static class MessageHeaderClass :

public PacketHeaderClass {public PacketHeaderClass {

public:public:

MessageHeaderClass() : MessageHeaderClass() : PacketHeaderClass("PacketHeader/Message",PacketHeaderClass("PacketHeader/Message",

sizeof(hdr_msg)) {sizeof(hdr_msg)) {

bind_offset(&hdr_msg::offset_);bind_offset(&hdr_msg::offset_);

}}

} class_msghdr;} class_msghdr;

Page 32: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3232USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

New Packet Header – Step 3

Enable tracing (packet.h):enum packet_t {enum packet_t {

PT_TCP,PT_TCP,……,,PT_MESSAGE,PT_MESSAGE,PT_NTYPE // This MUST be the LAST onePT_NTYPE // This MUST be the LAST one

};};class p_info {class p_info {

…………name_[PT_MESSAGE] = “message”;name_[PT_MESSAGE] = “message”;name_[PT_NTYPE]= "undefined";name_[PT_NTYPE]= "undefined";

…………};};

Page 33: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3333USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

New Packet Header – Step 4

Register new header (tcl/lib/ns-packet.tcl)

foreach pair {foreach pair {

{ Common off_cmn_ }{ Common off_cmn_ }

……

{ Message off_msg_ }{ Message off_msg_ }

}}

Page 34: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3434USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Packet Header: Caution

Some old code, e.g.:RtpAgent::RtpAgent() {RtpAgent::RtpAgent() {

…… ……

bind(“off_rtp_”, &off_rtp);bind(“off_rtp_”, &off_rtp);

}}

…………

hdr_rtp* rh = (hdr_rtp*)p->access(off_rtp_);hdr_rtp* rh = (hdr_rtp*)p->access(off_rtp_);

Don’t follow this example!

Page 35: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3535USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message – Step 1TclObject

NsObject

Connector Classifier

Delay AddrClassifierAgent McastClasifierQueue Trace

DropTail RED TCP Enq Deq Drop

Reno SACK

Message

Page 36: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3636USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message – Step 2

C++ class definition// Standard split object declaration// Standard split object declaration

static …static …

class MessageAgent : public Agent {class MessageAgent : public Agent {

public:public:

MessageAgent() : Agent(MessageAgent() : Agent(PT_MESSAGEPT_MESSAGE) {}) {}

virtual int command(int argc, const char*const* virtual int command(int argc, const char*const* argv);argv);

virtual void recv(Packet*, Handler*);virtual void recv(Packet*, Handler*);

};};

Page 37: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3737USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message – Step 3

Packet processing: sendint MessageAgent::command(int, const char*const* argv)int MessageAgent::command(int, const char*const* argv){{

Tcl& tcl = Tcl::instance();Tcl& tcl = Tcl::instance();if (strcmp(argv[1], "send") == 0) {if (strcmp(argv[1], "send") == 0) {

Packet* pkt = allocpkt();Packet* pkt = allocpkt();hdr_msg* mh = hdr_msg::access(pkt);hdr_msg* mh = hdr_msg::access(pkt);// We ignore message size check...// We ignore message size check...strcpy(mh->msg(), argv[2]);strcpy(mh->msg(), argv[2]);send(pkt, 0);send(pkt, 0);return (TCL_OK);return (TCL_OK);

}}return (Agent::command(argc, argv));return (Agent::command(argc, argv));

}}

Page 38: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3838USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Agent/Message – Step 4

Packet processing: receivevoid MessageAgent::recv(Packet* pkt, Handler*)void MessageAgent::recv(Packet* pkt, Handler*){{

hdr_msg* mh = hdr_msg::access(pkt);hdr_msg* mh = hdr_msg::access(pkt);

// OTcl callback// OTcl callbackchar wrk[128];char wrk[128];sprintf(wrk, "%s recv {%s}", name(), mh->msg());sprintf(wrk, "%s recv {%s}", name(), mh->msg());Tcl& tcl = Tcl::instance();Tcl& tcl = Tcl::instance();tcl.eval(wrk);tcl.eval(wrk);

Packet::free(pkt);Packet::free(pkt);}}

Page 39: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

3939USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Outline

Extending ns In OTcl In C++ Debugging: OTcl/C++, memory Pitfalls

Page 40: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4040USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Debugging C++ in ns

C++/OTcl debugging

Memory debugging purify dmalloc

Page 41: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4141USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

C++/OTcl Debugging

Usual technique Break inside command() Cannot examine states inside OTcl!

Solution Execute tcl-debug inside gdb

Page 42: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4242USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

C++/OTcl Debugging

(gdb) (gdb) call Tcl::instance().eval(“debug 1”)call Tcl::instance().eval(“debug 1”)15: lappend auto_path $dbg_library15: lappend auto_path $dbg_librarydbg15.3> wdbg15.3> w*0: application*0: application 15: lappend auto_path $dbg_library15: lappend auto_path $dbg_librarydbg15.4> Simulator info instancesdbg15.4> Simulator info instances_o1_o1dbg15.5> _o1 nowdbg15.5> _o1 now00dbg15.6> # and other fun stuffdbg15.6> # and other fun stuffdbg15.7> dbg15.7> cc(gdb) where(gdb) where#0 0x102218 in write()#0 0x102218 in write()............

Page 43: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4343USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Memory Debugging in ns

Purify Set PURIFY macro in ns Makefile Usually, put -colloctor=<ld_path>

Gray Watson’s dmalloc library http://www.dmalloc.com make distclean ./configure --with-

dmalloc=<dmalloc_path> Analyze results: dmalloc_summarize

Page 44: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4444USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

dmalloc: Usage

Turn on dmalloc alias dmalloc ’eval ‘\dmalloc –C \!*`’ dmalloc -l log low

dmalloc_summarize ns < logfile ns must be in current directory Itemize how much memory is

allocated in each function

Page 45: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4545USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Pitfalls

Scalability vs flexibility Or, how to write scalable simulation?

Memory conservation tipsMemory leaks

Page 46: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4646USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Scalability vs Flexibility

It’s tempting to write all-OTcl simulation Benefit: quick prototyping Cost: memory + runtime

Solution Control the granularity of your split

object by migrating methods from OTcl to C++

Page 47: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4747USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

THE Merit of OTcl

Program size, complexity

C/C++ OTcl

Smoothly adjust the granularity of scripting to balance extensibility and performanceWith complete compatibility with existing simulation scripts

high low

split objects

Page 48: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4848USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Object Granularity Tips

Functionality Per-packet processing C++ Hooks, frequently changing code

OTcl

Data management Complex/large data structure C++ One-time configuration variables

OTcl

Page 49: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

4949USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Memory Conservation Tips

Avoid trace-alltrace-all

Use arrays for a sequence of variables Instead of n$in$i, say n($i)n($i)

Avoid OTcl temporary variablesUse dynamic binding delay_bind()delay_bind() instead of bind()bind() See object.{h,cc}

Page 50: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

5050USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Memory Leaks

Purify or dmalloc, but be careful about split objects:for {set i 0} {$i < 500} {incr i} {for {set i 0} {$i < 500} {incr i} {

set a [new set a [new RandomVariable/Constant]RandomVariable/Constant]

}} It leaks memory, but can’t be detected!

Solution Explicitly delete EVERY split object that

was new-ed

Page 51: 1 Part III: Extending ns. USC INFORMATION SCIENCES INSTITUTE 2 Outline Extending ns In OTcl In C++ Debugging

5151USC INFORMATION SCIENCES INSTITUTEUSC INFORMATION SCIENCES INSTITUTE

Final Word

My extended ns dumps OTcl scripts! Find the last 10-20 lines of the dump Is the error related to “_o*** cmd

…” ? Check your command()

Otherwise, check the otcl script pointed by the error message