diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-24 12:12:31 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-24 12:12:31 +0200 |
| commit | 610d91472b3b37010130f33bd835c23e859caf56 (patch) | |
| tree | 48cc2cb7e425c69135095ad748389afd0192c4d1 /internal/statsengine/process.go | |
| parent | 0d4ef22478a470d86ce907beedcaa726d0d46c73 (diff) | |
statsengine: build snapshots outside engine mutex
Diffstat (limited to 'internal/statsengine/process.go')
| -rw-r--r-- | internal/statsengine/process.go | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/internal/statsengine/process.go b/internal/statsengine/process.go index 296312b..a0d29c1 100644 --- a/internal/statsengine/process.go +++ b/internal/statsengine/process.go @@ -18,6 +18,14 @@ type processStats struct { totalLatency uint64 } +type processSnapshotInput struct { + pid uint32 + comm string + count uint64 + totalBytes uint64 + totalLatency uint64 +} + func newProcessAccumulator() *processAccumulator { return &processAccumulator{byPID: make(map[uint32]*processStats)} } @@ -53,12 +61,33 @@ func (a *processAccumulator) Snapshot(elapsed time.Duration) []ProcessSnapshot { return nil } - rateDiv := elapsed.Seconds() - result := make([]ProcessSnapshot, 0, len(a.byPID)) + return buildProcessSnapshots(a.snapshotInputs(), elapsed) +} + +func (a *processAccumulator) snapshotInputs() []processSnapshotInput { + if a == nil { + return nil + } + + inputs := make([]processSnapshotInput, 0, len(a.byPID)) for _, stats := range a.byPID { - result = append(result, stats.toSnapshot(rateDiv)) + inputs = append(inputs, processSnapshotInput{ + pid: stats.pid, + comm: stats.comm, + count: stats.count, + totalBytes: stats.totalBytes, + totalLatency: stats.totalLatency, + }) } + return inputs +} +func buildProcessSnapshots(inputs []processSnapshotInput, elapsed time.Duration) []ProcessSnapshot { + rateDiv := elapsed.Seconds() + result := make([]ProcessSnapshot, 0, len(inputs)) + for _, in := range inputs { + result = append(result, in.toSnapshot(rateDiv)) + } sort.Slice(result, func(i, j int) bool { if result[i].Syscalls != result[j].Syscalls { return result[i].Syscalls > result[j].Syscalls @@ -68,11 +97,10 @@ func (a *processAccumulator) Snapshot(elapsed time.Duration) []ProcessSnapshot { } return result[i].PID < result[j].PID }) - return result } -func (s *processStats) toSnapshot(rateDiv float64) ProcessSnapshot { +func (s processSnapshotInput) toSnapshot(rateDiv float64) ProcessSnapshot { avg := 0.0 if s.count > 0 { avg = float64(s.totalLatency) / float64(s.count) |
