summaryrefslogtreecommitdiff
path: root/components/OpenBSD/ram.c
blob: f7c81d125040c75bb13f9a31fb422db3168da1d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdlib.h>
#include <unistd.h>

#include "../../util.h"

inline int
load_uvmexp(struct uvmexp *uvmexp)
{
	int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
	size_t size;

	size = sizeof(*uvmexp);

	return sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0 ? 1 : 0;
}

const char *
ram_free(void)
{
	struct uvmexp uvmexp;
	float free;
	int free_pages;

	if (load_uvmexp(&uvmexp)) {
		free_pages = uvmexp.npages - uvmexp.active;
		free = (double) (free_pages * uvmexp.pagesize) / 1024 / 1024 / 1024;
		return bprintf("%f", free);
	}

	return NULL;
}

const char *
ram_perc(void)
{
	struct uvmexp uvmexp;
	int percent;

	if (load_uvmexp(&uvmexp)) {
		percent = uvmexp.active * 100 / uvmexp.npages;
		return bprintf("%d", percent);
	}

	return NULL;
}

const char *
ram_total(void)
{
	struct uvmexp uvmexp;
	float total;

	if (load_uvmexp(&uvmexp)) {
		total = (double) (uvmexp.npages * uvmexp.pagesize) / 1024 / 1024 / 1024;
		return bprintf("%f", total);
	}

	return NULL;
}

const char *
ram_used(void)
{
	struct uvmexp uvmexp;
	float used;

	if (load_uvmexp(&uvmexp)) {
		used = (double) (uvmexp.active * uvmexp.pagesize) / 1024 / 1024 / 1024;
		return bprintf("%f", used);
	}

	return NULL;
}