Thread: Problem with profiling tool gprof

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Problem with profiling tool gprof

    Hi,
    I have a UDP client in C. I need to profile this program. I have used gprof. Now the makefile looks like:

    Code:
    all: UDPClientProject
    #Macro definitions
    CC = gcc
    CFLAGS = -lm
    INCLUDES = -I/usr/include/mysql
    LIBS = -L/usr/lib/mysql -lmysqlclient
    
    xTRClient.o: xTRClient.c map_registration.h
    	$(CC) $(CFLAGS) -pg -c xTRClient.c
    
    TCP_server_address_db.o: TCP_server_address_db.c map_registration.h
    	$(CC) $(CFLAGS) $(INCLUDES) -pg -c TCP_server_address_db.c
    
    UDPClientProject: xTRClient.o TCP_server_address_db.o
    	$(CC) -pg -o UDPClientProject xTRClient.o TCP_server_address_db.o $(LIBS)
    
    clean:
    	-rm xTRClient.o TCP_server_address_db.o
    After the program runs (its a large code, so it takes a while), the output for the profile looks like (important parts are only shown):

    Code:
    Flat profile:
    
    Each sample counts as 0.01 seconds.
     no time accumulated
    
      %   cumulative   self              self     total           
     time   seconds   seconds    calls  Ts/call  Ts/call  name    
      0.00      0.00     0.00      100     0.00     0.00  append
      0.00      0.00     0.00        8     0.00     0.00  delete_query
      0.00      0.00     0.00        8     0.00     0.00  get_map_notify_packet
      0.00      0.00     0.00        8     0.00     0.00  insert_query
      0.00      0.00     0.00        8     0.00     0.00  map_notify_packet_initialization
      0.00      0.00     0.00        8     0.00     0.00  map_register_packet_initialization
      0.00      0.00     0.00        8     0.00     0.00  tcp_server_access_main
      0.00      0.00     0.00        1     0.00     0.00  access_file_insert_data
    
    ................................
    
    granularity: each sample hit covers 4 byte(s) no time propagated
    
    index % time    self  children    called     name
                    0.00    0.00     100/100         access_file_insert_data [8]
    [1]      0.0    0.00    0.00     100         append [1]
    -----------------------------------------------
                    0.00    0.00       8/8           tcp_server_access_main [7]
    [2]      0.0    0.00    0.00       8         delete_query [2]
    -----------------------------------------------
                    0.00    0.00       8/8           main [15]
    [3]      0.0    0.00    0.00       8         get_map_notify_packet [3]
    -----------------------------------------------
                    0.00    0.00       8/8           tcp_server_access_main [7]
    [4]      0.0    0.00    0.00       8         insert_query [4]
    -----------------------------------------------
                    0.00    0.00       8/8           main [15]
    [5]      0.0    0.00    0.00       8         map_notify_packet_initialization [5]
    -----------------------------------------------
                    0.00    0.00       8/8           main [15]
    [6]      0.0    0.00    0.00       8         map_register_packet_initialization [6]
    -----------------------------------------------
                    0.00    0.00       8/8           main [15]
    [7]      0.0    0.00    0.00       8         tcp_server_access_main [7]
                    0.00    0.00       8/8           delete_query [2]
                    0.00    0.00       8/8           insert_query [4]
    -----------------------------------------------
                    0.00    0.00       1/1           main [15]
    [8]      0.0    0.00    0.00       1         access_file_insert_data [8]
                    0.00    0.00     100/100         append [1]
    -----------------------------------------------
    Please tell me if I had done something wrong or not in the makefile.
    Most importantly, why are all the time 0.0. The program is large and it takes quite a lot of time. Why am I getting 0.0 instead of the actual time?

    Another question is: how do u use gprof with a UDP server program. Because as u know, the UDP server goes into an infinite loop and I have to press CTRL+C to stop. If I stop the program in this way, then gmon.out doesn't get generated. What should I do?

    Bye.

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    By the way, I am using:
    Distributor ID: Ubuntu
    Description: Ubuntu 10.10
    Release: 10.10
    Codename: maverick

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    It might be taking a long time, but is all that time being spent in library code?
    I mean, 100 calls to append(), which does very little except call some library function isn't going to accumulate enough time of it's own. 0.01 seconds is a long time for a modern CPU.

    > the UDP server goes into an infinite loop and I have to press CTRL+C to stop. If I stop the program in this way, then gmon.out doesn't get generated. What should I do?
    You need to add a bit of test code to cause it to exit normally, say for example a set period of time after the last client has closed the connection.
    Or you send it a special "quit now" message over some interface.
    Or you have a signal handler for say SIGUSR1, which internally sets some "quit now" flag (to be tested in your infinite loop), and which you trigger by typing "kill -s USR1 pid" in a console.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    Quote Originally Posted by Salem View Post
    It might be taking a long time, but is all that time being spent in library code?
    I mean, 100 calls to append(), which does very little except call some library function isn't going to accumulate enough time of it's own. 0.01 seconds is a long time for a modern CPU.

    > the UDP server goes into an infinite loop and I have to press CTRL+C to stop. If I stop the program in this way, then gmon.out doesn't get generated. What should I do?
    You need to add a bit of test code to cause it to exit normally, say for example a set period of time after the last client has closed the connection.
    Or you send it a special "quit now" message over some interface.
    Or you have a signal handler for say SIGUSR1, which internally sets some "quit now" flag (to be tested in your infinite loop), and which you trigger by typing "kill -s USR1 pid" in a console.
    Hi, thanks for your reply and idea. But is there any way to get a millisecond or microsecond timing of the program?

    Bye.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Out of gprof - no.

    But look at it this way, if the wall-clock time of the entire program is say 10 seconds, but no single bit of your code is breaking 0.01 seconds, then what are you worried about (from a performance point of view).

    Knowing update() takes 12ms (say) doesn't help much, as re-writing it to take say 10ms won't make a bean of difference to the 10 seconds overall.

    REAL performance improvements are going to come from better UDP traffic management, and better use of mysql. It might look like a fairly innocent query, but are you sure of how much work it is actually causing?

    Not calling append() 100 times would probably help quite a lot, but making append() slightly quicker (whilst ignoring what it does to say mysql) won't change the overall picture.

    The first thing is, is 100 calls expected from your test case(s)? If it isn't, then that is something you really need to look into.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiling other than gprof
    By Kempelen in forum C Programming
    Replies: 1
    Last Post: 06-02-2011, 11:13 AM
  2. Gprof under windows
    By Kempelen in forum C Programming
    Replies: 7
    Last Post: 05-23-2008, 05:49 PM
  3. Profiling with GPROF
    By RoshanX in forum C Programming
    Replies: 2
    Last Post: 03-30-2007, 02:38 PM
  4. Visual C++ Tool Problem
    By wolfban in forum Tech Board
    Replies: 2
    Last Post: 07-23-2005, 07:41 PM
  5. BC++ FreeCommandLine Tool Problem
    By Kelvin in forum C++ Programming
    Replies: 8
    Last Post: 07-06-2002, 09:46 AM