首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

runtime/pprof

  • import "runtime/pprof"
  • 概况
  • 索引
  • 子目录

概况

软件包 pprof 以 pprof 可视化工具所期望的格式写入运行时分析数据。

分析 Go 程序

分析 Go 程序的第一步是启用分析。支持使用标准测试包构建的性能分析基准测试。例如,以下命令在当前目录中运行基准测试并将 CPU 和内存配置文件写入 cpu.prof 和 mem.prof:

代码语言:javascript
复制
go test -cpuprofile cpu.prof -memprofile mem.prof -bench .

要为独立程序添加等效分析支持,请将以下代码添加到主函数中:

代码语言:javascript
复制
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")

func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal("could not create CPU profile: ", err)
        }
        if err := pprof.StartCPUProfile(f); err != nil {
            log.Fatal("could not start CPU profile: ", err)
        }
        defer pprof.StopCPUProfile()
    }

    // ... rest of the program ...

    if *memprofile != "" {
        f, err := os.Create(*memprofile)
        if err != nil {
            log.Fatal("could not create memory profile: ", err)
        }
        runtime.GC() // get up-to-date statistics
        if err := pprof.WriteHeapProfile(f); err != nil {
            log.Fatal("could not write memory profile: ", err)
        }
        f.Close()
    }
}

还有一个标准的 HTTP 接口来分析数据。添加以下行将在 / debug / pprof / URL 下安装处理程序以下载实时配置文件:

代码语言:javascript
复制
import _ "net/http/pprof"

有关更多详细信息,请参阅 net / http / pprof 软件包。

配置文件可以通过 pprof 工具进行可视化处理:

代码语言:javascript
复制
go tool pprof cpu.prof

pprof 命令行提供了许多命令。通常使用的命令包括打印顶部程序热点摘要的“top”和打开热点及其调用图的交互图的“web”。使用“help”获取所有 pprof 命令的信息。

有关 pprof 的更多信息,请参阅https://github.com/google/pprof/blob/master/doc/pprof.md

索引

  • func Do(ctx context.Context, labels LabelSet, f func(context.Context))
  • func ForLabels(ctx context.Context, f func(key, value string) bool)
  • func Label(ctx context.Context, key string) (string, bool)
  • func Profiles() []*Profile
  • func SetGoroutineLabels(ctx context.Context)
  • func StartCPUProfile(w io.Writer) error
  • func StopCPUProfile()
  • func WithLabels(ctx context.Context, labels LabelSet) context.Context
  • func WriteHeapProfile(w io.Writer) error
  • type LabelSet
  • func Labels(args ...string) LabelSet
  • type Profile
  • func Lookup(name string) *Profile
  • func NewProfile(name string) *Profile
  • func (p *Profile) Add(value interface{}, skip int)
  • func (p *Profile) Count() int
  • func (p *Profile) Name() string
  • func (p *Profile) Remove(value interface{})
  • func (p *Profile) WriteTo(w io.Writer, debug int) error
  • Bugs

包文件

func DoSource

代码语言:javascript
复制
func Do(ctx context.Context, labels LabelSet, f func(context.Context))

使用添加到父标签映射中的给定标签的父上下文副本调用 f 。标签中的每个键/值对都按照提供的顺序插入到标签贴图中,覆盖同一个键的任何以前的值。增强标签贴图将在 f 调用期间设置,并在 f 返回时恢复。

func ForLabelsSource

代码语言:javascript
复制
func ForLabels(ctx context.Context, f func(key, value string) bool)

ForLabels 通过在上下文中设置每个标签来调用 f 。函数 f 应该返回 true 来继续迭代或 false 以尽早停止迭代。

func LabelSource

代码语言:javascript
复制
func Label(ctx context.Context, key string) (string, bool)

Label 用 ctx 上的给定关键字返回标签的值,以及指示该标签是否存在的布尔值。

func ProfilesSource

代码语言:javascript
复制
func Profiles() []*Profile

Profiles 文件返回所有已知配置文件的一部分,按名称排序。

func SetGoroutineLabelsSource

代码语言:javascript
复制
func SetGoroutineLabels(ctx context.Context)

SetGoroutineLabels 将当前 goroutine 的标签设置为与 ctx 匹配。这是比 Do 更低级别的 API,应尽可能使用它。

func StartCPUProfileSource

代码语言:javascript
复制
func StartCPUProfile(w io.Writer) error

StartCPUProfile 启用当前进程的 CPU 分析。分析时,配置文件将被缓冲并写入 w 。如果分析已启用,则 StartCPUProfile 将返回错误。

在类 Unix 系统上,默认情况下,StartCPUProfile 对于使用 -buildmode = c-archive 或 -buildmode = c-shared 构建的 Go 代码不起作用。StartCPUProfile 依赖于 SIGPROF 信号,但该信号将被传送到主程序的 SIGPROF 信号处理程序(如果有)而不是 Go 所使用的信号处理程序。要使其工作,请为 syscall.SIGPROF 调用 os / signal.Notify,但请注意,这样做可能会破坏主程序执行的任何分析。

func StopCPUProfileSource

代码语言:javascript
复制
func StopCPUProfile()

StopCPUProfile 停止当前 CPU 配置文件(如果有)。StopCPUProfile 仅在配置文件的所有写入完成后才会返回。

func WithLabelsSource

代码语言:javascript
复制
func WithLabels(ctx context.Context, labels LabelSet) context.Context

WithLabels 返回一个新的 context.Context,添加了给定的标签。标签使用相同的密钥覆盖先前的标签。

func WriteHeapProfileSource

代码语言:javascript
复制
func WriteHeapProfile(w io.Writer) error

WriteHeapProfile 是 Lookup(“heap”)的缩写。WriteTo(w,0)。它是为了向后兼容而保存的。

type LabelSetSource

LabelSet 是一组标签。

代码语言:javascript
复制
type LabelSet struct {
        // contains filtered or unexported fields
}

func LabelsSource

代码语言:javascript
复制
func Labels(args ...string) LabelSet

Labels 需要偶数个表示键值对的字符串,并使 LabelSet 包含它们。标签使用相同的密钥覆盖先前的标签。

type ProfileSource

配置文件是堆栈跟踪的集合,显示导致特定事件实例(例如分配)的调用序列。包可以创建和维护自己的配置文件; 最常见的用途是跟踪必须明确关闭的资源,例如文件或网络连接。

配置文件的方法可以同时从多个 goroutine 调用。

每个配置文件都有唯一的名称。一些配置文件是预定义的:

代码语言:javascript
复制
goroutine    - stack traces of all current goroutines
heap         - a sampling of all heap allocations
threadcreate - stack traces that led to the creation of new OS threads
block        - stack traces that led to blocking on synchronization primitives
mutex        - stack traces of holders of contended mutexes

这些预定义的配置文件在明确的 Add 或 Remove 方法调用中保持自己和恐慌。

堆概要报告最近完成的垃圾收集的统计数据; 它避免了最近的分配,以避免将配置文件从实时数据转向垃圾。如果根本没有垃圾回收,则堆配置文件会报告所有已知的分配。此异常主要用于在未启用垃圾回收的情况下运行的程序,通常用于调试目的。

CPU 配置文件不可用作配置文件。它有一个特殊的 API,StartCPUProfile 和 StopCPUProfile 函数,因为它在分析过程中将输出流输出到一个写入器。

代码语言:javascript
复制
type Profile struct {
        // contains filtered or unexported fields
}

func LookupSource

代码语言:javascript
复制
func Lookup(name string) *Profile

Lookup 将返回具有给定名称的配置文件,如果不存在此类配置文件,则返回 nil 。

func NewProfileSource

代码语言:javascript
复制
func NewProfile(name string) *Profile

NewProfile 使用给定的名称创建一个新的配置文件。如果具有该名称的配置文件已存在,则 NewProfile 会发生混乱。该惯例是使用“导入/路径”。前缀为每个包创建单独的名称空间。为了与读取 pprof 数据的各种工具兼容,配置文件名称不应包含空格。

func (*Profile) AddSource

代码语言:javascript
复制
func (p *Profile) Add(value interface{}, skip int)

Add 将当前执行堆栈添加到与值关联的配置文件。将商店值添加到内部映射中,因此值必须适合用作映射键,并且在相应的调用 Remove 之前不会进行垃圾收集。如果配置文件已经包含值的堆栈,则添加恐慌。

skip 参数的含义与 runtime.Caller 的 skip 和 controls 开始的地方相同。跳过 skip = 0开始追加函数的追踪。例如,给定这个执行堆栈:

代码语言:javascript
复制
Add
called from rpc.NewClient
called from mypkg.Run
called from main.main

跳过 skip = 0时,会在调用 Add rpc.NewClient 时添加堆栈跟踪。在 skip mypkg.Run 中调用 NewClient 时,skip skip = 1开始堆栈跟踪。

func (*Profile) CountSource

代码语言:javascript
复制
func (p *Profile) Count() int

Count 返回配置文件中当前执行堆栈的数量。

func (*Profile) NameSource

代码语言:javascript
复制
func (p *Profile) Name() string

Name 返回此配置文件的名称,该名称可以传递给查找以重新获取配置文件。

func (*Profile) RemoveSource

代码语言:javascript
复制
func (p *Profile) Remove(value interface{})

Remove 从配置文件中删除与值关联的执行堆栈。如果该值不在配置文件中,则它是无操作的。

func (*Profile) WriteToSource

代码语言:javascript
复制
func (p *Profile) WriteTo(w io.Writer, debug int) error

WriteTo 将配置文件的 pprof 格式的快照写入 w 。如果写入 w 返回错误,则 WriteTo 返回该错误。否则,WriteTo 返回 nil 。

调试参数启用额外的输出。传递 debug = 0仅打印 pprof 所需的十六进制地址。传递 debug = 1会添加将地址转换为函数名和行号的注释,以便程序员可以在不使用工具的情况下读取配置文件。

预定义的配置文件可以为其他调试值指定含义; 例如,在打印“goroutine”配置文件时,debug = 2意味着打印 goroutine 堆栈的格式与 Go 程序由于未发现的恐慌而死时使用的格式相同。

Bugs

子目录

Name

Synopsis

| .. |

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com