Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #if defined(__linux__)
12 get_swap_info(long *s_total
, long *s_free
, long *s_cached
)
20 { "SwapTotal", sizeof("SwapTotal") - 1, s_total
},
21 { "SwapFree", sizeof("SwapFree") - 1, s_free
},
22 { "SwapCached", sizeof("SwapCached") - 1, s_cached
},
24 size_t line_len
= 0, i
, left
;
27 /* get number of fields we want to extract */
28 for (i
= 0, left
= 0; i
< LEN(ent
); i
++) {
34 if (!(fp
= fopen("/proc/meminfo", "r"))) {
35 warn("fopen '/proc/meminfo':");
39 /* read file line by line and extract field information */
40 while (left
> 0 && getline(&line
, &line_len
, fp
) >= 0) {
41 for (i
= 0; i
< LEN(ent
); i
++) {
43 !strncmp(line
, ent
[i
].name
, ent
[i
].len
)) {
44 sscanf(line
+ ent
[i
].len
+ 1, "%ld kB\n",
53 warn("getline '/proc/meminfo':");
66 if (get_swap_info(NULL
, &free
, NULL
)) {
70 return fmt_human(free
* 1024, 1024);
76 long total
, free
, cached
;
78 if (get_swap_info(&total
, &free
, &cached
) || total
== 0) {
82 return bprintf("%d", 100 * (total
- free
- cached
) / total
);
90 if (get_swap_info(&total
, NULL
, NULL
)) {
94 return fmt_human(total
* 1024, 1024);
100 long total
, free
, cached
;
102 if (get_swap_info(&total
, &free
, &cached
)) {
106 return fmt_human((total
- free
- cached
) * 1024, 1024);
108 #elif defined(__OpenBSD__)
110 #include <sys/param.h> /* dbtob */
111 #include <sys/swap.h>
112 #include <sys/types.h>
116 getstats(int *total
, int *used
)
118 struct swapent
*sep
, *fsep
;
119 int rnswap
, nswap
, i
;
121 if ((nswap
= swapctl(SWAP_NSWAP
, 0, 0)) < 1) {
122 warn("swaptctl 'SWAP_NSWAP':");
125 if (!(fsep
= sep
= calloc(nswap
, sizeof(*sep
)))) {
126 warn("calloc 'nswap':");
129 if ((rnswap
= swapctl(SWAP_STATS
, (void *)sep
, nswap
)) < 0) {
130 warn("swapctl 'SWAP_STATA':");
133 if (nswap
!= rnswap
) {
134 warn("getstats: SWAP_STATS != SWAP_NSWAP");
141 for (i
= 0; i
< rnswap
; i
++) {
142 *total
+= sep
->se_nblks
>> 1;
143 *used
+= sep
->se_inuse
>> 1;
156 if (getstats(&total
, &used
)) {
160 return fmt_human((total
- used
) * 1024, 1024);
168 if (getstats(&total
, &used
)) {
176 return bprintf("%d", 100 * used
/ total
);
184 if (getstats(&total
, &used
)) {
188 return fmt_human(total
* 1024, 1024);
196 if (getstats(&total
, &used
)) {
200 return fmt_human(used
* 1024, 1024);