Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of <cycle 5> function in the output of cProfile analyzed using KchacheGrind?

I want to analyze the performance of the python code, I have used cProfile module for that and generated the .cprof file as mentioned in the python documentation. I am using pyprof2calltree python module to open the .cprof file into KCacheGrind. Screen Shot of KCacheGrind Tool. I have put the screenshot of the analysis result and it shows that function named cycle 5 is taking 100.04% of the CPU time. I am not able to what this stands for. It is also not showing any source code for this function.

like image 202
Bhargav Upadhyay Avatar asked Jun 08 '17 08:06

Bhargav Upadhyay


1 Answers

it shows that function named cycle 5 is taking 100.04% of the CPU time.

No, it shows that some "cycle 5" and all functions called from it and all called from them are using 100% "inclusive" time.

<cycle> are not real functions, it is how kcachegrind heuristically tries to get recursion information from the profiling format ("inclusive costs for calls inside of a cycle are meaningless"). This format (defined for callgrind) have no exact information of function call sequences (f1 calls f2 which calls f3 ...), only pairs caller-callee are stored. This format is exact only for "Self" time, but not for "Inclusive" (including all callees time) when there is recursion.

KCachegrind allow (and recommend) you to turn off the "Do Cycle Detection" with View Menu: https://kcachegrind.github.io/html/NewsOld.html

Cycle detection is switchable with a toolbar button. For GUI application, sometimes it's useful to switch cycle detection off, even if there are some visualization errors with recursive calls.

Without Cycle Detection no synthetic <cycle> functions will be generated, but some functions may have >100% "Incl." time. Try to use "Self" time, or profiling tools with better format (linux perf, operf, ocperf.py; google's cpuprofile and other uses profiling formats with full function call stacks). https://github.com/jrfonseca/gprof2dot lists many good formats and it also can visualize them correctly (if there is enough information). Try it with python profile format:

python profile

python -m profile -o output.pstats path/to/your/script arg1 arg2
gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png

python cProfile (formerly known as lsprof)

python -m cProfile -o output.pstats path/to/your/script arg1 arg2
gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png

python hotshot profiler

The hotshot profiler does not include a main function. Use the hotshotmain.py script instead.

 hotshotmain.py -o output.pstats path/to/your/script arg1 arg2
 gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png
like image 55
osgx Avatar answered Sep 22 '22 21:09

osgx