From 6ea8920dac3b7e3868707a84e58a5d7e10ebbbf3 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 24 Feb 2026 21:19:09 +0200 Subject: flamegraph: remove external tool path and document native generation --- internal/flags/flags.go | 19 ++----- internal/flamegraph/collapsed.go | 76 -------------------------- internal/flamegraph/tool.go | 114 --------------------------------------- internal/ior.go | 27 ++-------- 4 files changed, 7 insertions(+), 229 deletions(-) delete mode 100644 internal/flamegraph/collapsed.go delete mode 100644 internal/flamegraph/tool.go (limited to 'internal') diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 5bde9c6..9354bc3 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -21,8 +21,6 @@ var ( pidFilter atomic.Int64 ) -const flamegraphToolDefault = "" - var ( validCollapsedFields = []string{ "path", @@ -60,13 +58,10 @@ type Flags struct { FlamegraphName string TUIExportEnable bool - // To convert ior data into collapsed format + // To convert ior data into native SVG format IorDataFile string CollapsedFields []string CountField string - - // To generate the Flamegraph SVGs - FlamegraphTool string } func Get() Flags { @@ -110,22 +105,14 @@ func parse() { flag.StringVar(&singleton.FlamegraphName, "name", "default", "Name of the flamegraph, used to generate the SVG file") flag.BoolVar(&singleton.TUIExportEnable, "tuiExport", true, "Enable writing TUI snapshot export files") - flag.StringVar(&singleton.IorDataFile, "ior", "", "IOR data file to convert into collapsed format") + flag.StringVar(&singleton.IorDataFile, "ior", "", "IOR data file to convert into native SVG flamegraph") fields := flag.String("fields", "", fmt.Sprintf("Comma separated list of fields to collapse, valid are: %v", validCollapsedFields)) flag.StringVar(&singleton.CountField, "count", "count", - fmt.Sprintf("Count field to collaps, valid are: %v", validCollapsedCounts)) - - // https://github.com/brendangregg/FlameGraph - flag.StringVar(&singleton.FlamegraphTool, "flamegraphTool", - "", "Path to the flamegraph tool (e.g. flamegraph.pl or inferno-flamegraph)") + fmt.Sprintf("Count field to collapse, valid are: %v", validCollapsedCounts)) flag.Parse() pidFilter.Store(int64(singleton.PidFilter)) - if singleton.FlamegraphTool == "" { - singleton.FlamegraphTool = flamegraphToolDefault - } - singleton.TracepointsToAttach = extractTracepointFlags(*tracepointsToAttach) singleton.TracepointsToExclude = extractTracepointFlags(*tracepointsToExclude) diff --git a/internal/flamegraph/collapsed.go b/internal/flamegraph/collapsed.go deleted file mode 100644 index f04a38d..0000000 --- a/internal/flamegraph/collapsed.go +++ /dev/null @@ -1,76 +0,0 @@ -package flamegraph - -import ( - "fmt" - "os" - "strings" - - "github.com/DataDog/zstd" -) - -// Collapsed represents a structure used to process and store information -// related to a collapsed flamegraph. It includes the following fields: -// - iorFile: The path to the input/output report file. -// - fields: A list of field names used in the flamegraph processing. -// - countField: The name of the field that represents the count or weight -// in the flamegraph data. -type Collapsed struct { - iorFile string // Path to the input/output report file. - fields []string // List of field names used in processing. - countField string // Field name representing the count or weight. -} - -func NewCollapsed(iorFile string, fields []string, countField string) Collapsed { - return Collapsed{iorFile: iorFile, fields: fields, countField: countField} -} - -func (c Collapsed) Write(iorDataFile string) (string, error) { - outFile := fmt.Sprintf("%s.%s-by-%s.collapsed.zst", - strings.TrimSuffix(iorDataFile, ".ior.zst"), - strings.Join(c.fields, ":"), - c.countField, - ) - - if _, err := os.Stat(outFile); err == nil { - fmt.Println(outFile, "already exists!") - return outFile, nil - } - - // outFD should be zstd compressed - outFd, err := os.Create(outFile) - if err != nil { - return outFile, err - } - defer outFd.Close() - - fmt.Println("Reading", iorDataFile) - iod, err := newIorDataFromFile(iorDataFile) - if err != nil { - return outFile, err - } - - fmt.Println("Writing", outFile) - writer := zstd.NewWriter(outFd) - if err != nil { - return outFile, err - } - defer writer.Close() - - for record := range iod.iter() { - var fieldValues []string - for _, fieldName := range c.fields { - v, err := record.StringByName(fieldName) - if err != nil { - return outFile, fmt.Errorf("field %s: %w", fieldName, err) - } - fieldValues = append(fieldValues, v) - } - writer.Write([]byte(fmt.Sprintf("%s %d\n", - strings.Join(fieldValues, ";"), - record.Cnt.ValueByName(c.countField), - ))) - } - writer.Flush() - - return outFile, nil -} diff --git a/internal/flamegraph/tool.go b/internal/flamegraph/tool.go deleted file mode 100644 index a83c44f..0000000 --- a/internal/flamegraph/tool.go +++ /dev/null @@ -1,114 +0,0 @@ -package flamegraph - -import ( - "fmt" - "io" - "ior/internal/flags" - "os" - "os/exec" - "strings" - - "github.com/DataDog/zstd" -) - -// Tool represents a utility for generating flamegraphs. -// It contains the path to the flamegraph tool, the arguments to be passed to it, -// and the output file where the generated flamegraph will be stored. -type Tool struct { - flamegraphTool string // Path to the flamegraph tool executable. - args []string // Arguments to be passed to the flamegraph tool. - outFile string // Path to the output file where the flamegraph will be saved. -} - -func NewTool(collapsedFile string) (Tool, error) { - if strings.HasSuffix(collapsedFile, ".zst") { - var err error - collapsedFile, err = decompress(collapsedFile) - if err != nil { - return Tool{}, err - } - } - - t := Tool{ - flamegraphTool: flags.Get().FlamegraphTool, - args: []string{collapsedFile, "--hash"}, - outFile: strings.TrimSuffix(collapsedFile, ".collapsed") + ".svg", - } - - t.args = append(t.args, "--title") - t.args = append(t.args, fmt.Sprintf("I/O Traces (%s by %s)", - strings.Join(flags.Get().CollapsedFields, ","), flags.Get().CountField, - )) - - return t, nil -} - -func (t Tool) WriteSVG() error { - defer deleteFileIfEmpty(t.outFile) - - if _, err := os.Stat(t.outFile); err == nil { - fmt.Println(t.outFile, "already exists!") - return nil - } - cmd := exec.Command(t.flamegraphTool, t.args...) - fmt.Println("Running", cmd) - - outFd, err := os.Create(t.outFile) - if err != nil { - return err - } - defer outFd.Close() - - cmd.Stdout = outFd - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { - return err - } - - return nil -} - -func (t Tool) OutFile() string { - return t.outFile -} - -func decompress(compressedFile string) (string, error) { - decompressedFile := strings.TrimSuffix(compressedFile, ".zst") - - file, err := os.Open(compressedFile) - if err != nil { - return decompressedFile, err - } - defer file.Close() - - decoder := zstd.NewReader(file) - defer decoder.Close() - - decompressedFd, err := os.Create(decompressedFile) - if err != nil { - return decompressedFile, err - } - defer decompressedFd.Close() - - _, err = io.Copy(decompressedFd, decoder) - if err != nil { - return decompressedFile, err - } - - return decompressedFile, nil -} - -func deleteFileIfEmpty(file string) error { - if _, err := os.Stat(file); err == nil { - fileInfo, err := os.Stat(file) - if err != nil { - return err - } - if fileInfo.Size() == 0 { - fmt.Println("Deleting", file, "as it is empty") - return os.Remove(file) - } - } - return nil -} diff --git a/internal/ior.go b/internal/ior.go index 599736d..b136931 100644 --- a/internal/ior.go +++ b/internal/ior.go @@ -99,29 +99,10 @@ func Run() error { if iorFile != "" { noTraceRun = true - var svgFile string - if cfg.FlamegraphTool != "" { - collapsed := flamegraph.NewCollapsed(iorFile, cfg.CollapsedFields, cfg.CountField) - collapsedFile, err := collapsed.Write(iorFile) - if err != nil { - return err - } - - tool, err := flamegraph.NewTool(collapsedFile) - if err != nil { - return err - } - if err := tool.WriteSVG(); err != nil { - return err - } - svgFile = tool.OutFile() - } else { - native := flamegraph.NewNativeSVG(cfg.CollapsedFields, cfg.CountField) - var err error - svgFile, err = native.WriteSVGFromFile(iorFile) - if err != nil { - return err - } + native := flamegraph.NewNativeSVG(cfg.CollapsedFields, cfg.CountField) + svgFile, err := native.WriteSVGFromFile(iorFile) + if err != nil { + return err } if err := flamegraph.ServeSVG(svgFile); err != nil { -- cgit v1.2.3