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_human(free
* 1024, 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
);
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(total
* 1024, 1024);
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((total
- free
- cached
) * 1024, 1024);
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 if ((nswap
= swapctl(SWAP_NSWAP
, 0, 0)) < 1) {
145 warn("swaptctl 'SWAP_NSWAP':");
148 if (!(fsep
= sep
= calloc(nswap
, sizeof(*sep
)))) {
149 warn("calloc 'nswap':");
152 if ((rnswap
= swapctl(SWAP_STATS
, (void *)sep
, nswap
)) < 0) {
153 warn("swapctl 'SWAP_STATA':");
156 if (nswap
!= rnswap
) {
157 warn("getstats: SWAP_STATS != SWAP_NSWAP");
164 for (i
= 0; i
< rnswap
; i
++) {
165 *total
+= sep
->se_nblks
>> 1;
166 *used
+= sep
->se_inuse
>> 1;
179 if (getstats(&total
, &used
)) {
183 return fmt_human((total
- used
) * 1024, 1024);
191 if (getstats(&total
, &used
)) {
199 return bprintf("%d", 100 * used
/ total
);
207 if (getstats(&total
, &used
)) {
211 return fmt_human(total
* 1024, 1024);
219 if (getstats(&total
, &used
)) {
223 return fmt_human(used
* 1024, 1024);