Skip to content

Read the NetBSD open file count from pstat -T - #3820

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:netbsd-open-files
Oct 17, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:netbsd-open-files

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

FileSystem.getOpenFileDescriptors() on NetBSD read kern.nfiles. NetBSD doesn't have that sysctl, so the method always returned 0.

Change

NetBSD has no sysctl for this count at all. pstat -T reads the kernel's nfiles counter through kvm, and pstat is setgid kmem, so it works without root. Its first line is the system-wide total:

1100/10000 files

This is the same counter FreeBSD exposes as kern.openfiles and OpenBSD as kern.nfiles.

pstat is run by its absolute path (/usr/sbin/pstat), since /usr/sbin isn't always on an unprivileged user's PATH. SolarisSensors calls /usr/sbin/prtpicl the same way.

pstat -f is not an alternative. Its header counts KERN_FILE2 records, and the kernel filters those down to the files an unprivileged caller is allowed to see. On cfarm428 it reported 179 open files against the true 1100. See filemode() in pstat.c.

Verification

cfarm428 (NetBSD 10.1 evbarm aarch64, non-root):

Before After
File Descriptors 0/10000 1100/10000

All three configurations agree: JNA without the native library, JNA with it, and FFM. The count matches pstat -T run by hand.

A fixture test covers:

  • the captured output
  • a small count padded by pstat's %3d format
  • a pstat -f header, which must not be read as the total

Summary by CodeRabbit

  • Bug Fixes
    • Corrected open file descriptor counts on NetBSD by reading the system’s reported file total. Returned counts are now available even when the kern.nfiles system setting is unavailable.

getOpenFileDescriptors() read kern.nfiles, which NetBSD does not have, so
it always returned 0. NetBSD exposes no sysctl for the count. pstat -T
reads the kernel's nfiles counter through kvm, and is setgid kmem, so it
works without root and prints the system-wide total on its first line:

  1100/10000 files

pstat -f is not an alternative: it counts the KERN_FILE2 records, which
the kernel filters to the files an unprivileged caller may see (179 of
the same 1100 on cfarm428).

pstat is run by its absolute path, since /usr/sbin is not always on the
PATH of an unprivileged user.

Verified on cfarm428 (NetBSD 10.1 aarch64) in all three configurations:
File Descriptors 1100/10000, previously 0/10000.
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e2fddd94-d977-416f-936a-7850276aa75b
📥 Commits

Reviewing files that changed from the base of the PR and between a331030 and 070c32e.

📒 Files selected for processing: 3
  • CHANGELOG.md
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdFileSystem.java
  • oshi-common/src/test/java/oshi/software/common/os/unix/netbsd/NetBsdFileSystemTest.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout: 11
  • GitHub Check: Test JDK 25, OpenIndiana
  • GitHub Check: Test JDK 25, FreeBSD
  • GitHub Check: Test JDK 25, NetBSD
  • GitHub Check: Test JDK 25, OpenBSD
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Error Prone
  • GitHub Check: Analyze (java)
🧰 Additional context used
📚 Code guidelines: 2
AGENTS.md — auto-discovered
CONTRIBUTING.md — auto-discovered
📓 Path-based instructions: 2
Source excerpt: **Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`): `oshi-common` may not reference `com.sun.jna.**`, `java.lang.foreign.**`, or any `System.load`/`loadLibrary`.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/test/java/oshi/software/common/os/unix/netbsd/NetBsdFileSystemTest.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdFileSystem.java
Applicability reference: AGENTS.md: # AGENTS.md Instructions for AI coding agents working in the OSHI repository.

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Files:

  • CHANGELOG.md
🔇 Additional comments: 3
oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdFileSystem.java: 1

13-14: LGTM!

Also applies to: 39-40, 161-179

oshi-common/src/test/java/oshi/software/common/os/unix/netbsd/NetBsdFileSystemTest.java: 1

59-70: LGTM!

CHANGELOG.md: 1

27-27: LGTM!

📝 Walkthrough

NetBsdFileSystem.getOpenFileDescriptors() now runs /usr/sbin/pstat -T and parses the total-files count. Tests cover standard and padded counts, unmatched output, and empty input.

Changes: NetBSD open-file count

Layer / File(s) Summary
Read and parse the pstat file count
oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdFileSystem.java, oshi-common/src/test/java/oshi/software/common/os/unix/netbsd/NetBsdFileSystemTest.java, CHANGELOG.md
getOpenFileDescriptors() runs /usr/sbin/pstat -T instead of querying kern.nfiles. The parser returns the first matching count, or 0 when no count matches or parsing fails. Tests cover standard and padded counts, unmatched output, and empty input. The changelog records the update.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 070c3

No actionable merge-blocking risk is identified for the NetBSD open-file count change.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: reading the NetBSD open-file count from pstat -T.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit reads the pstat line,
And finds the count among the signs.
With padded digits, it gets them right;
With no count, zero comes to light.
Then hops away beneath the moon.

Comment @coderabbitai help to get the list of available commands.

@dbwiddis
dbwiddis merged commit ba7bc6a into oshi:master Oct 17, 2026
17 of 18 checks passed
@dbwiddis
dbwiddis deleted the netbsd-open-files branch October 17, 2026 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant