非常教程

Go参考手册

运行时 | runtime

runtime/pprof

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

概况

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

分析 Go 程序

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

go test -cpuprofile cpu.prof -memprofile mem.prof -bench .

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

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 下安装处理程序以下载实时配置文件:

import _ "net/http/pprof"

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

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

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

包文件

elf.go label.go map.go pprof.go proto.go protobuf.go protomem.go runtime.go

func DoSource

func Do(ctx context.Context, labels LabelSet, f func(context.Context))

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

func ForLabelsSource

func ForLabels(ctx context.Context, f func(key, value string) bool)

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

func LabelSource

func Label(ctx context.Context, key string) (string, bool)

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

func ProfilesSource

func Profiles() []*Profile

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

func SetGoroutineLabelsSource

func SetGoroutineLabels(ctx context.Context)

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

func StartCPUProfileSource

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

func StopCPUProfile()

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

func WithLabelsSource

func WithLabels(ctx context.Context, labels LabelSet) context.Context

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

func WriteHeapProfileSource

func WriteHeapProfile(w io.Writer) error

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

type LabelSetSource

LabelSet 是一组标签。

type LabelSet struct {
        // contains filtered or unexported fields
}

func LabelsSource

func Labels(args ...string) LabelSet

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

type ProfileSource

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

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

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

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 函数,因为它在分析过程中将输出流输出到一个写入器。

type Profile struct {
        // contains filtered or unexported fields
}

func LookupSource

func Lookup(name string) *Profile

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

func NewProfileSource

func NewProfile(name string) *Profile

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

func (*Profile) AddSource

func (p *Profile) Add(value interface{}, skip int)

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

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

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

func (p *Profile) Count() int

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

func (*Profile) NameSource

func (p *Profile) Name() string

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

func (*Profile) RemoveSource

func (p *Profile) Remove(value interface{})

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

func (*Profile) WriteToSource

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

  • ☞ 配置文件与用于生成它们的内核支持一样好。有关已知问题的详细信息,请参阅https://golang.org/issue/13841。

子目录

Name

Synopsis

| .. |

Go

Go 是一种编译型语言,它结合了解释型语言的游刃有余,动态类型语言的开发效率,以及静态类型的安全性。它也打算成为现代的,支持网络与多核计算的语言。要满足这些目标,需要解决一些语言上的问题:一个富有表达能力但轻量级的类型系统,并发与垃圾回收机制,严格的依赖规范等等。这些无法通过库或工具解决好,因此Go也就应运而生了。

主页 https://golang.org/
源码 https://go.googlesource.com/go
发布版本 1.9.2

Go目录

1.档案 | archive
2.缓冲区 | bufio
3.内置 | builtin
4.字节 | bytes
5.压缩 | compress
6.容器 | container
7.上下文 | context
8.加密 | crypto
9.数据库 | database
10.调试 | debug
11.编码 | encoding
12.错误 | errors
13. expvar
14.flag
15. fmt
16. go
17.散列 | hash
18.html
19.图像 | image
20.索引 | index
21.io
22.日志 | log
23.数学 | math
24. math/big
25.math/bits
26.math/cmplx
27.math/rand
28.拟态 | mime
29.net
30.net/http
31. net/mail
32. net/rpc
33.net/smtp
34. net/textproto
35. net/url
36.os
37.路径 | path
38.插件 | plugin
39.反射 | reflect
40.正则表达式 | regexp
41.运行时 | runtime
42.排序算法 | sort
43.转换 | strconv
44.字符串 | strings
45.同步 | sync
46.系统调用 | syscall
47.测试 | testing
48.文本 | text
49.时间戳 | time
50.unicode
51.不安全性 | unsafe
52.Go 语言数据类型
53.Go 语言基础语法
54.Go 语言结构
55.Go 语言 select 语句
56.Go 语言 switch 语句
57.Go 语言 if 语句嵌套
58.Go 语言 if…else 语句
59.Go 语言 if 语句
60.Go 语言运算符
61.Go 语言常量
62.Go 语言函数闭包
63.Go 语言函数作为实参
64.Go 语言函数引用传递值
65.Go 语言函数值传递值
66.Go 语言函数
67.Go 语言 goto 语句
68.Go 语言 continue 语句
69.Go 语言 break 语句
70.Go 语言循环嵌套
71.Go 语言 for 循环
72.Go 语言结构体
73.Go 语言指针作为函数参数
74.Go 语言指向指针的指针
75.Go 语言指针数组
76.Go 语言指针
77.Go 语言向函数传递数组
78.Go 语言多维数组
79.Go 语言变量作用域
80.Go 语言函数方法
81.Go 错误处理
82.Go 语言接口
83.Go 语言类型转换
84.Go 语言递归函数
85.Go 语言Map(集合)
86.Go 语言范围(Range)
87.Go 语言切片(Slice)
88.Go 并发
89.Go fmt.Sprintf 格式化字符串