Xinqi Bao's Git

ram: OS split
[slstatus.git] / components / OpenBSD / ram.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <sys/sysctl.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "../../util.h"
9
10 inline int
11 load_uvmexp(struct uvmexp *uvmexp)
12 {
13 int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
14 size_t size;
15
16 size = sizeof(*uvmexp);
17
18 return sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0 ? 1 : 0;
19 }
20
21 const char *
22 ram_free(void)
23 {
24 struct uvmexp uvmexp;
25 float free;
26 int free_pages;
27
28 if (load_uvmexp(&uvmexp)) {
29 free_pages = uvmexp.npages - uvmexp.active;
30 free = (double) (free_pages * uvmexp.pagesize) / 1024 / 1024 / 1024;
31 return bprintf("%f", free);
32 }
33
34 return NULL;
35 }
36
37 const char *
38 ram_perc(void)
39 {
40 struct uvmexp uvmexp;
41 int percent;
42
43 if (load_uvmexp(&uvmexp)) {
44 percent = uvmexp.active * 100 / uvmexp.npages;
45 return bprintf("%d", percent);
46 }
47
48 return NULL;
49 }
50
51 const char *
52 ram_total(void)
53 {
54 struct uvmexp uvmexp;
55 float total;
56
57 if (load_uvmexp(&uvmexp)) {
58 total = (double) (uvmexp.npages * uvmexp.pagesize) / 1024 / 1024 / 1024;
59 return bprintf("%f", total);
60 }
61
62 return NULL;
63 }
64
65 const char *
66 ram_used(void)
67 {
68 struct uvmexp uvmexp;
69 float used;
70
71 if (load_uvmexp(&uvmexp)) {
72 used = (double) (uvmexp.active * uvmexp.pagesize) / 1024 / 1024 / 1024;
73 return bprintf("%f", used);
74 }
75
76 return NULL;
77 }