2 * mem_openbsd.c - module to get memory/swap usages in percent, for OpenBSD
4 * Copyright (c) 2001 Seiichi SATO <ssato@sh.rim.or.jp>
6 * licensed under the GPL
19 #include <sys/param.h>
20 #include <sys/sysctl.h>
23 /* initialize function */
34 struct swapent
*swap_dev
;
40 num_swap
= swapctl(SWAP_NSWAP
, 0, 0);
44 swap_dev
= malloc(num_swap
* sizeof(*swap_dev
));
48 if (swapctl(SWAP_STATS
, swap_dev
, num_swap
) == -1)
51 for (i
= 0; i
< num_swap
; i
++) {
52 if (swap_dev
[i
].se_flags
& SWF_ENABLE
) {
53 stotal
+= swap_dev
[i
].se_nblks
;
54 sused
+= swap_dev
[i
].se_inuse
;
63 return (100 * (double) sused
/ (double) stotal
);
67 /* return mem/swap usage in percent 0 to 100 */
69 mem_getusage(int *per_mem
, int *per_swap
, const struct mem_options
*opts
)
73 size_t size
= sizeof(vm
);
74 static int mib
[] = { CTL_VM
, VM_METER
};
77 if (sysctl(mib
, 2, &vm
, &size
, NULL
, 0) < 0)
78 bzero(&vm
, sizeof(vm
));
80 /* calc mem usage in percent */
82 *per_mem
= 100 * (double) vm
.t_rm
/ (double) (vm
.t_rm
+ vm
.t_free
);
88 *per_swap
= get_swap_usage();
91 printf("t_rm total real memory in use %6d\n", vm
.t_rm
);
92 printf("t_arm active real memory %6d\n", vm
.t_arm
);
93 printf("t_rmshr shared real memory %6d\n", vm
.t_rmshr
);
94 printf("t_armshr active shared real memory %6d\n", vm
.t_armshr
);
95 printf("t_free free memory pages %6d\n", vm
.t_free
);