The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ... The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

nmlab.korea.ac.kr
from nmlab.korea.ac.kr More from this publisher
12.07.2015 Views

24.5.1 OTclOTcl, especially TclCL, provides a way to allocate new objects. However, it does not accordingly provide a garbage collectionmechanism for these allocated objects. This can easily lead to unintentional memory leaks. Important: tools such as dmallocand purify are unable to detect this kind of memory leaks. For example, consider this simple piece of OTcl script:set ns [new Simulator]for set i 0 $i < 500 incr iset a [new RandomVariable/Constant]One would expect that the memory usage should stay the same after the first RandomVariable is allocated. However, becauseOTcl does not have garbage collection, when the second RandomVariable is allocated, the previous one is not freed andhence results in memory leak. Unfortunately, there is no easy fix for this, because garbage collection of allocated objects isessentially incompatible with the spirit of Tcl. The only way to fix this now is to always explicitly free every allocated OTclobject in your script, in the same way that you take care of malloc-ed object in C/C++.24.5.2 C/C++Another source of memory leak is in C/C++. This is much easier to track given tools that are specifically designed for thistask, e.g., dmalloc and purify. nshas a special target ns-pure to build purified ns executable. First make sure that the macroPURIFY in the ns Makefile contains the right -collector for your linker (check purify man page if you don’t know what thisis). Then simply type make ns-pure. See earlier sections in this chapter on how to use ns with libdmalloc.219

Chapter 25Mathematical SupportThe simulator includes a small collection of mathematical functions used to implement random variate generation and integration.This area of the simulator is currently undergoing some changes.The procedures and functions described in this chapter can be found in ~ns/tools/rng.{cc, h}, ~ns/tools/random.{cc, h},~ns/tools/ranvar.{cc, h}, ~ns/tools/pareto.{cc, h}, ~ns/tools/expoo.{cc, h}, ~ns/tools/integrator.{cc, h}, and ~ns/tcl/lib/nsrandom.tcl.25.1 Random Number GenerationThe RNG class contains an implementation of the combined multiple recursive generator MRG32k3a proposed by L’Ecuyer[16]. The C++ code was adapted from [18]. This replaces the previous implementation of RNG, which used the minimalstandard multiplicative linear congruential generator of Park and Miller [27]. The newer (MRG32k3a) RNG is used in nsversions 2.1b9 and later.The MRG32k3a generator provides 1.8x10 19 independent streams of random numbers, each of which consists of 2.3x10 15substreams. Each substream has a period (i.e., the number of random numbers before overlap) of 7.6x10 22 . The period of theentire generator is 3.1x10 57 . Figure 25.1 provides a graphical idea of how the streams and substreams fit together.A default RNG (defaultRNG), created at simulator initialization time, is provided. If multiple random variables are used ina simulation, each random variable should use a separate RNG object. When a new RNG object is created, it is automaticallyseeded to the beginning of the next independent stream of random numbers. Used in this manner, the implementation allowsfor a maximum of 1.8x10 19 random variables.Often, multiple independent replications of a simulation are needed (i.e., to perform statistical analysis given multiple runswith fixed parameters). For each replication, a different substream should be used to ensure that the random number streamsare independent. (This process is given as an OTcl example later.) This implementation allows for a maximum of 2.3x10 15independent replications. Each random variable in a single replication can produce up to 7.6x10 22 random numbers beforeoverlapping.Note: Only the most common functions are described here. For more information, see [18] and the source code found intools/rng.h and tools/rng.cc. For a comparison of this RNG to the more common LCG16807 RNG (and whyLCG16807 is not a good RNG), see [17].220

24.5.1 OTclOTcl, especially TclCL, provides a way to alloc<strong>at</strong>e new objects. However, it does not accordingly provide a garbage collectionmechanism for these alloc<strong>at</strong>ed objects. This can easily lead to unintentional memory leaks. Important: tools such as dmalloc<strong>and</strong> purify are unable to detect this kind of memory leaks. For example, co<strong>ns</strong>ider this simple piece of OTcl script:set <strong>ns</strong> [new Simul<strong>at</strong>or]for set i 0 $i < 500 incr iset a [new R<strong>and</strong>omVariable/Co<strong>ns</strong>tant]One would expect th<strong>at</strong> the memory usage should stay the same after the first R<strong>and</strong>omVariable is alloc<strong>at</strong>ed. However, becauseOTcl does not have garbage collection, when the second R<strong>and</strong>omVariable is alloc<strong>at</strong>ed, the previous one is not freed <strong>and</strong>hence results in memory leak. Unfortun<strong>at</strong>ely, there is no easy fix for this, because garbage collection of alloc<strong>at</strong>ed objects isessentially incomp<strong>at</strong>ible with the spirit of Tcl. <strong>The</strong> only way to fix this now is to always explicitly free every alloc<strong>at</strong>ed OTclobject in your script, in the same way th<strong>at</strong> you take care of malloc-ed object in C/C++.24.5.2 C/C++Another source of memory leak is in C/C++. This is much easier to track given tools th<strong>at</strong> are specifically designed for thistask, e.g., dmalloc <strong>and</strong> purify. <strong>ns</strong>has a special target <strong>ns</strong>-pure to build purified <strong>ns</strong> executable. First make sure th<strong>at</strong> the macroPURIFY in the <strong>ns</strong> Makefile contai<strong>ns</strong> the right -collector for your linker (check purify man page if you don’t know wh<strong>at</strong> thisis). <strong>The</strong>n simply type make <strong>ns</strong>-pure. See earlier sectio<strong>ns</strong> in this chapter on how to use <strong>ns</strong> with libdmalloc.219

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!