Remove trailing whitespace.
[dockapps.git] / wmmemload / src / mem_openbsd.c
blob00a5b09ac293b8c801d95d0a5b3fa97daa828777
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 if ((num_swap = swapctl(SWAP_NSWAP, 0, 0)) == 0)
41 return 0;
43 if ((swap_dev = malloc(num_swap * sizeof(*swap_dev))) == NULL)
44 return 0;
46 if (swapctl(SWAP_STATS, swap_dev, num_swap) == -1)
47 return 0;
49 for (i = 0; i < num_swap; i++) {
50 if (swap_dev[i].se_flags & SWF_ENABLE) {
51 stotal += swap_dev[i].se_nblks;
52 sused += swap_dev[i].se_inuse;
56 free(swap_dev);
58 if (sused == 0)
59 return 0;
61 return (100 * (double) sused / (double) stotal);
65 /* return mem/swap usage in percent 0 to 100 */
66 void
67 mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
70 struct vmtotal vm;
71 size_t size = sizeof(vm);
72 static int mib[] = { CTL_VM, VM_METER };
74 /* get mem usage */
75 if (sysctl(mib, 2, &vm, &size, NULL, 0) < 0)
76 bzero(&vm, sizeof(vm));
78 /* calc mem usage in percent */
79 if (vm.t_rm > 0)
80 *per_mem = 100 * (double) vm.t_rm / (double) (vm.t_rm + vm.t_free);
82 if (*per_mem > 97) *per_mem = 100;
84 /* swap */
85 *per_swap = get_swap_usage();
87 #ifdef DEBUG
88 printf("t_rm total real memory in use %6d\n", vm.t_rm);
89 printf("t_arm active real memory %6d\n", vm.t_arm);
90 printf("t_rmshr shared real memory %6d\n", vm.t_rmshr);
91 printf("t_armshr active shared real memory %6d\n", vm.t_armshr);
92 printf("t_free free memory pages %6d\n", vm.t_free);
93 #endif
95 return;