Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

Subtract ZFS ARC size from RAM usage#1096

Merged
Jas-SinghFSU merged 7 commits into
Jas-SinghFSU:masterfrom
waltmck:master
Jan 3, 2026
Merged

Subtract ZFS ARC size from RAM usage#1096
Jas-SinghFSU merged 7 commits into
Jas-SinghFSU:masterfrom
waltmck:master

Conversation

@waltmck

@waltmck waltmck commented Aug 30, 2025

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread src/services/system/ramUsage/index.ts Outdated

// Get ZFS ARC size, if nonzero
let arcSize = 0;
const [arcSuccess, arcstatsBytes] = GLib.file_get_contents('/proc/spl/kstat/zfs/arcstats');

@Jas-SinghFSU Jas-SinghFSU Sep 2, 2025

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This will throw an exception if you don't have zfs as it won't be able to find that file - which will cause the ram calculation to fail in all cases unless zfs exists.

Most of this logic is sound, we can either:

  1. Wrap this in a try catch with an empty catch (a bit smelly)
  2. Move it out into a function and do a preliminary check to see if the path exists

I think approach 2 is cleaner so here we just do

            const arcSize = this._getZfsArcSize();

Then make a method down below

    private _getZfsArcSize(): number {
        const arcstatsPath = '/proc/spl/kstat/zfs/arcstats';

        if (!GLib.file_test(arcstatsPath, GLib.FileTest.EXISTS)) {
            return 0;
        }

        const [arcSuccess, arcstatsBytes] = GLib.file_get_contents(arcstatsPath);
        if (!arcSuccess || arcstatsBytes === undefined) {
            return 0;
        }

        const arcstats = new TextDecoder('utf-8').decode(arcstatsBytes);
        const arcSizeMatch = arcstats.match(/size\s+\d\s+(\d+)/);
        return arcSizeMatch ? parseInt(arcSizeMatch[1], 10) : 0;
    }

And we can reduce some nesting by using the early return pattern.

Also this is a bit wasteful since we're checking if the file exists on every ram calculation (every polling interval). Instead we can move hold a state int he constructor so we do that discovery on startup:

  constructor() {
      this._hasZfs = GLib.file_test('/proc/spl/kstat/zfs/arcstats', GLib.FileTest.EXISTS);
  }

Then we can just

      if (!this._hasZfs) return 0;

@Jas-SinghFSU

Copy link
Copy Markdown
Owner

Awesome, looks good with the exception of 1 linter error.

@waltmck

waltmck commented Jan 3, 2026

Copy link
Copy Markdown
Contributor Author

Great, I fixed the linter error. Thanks a lot for your review!

@Jas-SinghFSU Jas-SinghFSU merged commit b8c255b into Jas-SinghFSU:master Jan 3, 2026
1 check passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants