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

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

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

func newWorker() worker {
	return worker{collapsed: make(collapsed)}
}

// Run until ch is closed or has no more events and ctx is done.
func (w worker) run(ctx context.Context, wg *sync.WaitGroup, ch <-chan *event.Pair) {
	defer wg.Done()

	for {
		select {
		case ev := <-ch:
			filePath := ev.File.Name()
			pathMap, ok := w.collapsed[filePath]
			if !ok {
				pathMap = make(map[types.TraceId]counter)
			}

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

			w.collapsed[filePath] = pathMap
			// TODO: Enable Go race detector
			ev.Recycle()

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