DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Analyzing run-time behavior

Profiling archive or shared object library code with prof

You can use prof to profile archive library code as long as you specify -p when you compile the library source files, and again when you link the library with your program:

   $ cc -p -c function1.c function2.c
   $ ar -r libfoo.a function1.o function2.o
   $ cc -c test1.c
   $ cc -dn -p -o test1 -L. test1.o -lfoo
   $ test1
   $ prof test1
Note that in the above example, test1.c was not compiled with the -p option. Any clock ticks occurring in functions defined in files such as test1.c that are not compiled for profiling will still result in timing data being produced. However, no call count information, that is the number of times such functions are called, will be collected.

To profile test1 as well as the library code, you specify -p when you compile test1.c. Profiling data for both the library code and the test program are written to mon.out.

You can profile shared object library code with prof by specifying -p when you compile and link the library functions, and again when you link the library with your program.

   $ cc -K PIC -p -c function1.c
   $ cc -p -G -o libfunc1.so function1.o
   $ cc -K PIC -c function2.c
   $ cc -G -o libfunc2.so function2.o
   $ cc -p -c test1.c
   $ cc -p -o test1 -L. test1.o -lfunc1 -lfunc2
   $ LD_LIBRARY_PATH=. export LD_LIBRARY_PATH
   $ test1
Unlike lprof, prof writes profiling data for the library functions to the default mon.out data file. When the prof command is invoked it will print, by default, separate listings for the program and each shared library that has been loaded. Since libfunc2.so was compiled without the -p option, call count data will not be generated but any time spent in functions defined in libfunc2.so, only function2 in this case, will be recorded and displayed when prof is run. If no timing statistics and call counts have been collected for any loaded object, prof will produce a statement indicating how many symbols qualified for profiling yet no data was collected.

For example, using the commands listed above and the following source code,

   $ cat test1.c
   main()
   {
           int i;
           for (i=0;i<100000;i++)
   	/* spend some time */
   		;
           for (i=0;i<10;i++)
   		function1();
           function2();
   }
   

$ cat function1.c function1() { int i; for (i = 0; i < 1000000; i++) /* spend time */ ; } $ cat function2.c function2() { int j,c; for (j = 0; j < 500000; j++) c = c + 1; for (; j > 5; j--) c = c - 1; }

prof would generate output such as:
   $prof test1
   

Object: test1

%Time Seconds Cumsecs #Calls msec/call Name 1.0 0.01 0.01 1 10. main

Object: ./libfunc1.so:

%Time Seconds Cumsecs #Calls msec/call Name 90.0 0.90 0.91 10 90.0 function1

Object: ./libfunc2.so:

%Time Seconds Cumsecs #Calls msec/call Name 9.0 0.09 1.00 function2

Object: /usr/lib/libc.so.1

664/2248 qualified symbols: no time or call count data collected.

To produce one listing, the -j option to prof should be used causing prof to display the name of the object associated with each symbol.

   $prof -j test1
   

%Time Seconds Cumsecs #Calls msec/call Name 90.0 0.90 0.90 10 90.0 ./libfunc1.so:function1 9.0 0.09 0.99 ./libfunc2.so:function2 1.0 0.01 1.00 1 10. test1:main

For a discussion of how libraries are created and linked with your program, see ``Link editing''.


Next topic: Using fprof
Previous topic: profiling C++ programs with prof

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004