Fix sysinfo() totalram on XNU

HW_MEMSIZE is the 64-bit version of HW_PHYSMEM, which is inaccurate on
systems having more than 4gb of RAM.
This commit is contained in:
Justine Tunney 2022-12-18 02:10:18 -08:00
parent 0da47c51de
commit 5a3c646307
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
3 changed files with 4 additions and 4 deletions

View file

@ -13,7 +13,8 @@ struct sysinfo {
uint64_t totalswap; /* size of emergency memory */ uint64_t totalswap; /* size of emergency memory */
uint64_t freeswap; /* hopefully equal to totalswap */ uint64_t freeswap; /* hopefully equal to totalswap */
int16_t procs; /* number of processes */ int16_t procs; /* number of processes */
int16_t __ignore; /* padding */ int16_t __ignore1; /* padding */
int32_t __ignore2; /* padding */
uint64_t totalhigh; /* wut */ uint64_t totalhigh; /* wut */
uint64_t freehigh; /* wut */ uint64_t freehigh; /* wut */
uint32_t mem_unit; /* ram stuff above is multiples of this */ uint32_t mem_unit; /* ram stuff above is multiples of this */

View file

@ -21,7 +21,6 @@
#include "libc/calls/struct/sysinfo.internal.h" #include "libc/calls/struct/sysinfo.internal.h"
#include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timeval.h" #include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/vmmeter-meta.internal.h"
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/intrin/asan.internal.h" #include "libc/intrin/asan.internal.h"
#include "libc/intrin/strace.internal.h" #include "libc/intrin/strace.internal.h"
@ -32,7 +31,7 @@
#define CTL_KERN 1 #define CTL_KERN 1
#define CTL_HW 6 #define CTL_HW 6
#define KERN_BOOTTIME 21 #define KERN_BOOTTIME 21
#define HW_PHYSMEM 5 #define HW_PHYSMEM (IsXnu() ? 24 : 5)
static int64_t GetUptime(void) { static int64_t GetUptime(void) {
if (IsNetbsd()) return 0; // TODO(jart): Why? if (IsNetbsd()) return 0; // TODO(jart): Why?
@ -44,7 +43,7 @@ static int64_t GetUptime(void) {
} }
static int64_t GetPhysmem(void) { static int64_t GetPhysmem(void) {
uint64_t x; uint64_t x = 0;
size_t n = sizeof(x); size_t n = sizeof(x);
int mib[] = {CTL_HW, HW_PHYSMEM}; int mib[] = {CTL_HW, HW_PHYSMEM};
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return 0; if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return 0;