2017-09-17 16:45:03 +01:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-05-28 20:16:02 +01:00
|
|
|
#include <stdint.h>
|
2018-05-06 00:20:46 +01:00
|
|
|
#include <stdio.h>
|
2018-05-28 20:16:02 +01:00
|
|
|
#include <stdlib.h>
|
2018-05-06 00:20:46 +01:00
|
|
|
#include <string.h>
|
2018-05-01 18:01:22 +01:00
|
|
|
|
2022-10-26 21:14:53 +01:00
|
|
|
#include "../slstatus.h"
|
2022-10-27 22:18:30 +01:00
|
|
|
#include "../util.h"
|
2018-05-01 18:01:22 +01:00
|
|
|
|
2018-05-06 00:20:46 +01:00
|
|
|
#if defined(__linux__)
|
2018-05-28 20:16:02 +01:00
|
|
|
static int
|
|
|
|
get_swap_info(long *s_total, long *s_free, long *s_cached)
|
2018-05-01 18:01:22 +01:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2018-05-28 20:16:02 +01:00
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
const size_t len;
|
|
|
|
long *var;
|
|
|
|
} ent[] = {
|
|
|
|
{ "SwapTotal", sizeof("SwapTotal") - 1, s_total },
|
|
|
|
{ "SwapFree", sizeof("SwapFree") - 1, s_free },
|
|
|
|
{ "SwapCached", sizeof("SwapCached") - 1, s_cached },
|
|
|
|
};
|
|
|
|
size_t line_len = 0, i, left;
|
|
|
|
char *line = NULL;
|
|
|
|
|
|
|
|
/* get number of fields we want to extract */
|
2022-10-27 23:51:34 +01:00
|
|
|
for (i = 0, left = 0; i < LEN(ent); i++)
|
|
|
|
if (ent[i].var)
|
2018-05-28 20:16:02 +01:00
|
|
|
left++;
|
|
|
|
|
|
|
|
if (!(fp = fopen("/proc/meminfo", "r"))) {
|
|
|
|
warn("fopen '/proc/meminfo':");
|
|
|
|
return 1;
|
2018-05-01 18:01:22 +01:00
|
|
|
}
|
|
|
|
|
2018-05-28 20:16:02 +01:00
|
|
|
/* read file line by line and extract field information */
|
|
|
|
while (left > 0 && getline(&line, &line_len, fp) >= 0) {
|
|
|
|
for (i = 0; i < LEN(ent); i++) {
|
|
|
|
if (ent[i].var &&
|
|
|
|
!strncmp(line, ent[i].name, ent[i].len)) {
|
2018-07-08 16:42:58 +01:00
|
|
|
sscanf(line + ent[i].len + 1,
|
|
|
|
"%ld kB\n", ent[i].var);
|
2018-05-28 20:16:02 +01:00
|
|
|
left--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
if (ferror(fp)) {
|
|
|
|
warn("getline '/proc/meminfo':");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-05-01 19:20:58 +01:00
|
|
|
|
2018-05-28 20:16:02 +01:00
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
2018-05-01 19:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_free(const char *unused)
|
2018-05-01 19:20:58 +01:00
|
|
|
{
|
2018-05-28 20:16:02 +01:00
|
|
|
long free;
|
2018-05-01 19:20:58 +01:00
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (get_swap_info(NULL, &free, NULL))
|
2018-05-01 19:20:58 +01:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-21 13:44:21 +01:00
|
|
|
return fmt_human(free * 1024, 1024);
|
2017-09-17 15:18:17 +01:00
|
|
|
}
|
|
|
|
|
2018-05-01 18:01:22 +01:00
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_perc(const char *unused)
|
2018-05-01 18:01:22 +01:00
|
|
|
{
|
|
|
|
long total, free, cached;
|
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (get_swap_info(&total, &free, &cached) || total == 0)
|
2018-05-20 22:53:26 +01:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-20 23:16:54 +01:00
|
|
|
return bprintf("%d", 100 * (total - free - cached) / total);
|
2017-09-17 15:18:17 +01:00
|
|
|
}
|
2018-05-01 18:01:22 +01:00
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_total(const char *unused)
|
2018-05-01 18:01:22 +01:00
|
|
|
{
|
|
|
|
long total;
|
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (get_swap_info(&total, NULL, NULL))
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-21 13:44:21 +01:00
|
|
|
return fmt_human(total * 1024, 1024);
|
2017-09-17 15:18:17 +01:00
|
|
|
}
|
2018-05-01 18:01:22 +01:00
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_used(const char *unused)
|
2018-05-01 18:01:22 +01:00
|
|
|
{
|
|
|
|
long total, free, cached;
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (get_swap_info(&total, &free, &cached))
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-21 13:44:21 +01:00
|
|
|
return fmt_human((total - free - cached) * 1024, 1024);
|
2018-05-01 18:01:22 +01:00
|
|
|
}
|
|
|
|
#elif defined(__OpenBSD__)
|
2018-05-06 00:20:46 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/swap.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-05-21 22:03:34 +01:00
|
|
|
static int
|
2018-05-06 00:20:46 +01:00
|
|
|
getstats(int *total, int *used)
|
|
|
|
{
|
|
|
|
struct swapent *sep, *fsep;
|
|
|
|
int rnswap, nswap, i;
|
|
|
|
|
2018-05-21 22:03:34 +01:00
|
|
|
if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("swaptctl 'SWAP_NSWAP':");
|
2018-05-21 22:03:34 +01:00
|
|
|
return 1;
|
2018-05-06 21:28:56 +01:00
|
|
|
}
|
2018-05-21 22:03:34 +01:00
|
|
|
if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("calloc 'nswap':");
|
2018-05-21 22:03:34 +01:00
|
|
|
return 1;
|
2018-05-06 21:28:56 +01:00
|
|
|
}
|
2018-05-21 22:03:34 +01:00
|
|
|
if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("swapctl 'SWAP_STATA':");
|
2018-05-21 22:03:34 +01:00
|
|
|
return 1;
|
2018-05-06 21:28:56 +01:00
|
|
|
}
|
|
|
|
if (nswap != rnswap) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("getstats: SWAP_STATS != SWAP_NSWAP");
|
2018-05-21 22:03:34 +01:00
|
|
|
return 1;
|
2018-05-06 21:28:56 +01:00
|
|
|
}
|
2018-05-06 00:20:46 +01:00
|
|
|
|
|
|
|
*total = 0;
|
|
|
|
*used = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < rnswap; i++) {
|
2018-05-16 12:26:34 +01:00
|
|
|
*total += sep->se_nblks >> 1;
|
|
|
|
*used += sep->se_inuse >> 1;
|
2018-05-06 00:20:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(fsep);
|
2018-05-21 22:03:34 +01:00
|
|
|
|
|
|
|
return 0;
|
2018-05-06 00:20:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_free(const char *unused)
|
2018-05-06 00:20:46 +01:00
|
|
|
{
|
|
|
|
int total, used;
|
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (getstats(&total, &used))
|
2018-05-21 22:03:34 +01:00
|
|
|
return NULL;
|
2018-05-06 00:20:46 +01:00
|
|
|
|
2018-05-21 13:44:21 +01:00
|
|
|
return fmt_human((total - used) * 1024, 1024);
|
2018-05-06 00:20:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_perc(const char *unused)
|
2018-05-06 00:20:46 +01:00
|
|
|
{
|
|
|
|
int total, used;
|
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (getstats(&total, &used))
|
2018-05-21 22:03:34 +01:00
|
|
|
return NULL;
|
2018-05-06 00:20:46 +01:00
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (total == 0)
|
2018-05-20 23:02:33 +01:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-20 23:16:54 +01:00
|
|
|
return bprintf("%d", 100 * used / total);
|
2018-05-06 00:20:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_total(const char *unused)
|
2018-05-06 00:20:46 +01:00
|
|
|
{
|
|
|
|
int total, used;
|
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (getstats(&total, &used))
|
2018-05-21 22:03:34 +01:00
|
|
|
return NULL;
|
2018-05-06 00:20:46 +01:00
|
|
|
|
2018-05-21 13:44:21 +01:00
|
|
|
return fmt_human(total * 1024, 1024);
|
2018-05-06 00:20:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_used(const char *unused)
|
2018-05-06 00:20:46 +01:00
|
|
|
{
|
|
|
|
int total, used;
|
|
|
|
|
2022-10-27 23:51:34 +01:00
|
|
|
if (getstats(&total, &used))
|
2018-05-21 22:03:34 +01:00
|
|
|
return NULL;
|
2018-05-06 00:20:46 +01:00
|
|
|
|
2018-05-21 13:44:21 +01:00
|
|
|
return fmt_human(used * 1024, 1024);
|
2018-05-06 00:20:46 +01:00
|
|
|
}
|
2019-02-16 01:26:44 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
2022-10-27 22:18:30 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <kvm.h>
|
2019-02-16 01:26:44 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static int getswapinfo(struct kvm_swap *swap_info, size_t size)
|
|
|
|
{
|
|
|
|
kvm_t *kd;
|
|
|
|
|
|
|
|
kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, NULL);
|
2022-10-27 23:49:31 +01:00
|
|
|
if (kd == NULL) {
|
2019-02-16 01:26:44 +00:00
|
|
|
warn("kvm_openfiles '/dev/null':");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:21:02 +01:00
|
|
|
if (kvm_getswapinfo(kd, swap_info, size, 0 /* Unused flags */) < 0) {
|
2019-02-16 01:26:44 +00:00
|
|
|
warn("kvm_getswapinfo:");
|
|
|
|
kvm_close(kd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
kvm_close(kd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_free(const char *unused)
|
2019-02-16 01:26:44 +00:00
|
|
|
{
|
|
|
|
struct kvm_swap swap_info[1];
|
|
|
|
long used, total;
|
|
|
|
|
2022-10-27 23:49:31 +01:00
|
|
|
if (!getswapinfo(swap_info, 1))
|
2019-02-16 01:26:44 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
total = swap_info[0].ksw_total;
|
|
|
|
used = swap_info[0].ksw_used;
|
|
|
|
|
|
|
|
return fmt_human((total - used) * getpagesize(), 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_perc(const char *unused)
|
2019-02-16 01:26:44 +00:00
|
|
|
{
|
|
|
|
struct kvm_swap swap_info[1];
|
|
|
|
long used, total;
|
|
|
|
|
2022-10-27 23:49:31 +01:00
|
|
|
if (!getswapinfo(swap_info, 1))
|
2019-02-16 01:26:44 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
total = swap_info[0].ksw_total;
|
|
|
|
used = swap_info[0].ksw_used;
|
|
|
|
|
|
|
|
return bprintf("%d", used * 100 / total);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_total(const char *unused)
|
2019-02-16 01:26:44 +00:00
|
|
|
{
|
|
|
|
struct kvm_swap swap_info[1];
|
|
|
|
long total;
|
|
|
|
|
2022-10-27 23:49:31 +01:00
|
|
|
if (!getswapinfo(swap_info, 1))
|
2019-02-16 01:26:44 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
total = swap_info[0].ksw_total;
|
|
|
|
|
|
|
|
return fmt_human(total * getpagesize(), 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 21:16:05 +01:00
|
|
|
swap_used(const char *unused)
|
2019-02-16 01:26:44 +00:00
|
|
|
{
|
|
|
|
struct kvm_swap swap_info[1];
|
|
|
|
long used;
|
|
|
|
|
2022-10-27 23:49:31 +01:00
|
|
|
if (!getswapinfo(swap_info, 1))
|
2019-02-16 01:26:44 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
used = swap_info[0].ksw_used;
|
|
|
|
|
|
|
|
return fmt_human(used * getpagesize(), 1024);
|
|
|
|
}
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 17:10:39 +01:00
|
|
|
#endif
|