Skip to content

Reduce CPU consumption and heap allocations of SubsystemMountpoints() #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

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 <[email protected]>
@rockdaboot rockdaboot force-pushed the rockdaboot/optimize-subsystemmountpoints branch from ed982b4 to f217e0f Compare June 17, 2025 08:04
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.

1 participant