Xinqi Bao's Git
f5db66793ae2b5aabfe2883640aa89fc6ade3acd
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_human_2(free
* 1024, "B");
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
);
83 return bprintf("%d%%", 100 * (total
- free
- cached
) / total
);
92 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
96 if (!(match
= strstr(buf
, "SwapTotal"))) {
99 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
101 return fmt_human_2(total
* 1024, "B");
107 long total
, free
, cached
;
110 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
114 if (!(match
= strstr(buf
, "SwapTotal"))) {
117 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
119 if (!(match
= strstr(buf
, "SwapCached"))) {
122 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
124 if (!(match
= strstr(buf
, "SwapFree"))) {
127 sscanf(match
, "SwapFree: %ld kB\n", &free
);
129 return fmt_human_2((total
- free
- cached
) * 1024, "B");
131 #elif defined(__OpenBSD__)
133 #include <sys/param.h> /* dbtob */
134 #include <sys/swap.h>
135 #include <sys/types.h>
139 getstats(int *total
, int *used
)
141 struct swapent
*sep
, *fsep
;
142 int rnswap
, nswap
, i
;
144 nswap
= swapctl(SWAP_NSWAP
, 0, 0);
146 warn("swaptctl 'SWAP_NSWAP':");
149 fsep
= sep
= calloc(nswap
, sizeof(*sep
));
151 warn("calloc 'nswap':");
154 rnswap
= swapctl(SWAP_STATS
, (void *)sep
, nswap
);
156 warn("swapctl 'SWAP_STATA':");
159 if (nswap
!= rnswap
) {
160 warn("getstats: SWAP_STATS != SWAP_NSWAP");
166 for (i
= 0; i
< rnswap
; i
++) {
167 *total
+= sep
->se_nblks
>> 1;
168 *used
+= sep
->se_inuse
>> 1;
179 getstats(&total
, &used
);
181 return fmt_human_2((total
- used
) * 1024, "B");
189 getstats(&total
, &used
);
195 return bprintf("%d%%", 100 * used
/ total
);
203 getstats(&total
, &used
);
205 return fmt_human_2(total
* 1024, "B");
213 getstats(&total
, &used
);
215 return fmt_human_2(used
* 1024, "B");