Skip to content
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

[metric/memory] use of wrong source for Windows swap calculations #130

Open
leehinman opened this issue Feb 29, 2024 · 1 comment
Open
Labels
bug Something isn't working Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Comments

@leehinman
Copy link
Contributor

currently the windows memory module uses GlobalMemoryStatusEx to get the swap Total, Used & FreeBytes.

memoryStatusEx, err := windows.GlobalMemoryStatusEx()
if err != nil {
return memData, fmt.Errorf("error fetching global memory status: %w", err)
}
memData.Total = opt.UintWith(memoryStatusEx.TotalPhys)
memData.Free = opt.UintWith(memoryStatusEx.AvailPhys)
memData.Used.Bytes = opt.UintWith(memoryStatusEx.TotalPhys - memoryStatusEx.AvailPhys)
// We shouldn't really be doing this, but we also don't want to make breaking changes right now,
// and memory.actual is used by quite a few visualizations
memData.Actual.Free = memData.Free
memData.Actual.Used.Bytes = memData.Used.Bytes
memData.Swap.Free = opt.UintWith(memoryStatusEx.AvailPageFile)
memData.Swap.Total = opt.UintWith(memoryStatusEx.TotalPageFile)
memData.Swap.Used.Bytes = opt.UintWith(memoryStatusEx.TotalPageFile - memoryStatusEx.AvailPageFile)

But GlobalMemoryStatusEx is really a syscall of procGlobalMemoryStatusEx which ends up being procGlobalMemoryStatusEx = modkernel32.NewProc("GlobalMemoryStatusEx")

And the documentation for MemoryStatusEx says that we should be using GetPerformanceInfo instead.

ullTotalPageFile

The current committed memory limit for the system or the current process, whichever is smaller, in bytes. To get the system-wide committed memory limit, call GetPerformanceInfo.

ullAvailPageFile

The maximum amount of memory the current process can commit, in bytes. This value is equal to or smaller than the system-wide available commit value. To calculate the system-wide available commit value, call GetPerformanceInfo and subtract the value of CommitTotal from the value of CommitLimit.

This can lead to Metricbeat showing an incorrect amount of swap, especially when swap is configured to be very small or zero.

@leehinman leehinman added bug Something isn't working Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team labels Feb 29, 2024
@bczifra
Copy link
Member

bczifra commented Sep 27, 2024

Related to: elastic/beats#19875

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team
Projects
None yet
Development

No branches or pull requests

2 participants