diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-24 21:19:09 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-24 21:19:09 +0200 |
| commit | 6ea8920dac3b7e3868707a84e58a5d7e10ebbbf3 (patch) | |
| tree | 63138d32b6197522855a0b69e3c09068a0e1df41 /internal/flamegraph | |
| parent | 93d587a6f5ae453907de3d5556866b60bac405cb (diff) | |
flamegraph: remove external tool path and document native generation
Diffstat (limited to 'internal/flamegraph')
| -rw-r--r-- | internal/flamegraph/collapsed.go | 76 | ||||
| -rw-r--r-- | internal/flamegraph/tool.go | 114 |
2 files changed, 0 insertions, 190 deletions
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 -} |
