Xinqi Bao's Git

Add the percent sign to *_perc functions
[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_scaled(free * 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 return bprintf("%d%%", 100 * (total - free - cached) / total);
80 }
81
82 const char *
83 swap_total(void)
84 {
85 long total;
86 char *match;
87
88 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
89 return NULL;
90 }
91
92 if (!(match = strstr(buf, "SwapTotal"))) {
93 return NULL;
94 }
95 sscanf(match, "SwapTotal: %ld kB\n", &total);
96
97 return fmt_scaled(total * 1024);
98 }
99
100 const char *
101 swap_used(void)
102 {
103 long total, free, cached;
104 char *match;
105
106 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
107 return NULL;
108 }
109
110 if (!(match = strstr(buf, "SwapTotal"))) {
111 return NULL;
112 }
113 sscanf(match, "SwapTotal: %ld kB\n", &total);
114
115 if (!(match = strstr(buf, "SwapCached"))) {
116 return NULL;
117 }
118 sscanf(match, "SwapCached: %ld kB\n", &cached);
119
120 if (!(match = strstr(buf, "SwapFree"))) {
121 return NULL;
122 }
123 sscanf(match, "SwapFree: %ld kB\n", &free);
124
125 return fmt_scaled((total - free - cached) * 1024);
126 }
127 #elif defined(__OpenBSD__)
128 #include <stdlib.h>
129 #include <sys/param.h> /* dbtob */
130 #include <sys/swap.h>
131 #include <sys/types.h>
132 #include <unistd.h>
133
134 static void
135 getstats(int *total, int *used)
136 {
137 struct swapent *sep, *fsep;
138 int rnswap, nswap, i;
139
140 nswap = swapctl(SWAP_NSWAP, 0, 0);
141 if (nswap < 1) {
142 warn("swaptctl 'SWAP_NSWAP':");
143 }
144
145 fsep = sep = calloc(nswap, sizeof(*sep));
146 if (!sep) {
147 warn("calloc 'nswap':");
148 }
149
150 rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
151 if (rnswap < 0) {
152 warn("swapctl 'SWAP_STATA':");
153 }
154
155 if (nswap != rnswap) {
156 warn("getstats: SWAP_STATS != SWAP_NSWAP");
157 }
158
159 *total = 0;
160 *used = 0;
161
162 for (i = 0; i < rnswap; i++) {
163 *total += sep->se_nblks >> 1;
164 *used += sep->se_inuse >> 1;
165 }
166
167 free(fsep);
168 }
169
170 const char *
171 swap_free(void)
172 {
173 int total, used;
174
175 getstats(&total, &used);
176
177 return fmt_scaled((total - used) * 1024);
178 }
179
180 const char *
181 swap_perc(void)
182 {
183 int total, used;
184
185 getstats(&total, &used);
186
187 return bprintf("%d%%", 100 * used / total);
188 }
189
190 const char *
191 swap_total(void)
192 {
193 int total, used;
194
195 getstats(&total, &used);
196
197 return fmt_scaled(total * 1024);
198 }
199
200 const char *
201 swap_used(void)
202 {
203 int total, used;
204
205 getstats(&total, &used);
206
207 return fmt_scaled(used * 1024);
208 }
209 #endif