summaryrefslogtreecommitdiff
path: root/internal/flamegraph
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-04-06 23:03:54 +0300
committerPaul Buetow <paul@buetow.org>2025-04-06 23:03:54 +0300
commit138c0f9b6255ed61eee8390674c026daa23eddc5 (patch)
treee2d2494e8ce14fb0e504216cc6cb7a4eefed04ad /internal/flamegraph
parent95e3477875c0e31e0c6907ab6afe4ce48fb37391 (diff)
refactor
Diffstat (limited to 'internal/flamegraph')
-rw-r--r--internal/flamegraph/iordata.go21
-rw-r--r--internal/flamegraph/iordata_test.go12
-rw-r--r--internal/flamegraph/worker.go2
3 files changed, 21 insertions, 14 deletions
diff --git a/internal/flamegraph/iordata.go b/internal/flamegraph/iordata.go
index 2d4b46b..b91dbc3 100644
--- a/internal/flamegraph/iordata.go
+++ b/internal/flamegraph/iordata.go
@@ -3,9 +3,11 @@ package flamegraph
import (
"fmt"
"ior/internal/event"
+ "ior/internal/file"
"ior/internal/flags"
"ior/internal/types"
"iter"
+ "log"
"os"
"strings"
"time"
@@ -20,7 +22,7 @@ type traceIdType = types.TraceId
type commType = string
type pidType = uint32
type tidType = uint32
-type flagsType = string
+type flagsType = file.Flags
type pathMap map[pathType]map[traceIdType]map[commType]map[pidType]map[tidType]map[flagsType]counter
type iorData struct {
@@ -37,7 +39,7 @@ func newIorData() iorData {
func (iod iorData) add(ev *event.Pair) {
cnt := counter{count: 1, duration: ev.Duration, durationToPrev: ev.DurationToPrev}
iod.addPath(ev.FileName(), ev.EnterEv.GetTraceId(), ev.Comm, ev.EnterEv.GetPid(),
- ev.EnterEv.GetTid(), ev.FlagsString(), cnt)
+ ev.EnterEv.GetTid(), ev.Flags(), cnt)
}
func (iod iorData) addPath(path pathType, traceId traceIdType, comm commType,
@@ -116,8 +118,10 @@ func (iod iorData) commit() error {
filename := fmt.Sprintf("%s-%s-%s.ior.zst", hostname, flags.Get().FlamegraphName,
time.Now().Format("2006-01-02_15:04:05"))
+ log.Println("Writing", filename)
+ tmpFilename := fmt.Sprintf("%s.tmp", filename)
- file, err := os.Create(filename)
+ file, err := os.Create(tmpFilename)
if err != nil {
return err
}
@@ -126,8 +130,13 @@ func (iod iorData) commit() error {
encoder := zstd.NewWriter(file)
defer encoder.Close()
- // Write the data to a .txt file one line one entry and with a separator ␞, don't use JSON
- return nil
+ for line := range iod.lines() {
+ if _, err := encoder.Write([]byte(line + "\n")); err != nil {
+ return err
+ }
+ }
+
+ return os.Rename(tmpFilename, filename)
}
func (iod iorData) lines() iter.Seq[string] {
@@ -144,7 +153,7 @@ func (iod iorData) lines() iter.Seq[string] {
comm,
fmt.Sprint(pid),
fmt.Sprint(tid),
- flags,
+ flags.String(),
fmt.Sprintf("%d %d %d %d", cnt.count, cnt.duration, cnt.durationToPrev, cnt.bytes),
},
recordSeparator)
diff --git a/internal/flamegraph/iordata_test.go b/internal/flamegraph/iordata_test.go
index 7496067..ff17ca3 100644
--- a/internal/flamegraph/iordata_test.go
+++ b/internal/flamegraph/iordata_test.go
@@ -2,6 +2,7 @@ package flamegraph
import (
"ior/internal/types"
+ "syscall"
"testing"
)
@@ -12,7 +13,7 @@ func TestAddPath(t *testing.T) {
comm := commType("testComm")
pid := pidType(1234)
tid := tidType(5678)
- flags := flagsType("O_RDWR")
+ flags := flagsType(syscall.O_RDONLY)
cnt1 := counter{count: 1, duration: 1000, durationToPrev: 100}
iod.addPath(path, traceId, comm, pid, tid, flags, cnt1)
@@ -31,8 +32,8 @@ func TestAddPath(t *testing.T) {
}
func TestMerge(t *testing.T) {
- rdwrFlag := flagsType("O_RDWR")
- roFlag := flagsType("O_RDONLY")
+ rdwrFlag := flagsType(syscall.O_RDWR)
+ roFlag := flagsType(syscall.O_RDONLY)
traceId := types.SYS_ENTER_OPENAT
// Initialize two iorData instances with sample data
@@ -71,8 +72,8 @@ func TestMerge(t *testing.T) {
if len(merged.paths) != 2 {
t.Errorf("Expected 2 paths, got %d", len(merged.paths))
}
- if merged.paths["path1"][traceId]["comm1"][100][1000][flagsType("O_RDWR")].count != 10 {
- t.Errorf("Expected counter 10, got %d", merged.paths["path1"][1]["comm1"][100][1000][flagsType("O_RDWR")].count)
+ if merged.paths["path1"][traceId]["comm1"][100][1000][roFlag].count != 10 {
+ t.Errorf("Expected counter 10, got %d", merged.paths["path1"][1]["comm1"][100][1000][rdwrFlag].count)
}
if merged.paths["path2"][traceId]["comm2"][101][1000][roFlag].count != 60 {
t.Errorf("Expected counter 60, got %d", merged.paths["path2"][1]["comm2"][101][1000][roFlag].count)
@@ -88,7 +89,6 @@ func TestMerge(t *testing.T) {
var lines []string
for line := range merged.lines() {
- t.Log(line)
lines = append(lines, line)
}
diff --git a/internal/flamegraph/worker.go b/internal/flamegraph/worker.go
index a24be6d..40f6d3f 100644
--- a/internal/flamegraph/worker.go
+++ b/internal/flamegraph/worker.go
@@ -2,7 +2,6 @@ package flamegraph
import (
"context"
- "fmt"
"ior/internal/event"
"sync"
"time"
@@ -23,7 +22,6 @@ func (w worker) run(ctx context.Context, wg *sync.WaitGroup, ch <-chan *event.Pa
for {
select {
case ev := <-ch:
- fmt.Println("worker got event", ev)
w.iod.add(ev)
ev.Recycle()