Xinqi Bao's Git

Add basic backlight percentage support
[slstatus.git] / components / swap.c
1 /* See LICENSE file for copyright and license details. */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "../util.h"
7
8 #if defined(__linux__)
9 static size_t
10 pread(const char *path, char *buf, size_t bufsiz)
11 {
12 FILE *fp;
13 size_t bytes_read;
14
15 if (!(fp = fopen(path, "r"))) {
16 warn("fopen '%s':", path);
17 return 0;
18 }
19 if (!(bytes_read = fread(buf, sizeof(char), bufsiz, fp))) {
20 warn("fread '%s':", path);
21 fclose(fp);
22 return 0;
23 }
24 fclose(fp);
25
26 buf[bytes_read] = '\0';
27
28 return bytes_read;
29 }
30
31 const char *
32 swap_free(void)
33 {
34 long total, free;
35 char *match;
36
37 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
38 return NULL;
39 }
40
41 if (!(match = strstr(buf, "SwapTotal"))) {
42 return NULL;
43 }
44 sscanf(match, "SwapTotal: %ld kB\n", &total);
45
46 if (!(match = strstr(buf, "SwapFree"))) {
47 return NULL;
48 }
49 sscanf(match, "SwapFree: %ld kB\n", &free);
50
51 return fmt_human(free * 1024, 1024);
52 }
53
54 const char *
55 swap_perc(void)
56 {
57 long total, free, cached;
58 char *match;
59
60 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
61 return NULL;
62 }
63
64 if (!(match = strstr(buf, "SwapTotal"))) {
65 return NULL;
66 }
67 sscanf(match, "SwapTotal: %ld kB\n", &total);
68
69 if (!(match = strstr(buf, "SwapCached"))) {
70 return NULL;
71 }
72 sscanf(match, "SwapCached: %ld kB\n", &cached);
73
74 if (!(match = strstr(buf, "SwapFree"))) {
75 return NULL;
76 }
77 sscanf(match, "SwapFree: %ld kB\n", &free);
78
79 if (total == 0) {
80 return NULL;
81 }
82
83 return bprintf("%d", 100 * (total - free - cached) / total);
84 }
85
86 const char *
87 swap_total(void)
88 {
89 long total;
90 char *match;
91
92 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
93 return NULL;
94 }
95
96 if (!(match = strstr(buf, "SwapTotal"))) {
97 return NULL;
98 }
99 sscanf(match, "SwapTotal: %ld kB\n", &total);
100
101 return fmt_human(total * 1024, 1024);
102 }
103
104 const char *
105 swap_used(void)
106 {
107 long total, free, cached;
108 char *match;
109
110 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
111 return NULL;
112 }
113
114 if (!(match = strstr(buf, "SwapTotal"))) {
115 return NULL;
116 }
117 sscanf(match, "SwapTotal: %ld kB\n", &total);
118
119 if (!(match = strstr(buf, "SwapCached"))) {
120 return NULL;
121 }
122 sscanf(match, "SwapCached: %ld kB\n", &cached);
123
124 if (!(match = strstr(buf, "SwapFree"))) {
125 return NULL;
126 }
127 sscanf(match, "SwapFree: %ld kB\n", &free);
128
129 return fmt_human((total - free - cached) * 1024, 1024);
130 }
131 #elif defined(__OpenBSD__)
132 #include <stdlib.h>
133 #include <sys/param.h> /* dbtob */
134 #include <sys/swap.h>
135 #include <sys/types.h>
136 #include <unistd.h>
137
138 static int
139 getstats(int *total, int *used)
140 {
141 struct swapent *sep, *fsep;
142 int rnswap, nswap, i;
143
144 if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) {
145 warn("swaptctl 'SWAP_NSWAP':");
146 return 1;
147 }
148 if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) {
149 warn("calloc 'nswap':");
150 return 1;
151 }
152 if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) {
153 warn("swapctl 'SWAP_STATA':");
154 return 1;
155 }
156 if (nswap != rnswap) {
157 warn("getstats: SWAP_STATS != SWAP_NSWAP");
158 return 1;
159 }
160
161 *total = 0;
162 *used = 0;
163
164 for (i = 0; i < rnswap; i++) {
165 *total += sep->se_nblks >> 1;
166 *used += sep->se_inuse >> 1;
167 }
168
169 free(fsep);
170
171 return 0;
172 }
173
174 const char *
175 swap_free(void)
176 {
177 int total, used;
178
179 if (getstats(&total, &used)) {
180 return NULL;
181 }
182
183 return fmt_human((total - used) * 1024, 1024);
184 }
185
186 const char *
187 swap_perc(void)
188 {
189 int total, used;
190
191 if (getstats(&total, &used)) {
192 return NULL;
193 }
194
195 if (total == 0) {
196 return NULL;
197 }
198
199 return bprintf("%d", 100 * used / total);
200 }
201
202 const char *
203 swap_total(void)
204 {
205 int total, used;
206
207 if (getstats(&total, &used)) {
208 return NULL;
209 }
210
211 return fmt_human(total * 1024, 1024);
212 }
213
214 const char *
215 swap_used(void)
216 {
217 int total, used;
218
219 if (getstats(&total, &used)) {
220 return NULL;
221 }
222
223 return fmt_human(used * 1024, 1024);
224 }
225 #endif