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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
package flamegraph
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"ior/internal/event"
"ior/internal/file"
"ior/internal/types"
"iter"
"os"
"strings"
"time"
// Is there a zstd library part of Go 1.25
"github.com/DataDog/zstd"
)
type pathType = string
type traceIdType = types.TraceId
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
type recordKey struct {
Path pathType
TraceID traceIdType
Comm commType
Pid pidType
Tid tidType
Flags flagsType
}
type iorData struct {
records map[recordKey]Counter
}
func newIorData() iorData {
return iorData{records: make(map[recordKey]Counter)}
}
func newIorDataFromFile(filename string) (iorData, error) {
iod := newIorData()
if err := iod.loadFromFile(filename); err != nil {
return iorData{}, err
}
return iod, nil
}
// LoadFromFile loads an .ior.zst file and returns an iterator over all records.
func LoadFromFile(filename string) (iter.Seq[IterRecord], error) {
iod, err := newIorDataFromFile(filename)
if err != nil {
return nil, fmt.Errorf("load ior data from %s: %w", filename, err)
}
return iod.iter(), nil
}
func cloneString(s string) string {
// Clone the string by creating a new string with the same content
// This is a workaround to avoid using unsafe package
return string([]byte(s))
}
func (iod iorData) addEventPair(ev *event.Pair) {
cnt := Counter{Count: 1, Duration: ev.Duration, DurationToPrev: ev.DurationToPrev, Bytes: ev.Bytes}
iod.add(ev.FileName(), ev.EnterEv.GetTraceId(), strings.TrimSpace(ev.Comm), ev.EnterEv.GetPid(),
ev.EnterEv.GetTid(), ev.Flags(), cnt)
}
func (iod iorData) add(path pathType, traceId traceIdType, comm commType,
pid pidType, tid tidType, flags flagsType, addCnt Counter) {
key := recordKey{
Path: path,
TraceID: traceId,
Comm: comm,
Pid: pid,
Tid: tid,
Flags: flags,
}
cnt, ok := iod.records[key]
if !ok {
iod.records[key] = addCnt
return
}
iod.records[key] = cnt.add(addCnt)
}
func (iod iorData) merge(other iorData) iorData {
for key, cnt := range other.records {
iod.add(key.Path, key.TraceID, key.Comm, key.Pid, key.Tid, key.Flags, cnt)
}
return iod
}
func (iod iorData) serializeToFile(flamegraphName string) error {
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
if flamegraphName == "" {
flamegraphName = "default"
}
filename := fmt.Sprintf("%s-%s-%s.ior.zst", hostname, flamegraphName,
time.Now().Format("2006-01-02_15:04:05"))
fmt.Println("Writing", filename)
tmpFilename := fmt.Sprintf("%s.tmp", filename)
file, err := os.Create(tmpFilename)
if err != nil {
return err
}
defer file.Close()
encoder := zstd.NewWriter(file)
defer encoder.Close()
gobEncoder := gob.NewEncoder(encoder)
if err := gobEncoder.Encode(iod.records); err != nil {
return err
}
if err := encoder.Flush(); err != nil {
return err
}
return os.Rename(tmpFilename, filename)
}
func (iod *iorData) loadFromFile(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
decoder := zstd.NewReader(file)
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 {
return err
}
decoder = zstd.NewReader(file)
defer decoder.Close()
var buffer bytes.Buffer
if _, err = io.Copy(&buffer, decoder); err != nil {
return err
}
return iod.deserialize(&buffer)
}
func (iod iorData) serialize() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(iod.records)
return buf.Bytes(), err
}
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 {
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
}
return nil
}
// IterRecord is a single record returned by the iterator.
type IterRecord struct {
Path string
TraceID types.TraceId
Comm string
Pid uint32
Tid uint32
Flags file.Flags
Cnt Counter
}
// StringByName returns the string representation of a field by name.
// Returns an error if the field name is not recognized.
func (ir IterRecord) StringByName(name string) (string, error) {
switch name {
case "path":
return strings.Join(strings.Split(ir.Path, "/"), ";/"), nil
case "comm":
return ir.Comm, nil
case "tracepoint":
return ir.TraceID.String(), nil
case "pid":
return fmt.Sprint(ir.Pid), nil
case "tid":
return fmt.Sprint(ir.Tid), nil
case "flags":
return ir.Flags.String(), nil
default:
return "", fmt.Errorf("unknown field %q in record", name)
}
}
func (iod iorData) iter() iter.Seq[IterRecord] {
return func(yield func(IterRecord) bool) {
for key, cnt := range iod.records {
record := IterRecord{
Path: key.Path,
TraceID: key.TraceID,
Comm: key.Comm,
Pid: key.Pid,
Tid: key.Tid,
Flags: key.Flags,
Cnt: cnt,
}
if !yield(record) {
return
}
}
}
}
|