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/swap.h>
111 #include <sys/types.h>
115 getstats(int *total
, int *used
)
117 struct swapent
*sep
, *fsep
;
118 int rnswap
, nswap
, i
;
120 if ((nswap
= swapctl(SWAP_NSWAP
, 0, 0)) < 1) {
121 warn("swaptctl 'SWAP_NSWAP':");
124 if (!(fsep
= sep
= calloc(nswap
, sizeof(*sep
)))) {
125 warn("calloc 'nswap':");
128 if ((rnswap
= swapctl(SWAP_STATS
, (void *)sep
, nswap
)) < 0) {
129 warn("swapctl 'SWAP_STATA':");
132 if (nswap
!= rnswap
) {
133 warn("getstats: SWAP_STATS != SWAP_NSWAP");
140 for (i
= 0; i
< rnswap
; i
++) {
141 *total
+= sep
->se_nblks
>> 1;
142 *used
+= sep
->se_inuse
>> 1;
155 if (getstats(&total
, &used
)) {
159 return fmt_human((total
- used
) * 1024, 1024);
167 if (getstats(&total
, &used
)) {
175 return bprintf("%d", 100 * used
/ total
);
183 if (getstats(&total
, &used
)) {
187 return fmt_human(total
* 1024, 1024);
195 if (getstats(&total
, &used
)) {
199 return fmt_human(used
* 1024, 1024);