29.3.16

Static Golang Call graphs

(warning: you have to zoom in, or else it will be unreadable)
I've been playing around with cleaning up kubernetes e2e wrapper functionality lately, and found the need to create call graphs.

In the past, I've done this with java parsers and the graphviz API.

But, for golang, I found the easiest way was to use callgraph, which actually is graphviz friendly.

When I first ran it, it gave me issues with CGO, package paths, etc. etc.  Basically you need to do 3 things.

  • make sure your gopath is correct when you run it.  if the code doesnt compile, the paths cant be generated.
  • on a mac (at least) , export the disabled CGO env var.
  • rather then being clever with package exclusion, i just grepped out the edges i cared about and reconstituted a correct graphviz file after the fact.  otherwise the file is too big and its useless.
 weve been diving into the dependency calls in test/e2e, and found an easy way to make call graphs.


So, here's how i did this for kubernetes.  The instructions should work WLOG for any golang project, esp. since kubernetes is much more complex than most golang projects....


First, get callgraph
Now export your gopath (you fish out the correct one by running hack/build-go.sh and echo'ing the gopath before the build steps)

export GOPATH=/Users/jayunit100/Development/gopath/src/k8s.io/kubernetes/_output/local/go:/Users/jayunit100/Development/gopath/src/k8s.io/kubernetes/Godeps/_workspace:/Users/jayunit100/Development/gopath/

Now, disable CGO, its not needed and can cause issues here... export CGO_ENABLED=0

Finally, run callgraph.  After it runs, grep out the stuff you care about or else you will get ~ an unusable half million line file. ~/Development/gopath/bin/
callgraph -format=graphviz -algo static ./test/e2e/*.go | grep -v exitsyscall | grep -v gopkg | grep e2e | grep -v \( | grep -v init | grep wait > e2e.dot

Since you grepped out parts of the file, correct graphviz formatting add "digraph xyz" >> {"
add "}" to the bottom.

Format and view your lovely new graph.
neato e2e.dot > e2en.dot ; dot -Tpng e2en.dot > e2e.png ; open e2e.png

regarding the hacky step in the middle where we grep certain things out... Im sure there might be a more elegant way to deal with removing unwanted packages.  For me it seemed to be an all or nothing deal with callgraph.

No comments:

Post a Comment