问题
I'm having a hard time tracking down unnecessary redundant calls within a rather complex algorithm.
It looks like (some of my) my algorithm(s) is/are seriously slowed down by redundant calls (in several subroutines) of a non-cached and comparatively expensive function. To confirm this I'd like to utilize Dtrace to detect multiple calls of a single function within a given branch of the call stack tree.
I'd like to be able to ask dtrace to:
search the call stack tree within a given function (here
"foo();"
, see attached image) for duplicate function calls ("c();"
, e.g.)log them with their respective call count (here: 3x for
"c();"
)
and if possible also
- log the call stacks of each occurrence (
"foo()/a()/c()"
,"foo()/a()/b()/c()"
,"foo()/a()/b()/d()/c()"
).
Is this possible? And if yes, any idea how?
Thanks in advance!
Note: I used C in my sample code even though my code is actually in Objective-C, but this kind of thing should be kind of language-agnostic, shouldn't it? The general approach/idea at least.
回答1:
The method I use is random-pausing, as in this example. The idea is what you want to see is stack traces weighted by the wall-clock time they are responsible for. Function call sites responsible for significant time are preferentially displayed.
Then you just look at those. The stack traces tell you why they are being executed. From that you can tell if there is a way to do without them. If you do so, the time you save is the same as the fraction of time they were on the stack.
Note: If you do this, you don't need to care how many times the function was called from that site (or anywhere), or how long it takes to execute. All you need to care about is that the call site was on the stack on at least two samples, and that it can be removed.
来源:https://stackoverflow.com/questions/7147758/detecting-redundant-function-calls-within-call-stack-tree-with-dtrace