summaryrefslogtreecommitdiff
path: root/internal/flamegraph/collapsed.go
blob: 3d2724eb7d6464d87395995ddf3e011425584559 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package flamegraph

import (
	"fmt"
	"ior/internal/types"
	"os"
	"strings"
	"sync"
)

type counter struct {
	count    uint64
	duration uint64
}

func (c *counter) merge(other counter) {
	c.count += other.count
	c.duration += other.duration
}

// TODO: make it generic, generate multiple trace points
// path, traceid (syscall name), comm, pid, tid
// traceid, path is by default set in this order
// store an intermediate format which then can be converted to the others...
// e.g.    path ¶ traceid ¶ comm ¶ pid ¶ tid ¶ flags ¶ counter
// counter can also have bytes (for reads and writes)
type collapsed map[string]map[types.TraceId]counter

// TODO: Unit test this
func (c collapsed) merge(other collapsed) (merged int) {
	for k, v := range other {
		if _, ok := c[k]; !ok {
			c[k] = make(map[types.TraceId]counter)
		}
		for traceId, cnt := range v {
			if existingCnt, ok := c[k][traceId]; ok {
				existingCnt.merge(cnt)
				merged++
				c[k][traceId] = existingCnt
				continue
			}
			c[k][traceId] = cnt
		}
	}
	return
}

func (c collapsed) dump() {
	var wg sync.WaitGroup
	wg.Add(4)

	go c.dumpBy(&wg, "ior-by-path-count-flamegraph.collapsed", true, func(cnt counter) uint64 {
		return cnt.count
	})
	go c.dumpBy(&wg, "ior-by-path-duration-flamegraph.collapsed", true, func(cnt counter) uint64 {
		return cnt.duration
	})
	go c.dumpBy(&wg, "ior-by-syscall-count-flamegraph.collapsed", false, func(cnt counter) uint64 {
		return cnt.count
	})
	go c.dumpBy(&wg, "ior-by-syscall-duration-flamegraph.collapsed", false, func(cnt counter) uint64 {
		return cnt.duration
	})

	wg.Wait()
}

func (c collapsed) dumpBy(wg *sync.WaitGroup, outfile string, syscallAtTop bool, by func(counter) uint64) {
	defer wg.Done()

	defer fmt.Println("Dumping done")
	fmt.Println("Dumping", outfile)

	file, err := os.Create(outfile)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	for path, value := range c {
		var sb strings.Builder

		for i, part := range strings.Split(path, "/") {
			if i > 1 {
				sb.WriteString(";")
				sb.WriteString("/")
			}
			sb.WriteString(part)
		}

		for traceId, cnt := range value {
			var err error
			if syscallAtTop {
				_, err = fmt.Fprintf(file, "%s;syscall`%s %v\n", sb.String(), traceId.Name(), by(cnt))
			} else {
				_, err = fmt.Fprintf(file, "syscall`%s;%s %v\n", traceId.Name(), sb.String(), by(cnt))
			}
			if err != nil {
				panic(err)
			}
		}
	}
}