Xinqi Bao's Git

48144e44438315e004dc699424e7ccd444a78eed
[slstatus.git] / components / ram.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stdio.h>
3
4 #include "../util.h"
5
6 #if defined(__linux__)
7 #include <stdint.h>
8
9 const char *
10 ram_free(void)
11 {
12 uintmax_t free;
13
14 if (pscanf("/proc/meminfo",
15 "MemTotal: %ju kB\n"
16 "MemFree: %ju kB\n"
17 "MemAvailable: %ju kB\n",
18 &free, &free, &free) != 3) {
19 return NULL;
20 }
21
22 return fmt_human(free * 1024, 1024);
23 }
24
25 const char *
26 ram_perc(void)
27 {
28 uintmax_t total, free, buffers, cached;
29
30 if (pscanf("/proc/meminfo",
31 "MemTotal: %ju kB\n"
32 "MemFree: %ju kB\n"
33 "MemAvailable: %ju kB\n"
34 "Buffers: %ju kB\n"
35 "Cached: %ju kB\n",
36 &total, &free, &buffers, &buffers, &cached) != 5) {
37 return NULL;
38 }
39
40 if (total == 0) {
41 return NULL;
42 }
43
44 return bprintf("%d", 100 * ((total - free) - (buffers + cached))
45 / total);
46 }
47
48 const char *
49 ram_total(void)
50 {
51 uintmax_t total;
52
53 if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total)
54 != 1) {
55 return NULL;
56 }
57
58 return fmt_human(total * 1024, 1024);
59 }
60
61 const char *
62 ram_used(void)
63 {
64 uintmax_t total, free, buffers, cached;
65
66 if (pscanf("/proc/meminfo",
67 "MemTotal: %ju kB\n"
68 "MemFree: %ju kB\n"
69 "MemAvailable: %ju kB\n"
70 "Buffers: %ju kB\n"
71 "Cached: %ju kB\n",
72 &total, &free, &buffers, &buffers, &cached) != 5) {
73 return NULL;
74 }
75
76 return fmt_human((total - free - buffers - cached) * 1024,
77 1024);
78 }
79 #elif defined(__OpenBSD__)
80 #include <stdlib.h>
81 #include <sys/sysctl.h>
82 #include <sys/types.h>
83 #include <unistd.h>
84
85 #define LOG1024 10
86 #define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024))
87
88 inline int
89 load_uvmexp(struct uvmexp *uvmexp)
90 {
91 int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
92 size_t size;
93
94 size = sizeof(*uvmexp);
95
96 if (sysctl(uvmexp_mib, 2, uvmexp, &size, NULL, 0) >= 0) {
97 return 1;
98 }
99
100 return 0;
101 }
102
103 const char *
104 ram_free(void)
105 {
106 struct uvmexp uvmexp;
107 int free_pages;
108
109 if (load_uvmexp(&uvmexp)) {
110 free_pages = uvmexp.npages - uvmexp.active;
111 return fmt_human(pagetok(free_pages, uvmexp.pageshift) *
112 1024, 1024);
113 }
114
115 return NULL;
116 }
117
118 const char *
119 ram_perc(void)
120 {
121 struct uvmexp uvmexp;
122 int percent;
123
124 if (load_uvmexp(&uvmexp)) {
125 percent = uvmexp.active * 100 / uvmexp.npages;
126 return bprintf("%d", percent);
127 }
128
129 return NULL;
130 }
131
132 const char *
133 ram_total(void)
134 {
135 struct uvmexp uvmexp;
136
137 if (load_uvmexp(&uvmexp)) {
138 return fmt_human(pagetok(uvmexp.npages,
139 uvmexp.pageshift) * 1024,
140 1024);
141 }
142
143 return NULL;
144 }
145
146 const char *
147 ram_used(void)
148 {
149 struct uvmexp uvmexp;
150
151 if (load_uvmexp(&uvmexp)) {
152 return fmt_human(pagetok(uvmexp.active,
153 uvmexp.pageshift) * 1024,
154 1024);
155 }
156
157 return NULL;
158 }
159 #endif