2009年2月4日星期三

The Importance of not Using "bind()" in NS2

As described in my last post, I used valgrind to find out the memory usage of my simulator. Finally, I found that a invocation of "bind()" in UdpAgent::UdpAgent()" consumes most memory. That reminds me that the ns2 manual recommends to use dynamic binding instead of "bind" when the object of a class is frequently created. In my case, the P2P application requires two UdpAgents for a connection. Since the connections are created and removed constantly, the memory consumed by bind() keeps increasing. To solve this problem, I modified class UdpAgent as follows:

apps/udp.h
add two declarations:
virtual void delay_bind_init_all();
virtual int delay_bind_dispatch(const char *varName, const char *localName, TclObject *tracer);
apps/udp.cc
delete "bind("packetSize_", &size_);" in UdpAgent::UdpAgent() and UdpAgent::UdpAgent(packet_t type)
add two definition:
void UdpAgent::delay_bind_init_all()
{
delay_bind_init_one("packetSize_");
Agent::delay_bind_init_all();
}

int UdpAgent::delay_bind_dispatch(const char *varName, const char *localName, TclObject *tracer)
{
if (delay_bind(varName, localName, "packetSize_", &size_, tracer))
return TCL_OK;
return Agent::delay_bind_dispatch(varName, localName, tracer);
}

Then type "make clean; make" to rebuild ns2. Now the memory usage and simulation time is reduced significantly.

I'm not very clear about what does it do when "bind()" is invoked and why the memory is not released even when the UdpAgent object is deleted. Anyone who knows the answer please post me a message.

没有评论: