diff options
| author | Paul Buetow <paul@buetow.org> | 2025-04-10 21:28:45 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-04-10 21:28:45 +0300 |
| commit | 017494938f061fd1276f2a54b1df0e7002655e9f (patch) | |
| tree | 9c98fbf31b524233b637079a0482b4255eb6e388 /internal/flamegraph/iordatacollector.go | |
| parent | 9572fa5d087731f68d55517847833f6203b9a70d (diff) | |
can serialize and deserialize to/from gob
Diffstat (limited to 'internal/flamegraph/iordatacollector.go')
| -rw-r--r-- | internal/flamegraph/iordatacollector.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/internal/flamegraph/iordatacollector.go b/internal/flamegraph/iordatacollector.go new file mode 100644 index 0000000..6a97379 --- /dev/null +++ b/internal/flamegraph/iordatacollector.go @@ -0,0 +1,60 @@ +package flamegraph + +import ( + "context" + "fmt" + "ior/internal/event" + "ior/internal/flags" + "os" + "runtime" + "sync" +) + +// TODO: Idea, show time spent between the syscalls (off syscalls) as well, but in a different color +type IorDataCollector struct { + flags flags.Flags + Ch chan *event.Pair + Done chan struct{} + workers []worker +} + +func New() IorDataCollector { + f := IorDataCollector{ + Ch: make(chan *event.Pair, 4096), + Done: make(chan struct{}), + } + numWorkers := runtime.NumCPU() / 4 + if numWorkers == 0 { + numWorkers = 1 + } + for range numWorkers { + f.workers = append(f.workers, newWorker()) + } + return f +} + +func (f IorDataCollector) Start(ctx context.Context) { + go func() { + defer close(f.Done) + var wg sync.WaitGroup + wg.Add(len(f.workers)) + + for i, worker := range f.workers { + fmt.Println("Starting flamegraph worker", i) + go worker.run(ctx, &wg, f.Ch) + } + wg.Wait() + + iod := f.workers[0].iod + if len(f.workers) > 1 { + for i, w := range f.workers[1:] { + iod = iod.merge(w.iod) + fmt.Println("Worker", i+1, "merged") + } + } + if err := iod.serializeToFile(); err != nil { + fmt.Println(err) + os.Exit(2) + } + }() +} |
