2015-07-11 39 views
12

我正在寻找为golang项目生成调用图的可能性。类似于C++类的Doxygen's diagram functionality(选项CALL_GRAPH = YES)。在golang中创建调用图

到目前为止,我发现

http://saml.rilspace.org/profiling-and-creating-call-graphs-for-go-programs-with-go-tool-pprof

http://blog.golang.org/profiling-go-programs

此样本程序的调用堆栈每秒,而在程序运行,并为分析有用的图表100倍。如果你的程序大部分时间花在与你无关的功能上,我发现这个解决方案不是非常有用。

再有是这样的:

https://godoc.org/golang.org/x/tools/go/callgraph/static

从它的描述听起来像什么,我需要,但似乎没有文档,我不知道如何使用它。

我还发现

https://github.com/davecheney/graphpkg/blob/master/README.md

https://github.com/paetzke/go-dep-graph/blob/master/README.org

,但他们只创建的依赖关系图。

回答

9

到这里看看:http://dave.cheney.net/2014/10/22/simple-profiling-package-moved-updated

func main() { 
    defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop() 
    // Rest of program 
} 

生成并运行您的程序按正常。你会看到提到的分析钩:

2015/07/12 09:02:02 profile: cpu profiling enabled, cpu.pprof 

运行程序(板凳它,通过它运行等)生成运行时配置文件。一旦你打你想要什么,退出并生成调用图:

go tool pprof --pdf $YOURPROGBINARY cpu.pprof > cgraph.pdf 

您还可以运行go tool pprof $YOURPROGBINARY cpu.pprof得到一个交互式的提示,你可以打电话top10web生成SVG。在pprof提示符下键入help以获取命令列表。

例如 - 这里有一个缓冲池实现我写的CPU简介:

~/Desktop go tool pprof poolio cpu.pprof 
Entering interactive mode (type "help" for commands) 
(pprof) top5 
24770ms of 35160ms total (70.45%) 
Dropped 217 nodes (cum <= 175.80ms) 
Showing top 5 nodes out of 74 (cum >= 650ms) 
     flat flat% sum%  cum cum% 
    12520ms 35.61% 35.61% 12520ms 35.61% runtime.mach_semaphore_wait 
    9300ms 26.45% 62.06%  9360ms 26.62% syscall.Syscall 
    1380ms 3.92% 65.98%  2120ms 6.03% encoding/json.(*encodeState).string 
    1030ms 2.93% 68.91%  1030ms 2.93% runtime.kevent 
    540ms 1.54% 70.45%  650ms 1.85% runtime.mallocgc 

下面是生成从提示一个PNG的快捷方式:

(pprof) png > graph.png 
Generating report in graph.png 

它输出这样的:

callgraph-example-poolio

+0

这是所描述的方法我提到的第一个链接;我忘了链接到原始网站。对我来说这并没有给出令人满意的结果;我看到很多我不感兴趣的函数调用,但是我没有看到我感兴趣的函数调用。在一些分析运行后,我看到一些有趣的功能,但从来没有。没有一个静态工具,它只是通过代码并创建图形? – alex

+0

我的问题之一是我也对仅执行一次的函数感兴趣(在安装过程中)。有没有办法提高工具的采样率,s.t.短跑功能也被抽样的概率增加了? – alex

+0

采样率在运行时硬编码/ pprof.go:587通过runtime.SetCPUProfileRate(hz)hz = 100,即每秒100个采样。如果在调用profile.Start()之前调用runtime.SetCPUProfileRate(desiredSamplingRate)*,则可以覆盖它。尝试在调用profile.Start()后设置采样率不起作用(“运行时:无法设置cpu配置文件速率,直到先前的配置文件已完成。”)。 – alex

9

您近在咫尺... /x/tools/go/callgraph/static。我很确定go install golang.org/x/tools/cmd/callgraph是你想要的。一旦安装运行它没有参数,看到它的完整的帮助/使用。

(一般情况下,下... /x/tools/的事情有些重复使用的封装,前端命令行下的生活... /x/tools/cmd,你可以安装他们所有go install golang.org/x/tools/cmd/...,文字/...比赛的所有子包)。

E.g.运行刚callgraph产生开头用法输出:

调用图:显示Go程序的调用图。

用法:

callgraph [-algo=static|cha|rta|pta] [-test] [-format=...] <args>...

标志:

-algo指定调用图构造算法,其一:

 static  static calls only (unsound) 
     cha   Class Hierarchy Analysis 
     rta   Rapid Type Analysis 
     pta   inclusion-based Points-To Analysis 

     The algorithms are ordered by increasing precision in their 
     treatment of dynamic calls (and thus also computational cost). 
     RTA and PTA require a whole program (main or test), and 
     include only functions reachable from main. 

-test包括包在分析测试。

-format指定显示每个调用图边的格式。 之一:

 digraph  output suitable for input to 
        golang.org/x/tools/cmd/digraph. 
     graphviz output in AT&T GraphViz (.dot) format. 

它可以产生任意格式的输出(使用Go的模板语法)或graphviz的或有向图的输出。最后是一个工具,您可以使用go install golang.org/x/tools/cmd/digraph进行安装(并且再次通过无参数运行可以看到完整/帮助使用情况),并且可以回答有关任意有向图的查询(显然包括调用图)。

+0

谢谢!我使用不同的算法标记对callgraph进行了实验,并尝试使用graphviz/dot生成pdf或使用有向图查询callgraph的输出。不幸的是,callgraph的输出主要包含libs,并且从callgraph的输出生成pdf并不能提供任何可用的内容。 digraph的查询功能可能会有帮助。对我来说最好的解决方案是,如果我可以过滤掉lib函数调用 - 特别是深度嵌套函数调用,为此我的代码甚至没有责任。也许我可以用callgraph的结果做一些后处理。 – alex

+0

还有另一个阻碍我使用callgraph的主要障碍 - 它拒绝工作,当我导入ZeroMQ绑定(github.com/pebbe/zmq3)时。第一个输出是“cgo pkg-config不支持”,并且比zmq包中的许多“未声明的名称”错误,尽管它们在包中声明。 – alex