C: analyze bottleneck in C programs

My C is critical. Some functions are called millions of times, so I would like to know how much time is spent on each function, giving me the following:

Total time: 100s forward(): 20s; align(): 15s; ... others: 1s. 

Can a debugger perform such an analysis? I use Eclipse CDT on Ubuntu and use gdb for debugging. Some guy suggested Valgrind , but I did not find a suitable one. I found some questions about C # or php or perl profiling, any suggestions for C? Thanks.

=============================================

Followup: thanks for the help, gprof seems really nice. Here is the manual link: http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html .

Question about the interpretation of the resume:

 Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls us/call us/call name 61.29 9.18 9.18 bwa_print_sam_SQ 21.96 12.47 3.29 bwt_sa 4.01 13.07 0.60 bns_coor_pac2real 3.87 13.65 0.58 bwt_match_exact_alt 2.60 14.04 0.39 bwa_read_seq 1.00 14.19 0.15 bwt_match_gap 0.80 14.45 0.12 seq_reverse 

If I'm not mistaken, he says that the bwa_print_sam_SQ function takes up 61.29% of the total time. But my program runs for 96.24 seconds, this function should work for about 60 seconds. Why is the โ€œcumulativeโ€ second column only 9.18? The manual says:

 cumulative seconds This is the cumulative total number of seconds the computer spent executing this functions, plus the time spent in all the functions above this one in this table. 

And I use the parameter

 "gprof -f pe_sai2sam_se_core -f bwa_print_sam_SQ -f seq_reverse ./peta > gprof", 

where the pe_sai2sam_se_core function calls bwa_print_sam_SQ in a long time loop. Why does the report say:

 index % time self children called name <spontaneous> [1] 61.3 9.18 0.00 bwa_print_sam_SQ [1] ----------------------------------------------- <spontaneous> [8] 0.8 0.12 0.00 seq_reverse [8] ----------------------------------------------- 

He did not say anything about pe_sai2sam_se_core ... Why?

+4
source share
1 answer

You do not need a debugger. What you need is called a profiler. Since you mention Ubuntu, you probably want to start with gprof .

Here you can use gprof :

  • Disabling all compiler optimizations for your program ( -O0 ) is optional, of course
  • Add the -g and -pg
  • Restore and run the program as usual
  • At this point, your program should have created the gmon.out file in cwd
  • Use gprof to verify data:

     gprof ./your_program > prof 

Now you can view the prof file. It starts with a flat profile that just tells you how much time it spends in various functions.

+9
source

Source: https://habr.com/ru/post/1385672/


All Articles