Skip to content

Conversation

@waltmck
Copy link

@waltmck waltmck commented Aug 30, 2025

No description provided.


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

@Jas-SinghFSU Jas-SinghFSU Sep 2, 2025

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;

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.

2 participants