2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-08-18 22:43:03 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/struct/sysinfo.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/struct/sysinfo.internal.h"
|
2022-08-18 22:43:03 +00:00
|
|
|
#include "libc/calls/struct/timespec.h"
|
|
|
|
#include "libc/calls/struct/timeval.h"
|
|
|
|
#include "libc/calls/struct/vmmeter-meta.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
2021-05-14 12:36:58 +00:00
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-09-13 18:20:35 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
2021-05-14 12:36:58 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-08-18 22:43:03 +00:00
|
|
|
#define CTL_KERN 1
|
|
|
|
#define CTL_HW 6
|
|
|
|
#define KERN_BOOTTIME 21
|
|
|
|
#define HW_PHYSMEM 5
|
|
|
|
|
|
|
|
static int64_t GetUptime(void) {
|
|
|
|
if (IsNetbsd()) return 0; // TODO(jart): Why?
|
|
|
|
struct timeval x;
|
|
|
|
size_t n = sizeof(x);
|
|
|
|
int mib[] = {CTL_KERN, KERN_BOOTTIME};
|
2022-09-13 18:20:35 +00:00
|
|
|
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return 0;
|
2022-08-18 22:43:03 +00:00
|
|
|
return _timespec_real().tv_sec - x.tv_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t GetPhysmem(void) {
|
|
|
|
uint64_t x;
|
|
|
|
size_t n = sizeof(x);
|
|
|
|
int mib[] = {CTL_HW, HW_PHYSMEM};
|
2022-09-13 18:20:35 +00:00
|
|
|
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return 0;
|
2022-08-18 22:43:03 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sys_sysinfo_bsd(struct sysinfo *info) {
|
|
|
|
info->uptime = GetUptime();
|
|
|
|
info->totalram = GetPhysmem();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Returns amount of system ram, cores, etc.
|
2022-08-18 22:43:03 +00:00
|
|
|
*
|
|
|
|
* Only the `totalram` field is supported on all platforms right now.
|
|
|
|
* Support is best on Linux. Fields will be set to zero when they're not
|
|
|
|
* known.
|
|
|
|
*
|
2020-06-15 14:18:57 +00:00
|
|
|
* @return 0 on success or -1 w/ errno
|
2022-08-18 22:43:03 +00:00
|
|
|
* @error EFAULT
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
int sysinfo(struct sysinfo *info) {
|
|
|
|
int rc;
|
2022-08-18 22:43:03 +00:00
|
|
|
struct sysinfo x = {0};
|
|
|
|
if (IsAsan() && info && !__asan_is_valid(info, sizeof(*info))) {
|
|
|
|
rc = efault();
|
|
|
|
} else if (!IsWindows()) {
|
|
|
|
if (IsLinux()) {
|
|
|
|
rc = sys_sysinfo(&x);
|
|
|
|
} else {
|
|
|
|
rc = sys_sysinfo_bsd(&x);
|
2021-05-14 12:36:58 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2022-08-18 22:43:03 +00:00
|
|
|
rc = sys_sysinfo_nt(&x);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-03-23 02:51:27 +00:00
|
|
|
if (rc != -1) {
|
2022-08-18 22:43:03 +00:00
|
|
|
x.procs = MAX(1, x.procs);
|
|
|
|
x.mem_unit = MAX(1, x.mem_unit);
|
|
|
|
x.totalram = MAX((8 * 1024 * 1024) / x.mem_unit, x.totalram);
|
|
|
|
memcpy(info, &x, sizeof(x));
|
2022-03-23 02:51:27 +00:00
|
|
|
}
|
2022-08-18 22:43:03 +00:00
|
|
|
STRACE("sysinfo(%p) → %d% m", info, rc);
|
2020-06-15 14:18:57 +00:00
|
|
|
return rc;
|
|
|
|
}
|