From 3a2aa5f2a8b417b4aa4c9148b245d85fcc22a61c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 6 Mar 2026 16:52:02 +0200 Subject: refactor: remove legacy iordata path map fallback (task 384) --- integrationtests/parse_test.go | 55 +++++++++++++++++++----------------------- internal/flamegraph/iordata.go | 51 +++++++-------------------------------- 2 files changed, 34 insertions(+), 72 deletions(-) diff --git a/integrationtests/parse_test.go b/integrationtests/parse_test.go index 65781a5..215b4b8 100644 --- a/integrationtests/parse_test.go +++ b/integrationtests/parse_test.go @@ -15,44 +15,39 @@ import ( ) // writeIorZst creates a minimal .ior.zst file from known data. -// It encodes a pathMap (the same nested-map structure used internally by iorData) -// so that LoadTestResult can decode it via the public flamegraph.LoadFromFile API. +// It encodes the current record-key map format used by flamegraph iorData. func writeIorZst(t *testing.T, dir string, records []flamegraph.IterRecord) string { t.Helper() - // Build the nested map matching iorData.paths layout: - // path → traceId → comm → pid → tid → flags → Counter - type ( - flagsMap = map[file.Flags]flamegraph.Counter - tidMap = map[uint32]flagsMap - pidMap = map[uint32]tidMap - commMap = map[string]pidMap - traceIdMap = map[types.TraceId]commMap - pathMapType = map[string]traceIdMap - ) - - paths := make(pathMapType) + type recordKey struct { + Path string + TraceID types.TraceId + Comm string + Pid uint32 + Tid uint32 + Flags file.Flags + } + + flat := make(map[recordKey]flamegraph.Counter) for _, r := range records { - if paths[r.Path] == nil { - paths[r.Path] = make(traceIdMap) - } - if paths[r.Path][r.TraceID] == nil { - paths[r.Path][r.TraceID] = make(commMap) - } - if paths[r.Path][r.TraceID][r.Comm] == nil { - paths[r.Path][r.TraceID][r.Comm] = make(pidMap) - } - if paths[r.Path][r.TraceID][r.Comm][r.Pid] == nil { - paths[r.Path][r.TraceID][r.Comm][r.Pid] = make(tidMap) - } - if paths[r.Path][r.TraceID][r.Comm][r.Pid][r.Tid] == nil { - paths[r.Path][r.TraceID][r.Comm][r.Pid][r.Tid] = make(flagsMap) + key := recordKey{ + Path: r.Path, + TraceID: r.TraceID, + Comm: r.Comm, + Pid: r.Pid, + Tid: r.Tid, + Flags: r.Flags, } - paths[r.Path][r.TraceID][r.Comm][r.Pid][r.Tid][r.Flags] = r.Cnt + current := flat[key] + current.Count += r.Cnt.Count + current.Duration += r.Cnt.Duration + current.DurationToPrev += r.Cnt.DurationToPrev + current.Bytes += r.Cnt.Bytes + flat[key] = current } var buf bytes.Buffer - if err := gob.NewEncoder(&buf).Encode(paths); err != nil { + if err := gob.NewEncoder(&buf).Encode(flat); err != nil { t.Fatalf("gob encode: %v", err) } diff --git a/internal/flamegraph/iordata.go b/internal/flamegraph/iordata.go index a205916..4a562e3 100644 --- a/internal/flamegraph/iordata.go +++ b/internal/flamegraph/iordata.go @@ -5,7 +5,6 @@ import ( "encoding/gob" "errors" "fmt" - "io" "iter" "os" "strings" @@ -25,7 +24,6 @@ type commType = string type pidType = uint32 type tidType = uint32 type flagsType = file.Flags -type pathMap map[pathType]map[traceIdType]map[commType]map[pidType]map[tidType]map[flagsType]Counter var hostnameFn = os.Hostname @@ -157,23 +155,14 @@ func (iod *iorData) loadFromFile(filename string) error { defer decoder.Close() var records map[recordKey]Counter - if err := gob.NewDecoder(decoder).Decode(&records); err == nil && len(records) > 0 { - iod.records = records - return nil - } - - // Fallback path for legacy payloads and empty-map ambiguity. - if _, err := file.Seek(0, io.SeekStart); err != nil { + if err := gob.NewDecoder(decoder).Decode(&records); err != nil { return err } - decoder = zstd.NewReader(file) - defer decoder.Close() - - var buffer bytes.Buffer - if _, err = io.Copy(&buffer, decoder); err != nil { - return err + if records == nil { + records = make(map[recordKey]Counter) } - return iod.deserialize(&buffer) + iod.records = records + return nil } func (iod iorData) serialize() ([]byte, error) { @@ -184,36 +173,14 @@ func (iod iorData) serialize() ([]byte, error) { } func (iod *iorData) deserialize(buf *bytes.Buffer) error { - raw := append([]byte(nil), buf.Bytes()...) - dec := gob.NewDecoder(bytes.NewReader(raw)) var records map[recordKey]Counter - if err := dec.Decode(&records); err == nil && len(records) > 0 { - iod.records = records - return nil - } - - var legacy pathMap - if err := gob.NewDecoder(bytes.NewReader(raw)).Decode(&legacy); err != nil { + if err := gob.NewDecoder(bytes.NewReader(buf.Bytes())).Decode(&records); err != nil { return err } - - iod.records = make(map[recordKey]Counter) - for path, traceIDMap := range legacy { - for traceID, commMap := range traceIDMap { - for comm, pidMap := range commMap { - for pid, tidMap := range pidMap { - for tid, flagsMap := range tidMap { - for f, cnt := range flagsMap { - iod.add(path, traceID, comm, pid, tid, f, cnt) - } - } - } - } - } - } - if len(iod.records) == 0 && records != nil { - iod.records = records + if records == nil { + records = make(map[recordKey]Counter) } + iod.records = records return nil } -- cgit v1.2.3