Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
11 get_swap_info(long *s_total
, long *s_free
, long *s_cached
)
19 { "SwapTotal", sizeof("SwapTotal") - 1, s_total
},
20 { "SwapFree", sizeof("SwapFree") - 1, s_free
},
21 { "SwapCached", sizeof("SwapCached") - 1, s_cached
},
23 size_t line_len
= 0, i
, left
;
26 /* get number of fields we want to extract */
27 for (i
= 0, left
= 0; i
< LEN(ent
); i
++) {
33 if (!(fp
= fopen("/proc/meminfo", "r"))) {
34 warn("fopen '/proc/meminfo':");
38 /* read file line by line and extract field information */
39 while (left
> 0 && getline(&line
, &line_len
, fp
) >= 0) {
40 for (i
= 0; i
< LEN(ent
); i
++) {
42 !strncmp(line
, ent
[i
].name
, ent
[i
].len
)) {
43 sscanf(line
+ ent
[i
].len
+ 1, "%ld kB\n",
52 warn("getline '/proc/meminfo':");
65 if (get_swap_info(NULL
, &free
, NULL
)) {
69 return fmt_human(free
* 1024, 1024);
75 long total
, free
, cached
;
77 if (get_swap_info(&total
, &free
, &cached
) || total
== 0) {
81 return bprintf("%d", 100 * (total
- free
- cached
) / total
);
89 if (get_swap_info(&total
, NULL
, NULL
)) {
93 return fmt_human(total
* 1024, 1024);
99 long total
, free
, cached
;
101 if (get_swap_info(&total
, &free
, &cached
)) {
105 return fmt_human((total
- free
- cached
) * 1024, 1024);
107 #elif defined(__OpenBSD__)
109 #include <sys/swap.h>
110 #include <sys/types.h>
114 getstats(int *total
, int *used
)
116 struct swapent
*sep
, *fsep
;
117 int rnswap
, nswap
, i
;
119 if ((nswap
= swapctl(SWAP_NSWAP
, 0, 0)) < 1) {
120 warn("swaptctl 'SWAP_NSWAP':");
123 if (!(fsep
= sep
= calloc(nswap
, sizeof(*sep
)))) {
124 warn("calloc 'nswap':");
127 if ((rnswap
= swapctl(SWAP_STATS
, (void *)sep
, nswap
)) < 0) {
128 warn("swapctl 'SWAP_STATA':");
131 if (nswap
!= rnswap
) {
132 warn("getstats: SWAP_STATS != SWAP_NSWAP");
139 for (i
= 0; i
< rnswap
; i
++) {
140 *total
+= sep
->se_nblks
>> 1;
141 *used
+= sep
->se_inuse
>> 1;
154 if (getstats(&total
, &used
)) {
158 return fmt_human((total
- used
) * 1024, 1024);
166 if (getstats(&total
, &used
)) {
174 return bprintf("%d", 100 * used
/ total
);
182 if (getstats(&total
, &used
)) {
186 return fmt_human(total
* 1024, 1024);
194 if (getstats(&total
, &used
)) {
198 return fmt_human(used
* 1024, 1024);