2017-09-17 11:45:03 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-05-28 15:16:02 -04:00
|
|
|
#include <stdint.h>
|
2018-05-05 19:20:46 -04:00
|
|
|
#include <stdio.h>
|
2018-05-28 15:16:02 -04:00
|
|
|
#include <stdlib.h>
|
2018-05-05 19:20:46 -04:00
|
|
|
#include <string.h>
|
2018-05-01 13:01:22 -04:00
|
|
|
|
2018-05-05 19:20:46 -04:00
|
|
|
#include "../util.h"
|
2018-05-01 13:01:22 -04:00
|
|
|
|
2018-05-05 19:20:46 -04:00
|
|
|
#if defined(__linux__)
|
2018-05-28 15:16:02 -04:00
|
|
|
static int
|
|
|
|
get_swap_info(long *s_total, long *s_free, long *s_cached)
|
2018-05-01 13:01:22 -04:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2018-05-28 15:16:02 -04: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 */
|
|
|
|
for (i = 0, left = 0; i < LEN(ent); i++) {
|
|
|
|
if (ent[i].var) {
|
|
|
|
left++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(fp = fopen("/proc/meminfo", "r"))) {
|
|
|
|
warn("fopen '/proc/meminfo':");
|
|
|
|
return 1;
|
2018-05-01 13:01:22 -04:00
|
|
|
}
|
|
|
|
|
2018-05-28 15:16:02 -04: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 11:42:58 -04:00
|
|
|
sscanf(line + ent[i].len + 1,
|
|
|
|
"%ld kB\n", ent[i].var);
|
2018-05-28 15:16:02 -04:00
|
|
|
left--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
if (ferror(fp)) {
|
|
|
|
warn("getline '/proc/meminfo':");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-05-01 14:20:58 -04:00
|
|
|
|
2018-05-28 15:16:02 -04:00
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
2018-05-01 14:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
swap_free(void)
|
|
|
|
{
|
2018-05-28 15:16:02 -04:00
|
|
|
long free;
|
2018-05-01 14:20:58 -04:00
|
|
|
|
2018-05-28 15:16:02 -04:00
|
|
|
if (get_swap_info(NULL, &free, NULL)) {
|
2018-05-01 14:20:58 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-21 08:44:21 -04:00
|
|
|
return fmt_human(free * 1024, 1024);
|
2017-09-17 10:18:17 -04:00
|
|
|
}
|
|
|
|
|
2018-05-01 13:01:22 -04:00
|
|
|
const char *
|
|
|
|
swap_perc(void)
|
|
|
|
{
|
|
|
|
long total, free, cached;
|
|
|
|
|
2018-05-28 15:16:02 -04:00
|
|
|
if (get_swap_info(&total, &free, &cached) || total == 0) {
|
2018-05-20 17:53:26 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-20 18:16:54 -04:00
|
|
|
return bprintf("%d", 100 * (total - free - cached) / total);
|
2017-09-17 10:18:17 -04:00
|
|
|
}
|
2018-05-01 13:01:22 -04:00
|
|
|
|
|
|
|
const char *
|
|
|
|
swap_total(void)
|
|
|
|
{
|
|
|
|
long total;
|
|
|
|
|
2018-05-28 15:16:02 -04:00
|
|
|
if (get_swap_info(&total, NULL, NULL)) {
|
2018-05-01 13:01:22 -04:00
|
|
|
return NULL;
|
2018-05-06 16:28:56 -04:00
|
|
|
}
|
2018-05-01 13:01:22 -04:00
|
|
|
|
2018-05-21 08:44:21 -04:00
|
|
|
return fmt_human(total * 1024, 1024);
|
2017-09-17 10:18:17 -04:00
|
|
|
}
|
2018-05-01 13:01:22 -04:00
|
|
|
|
|
|
|
const char *
|
|
|
|
swap_used(void)
|
|
|
|
{
|
|
|
|
long total, free, cached;
|
2017-09-17 10:18:17 -04:00
|
|
|
|
2018-05-28 15:16:02 -04:00
|
|
|
if (get_swap_info(&total, &free, &cached)) {
|
2018-05-01 13:01:22 -04:00
|
|
|
return NULL;
|
2018-05-06 16:28:56 -04:00
|
|
|
}
|
2017-09-17 10:18:17 -04:00
|
|
|
|
2018-05-21 08:44:21 -04:00
|
|
|
return fmt_human((total - free - cached) * 1024, 1024);
|
2018-05-01 13:01:22 -04:00
|
|
|
}
|
|
|
|
#elif defined(__OpenBSD__)
|
2018-05-05 19:20:46 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/swap.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-05-21 17:03:34 -04:00
|
|
|
static int
|
2018-05-05 19:20:46 -04:00
|
|
|
getstats(int *total, int *used)
|
|
|
|
{
|
|
|
|
struct swapent *sep, *fsep;
|
|
|
|
int rnswap, nswap, i;
|
|
|
|
|
2018-05-21 17:03:34 -04:00
|
|
|
if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) {
|
2018-05-18 04:59:05 -04:00
|
|
|
warn("swaptctl 'SWAP_NSWAP':");
|
2018-05-21 17:03:34 -04:00
|
|
|
return 1;
|
2018-05-06 16:28:56 -04:00
|
|
|
}
|
2018-05-21 17:03:34 -04:00
|
|
|
if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) {
|
2018-05-18 04:59:05 -04:00
|
|
|
warn("calloc 'nswap':");
|
2018-05-21 17:03:34 -04:00
|
|
|
return 1;
|
2018-05-06 16:28:56 -04:00
|
|
|
}
|
2018-05-21 17:03:34 -04:00
|
|
|
if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) {
|
2018-05-18 04:59:05 -04:00
|
|
|
warn("swapctl 'SWAP_STATA':");
|
2018-05-21 17:03:34 -04:00
|
|
|
return 1;
|
2018-05-06 16:28:56 -04:00
|
|
|
}
|
|
|
|
if (nswap != rnswap) {
|
2018-05-18 04:59:05 -04:00
|
|
|
warn("getstats: SWAP_STATS != SWAP_NSWAP");
|
2018-05-21 17:03:34 -04:00
|
|
|
return 1;
|
2018-05-06 16:28:56 -04:00
|
|
|
}
|
2018-05-05 19:20:46 -04:00
|
|
|
|
|
|
|
*total = 0;
|
|
|
|
*used = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < rnswap; i++) {
|
2018-05-16 07:26:34 -04:00
|
|
|
*total += sep->se_nblks >> 1;
|
|
|
|
*used += sep->se_inuse >> 1;
|
2018-05-05 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(fsep);
|
2018-05-21 17:03:34 -04:00
|
|
|
|
|
|
|
return 0;
|
2018-05-05 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
swap_free(void)
|
|
|
|
{
|
|
|
|
int total, used;
|
|
|
|
|
2018-05-21 17:03:34 -04:00
|
|
|
if (getstats(&total, &used)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-05 19:20:46 -04:00
|
|
|
|
2018-05-21 08:44:21 -04:00
|
|
|
return fmt_human((total - used) * 1024, 1024);
|
2018-05-05 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
swap_perc(void)
|
|
|
|
{
|
|
|
|
int total, used;
|
|
|
|
|
2018-05-21 17:03:34 -04:00
|
|
|
if (getstats(&total, &used)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-05 19:20:46 -04:00
|
|
|
|
2018-05-20 18:02:33 -04:00
|
|
|
if (total == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-20 18:16:54 -04:00
|
|
|
return bprintf("%d", 100 * used / total);
|
2018-05-05 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
swap_total(void)
|
|
|
|
{
|
|
|
|
int total, used;
|
|
|
|
|
2018-05-21 17:03:34 -04:00
|
|
|
if (getstats(&total, &used)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-05 19:20:46 -04:00
|
|
|
|
2018-05-21 08:44:21 -04:00
|
|
|
return fmt_human(total * 1024, 1024);
|
2018-05-05 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
swap_used(void)
|
|
|
|
{
|
|
|
|
int total, used;
|
|
|
|
|
2018-05-21 17:03:34 -04:00
|
|
|
if (getstats(&total, &used)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-05 19:20:46 -04:00
|
|
|
|
2018-05-21 08:44:21 -04:00
|
|
|
return fmt_human(used * 1024, 1024);
|
2018-05-05 19:20:46 -04:00
|
|
|
}
|
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 12:10:39 -04:00
|
|
|
#endif
|