diff options
Diffstat (limited to 'src/detection/loadavg')
| -rw-r--r-- | src/detection/loadavg/loadavg.h | 5 | ||||
| -rw-r--r-- | src/detection/loadavg/loadavg_bsd.c | 23 | ||||
| -rw-r--r-- | src/detection/loadavg/loadavg_linux.c | 33 | ||||
| -rw-r--r-- | src/detection/loadavg/loadavg_nosupport.c | 5 | ||||
| -rw-r--r-- | src/detection/loadavg/loadavg_sunos.c | 7 |
5 files changed, 73 insertions, 0 deletions
diff --git a/src/detection/loadavg/loadavg.h b/src/detection/loadavg/loadavg.h new file mode 100644 index 0000000..4cfa696 --- /dev/null +++ b/src/detection/loadavg/loadavg.h @@ -0,0 +1,5 @@ +#pragma once + +#include "fastfetch.h" + +const char* ffDetectLoadavg(double result[3]); diff --git a/src/detection/loadavg/loadavg_bsd.c b/src/detection/loadavg/loadavg_bsd.c new file mode 100644 index 0000000..83896b2 --- /dev/null +++ b/src/detection/loadavg/loadavg_bsd.c @@ -0,0 +1,23 @@ +#include "detection/loadavg/loadavg.h" + +#include <sys/sysctl.h> + +#if __FreeBSD__ || __OpenBSD__ || __NetBSD__ + #include <sys/types.h> + #include <sys/resource.h> + #if __FreeBSD__ + #include <vm/vm_param.h> + #endif +#endif + +const char* ffDetectLoadavg(double result[3]) { + struct loadavg load; + size_t size = sizeof(load); + if (sysctl((int[]) { CTL_VM, VM_LOADAVG }, 2, &load, &size, NULL, 0) < 0) { + return "sysctl({CTL_VM, VM_LOADAVG}) failed"; + } + for (int i = 0; i < 3; i++) { + result[i] = (double) load.ldavg[i] / (double) load.fscale; + } + return NULL; +} diff --git a/src/detection/loadavg/loadavg_linux.c b/src/detection/loadavg/loadavg_linux.c new file mode 100644 index 0000000..a169312 --- /dev/null +++ b/src/detection/loadavg/loadavg_linux.c @@ -0,0 +1,33 @@ +#include "detection/loadavg/loadavg.h" +#include "common/io.h" + +#include <sys/sysinfo.h> + +const char* ffDetectLoadavg(double result[3]) { +#ifndef __ANDROID__ // cat: /proc/loadavg: Permission denied + + // Don't use syscall for container compatibility. #620 + char buf[64]; + ssize_t nRead = ffReadFileData("/proc/loadavg", sizeof(buf) - 1, buf); + if (nRead > 0) { + buf[nRead] = '\0'; + + if (sscanf(buf, "%lf%lf%lf", &result[0], &result[1], &result[2]) == 3) { + return NULL; + } + } + +#endif +#ifndef __GNU__ + // getloadavg requires higher ANDROID_API version + struct sysinfo si; + if (sysinfo(&si) < 0) { + return "sysinfo() failed"; + } + + for (int i = 0; i < 3; i++) { + result[i] = (double) si.loads[i] / (1 << SI_LOAD_SHIFT); + } +#endif + return NULL; +} diff --git a/src/detection/loadavg/loadavg_nosupport.c b/src/detection/loadavg/loadavg_nosupport.c new file mode 100644 index 0000000..f14c717 --- /dev/null +++ b/src/detection/loadavg/loadavg_nosupport.c @@ -0,0 +1,5 @@ +#include "detection/loadavg/loadavg.h" + +const char* ffDetectLoadavg(FF_A_UNUSED double result[3]) { + return "Not supported on this platform"; +} diff --git a/src/detection/loadavg/loadavg_sunos.c b/src/detection/loadavg/loadavg_sunos.c new file mode 100644 index 0000000..91e3edd --- /dev/null +++ b/src/detection/loadavg/loadavg_sunos.c @@ -0,0 +1,7 @@ +#include "detection/loadavg/loadavg.h" + +#include <sys/loadavg.h> + +const char* ffDetectLoadavg(double result[3]) { + return getloadavg(result, 3) == 3 ? NULL : "getloadavg() failed"; +} |