Thread: programming profiling using profil

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    programming profiling using profil

    Hi,

    I want to profile my multithreaded (pthreads) program, and I found that I can use profil tool of linux. (man profil).

    But it does require arguments like:
    int profil(char *samples, size_t size, u_long offset, u_int scale);
    and I really dont know how to provide these arguments. Does any body has experience in writing program using profil, or perhaps write a sample code to let me know to specify a specific code in a thread to profile

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This page: http://linux.about.com/library/cmd/blcmdl3_profil.htm
    says:
    buffer is a block or memoyr, bufsiz it's size.
    offset and scale are used to scale down the results to fit into a smaller buffer.

    Offset would be the first address that you want to profile.
    Scale allows you to determine how fine-grained the profiling should be - if you want to profile every byte, give it a value of 65536 - that essentially means that your buffer needs to be 2 x the size of the code you are profiling. A smaller scale gives you less precise results, but you don't need as large a buffer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    thanks,

    how to specify the block of memory, if i want to profile between two points in a thread:

    thread ()
    {
    ....
    point1:
    ...

    point2:

    profile(.....)
    }
    how do i specify the block of memory for these two points. and how do i know the size of the block in memory.
    so for i understand offset if the beginning address of the block, size is how long is the block, but how to specify these parameters. Also when the profiling returns does it return the count of the clock ticks.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If we assume that point1 and point2 isn't many megabytes of code apart (reasonable assumption if it's a thread function, I believe), then I would set the scale to 65536, and try to figure out the size fo the thread function itself - for example by taking the address of the thread function and the function after - allocate that size times sizeof(u_short).

    If thread calls a lot of other functions, find the first function and the end of the last function that you call, and use that for size.

    If you want to save a bit of memory, set scale to 32768 and half the allocation size - or 16384 and a quarter of the allocation size.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    If I am right I can get address of a function like this

    void (*foo)() = NULL;
    foo = &thread;

    but it does return me 1 always, any idea to get it correctly?

    how to make the buf to point to the block of memory in between two functions?

    Thanks

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you don't want buf to point to the code - you want buf to point to a chunk of memory with a size corresponding to the size of code you want to profile (e.g. that size * sizeof(u_short)) - for example, you can allocate it using malloc, or have a fixed size array of u_short buffer[2000] if you think 2000 bytes will cover your function).

    Not sure how &thread can be 1. But &thread would be a good value for "offset".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    Hey,

    I code it like this. Can you help me in fixing the size and offset in the profil. And also how to receive the number of cpu ticks from the function, is that the return value of profil.

    Code:
    #include <unistd.h>
    int main()
    {
    	int (*foo)() = NULL;
    	foo = &main;
    
    	int size = 3000;
    	char *buff;
    	unsigned int bufsize = size * sizeof(short);
    	buff = (char*)malloc(bufsize * sizeof(short));
    	unsigned int offset = (unsigned int)foo;
    	unsigned int scale = 65536;
    
    	int y = profil(buff, bufsize, offset, scale);
    
    	int x = 10;
    	for(int i=0; i<10; i++)
    	{
    		x = x * x;
    		x = x / x;
    	}
    
    	return 0;
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    For timing, I'd use clock(). profil() doesn't return the number of ticks or any other meaningfull value - it returns zero on success (and I'm not sure what happens if it fails).

    Your loop is far too short to take 10 milliseconds (the normal timer tick in Linux). It also only contains a few instructions, so I guess it will not be very likely to give you much meaningfull information, but if you make it a few million iterations, you should be able to see something in your buffer. Just check if any of the entries in buffer are non-zero after the loop.

    Also, you don't actually need the variable foo - just use main wherever you use foo now.

    Also, you shouldn't cast malloc() - but you are not including stdlib, so I guess the compiler complains about the malloc line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    10
    i know about clock, but the problem is that i will use this in multiple threads, and clock will include time of all threads scheduled between two points.

    I checked the buffer after changing the loops iteration, infact creating another loop inside the loop of a long iteration, but I dont get any non zero value in buffer.

    If I dont cast malloc, i receive an invalid conversion error, and compiler does not complain of not using stdlib. btw i am using gcc so it might not be required here. (but i dunno)

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is complaining about the conversion BECAUSE you are not using stdlib.h - if you do (and you are compiling your code as C, rather than C++), then it should be fine to just assign the result from malloc into any pointer variable.

    There is no simple way to get individual threads timing - it's been discussed a few times, and you can read the pseudo-file at /proc/<pid>/stat/<thread> or some such.

    Note also that if you run multiple threads, profil will not care WHICH thread you are in, just where in the code it is. So if multiple threads are running in the same function, you won't see which of the threads you are spending time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. profiling
    By zaac in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2009, 10:06 AM
  2. Profiling for c code in eclipse
    By hai12345 in forum C Programming
    Replies: 2
    Last Post: 10-10-2008, 04:46 PM
  3. I can't get profiling to work with Visual Studio 6
    By Darkness in forum C++ Programming
    Replies: 7
    Last Post: 12-30-2004, 10:37 PM
  4. Memory profiling and debugging tools
    By nickname_changed in forum Linux Programming
    Replies: 4
    Last Post: 09-14-2004, 03:58 PM
  5. Enabling Profiling in VS.NET
    By xds4lx in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-17-2002, 01:10 PM