首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Go程序中使用pprof

如何在Go程序中使用pprof
EN

Stack Overflow用户
提问于 2012-12-04 17:06:56
回答 2查看 18.5K关注 0票数 9

如何在Go程序中使用pprof?

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

文档中写的是go tool pprof http://localhost:6060/debug/pprof/heap,但它不起作用。

那么,下面的_是什么意思呢?

import _ "net/http/pprof"

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-11 15:52:14

基于your comment,问题可能是您没有使用正确的端口号。

如果您在http://localhost:9997上运行http服务器,那么我认为您应该使用http://localhost:9997运行该命令

代码语言:javascript
复制
$ go tool pprof http://localhost:9997/debug/pprof/heap

根据net/http/pprof pkg doc page的说法,如果您的应用程序已经在运行http服务器,则不需要启动一个服务器,只需在程序中的某处包含import _ "net/http/pprof"即可。http://localhost:6060是作为示例启动的服务器,主机和端口是任意的。

import _ "net/http/pprof"表示包已导入,但您不使用其导出的任何标识符。根据go language spec的说法,这将仅仅是因为它的副作用而导入该软件包。这些副作用包括,我认为,执行package's source files中定义的init() functions,显然还有registered variables中定义的。

另外,你可能会发现这篇博客文章很有帮助:

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

票数 11
EN

Stack Overflow用户

发布于 2012-12-04 17:24:14

“不工作”是什么意思?预期是什么,观察到的是什么?

代码语言:javascript
复制
$ go tool pprof -h

以查看此工具版本的帮助。我的本地版本在发布时也不是最好的(即。在两者之间)。它显示:

代码语言:javascript
复制
(10:16) jnml@fsc-r550:~$ go tool pprof -h
Option h is ambiguous (heapcheck, help)
Invalid option(s)

Usage:
pprof [options] <program> <profiles>
   <profiles> is a space separated list of profile names.
pprof [options] <symbolized-profiles>
   <symbolized-profiles> is a list of profile files where each file contains
   the necessary symbol mappings  as well as profile data (likely generated
   with --raw).
pprof [options] <profile>
   <profile> is a remote form.  Symbols are obtained from host:port/pprof/symbol

   Each name can be:
   /path/to/profile        - a path to a profile file
   host:port[/<service>]   - a location of a service to get profile from

   The /<service> can be /pprof/heap, /pprof/profile, /pprof/pmuprofile,
                         /pprof/growth, /pprof/contention, /pprof/wall,
                         /pprof/thread, or /pprof/filteredprofile.
   For instance:
     pprof http://myserver.com:80/pprof/heap
   If /<service> is omitted, the service defaults to /pprof/profile (cpu profiling).
pprof --symbols <program>
   Maps addresses to symbol names.  In this mode, stdin should be a
   list of library mappings, in the same format as is found in the heap-
   and cpu-profile files (this loosely matches that of /proc/self/maps
   on linux), followed by a list of hex addresses to map, one per line.

   For more help with querying remote servers, including how to add the
   necessary server-side support code, see this filename (or one like it):

   /usr/doc/google-perftools-1.5/pprof_remote_servers.html

Options:
   --cum               Sort by cumulative data
   --base=<base>       Subtract <base> from <profile> before display
   --interactive       Run in interactive mode (interactive "help" gives help) [default]
   --seconds=<n>       Length of time for dynamic profiles [default=30 secs]
   --add_lib=<file>    Read additional symbols and line info from the given library
   --lib_prefix=<dir>  Comma separated list of library path prefixes

Reporting Granularity:
   --addresses         Report at address level
   --lines             Report at source line level
   --functions         Report at function level [default]
   --files             Report at source file level

Output type:
   --text              Generate text report
   --callgrind         Generate callgrind format to stdout
   --gv                Generate Postscript and display
   --web               Generate SVG and display
   --list=<regexp>     Generate source listing of matching routines
   --disasm=<regexp>   Generate disassembly of matching routines
   --symbols           Print demangled symbol names found at given addresses
   --dot               Generate DOT file to stdout
   --ps                Generate Postcript to stdout
   --pdf               Generate PDF to stdout
   --svg               Generate SVG to stdout
   --gif               Generate GIF to stdout
   --raw               Generate symbolized pprof data (useful with remote fetch)

Heap-Profile Options:
   --inuse_space       Display in-use (mega)bytes [default]
   --inuse_objects     Display in-use objects
   --alloc_space       Display allocated (mega)bytes
   --alloc_objects     Display allocated objects
   --show_bytes        Display space in bytes
   --drop_negative     Ignore negative differences

Contention-profile options:
   --total_delay       Display total delay at each region [default]
   --contentions       Display number of delays at each region
   --mean_delay        Display mean delay at each region

Call-graph Options:
   --nodecount=<n>     Show at most so many nodes [default=80]
   --nodefraction=<f>  Hide nodes below <f>*total [default=.005]
   --edgefraction=<f>  Hide edges below <f>*total [default=.001]
   --focus=<regexp>    Focus on nodes matching <regexp>
   --ignore=<regexp>   Ignore nodes matching <regexp>
   --scale=<n>         Set GV scaling [default=0]
   --heapcheck         Make nodes with non-0 object counts
                       (i.e. direct leak generators) more visible

Miscellaneous:
   --tools=<prefix>    Prefix for object tool pathnames
   --test              Run unit tests
   --help              This message
   --version           Version information

Environment Variables:
   PPROF_TMPDIR        Profiles directory. Defaults to $HOME/pprof
   PPROF_TOOLS         Prefix for object tools pathnames

Examples:

pprof /bin/ls ls.prof
                       Enters "interactive" mode
pprof --text /bin/ls ls.prof
                       Outputs one line per procedure
pprof --web /bin/ls ls.prof
                       Displays annotated call-graph in web browser
pprof --gv /bin/ls ls.prof
                       Displays annotated call-graph via 'gv'
pprof --gv --focus=Mutex /bin/ls ls.prof
                       Restricts to code paths including a .*Mutex.* entry
pprof --gv --focus=Mutex --ignore=string /bin/ls ls.prof
                       Code paths including Mutex but not string
pprof --list=getdir /bin/ls ls.prof
                       (Per-line) annotated source listing for getdir()
pprof --disasm=getdir /bin/ls ls.prof
                       (Per-PC) annotated disassembly for getdir()

pprof http://localhost:1234/
                       Enters "interactive" mode
pprof --text localhost:1234
                       Outputs one line per procedure for localhost:1234
pprof --raw localhost:1234 > ./local.raw
pprof --text ./local.raw
                       Fetches a remote profile for later analysis and then
                       analyzes it in text mode.

FATAL ERROR: Invalid option(s)
go tool pprof: exit status 1
(10:16) jnml@fsc-r550:~$ 

关于第二个问题:‘`import _“foo”习惯用法只是为了foo初始化的副作用而导入包foo。例如,这可能包括注册foo提供的功能,以便在其他包中使用。一个具体的例子是特定的图像格式handling packages (见“子目录”底部)和抽象的image包。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13699297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档