elf.go

1
2
3
// elfBuildID returns the GNU build ID of the named ELF binary,
// without introducing a dependency on debug/elf and its dependencies.
func elfBuildID(file string) (string, error)

返回ELF二进制文件的GNU build ID

label.go

通过 context.Context 上操作kv值,达到设置 label的目的

map.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// A profMap is a map from (stack, tag) to mapEntry.
// It grows without bound, but that's assumed to be OK.
type profMap struct {
hash map[uintptr]*profMapEntry
all *profMapEntry
last *profMapEntry
free []profMapEntry
freeStk []uintptr
}

// A profMapEntry is a single entry in the profMap.
type profMapEntry struct {
nextHash *profMapEntry // next in hash list
nextAll *profMapEntry // next in list of all entries
stk []uintptr
tag unsafe.Pointer
count int64
}

func (m *profMap) lookup(stk []uint64, tag unsafe.Pointer) *profMapEntry

大概是标签查找

pprof.go

  1. 定义了Profile

    1
    2
    3
    4
    5
    6
    7
    type Profile struct {
    name string
    mu sync.Mutex
    m map[interface{}][]uintptr
    count func() int
    write func(io.Writer, int) error
    }
  2. 初始化了一些Profile

    goroutine,threadcreate,heap,allocs,block,mutex

  3. 提供 NewProfile(name string) 方法支持自定义生成

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

    将和value相关的执行栈加入到profile.Add在内部实现的map中存储value,所以value必须是可以被用作map的key,而且在没有收到移除(Remove)指令前不会被自动gc掉.

  5. writexxx

proto.go

基本是构建pprof文件信息的方法

protobuf.go

编码,buffer

protomem.go

runtime.go

标记当前线程


internal子包

为了给外部调用,里面多封了一层profile包

  1. encode.go

    对Profile的编解码

  2. filter.go

    过滤

  3. legacy_profile.go
  4. profile.go
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    // Profile is an in-memory representation of profile.proto.
    type Profile struct {
    SampleType []*ValueType
    DefaultSampleType string
    Sample []*Sample
    Mapping []*Mapping
    Location []*Location
    Function []*Function
    Comments []string

    DropFrames string
    KeepFrames string

    TimeNanos int64
    DurationNanos int64
    PeriodType *ValueType
    Period int64

    commentX []int64
    dropFramesX int64
    keepFramesX int64
    stringTable []string
    defaultSampleTypeX int64
    }

profile.proto文件的内存信息