Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
14 if (pscanf("/proc/meminfo",
17 "MemAvailable: %ju kB\n",
18 &free
, &free
, &free
) != 3) {
22 return fmt_human(free
* 1024, 1024);
28 uintmax_t total
, free
, buffers
, cached
;
30 if (pscanf("/proc/meminfo",
33 "MemAvailable: %ju kB\n"
36 &total
, &free
, &buffers
, &buffers
, &cached
) != 5) {
44 return bprintf("%d", 100 * ((total
- free
) - (buffers
+ cached
))
53 if (pscanf("/proc/meminfo", "MemTotal: %ju kB\n", &total
)
58 return fmt_human(total
* 1024, 1024);
64 uintmax_t total
, free
, buffers
, cached
;
66 if (pscanf("/proc/meminfo",
69 "MemAvailable: %ju kB\n"
72 &total
, &free
, &buffers
, &buffers
, &cached
) != 5) {
76 return fmt_human((total
- free
- buffers
- cached
) * 1024,
79 #elif defined(__OpenBSD__)
81 #include <sys/sysctl.h>
82 #include <sys/types.h>
86 #define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024))
89 load_uvmexp(struct uvmexp
*uvmexp
)
91 int uvmexp_mib
[] = {CTL_VM
, VM_UVMEXP
};
94 size
= sizeof(*uvmexp
);
96 if (sysctl(uvmexp_mib
, 2, uvmexp
, &size
, NULL
, 0) >= 0) {
106 struct uvmexp uvmexp
;
109 if (load_uvmexp(&uvmexp
)) {
110 free_pages
= uvmexp
.npages
- uvmexp
.active
;
111 return fmt_human(pagetok(free_pages
, uvmexp
.pageshift
) *
121 struct uvmexp uvmexp
;
124 if (load_uvmexp(&uvmexp
)) {
125 percent
= uvmexp
.active
* 100 / uvmexp
.npages
;
126 return bprintf("%d", percent
);
135 struct uvmexp uvmexp
;
137 if (load_uvmexp(&uvmexp
)) {
138 return fmt_human(pagetok(uvmexp
.npages
,
139 uvmexp
.pageshift
) * 1024,
149 struct uvmexp uvmexp
;
151 if (load_uvmexp(&uvmexp
)) {
152 return fmt_human(pagetok(uvmexp
.active
,
153 uvmexp
.pageshift
) * 1024,
159 #elif defined(__FreeBSD__)
160 #include <sys/sysctl.h>
161 #include <sys/vmmeter.h>
163 #include <vm/vm_param.h>
167 struct vmtotal vm_stats
;
168 int mib
[] = {CTL_VM
, VM_TOTAL
};
171 len
= sizeof(struct vmtotal
);
172 if (sysctl(mib
, 2, &vm_stats
, &len
, NULL
, 0) == -1
176 return fmt_human(vm_stats
.t_free
* getpagesize(), 1024);
184 len
= sizeof(npages
);
185 if (sysctlbyname("vm.stats.vm.v_page_count", &npages
, &len
, NULL
, 0) == -1
189 return fmt_human(npages
* getpagesize(), 1024);
198 len
= sizeof(npages
);
199 if (sysctlbyname("vm.stats.vm.v_page_count", &npages
, &len
, NULL
, 0) == -1
203 if (sysctlbyname("vm.stats.vm.v_active_count", &active
, &len
, NULL
, 0) == -1
207 return bprintf("%d", active
* 100 / npages
);
215 len
= sizeof(active
);
216 if (sysctlbyname("vm.stats.vm.v_active_count", &active
, &len
, NULL
, 0) == -1
220 return fmt_human(active
* getpagesize(), 1024);