summaryrefslogtreecommitdiff
path: root/internal/flamegraph/collapsed.go
blob: f04a38dc3f1ec786259af470036d38920548437a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
}