Skip to content

Conversation

@rockdaboot
Copy link
Contributor

@rockdaboot rockdaboot commented May 28, 2025

What does this PR do?

This PR reduces memory allocations and thus GC activity in a hot path (SubsystemMountpoints()).

It also adds a benchmark that can be run with

cd metric/system/cgroup
go test . -bench SubsystemMountpoints -run nothing -count=1

Benchmark results Before

goos: linux
goarch: amd64
pkg: github.com/elastic/elastic-agent-system-metrics/metric/system/cgroup
cpu: 12th Gen Intel(R) Core(TM) i7-12800H
BenchmarkSubsystemMountpoints-20           24133             47672 ns/op           17176 B/op        159 allocs/op

Benchmark results After

goos: linux
goarch: amd64
pkg: github.com/elastic/elastic-agent-system-metrics/metric/system/cgroup
cpu: 12th Gen Intel(R) Core(TM) i7-12800H
BenchmarkSubsystemMountpoints-20           38503             33503 ns/op            7000 B/op         39 allocs/op

It became aware when looking at the Universal Profiling flamegraph for auditbeat that
SubsystemMountpoints() was consuming ~25% of the CPU usage of auditbeat. Additionally, it created a lot of unneeded heap allocations for temporary (function-local) variables. The GC ate another ~21% of the total CPU consumption of auditbeat.

Screenshot (data from a real set of >200 Elasticsearch clusters)

Screenshot_20250528_183159

Why is it important?

As long running applications, the beats should consume as less CPU as possible.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works

@rockdaboot rockdaboot self-assigned this May 28, 2025
@rockdaboot rockdaboot changed the title Rockdaboot/optimize subsystemmountpoints Reduce CPU consumption and heap allocations of SubsystemMountpoints() May 28, 2025
@rockdaboot rockdaboot force-pushed the rockdaboot/optimize-subsystemmountpoints branch 2 times, most recently from c447378 to a457e27 Compare May 31, 2025 21:07
@rockdaboot rockdaboot mentioned this pull request Jun 4, 2025
3 tasks
kruskall added a commit that referenced this pull request Jun 16, 2025
## What does this PR do?

Update golangci-lint to 1.64.8, the latest version before v2, which
requires fundamental changes to the config file (for a follow-up PR).

<!-- Mandatory
Explain here the changes you made on the PR. Please explain the WHAT:
patterns used, algorithms implemented, design architecture, message
processing, etc.
-->

## Why is it important?

PR #220 requires a newer version of the linter in order to allow Go 1.23
iterators.

## Checklist

- [x] My code follows the style guidelines of this project
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have added tests that prove my fix is effective or that my
feature works

---------

Co-authored-by: kruskall <99559985+kruskall@users.noreply.github.com>
@rockdaboot rockdaboot force-pushed the rockdaboot/optimize-subsystemmountpoints branch 2 times, most recently from 65d8294 to f470f43 Compare June 23, 2025 08:48
@rockdaboot rockdaboot force-pushed the rockdaboot/optimize-subsystemmountpoints branch from 83eb30f to e8045a3 Compare July 25, 2025 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants