如何在Go程序中使用pprof

huangapple go评论117阅读模式
英文:

How to use pprof in Go program

问题

如何在Go程序中使用pprof?

有一个名为net/http/pprof的Go包,但我无法使用它。

文档中说go tool pprof http://localhost:6060/debug/pprof/heap,但这并不起作用。

还有,下划线(_)代表什么意思?

import _ "net/http/pprof"

英文:

How to use pprof in Go program?

There is a Go package named net/http/pprof,but I can't use it.

The document says go tool pprof http://localhost:6060/debug/pprof/heap ,which does not work.

And,what does the below _ mean?

import _ "net/http/pprof"

答案1

得分: 11

根据你的评论,问题可能是你没有使用正确的端口号。

如果你在http://localhost:9997上运行一个http服务器,那么我认为你想要使用以下命令运行:

  1. $ go tool pprof http://localhost:9997/debug/pprof/heap

根据net/http/pprof包文档页面,如果你的应用程序已经运行了一个http服务器,你不需要启动一个新的服务器,只需要在程序中的某个地方包含import _ "net/http/pprof"即可。http://localhost:6060是作为示例启动的服务器,主机和端口是任意的。

import _ "net/http/pprof"表示导入了该包,但你不使用它的任何导出标识符。根据go语言规范,这将仅为了其副作用而导入该包。这些副作用涉及到,我认为,执行包的源文件中定义的init()函数,以及显然的注册变量

此外,你可能会发现这篇博文有帮助:

http://blog.golang.org/2011/06/profiling-go-programs.html

英文:

Based on your comment, the issue might be that you're not using the correct port number.

If you are running an http server at http://localhost:9997, then I think you want to run the command with http://localhost:9997:

  1. $ go tool pprof http://localhost:9997/debug/pprof/heap

According to the net/http/pprof pkg doc page, if your application is already running an http server you do not need to start one and only need to include the import _ "net/http/pprof" somewhere in your program. http://localhost:6060 is the server started as an example and the host and port are arbitrary.

import _ "net/http/pprof" means the package is imported but you do not use any of its exported identifiers. According to the go language spec, this will import the package solely for its side effects. These side effects involve, I think, the execution of the init() functions defined in the package's source files and, apparently, registered variables.

Also, you might find this blog post helpful:

http://blog.golang.org/2011/06/profiling-go-programs.html

答案2

得分: 2

什么是“不起作用”的意思?期望的是什么,实际观察到的是什么?

运行

  1. $ go tool pprof -h

查看您版本的此工具的帮助。我的本地版本既不是最新版本也不是发布版本(即介于两者之间)。它显示:

  1. (10:16) jnml@fsc-r550:~$ go tool pprof -h
  2. 选项 h 不明确(heapcheck, help
  3. 无效的选项
  4. 用法:
  5. pprof [选项] <程序> <配置文件>
  6. <配置文件> 是一个以空格分隔的配置文件名称列表。
  7. pprof [选项] <符号化的配置文件>
  8. <符号化的配置文件> 是一个包含必要的符号映射以及配置文件数据(可能是使用 --raw 生成的)的配置文件列表。
  9. pprof [选项] <配置文件>
  10. <配置文件> 是远程形式。符号从 host:port/pprof/symbol 获取
  11. 每个名称可以是:
  12. /path/to/profile - 配置文件的路径
  13. host:port[/<service>] - 获取配置文件的服务位置
  14. /<service> 可以是 /pprof/heap, /pprof/profile, /pprof/pmuprofile,
  15. /pprof/growth, /pprof/contention, /pprof/wall,
  16. /pprof/thread /pprof/filteredprofile
  17. 例如:
  18. pprof http://myserver.com:80/pprof/heap
  19. 如果省略 /<service>,则服务默认为 /pprof/profileCPU 分析)。
  20. pprof --symbols <程序>
  21. 将地址映射到符号名称。在此模式下,stdin 应该是一个库映射列表,格式与堆和 CPU 配置文件中找到的格式相同(在 Linux 上大致匹配 /proc/self/maps),然后是要映射的十六进制地址列表,每行一个。
  22. 有关查询远程服务器的更多帮助,包括如何添加必要的服务器端支持代码,请参阅此文件名(或类似文件):
  23. /usr/doc/google-perftools-1.5/pprof_remote_servers.html
  24. 选项:
  25. --cum 按累积数据排序
  26. --base=<base> 在显示之前从<配置文件>中减去<base>
  27. --interactive 运行交互模式(交互“help”提供帮助)[默认]
  28. --seconds=<n> 动态配置文件的时间长度[默认=30秒]
  29. --add_lib=<file> 从给定库中读取附加符号和行信息
  30. --lib_prefix=<dir> 逗号分隔的库路径前缀列表
  31. 报告粒度:
  32. --addresses 按地址级别报告
  33. --lines 按源代码行级别报告
  34. --functions 按函数级别报告[默认]
  35. --files 按源文件级别报告
  36. 输出类型:
  37. --text 生成文本报告
  38. --callgrind 生成 callgrind 格式到 stdout
  39. --gv 生成 Postscript 并显示
  40. --web 生成 SVG 并显示
  41. --list=<regexp> 生成匹配的例程的源代码列表
  42. --disasm=<regexp> 生成匹配的例程的反汇编
  43. --symbols 打印在给定地址找到的解码符号名称
  44. --dot 生成 DOT 文件到 stdout
  45. --ps 生成 Postcript stdout
  46. --pdf 生成 PDF stdout
  47. --svg 生成 SVG stdout
  48. --gif 生成 GIF stdout
  49. --raw 生成符号化的 pprof 数据(与远程获取一起使用时有用)
  50. 堆配置文件选项:
  51. --inuse_space 显示已使用的(兆字节)[默认]
  52. --inuse_objects 显示已使用的对象
  53. --alloc_space 显示已分配的(兆字节)
  54. --alloc_objects 显示已分配的对象
  55. --show_bytes 以字节显示空间
  56. --drop_negative 忽略负差异
  57. 冲突配置文件选项:
  58. --total_delay 显示每个区域的总延迟[默认]
  59. --contentions 显示每个区域的延迟次数
  60. --mean_delay 显示每个区域的平均延迟
  61. 调用图选项:
  62. --nodecount=<n> 最多显示这么多个节点[默认=80]
  63. --nodefraction=<f> 隐藏总数的<f>以下的节点[默认=.005]
  64. --edgefraction=<f> 隐藏总数的<f>以下的边[默认=.001]
  65. --focus=<regexp> 仅显示与<regexp>匹配的节点
  66. --ignore=<regexp> 忽略与<regexp>匹配的节点
  67. --scale=<n> 设置 GV 缩放[默认=0]
  68. --heapcheck 使具有非零对象计数(即直接泄漏生成器)的节点更可见
  69. 其他:
  70. --tools=<prefix> 对象工具路径名前缀
  71. --test 运行单元测试
  72. --help 此消息
  73. --version 版本信息
  74. 环境变量:
  75. PPROF_TMPDIR 配置文件目录。默认为 $HOME/pprof
  76. PPROF_TOOLS 对象工具路径名前缀
  77. 示例:
  78. pprof /bin/ls ls.prof
  79. 进入“交互”模式
  80. pprof --text /bin/ls ls.prof
  81. 每个过程输出一行
  82. pprof --web /bin/ls ls.prof
  83. Web 浏览器中显示带注释的调用图
  84. pprof --gv /bin/ls ls.prof
  85. 通过 'gv' 显示带注释的调用图
  86. pprof --gv --focus=Mutex /bin/ls ls.prof
  87. 限制到包含 .*Mutex.* 条目的代码路径
  88. pprof --gv --focus=Mutex --ignore=string /bin/ls ls.prof
  89. 包含 Mutex 但不包含 string 的代码路径
  90. pprof --list=getdir /bin/ls ls.prof
  91. getdir() 的(每行)带注释源代码列表
  92. pprof --disasm=getdir /bin/ls ls.prof
  93. getdir() 的(每个 PC)带注释反汇编
  94. pprof http://localhost:1234/
  95. 进入“交互”模式
  96. pprof --text localhost:1234
  97. localhost:1234 每个过程输出一行
  98. pprof --raw localhost:1234 > ./local.raw
  99. pprof --text ./local.raw
  100. 获取远程配置文件以供以后分析,然后以文本模式分析。
  101. 致命错误:无效的选项
  102. go tool pprof: 退出状态 1
  103. (10:16) jnml@fsc-r550:~$

关于第二个问题:import _ "foo" 的习惯用法是仅为了 foo 的初始化副作用而导入包 foo。这可能包括将 foo 提供的功能注册到其他包中可用的功能。一个具体的例子是特定的图像格式处理包(在“子目录”底部查看)和抽象的image包。

英文:

What "does not work" means? What is expected and what is observed instead?

Run

  1. $ go tool pprof -h

to see help for your version of this tool. My local version is not at tip neither at release (ie. in between). It shows:

  1. (10:16) jnml@fsc-r550:~$ go tool pprof -h
  2. Option h is ambiguous (heapcheck, help)
  3. Invalid option(s)
  4. Usage:
  5. pprof [options] &lt;program&gt; &lt;profiles&gt;
  6. &lt;profiles&gt; is a space separated list of profile names.
  7. pprof [options] &lt;symbolized-profiles&gt;
  8. &lt;symbolized-profiles&gt; is a list of profile files where each file contains
  9. the necessary symbol mappings as well as profile data (likely generated
  10. with --raw).
  11. pprof [options] &lt;profile&gt;
  12. &lt;profile&gt; is a remote form. Symbols are obtained from host:port/pprof/symbol
  13. Each name can be:
  14. /path/to/profile - a path to a profile file
  15. host:port[/&lt;service&gt;] - a location of a service to get profile from
  16. The /&lt;service&gt; can be /pprof/heap, /pprof/profile, /pprof/pmuprofile,
  17. /pprof/growth, /pprof/contention, /pprof/wall,
  18. /pprof/thread, or /pprof/filteredprofile.
  19. For instance:
  20. pprof http://myserver.com:80/pprof/heap
  21. If /&lt;service&gt; is omitted, the service defaults to /pprof/profile (cpu profiling).
  22. pprof --symbols &lt;program&gt;
  23. Maps addresses to symbol names. In this mode, stdin should be a
  24. list of library mappings, in the same format as is found in the heap-
  25. and cpu-profile files (this loosely matches that of /proc/self/maps
  26. on linux), followed by a list of hex addresses to map, one per line.
  27. For more help with querying remote servers, including how to add the
  28. necessary server-side support code, see this filename (or one like it):
  29. /usr/doc/google-perftools-1.5/pprof_remote_servers.html
  30. Options:
  31. --cum Sort by cumulative data
  32. --base=&lt;base&gt; Subtract &lt;base&gt; from &lt;profile&gt; before display
  33. --interactive Run in interactive mode (interactive &quot;help&quot; gives help) [default]
  34. --seconds=&lt;n&gt; Length of time for dynamic profiles [default=30 secs]
  35. --add_lib=&lt;file&gt; Read additional symbols and line info from the given library
  36. --lib_prefix=&lt;dir&gt; Comma separated list of library path prefixes
  37. Reporting Granularity:
  38. --addresses Report at address level
  39. --lines Report at source line level
  40. --functions Report at function level [default]
  41. --files Report at source file level
  42. Output type:
  43. --text Generate text report
  44. --callgrind Generate callgrind format to stdout
  45. --gv Generate Postscript and display
  46. --web Generate SVG and display
  47. --list=&lt;regexp&gt; Generate source listing of matching routines
  48. --disasm=&lt;regexp&gt; Generate disassembly of matching routines
  49. --symbols Print demangled symbol names found at given addresses
  50. --dot Generate DOT file to stdout
  51. --ps Generate Postcript to stdout
  52. --pdf Generate PDF to stdout
  53. --svg Generate SVG to stdout
  54. --gif Generate GIF to stdout
  55. --raw Generate symbolized pprof data (useful with remote fetch)
  56. Heap-Profile Options:
  57. --inuse_space Display in-use (mega)bytes [default]
  58. --inuse_objects Display in-use objects
  59. --alloc_space Display allocated (mega)bytes
  60. --alloc_objects Display allocated objects
  61. --show_bytes Display space in bytes
  62. --drop_negative Ignore negative differences
  63. Contention-profile options:
  64. --total_delay Display total delay at each region [default]
  65. --contentions Display number of delays at each region
  66. --mean_delay Display mean delay at each region
  67. Call-graph Options:
  68. --nodecount=&lt;n&gt; Show at most so many nodes [default=80]
  69. --nodefraction=&lt;f&gt; Hide nodes below &lt;f&gt;*total [default=.005]
  70. --edgefraction=&lt;f&gt; Hide edges below &lt;f&gt;*total [default=.001]
  71. --focus=&lt;regexp&gt; Focus on nodes matching &lt;regexp&gt;
  72. --ignore=&lt;regexp&gt; Ignore nodes matching &lt;regexp&gt;
  73. --scale=&lt;n&gt; Set GV scaling [default=0]
  74. --heapcheck Make nodes with non-0 object counts
  75. (i.e. direct leak generators) more visible
  76. Miscellaneous:
  77. --tools=&lt;prefix&gt; Prefix for object tool pathnames
  78. --test Run unit tests
  79. --help This message
  80. --version Version information
  81. Environment Variables:
  82. PPROF_TMPDIR Profiles directory. Defaults to $HOME/pprof
  83. PPROF_TOOLS Prefix for object tools pathnames
  84. Examples:
  85. pprof /bin/ls ls.prof
  86. Enters &quot;interactive&quot; mode
  87. pprof --text /bin/ls ls.prof
  88. Outputs one line per procedure
  89. pprof --web /bin/ls ls.prof
  90. Displays annotated call-graph in web browser
  91. pprof --gv /bin/ls ls.prof
  92. Displays annotated call-graph via &#39;gv&#39;
  93. pprof --gv --focus=Mutex /bin/ls ls.prof
  94. Restricts to code paths including a .*Mutex.* entry
  95. pprof --gv --focus=Mutex --ignore=string /bin/ls ls.prof
  96. Code paths including Mutex but not string
  97. pprof --list=getdir /bin/ls ls.prof
  98. (Per-line) annotated source listing for getdir()
  99. pprof --disasm=getdir /bin/ls ls.prof
  100. (Per-PC) annotated disassembly for getdir()
  101. pprof http://localhost:1234/
  102. Enters &quot;interactive&quot; mode
  103. pprof --text localhost:1234
  104. Outputs one line per procedure for localhost:1234
  105. pprof --raw localhost:1234 &gt; ./local.raw
  106. pprof --text ./local.raw
  107. Fetches a remote profile for later analysis and then
  108. analyzes it in text mode.
  109. FATAL ERROR: Invalid option(s)
  110. go tool pprof: exit status 1
  111. (10:16) jnml@fsc-r550:~$

On the second question: The `import _ "foo"' idiom is importing package foo only for the side effects of foo's initialization. That may include for example registering functionality provided by foo to be usable from within other packages. A concrete example for this are the specific image format handling packages (see bottom at"Subdirectories") and the abstract image package.

huangapple
  • 本文由 发表于 2012年12月4日 17:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/13699297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定