wmclockmon: update change-log
[dockapps.git] / wmmemload / src / mem_openbsd.c
blobe07866394d9c371f010105c494b51b6731ff3aab
1 /*
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
7 */
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "mem.h"
19 #include <sys/param.h>
20 #include <sys/sysctl.h>
21 #include <sys/swap.h>
23 /* initialize function */
24 void
25 mem_init(void)
27 return;
30 /* get swap usage */
31 static int
32 get_swap_usage(void)
34 struct swapent *swap_dev;
35 int num_swap;
36 int stotal, sused, i;
38 stotal = sused = 0;
40 num_swap = swapctl(SWAP_NSWAP, 0, 0);
41 if (num_swap == 0)
42 return 0;
44 swap_dev = malloc(num_swap * sizeof(*swap_dev));
45 if (swap_dev == NULL)
46 return 0;
48 if (swapctl(SWAP_STATS, swap_dev, num_swap) == -1)
49 return 0;
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;
58 free(swap_dev);
60 if (sused == 0)
61 return 0;
63 return (100 * (double) sused / (double) stotal);
67 /* return mem/swap usage in percent 0 to 100 */
68 void
69 mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
72 struct vmtotal vm;
73 size_t size = sizeof(vm);
74 static int mib[] = { CTL_VM, VM_METER };
76 /* get mem usage */
77 if (sysctl(mib, 2, &vm, &size, NULL, 0) < 0)
78 bzero(&vm, sizeof(vm));
80 /* calc mem usage in percent */
81 if (vm.t_rm > 0)
82 *per_mem = 100 * (double) vm.t_rm / (double) (vm.t_rm + vm.t_free);
84 if (*per_mem > 97)
85 *per_mem = 100;
87 /* swap */
88 *per_swap = get_swap_usage();
90 #ifdef DEBUG
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);
96 #endif
98 return;