diff options
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | internal/event/event.go | 8 | ||||
| -rw-r--r-- | internal/flamegraph/counter.go | 1 | ||||
| -rw-r--r-- | internal/flamegraph/iordata.go | 42 | ||||
| -rw-r--r-- | internal/flamegraph/worker.go | 2 |
6 files changed, 42 insertions, 18 deletions
@@ -2,4 +2,7 @@ module ior go 1.24 -require github.com/aquasecurity/libbpfgo v0.6.0-libbpf-1.3.0.20240111220235-90dbffffbdab +require ( + github.com/DataDog/zstd v1.5.7 + github.com/aquasecurity/libbpfgo v0.6.0-libbpf-1.3.0.20240111220235-90dbffffbdab +) @@ -1,3 +1,5 @@ +github.com/DataDog/zstd v1.5.7 h1:ybO8RBeh29qrxIhCA9E8gKY6xfONU9T6G6aP9DTKfLE= +github.com/DataDog/zstd v1.5.7/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/aquasecurity/libbpfgo v0.6.0-libbpf-1.3.0.20240111220235-90dbffffbdab h1:w74AraWsnj+AgEOk2uERlLtECCWutMtuwCGCCWzpBBs= github.com/aquasecurity/libbpfgo v0.6.0-libbpf-1.3.0.20240111220235-90dbffffbdab/go.mod h1:0rEApF1YBHGuZ4C8OYI9q5oDBVpgqtRqYATePl9mCDk= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/internal/event/event.go b/internal/event/event.go index dfff270..f835aaf 100644 --- a/internal/event/event.go +++ b/internal/event/event.go @@ -30,7 +30,7 @@ type Pair struct { Duration uint64 // To calculate the time difference from the previoud event. - durationToPrev uint64 + DurationToPrev uint64 } func NewPair(enterEv Event) *Pair { @@ -42,7 +42,7 @@ func NewPair(enterEv Event) *Pair { func (e *Pair) CalculateDurations(prevPairTime uint64) { e.Duration = e.ExitEv.GetTime() - e.EnterEv.GetTime() if prevPairTime > 0 { - e.durationToPrev = e.EnterEv.GetTime() - prevPairTime + e.DurationToPrev = e.EnterEv.GetTime() - prevPairTime } } @@ -55,7 +55,7 @@ const EventStreamHeader = "durationToPrevNs,durationNs,comm,pid.tid,name,ret,not func (e *Pair) String() string { var sb strings.Builder - sb.WriteString(fmt.Sprintf("%08d,%08d", e.durationToPrev, e.Duration)) + sb.WriteString(fmt.Sprintf("%08d,%08d", e.DurationToPrev, e.Duration)) sb.WriteString(",") sb.WriteString(e.Comm) @@ -90,6 +90,6 @@ func (e *Pair) Dump() string { func (e *Pair) Recycle() { e.EnterEv.Recycle() e.ExitEv.Recycle() - e.durationToPrev = 0 + e.DurationToPrev = 0 poolOfEventPairs.Put(e) } diff --git a/internal/flamegraph/counter.go b/internal/flamegraph/counter.go index 4a63f50..98ac035 100644 --- a/internal/flamegraph/counter.go +++ b/internal/flamegraph/counter.go @@ -4,6 +4,7 @@ type counter struct { count uint64 duration uint64 durationToPrev uint64 + // bytes uint64 TODO implement } func (c counter) add(other counter) counter { diff --git a/internal/flamegraph/iordata.go b/internal/flamegraph/iordata.go index 80571d9..c167f22 100644 --- a/internal/flamegraph/iordata.go +++ b/internal/flamegraph/iordata.go @@ -4,14 +4,14 @@ import ( "encoding/json" "fmt" "ior/internal/event" + "ior/internal/flags" "ior/internal/types" "os" "time" -) -const fileSuffix = ".ior" + "github.com/DataDog/zstd" +) -// e.g. pathType ¶ traceid ¶ comm ¶ pid ¶ tid ¶ flags ¶ counter type pathType = string type traceIdType = types.TraceId type commType = string @@ -19,14 +19,24 @@ type pidType = uint32 type tidType = uint32 type flagsType = string type pathMap map[pathType]map[traceIdType]map[commType]map[pidType]map[tidType]map[flagsType]counter -type iorData struct{ paths pathMap } + +type iorData struct { + flags flags.Flags + paths pathMap +} // TODO: Flag to enable iorData // TODO: Name flag for iorData (outfile format: hostname-name-timestamp.ior.zst) // TODO: Output path for iorData flag // TODO: Add helper to convert .ior data file to collapsed format -func newIorData() iorData { return iorData{paths: make(pathMap)} } +func newIorData(flags flags.Flags) iorData { + return iorData{ + flags: flags, + paths: make(pathMap), + } +} +// TODO: Unit test func (iod iorData) add(ev *event.Pair) { // type Pair struct { // EnterEv, ExitEv Event @@ -38,15 +48,16 @@ func (iod iorData) add(ev *event.Pair) { // PrevPair *Pair // durationToPrev uint64 // } - // TODO: Add duration to prev to counter cnt := counter{ - count: 1, - duration: ev.Duration, + count: 1, + duration: ev.Duration, + durationToPrev: ev.DurationToPrev, } iod.addPath(ev.File.Name(), ev.EnterEv.GetTraceId(), ev.Comm, ev.EnterEv.GetPid(), ev.EnterEv.GetTid(), ev.File.FlagsString(), cnt) } +// TODO: Unit test func (iod iorData) addPath(path pathType, traceId traceIdType, comm commType, pid pidType, tid tidType, flags flagsType, addCnt counter) { @@ -84,18 +95,25 @@ func (iod iorData) addPath(path pathType, traceId traceIdType, comm commType, } func (iod iorData) commit() error { - currentTime := time.Now().Format("2006-01-02_15:04:05") hostname, err := os.Hostname() if err != nil { panic(err) } - filename := fmt.Sprintf("%s-%s.%s", hostname, currentTime, fileSuffix) + + filename := fmt.Sprintf("%s-%s-%s.ior.zst", hostname, iod.flags.FlamegraphName, + time.Now().Format("2006-01-02_15:04:05")) file, err := os.Create(filename) if err != nil { return err } defer file.Close() - encoder := json.NewEncoder(file) - return encoder.Encode(iod.paths) + encoder, err := zstd.NewWriter(file) + if err != nil { + return err + } + defer encoder.Close() + + jsonEncoder := json.NewEncoder(encoder) + return jsonEncoder.Encode(iod.paths) } diff --git a/internal/flamegraph/worker.go b/internal/flamegraph/worker.go index 6c2b9cd..e47329e 100644 --- a/internal/flamegraph/worker.go +++ b/internal/flamegraph/worker.go @@ -17,7 +17,7 @@ type worker struct { func newWorker() worker { return worker{ collapsed: make(collapsed), // COLLAPSED: Retire ocne newIorData implemented - iod: newIorData(), + iod: newIorData(), // TODO: make flags global, so i don't have to pass them through the whole code base } } |
