1

I want to profile a portion of the C code (user_defined_function())using and calculate the time taken to execute it. Any pointers on how to do this would be very helpful. Thanks in advance!!

#include <stdio.h>
int main()  
{  
    //some statements;

    //Begin Profiling  
    user_defined_function();  
    //End Profiling  

    //some statements;
    return 0;  
}  
Vipin
  • 13
  • 1
  • 6
  • SO is not well suited for open-ended questions, and it's unlikely that anyone will write a complete tutorial for you. Please narrow down your question, and explain what problem you are having. – user694733 Mar 01 '18 at 08:47
  • Possible duplicate of [Execution time of C program](https://stackoverflow.com/questions/5248915/execution-time-of-c-program) – nima moradi Mar 01 '18 at 08:58
  • 1
    Thanks for the quick response. I am sorry, I realized was not being clear with my original question. So, I am able to calculate the execution time using clock( ) but I am not sure how to use oprofile with in the c program. The official documentation for oprofile is currently unavailable. I am able to use oprofile for profiling the entire application but not for a portion of the code. Also, I am not looking for some one to write up an entire tutorial for me here. I would like to get some pointers about how to use it from someone who has experience using it. Thanks! – Vipin Mar 01 '18 at 21:37

1 Answers1

0

I don't see turn on / turn off markers in the http://oprofile.sourceforge.net/doc/index.html and http://oprofile.sourceforge.net/faq/ documentation. Probably calling (fork+exec) opcontrol with --start and --stop will help if the code to be profiled is long enough.

With perf tool in profiling (sampling) mode perf record (and/or probably operf which is based on the same perf_event_open syscall) you can try to profile full program and add some markers at Begin Profiling and End Profiling points (by using some custom tracing event), then you can dump entire perf.data with perf script, find events of your markers and cut only part of the profile between markers (every event in perf.data has timestamp and they are ordered or can be sorted by time).

With direct use of perf_event_open syscall you can enable and disable profiling from the same process with ioctl calls described in the "man 2 perf_event_open" page on the fd descriptor of perf with PERF_EVENT_IOC_ENABLE / PERF_EVENT_IOC_DISABLE actions. Man page also lists using prctl to temporary disable and reenable profiling on the program (this may even work with oprofile, disable at start of main, enable at Begin, disable at End)

Using prctl(2) A process can enable or disable all the event groups that are attached to it using the prctl(2) PR_TASK_PERF_EVENTS_ENABLE and PR_TASK_PERF_EVENTS_DISABLE operations.

Another way of using performance counter is not sampling profiling, but counting (perf stat ./your_program / perf stat -d ./your_program does this). This mode will not give you list of 'hot' functions, it just will say that your code did 100 millions of instructions in 130 millions of cycles, with 10 mln L1 cache hits and 5 mln L1 cache misses. There are wrappers to enable counting on parts of program, for example: PAPI http://icl.cs.utk.edu/papi/ (PAPI_start_counters), perfmon2 (libpfm3,libpfm4), https://github.com/RRZE-HPC/likwid (pdf, likwid_markerStartRegion), http://halobates.de/jevents.html & http://halobates.de/simple-pmu, etc..

osgx
  • 90,338
  • 53
  • 357
  • 513
  • Thank you very much. perf_event_open( ) with ioctl calls is exactly what I was looking for!! – Vipin Mar 23 '18 at 07:24
  • VIPIN, check also "prctl(2) PR_TASK_PERF_EVENTS_ENABLE and PR_TASK_PERF_EVENTS_DISABLE operations" - they may work with externals tools like perf and operf/oprofile. – osgx Mar 23 '18 at 14:55