summaryrefslogtreecommitdiff
path: root/internal/flamegraph/worker.go
blob: e47329e71bc61e6c31044fb3ba9392d44106ab5b (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
package flamegraph

import (
	"context"
	"ior/internal/event"
	"ior/internal/types"
	"sync"
	"time"
)

type worker struct {
	collapsed collapsed
	iod       iorData
	done      chan struct{}
}

func newWorker() worker {
	return worker{
		collapsed: make(collapsed), // COLLAPSED: Retire ocne newIorData implemented
		iod:       newIorData(),    // TODO: make flags global, so i don't have to pass them through the whole code base
	}
}

func (w worker) run(ctx context.Context, wg *sync.WaitGroup, ch <-chan *event.Pair) {
	defer wg.Done()

	for {
		select {
		case ev := <-ch:
			// var filePath string
			// if ev.File == nil {
			// 	filePath = "N:file"
			// } else {
			// 	filePath = ev.File.Name()
			// }
			ev.Recycle()

		default:
			select {
			case <-ctx.Done():
				return
			default:
				time.Sleep(time.Millisecond * 10)
			}
		}
	}
}

// TODO: Retire collapsed
func (w worker) runCollapsed(ctx context.Context, wg *sync.WaitGroup, ch <-chan *event.Pair) {
	{
		defer wg.Done()

		for {
			select {
			case ev := <-ch:
				var filePath string
				if ev.File == nil {
					filePath = "N:file"
				} else {
					filePath = ev.File.Name()
				}
				pathMap, ok := w.collapsed[filePath]
				if !ok {
					pathMap = make(map[types.TraceId]collapsedCounter)
				}

				traceId := ev.EnterEv.GetTraceId()
				cnt := pathMap[traceId]
				cnt.count++
				cnt.duration += ev.Duration
				pathMap[traceId] = cnt

				w.collapsed[filePath] = pathMap
				ev.Recycle()

			default:
				select {
				case <-ctx.Done():
					return
				default:
					time.Sleep(time.Millisecond * 10)
				}
			}
		}
	}
}