jiffyclub / snakeviz

An in-browser Python profile viewer
https://jiffyclub.github.io/snakeviz/
Other
2.36k stars 139 forks source link

A function called by different function should have different called-info #150

Closed witie41 closed 4 years ago

witie41 commented 4 years ago

hello if function A called by function B and function C,there is only one called-info of function A in Snakeviz which is sum by both called by B and C.it leading to some incorrect graph such as some function's cumtime is smaller than their child's.

def A():
    pass
def B():
    A()
def C():
    for i in range(1000):
        A()
if __name__ == "__main__":
    B()
    C()

snakeviz_result thanks

witie41 commented 4 years ago

sorry,I just have a look about the result of cProfile,which give too less info to restore the call stack...

embray commented 4 years ago

Just to follow up, since I was just investigating this, I actually got a totally different result from you, where the call stack shows C but not B:

Screenshot from 2020-08-18 13-15-08

However, looking at the profile data, I see that zero time is reported in B. I'm not 100% certain but the Python interpreter does have a few built-in optimizations, so it's possible that it detected that B() does nothing but call A() and optimized away the call to B().

If I change it to something like:

def B():
    time.sleep(0.001)
    A()

then the call stack looks more like you're expecting:

Screenshot from 2020-08-18 13-17-01

I'm not sure why you had such different results though.

witie41 commented 4 years ago

Sorry to reply you late. I think you are right about the optimize of fun B,but my question is why the time showed below will more than itself. e.g.the function A's time is longer than function C's in my first picture.

embray commented 4 years ago

I'm not really sure I understand your question, since your original picture didn't show a call graph for C, only A and B. In any case, with such "do nothing" functions, so little time is spent in them that it may be difficult for the profiler to provide accurate accounting.

witie41 commented 4 years ago

I mean that a function's time spent should be separated by it's different caller,such as C's time spent should be separated into C from A and C from B if C called by A and B both. if not, it's just look like the time spent of C is longer than it`s caller B or A

jiffyclub commented 4 years ago

This is definitely a weird case! But yeah, what you're observing is due to cProfile only reporting the total time spent in a function, not differentiating how much time is spent when called in different call trees. SnakeViz displays exactly what's in the cProfile data.

witie41 commented 4 years ago

yeah,it can`t be solved by snakeviz,but thank you for discussion~

embray commented 4 years ago

@witie41 (As an aside, please do not use the backtick symbol ` when you mean to use an apostrophe ', as the backtick has special meaning in GitHub markdown. If for some reason you can't use the apostrophe I would just omit it.)

witie41 commented 4 years ago

hahahaha,I get it thanks~