Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 pread(const char *path
, char *buf
, size_t bufsiz
)
15 if (!(fp
= fopen(path
, "r"))) {
16 warn("fopen '%s':", path
);
19 if (!(bytes_read
= fread(buf
, sizeof(char), bufsiz
, fp
))) {
20 warn("fread '%s':", path
);
26 buf
[bytes_read
] = '\0';
37 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
41 if (!(match
= strstr(buf
, "SwapTotal"))) {
44 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
46 if (!(match
= strstr(buf
, "SwapFree"))) {
49 sscanf(match
, "SwapFree: %ld kB\n", &free
);
51 return fmt_scaled(free
* 1024);
57 long total
, free
, cached
;
60 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
64 if (!(match
= strstr(buf
, "SwapTotal"))) {
67 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
69 if (!(match
= strstr(buf
, "SwapCached"))) {
72 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
74 if (!(match
= strstr(buf
, "SwapFree"))) {
77 sscanf(match
, "SwapFree: %ld kB\n", &free
);
79 return bprintf("%d%%", 100 * (total
- free
- cached
) / total
);
88 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
92 if (!(match
= strstr(buf
, "SwapTotal"))) {
95 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
97 return fmt_scaled(total
* 1024);
103 long total
, free
, cached
;
106 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
110 if (!(match
= strstr(buf
, "SwapTotal"))) {
113 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
115 if (!(match
= strstr(buf
, "SwapCached"))) {
118 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
120 if (!(match
= strstr(buf
, "SwapFree"))) {
123 sscanf(match
, "SwapFree: %ld kB\n", &free
);
125 return fmt_scaled((total
- free
- cached
) * 1024);
127 #elif defined(__OpenBSD__)
129 #include <sys/param.h> /* dbtob */
130 #include <sys/swap.h>
131 #include <sys/types.h>
135 getstats(int *total
, int *used
)
137 struct swapent
*sep
, *fsep
;
138 int rnswap
, nswap
, i
;
140 nswap
= swapctl(SWAP_NSWAP
, 0, 0);
142 warn("swaptctl 'SWAP_NSWAP':");
145 fsep
= sep
= calloc(nswap
, sizeof(*sep
));
147 warn("calloc 'nswap':");
150 rnswap
= swapctl(SWAP_STATS
, (void *)sep
, nswap
);
152 warn("swapctl 'SWAP_STATA':");
155 if (nswap
!= rnswap
) {
156 warn("getstats: SWAP_STATS != SWAP_NSWAP");
162 for (i
= 0; i
< rnswap
; i
++) {
163 *total
+= sep
->se_nblks
>> 1;
164 *used
+= sep
->se_inuse
>> 1;
175 getstats(&total
, &used
);
177 return fmt_scaled((total
- used
) * 1024);
185 getstats(&total
, &used
);
187 return bprintf("%d%%", 100 * used
/ total
);
195 getstats(&total
, &used
);
197 return fmt_scaled(total
* 1024);
205 getstats(&total
, &used
);
207 return fmt_scaled(used
* 1024);