master ohmu/fastfetch-openbsd / src / detection / memory / memory_bsd.c
ohmu  ·  2026-10-15
 1#include "memory.h"
 2#include "common/debug.h"
 3#include "common/sysctl.h"
 4
 5#include <errno.h>
 6#include <string.h>
 7
 8const char* ffDetectMemory(FFMemoryResult* ram) {
 9    size_t length = sizeof(ram->bytesTotal);
10    if (sysctl((int[]) { CTL_HW, HW_PHYSMEM }, 2, &ram->bytesTotal, &length, nullptr, 0)) {
11        FF_DEBUG("sysctl(hw.physmem) failed: %s", strerror(errno));
12        return "Failed to read hw.physmem";
13    }
14
15    // vm.stats.vm.* are int values
16    int32_t pagesFree = ffSysctlGetInt("vm.stats.vm.v_free_count", 0) + ffSysctlGetInt("vm.stats.vm.v_inactive_count", 0) + ffSysctlGetInt("vm.stats.vm.v_cache_count", 0);
17    int64_t bytesCache = ffSysctlGetInt64("vfs.bufspace", 0);
18
19    ram->bytesUsed = ram->bytesTotal - ((uint64_t) pagesFree << instance.state.platform.sysinfo.pageSizeShift) - (uint64_t) bytesCache;
20
21    int64_t bytesArc = ffSysctlGetInt64("kstat.zfs.misc.arcstats.size", 0);
22    if (bytesArc > 0) bytesArc -= ffSysctlGetInt64("kstat.zfs.misc.arcstats.c_min", 0);
23    if (bytesArc > 0 && (uint64_t) bytesArc < ram->bytesUsed) {
24        ram->bytesUsed -= (uint64_t) bytesArc;
25    }
26
27    return nullptr;
28}