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 fprintf(stderr
, "fopen '%s': %s\n", path
,
20 if ((bytes_read
= fread(buf
, sizeof(char), bufsiz
, fp
)) == 0) {
21 fprintf(stderr
, "fread '%s': %s\n", path
,
28 buf
[bytes_read
] = '\0';
39 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
43 if ((match
= strstr(buf
, "SwapTotal")) == NULL
)
45 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
47 if ((match
= strstr(buf
, "SwapFree")) == NULL
)
49 sscanf(match
, "SwapFree: %ld kB\n", &free
);
51 return bprintf("%f", (float)free
/ 1024 / 1024);
57 long total
, free
, cached
;
60 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
64 if ((match
= strstr(buf
, "SwapTotal")) == NULL
)
66 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
68 if ((match
= strstr(buf
, "SwapCached")) == NULL
)
70 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
72 if ((match
= strstr(buf
, "SwapFree")) == NULL
)
74 sscanf(match
, "SwapFree: %ld kB\n", &free
);
76 return bprintf("%d", 100 * (total
- free
- cached
) / total
);
85 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
89 if ((match
= strstr(buf
, "SwapTotal")) == NULL
)
91 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
93 return bprintf("%f", (float)total
/ 1024 / 1024);
99 long total
, free
, cached
;
102 if (!pread("/proc/meminfo", buf
, sizeof(buf
) - 1)) {
106 if ((match
= strstr(buf
, "SwapTotal")) == NULL
)
108 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
110 if ((match
= strstr(buf
, "SwapCached")) == NULL
)
112 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
114 if ((match
= strstr(buf
, "SwapFree")) == NULL
)
116 sscanf(match
, "SwapFree: %ld kB\n", &free
);
118 return bprintf("%f", (float)(total
- free
- cached
) / 1024 / 1024);
120 #elif defined(__OpenBSD__)
122 #include <sys/param.h> /* dbtob */
123 #include <sys/swap.h>
124 #include <sys/types.h>
127 #define dbtoqb(b) dbtob((int64_t)(b))
130 getstats(int *total
, int *used
)
132 struct swapent
*sep
, *fsep
;
133 int rnswap
, nswap
, i
;
135 nswap
= swapctl(SWAP_NSWAP
, 0, 0);
137 fprintf(stderr
, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno
));
139 fsep
= sep
= calloc(nswap
, sizeof(*sep
));
141 fprintf(stderr
, "calloc 'nswap': %s\n", strerror(errno
));
143 rnswap
= swapctl(SWAP_STATS
, (void *)sep
, nswap
);
145 fprintf(stderr
, "swapctl 'SWAP_STATA': %s\n", strerror(errno
));
148 fprintf(stderr
, "SWAP_STATS != SWAP_NSWAP\n");
153 for (i
= 0; i
< rnswap
; i
++) {
154 *total
+= dbtoqb(sep
->se_nblks
);
155 *used
+= dbtoqb(sep
->se_inuse
);
166 getstats(&total
, &used
);
168 return bprintf("%f", (float)(total
- used
) / 1024 / 1024 / 1024);
176 getstats(&total
, &used
);
178 return bprintf("%d", 100 * used
/ total
);
186 getstats(&total
, &used
);
188 return bprintf("%f", (float)total
/ 1024 / 1024 / 1024);
196 getstats(&total
, &used
);
198 return bprintf("%f", (float)used
/ 1024 / 1024 / 1024);