Skip to content

mem: populate Cached from bcachestats on OpenBSD#2029

Merged
shirou merged 2 commits intoshirou:masterfrom
skartikey:fix/openbsd-mem-cached
Mar 21, 2026
Merged

mem: populate Cached from bcachestats on OpenBSD#2029
shirou merged 2 commits intoshirou:masterfrom
skartikey:fix/openbsd-mem-cached

Conversation

@skartikey
Copy link
Copy Markdown
Contributor

On OpenBSD, VirtualMemoryStat.Cached was hardcoded to 0 while Buffers was populated from bcachestats.Numbufpages. Since the buffer cache is the closest OpenBSD equivalent to Linux cached memory, this change populates Cached from the same source.

This fixes the Available memory calculation:

Available = Inactive + Cached + Free

Previously, Available was understated on OpenBSD because Cached was always 0, affecting downstream consumers (e.g. influxdata/telegraf#18469).

On OpenBSD, Cached was hardcoded to 0 with the comment "not available",
while Buffers was populated from bcachestats.Numbufpages (buffer cache pages).
Since the buffer cache is the closest OpenBSD equivalent to Linux cached memory,
populate Cached from the same source.

This fixes the Available memory calculation which includes Cached:
  Available = Inactive + Cached + Free

Previously, Available was understated because Cached was always 0.

Ref: influxdata/telegraf#18469
Copy link
Copy Markdown
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Clean and well-motivated fix.

The reordering so that the bcachestats syscall happens before constructing ret is a nice improvement — no more partially-populated struct on error paths.

One minor suggestion: it might be worth adding a brief comment explaining why Cached and Buffers share the same value, to help future maintainers understand this is intentional and not a copy-paste mistake. Something like:

  // On OpenBSD, the buffer cache is the closest equivalent to both
  // Linux's Buffers and Cached memory.
  bcache := uint64(bcs.Numbufpages) * p

Not a blocker though. But if you have time, it'd be great if you could update the PR to add that comment. Thanks for the fix and the clear PR description!

@skartikey skartikey requested a review from shirou March 20, 2026 18:59
@shirou
Copy link
Copy Markdown
Owner

shirou commented Mar 21, 2026

Thank you for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants