summaryrefslogtreecommitdiff
path: root/internal/flamegraph/counter.go
blob: 441db68cda86951a81f54a3828f1664bbe2110a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package flamegraph

import (
	"fmt"
)

// Counter aggregates statistics for a logical flamegraph node.
//
// Duration and DurationToPrev use the same timing semantics as event.Pair:
//   - Duration is the syscall runtime on the same thread.
//   - DurationToPrev is the inter-syscall gap on the same thread and is attributed
//     to the current node; there is no separate "idle" pseudo-node.
//
// Bytes is only populated for read/write/transfer syscalls.
type Counter struct {
	Count          uint64
	Duration       uint64
	DurationToPrev uint64
	Bytes          uint64 // Bytes transferred (only set for read/write/transfer syscalls)
}

func (c Counter) add(other Counter) Counter {
	c.Count += other.Count
	c.Duration += other.Duration
	c.DurationToPrev += other.DurationToPrev
	c.Bytes += other.Bytes

	return c
}

func (c Counter) ValueByName(name string) (uint64, error) {
	switch name {
	case "count":
		return c.Count, nil
	case "duration":
		return c.Duration, nil
	case "durationToPrev":
		return c.DurationToPrev, nil
	case "bytes":
		return c.Bytes, nil
	default:
		return 0, fmt.Errorf("unknown counter field %q", name)
	}
}