2009年1月14日星期三

Using gprof with NS2

gprof is a powerful tool to measure the running time of each function in a program, which is widely used to analyze and speed up programs.

To use gprof, a program should be BOTH compiled and linked with -pg, -g is also needed for compilation. For example, given a source file hello.c, it should be compiled using gcc -g -pg hello.c -o hello

In NS2, since the compilation process is done using Makefile, which specifies compilation options and link options in a separate form, two modifications should be done to the Makefile as follows:

CCOPT = -Wall -g -pg
LDFLAGS = -Wl,-export-dynamic -pg


Type "make clean & make" to regenerate executable file. Then run your simulation as usual. When ns2 exits, a new file "gmon.out is created. You can use gprof NS_BIN_DIR/ns gmon.out to see the results, where NS_BIN_DIR denotes the directory that the executable file "ns" is located.

1 条评论:

Felipe 说...

hey, did you ever get ns to show the call stack with gprof?