wmmemload: Use sysctl to get memory usage in FreeBSD.
Previously, kvm_read was used to get the memory usage in FreeBSD. However, this has been deprecated in favor of sysctl. This patch implements this change. Some work had previously been done in this direction, as could be seen in the file src/mem_freebsd_sysctl.c. Since this file is no longer needed, it has been removed. Additionally, some formatting has been done on the remaining older code for a more consistent style.
This commit is contained in:
parent
ba72863015
commit
a473280ab9
|
@ -14,7 +14,6 @@ wmmemload_SOURCES = \
|
||||||
EXTRA_wmmemload_SOURCES = \
|
EXTRA_wmmemload_SOURCES = \
|
||||||
mem_linux.c \
|
mem_linux.c \
|
||||||
mem_freebsd.c \
|
mem_freebsd.c \
|
||||||
mem_freebsd_sysctl.c \
|
|
||||||
mem_openbsd.c \
|
mem_openbsd.c \
|
||||||
mem_solaris.c
|
mem_solaris.c
|
||||||
|
|
||||||
|
|
|
@ -13,116 +13,132 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "mem.h"
|
#include "mem.h"
|
||||||
|
|
||||||
#include <kvm.h>
|
#include <kvm.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/vmmeter.h>
|
#include <sys/errno.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
static kvm_t *kvm_data = NULL;
|
static kvm_t *kvm_data;
|
||||||
static int pageshift;
|
|
||||||
static struct nlist nlst[] = { {"_cp_time"}, {"_cnt"}, {0} };
|
|
||||||
|
|
||||||
/* initialize function */
|
/* initialize function */
|
||||||
void mem_init(void)
|
void mem_init(void)
|
||||||
{
|
{
|
||||||
int pagesize = getpagesize();
|
kvm_data = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open");
|
||||||
pageshift = 0;
|
|
||||||
|
|
||||||
while (pagesize > 1) {
|
if (kvm_data == NULL) {
|
||||||
pageshift++;
|
fprintf(stderr, "can't open kernel virtual memory");
|
||||||
pagesize >>= 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
kvm_data = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open");
|
/* drop setgid & setuid (the latter should not be there really) */
|
||||||
|
seteuid(getuid());
|
||||||
|
setegid(getgid());
|
||||||
|
|
||||||
if (kvm_data == NULL) {
|
if (geteuid() != getuid() || getegid() != getgid()) {
|
||||||
fprintf(stderr, "can't open kernel virtual memory");
|
fprintf(stderr, "unable to drop privileges");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
kvm_nlist(kvm_data, nlst);
|
|
||||||
|
|
||||||
if (nlst[0].n_type == 0 || nlst[1].n_type == 0) {
|
return;
|
||||||
fprintf(stderr, "error extracting symbols");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
/* drop setgid & setuid (the latter should not be there really) */
|
|
||||||
seteuid(getuid());
|
|
||||||
setegid(getgid());
|
|
||||||
|
|
||||||
if (geteuid() != getuid() || getegid() != getgid()) {
|
|
||||||
fprintf(stderr, "unable to drop privileges");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
|
||||||
|
|
||||||
|
static void getsysctl(const char *name, void *ptr, size_t len)
|
||||||
|
{
|
||||||
|
size_t nlen = len;
|
||||||
|
|
||||||
|
if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
|
||||||
|
fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name,
|
||||||
|
strerror(errno));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (nlen != len) {
|
||||||
|
fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n",
|
||||||
|
name, (unsigned long)len, (unsigned long)nlen);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* return mem/swap usage in percent 0 to 100 */
|
/* return mem/swap usage in percent 0 to 100 */
|
||||||
void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
|
void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
|
||||||
{
|
{
|
||||||
struct vmmeter vm;
|
u_int mtotal, mwired, mcached, mfree, mused;
|
||||||
int bufspace;
|
|
||||||
static int swap_firsttime = 1;
|
|
||||||
static int swappgsin = -1;
|
|
||||||
static int swappgsout = -1;
|
|
||||||
static int swapmax = 0, swapused = 0;
|
|
||||||
time_t cur_time;
|
|
||||||
static time_t last_time_swap = 0;
|
|
||||||
u_int mused;
|
|
||||||
|
|
||||||
/* get mem usage */
|
|
||||||
if (kvm_read(kvm_data, nlst[0].n_value, &bufspace, sizeof(bufspace)) !=
|
|
||||||
sizeof(bufspace))
|
|
||||||
exit(1);
|
|
||||||
if (kvm_read(kvm_data, nlst[1].n_value, &vm, sizeof(vm)) != sizeof(vm))
|
|
||||||
exit(1);
|
|
||||||
|
|
||||||
/* get swap usage */
|
|
||||||
/* only calculate when first time or when changes took place */
|
|
||||||
/* do not call it more than 1 time per 2 seconds */
|
|
||||||
/* otherwise it can eat up to 50% of CPU time on heavy swap activity */
|
|
||||||
cur_time = time(NULL);
|
|
||||||
if (swap_firsttime ||
|
|
||||||
(((vm.v_swappgsin > swappgsin) || (vm.v_swappgsout > swappgsout))
|
|
||||||
&& cur_time > last_time_swap + 1)) {
|
|
||||||
|
|
||||||
struct kvm_swap swap;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
swapmax = 0;
|
|
||||||
swapused = 0;
|
|
||||||
|
|
||||||
n = kvm_getswapinfo(kvm_data, &swap, 1, 0);
|
|
||||||
if (n >= 0 && swap.ksw_total != 0) {
|
|
||||||
swapmax = swap.ksw_total;
|
|
||||||
swapused = swap.ksw_used;
|
|
||||||
}
|
|
||||||
|
|
||||||
swap_firsttime = 0;
|
|
||||||
last_time_swap = cur_time;
|
|
||||||
}
|
|
||||||
swappgsin = vm.v_swappgsin;
|
|
||||||
swappgsout = vm.v_swappgsout;
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf ("-------------------\n");
|
u_int pagesize;
|
||||||
printf ("total:%10d\n", vm.v_page_count * vm.v_page_size);
|
#endif
|
||||||
printf ("free :%10d\n", vm.v_free_count * vm.v_page_size);
|
u_int new_swappgsin, new_swappgsout;
|
||||||
printf ("act :%10d\n", vm.v_active_count * vm.v_page_size);
|
static int swap_firsttime = 1;
|
||||||
printf ("inact:%10d\n", vm.v_inactive_count * vm.v_page_size);
|
static int swappgsin = -1;
|
||||||
printf ("wired:%10d\n", vm.v_wire_count * vm.v_page_size);
|
static int swappgsout = -1;
|
||||||
printf ("cache:%10d\n", vm.v_cache_count * vm.v_page_size);
|
static int swapmax, swapused;
|
||||||
printf ("-------------------\n");
|
time_t cur_time;
|
||||||
|
static time_t last_time_swap;
|
||||||
|
|
||||||
|
|
||||||
|
/* get mem usage */
|
||||||
|
GETSYSCTL("vm.stats.vm.v_page_count", mtotal);
|
||||||
|
GETSYSCTL("vm.stats.vm.v_wire_count", mwired);
|
||||||
|
GETSYSCTL("vm.stats.vm.v_cache_count", mcached);
|
||||||
|
GETSYSCTL("vm.stats.vm.v_free_count", mfree);
|
||||||
|
#ifdef DEBUG
|
||||||
|
GETSYSCTL("hw.pagesize", pagesize);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* calc mem/swap usage in percent */
|
/* get swap usage */
|
||||||
mused = vm.v_page_count - vm.v_free_count;
|
/* only calculate when first time or when changes took place */
|
||||||
if (opts->ignore_wired) mused -= vm.v_wire_count;
|
/* do not call it more than 1 time per 2 seconds */
|
||||||
if (opts->ignore_cached) mused -= vm.v_cache_count;
|
/* otherwise it can eat up to 50% of CPU time on heavy swap activity */
|
||||||
|
cur_time = time(NULL);
|
||||||
|
|
||||||
*per_mem = 100 * (double) mused / (double) vm.v_page_count;
|
GETSYSCTL("vm.stats.vm.v_swappgsin", new_swappgsin);
|
||||||
*per_swap = 100 * (double) swapused / (double) swapmax;
|
GETSYSCTL("vm.stats.vm.v_swappgsout", new_swappgsout);
|
||||||
|
|
||||||
if (*per_mem > 97) *per_mem = 100;
|
if (swap_firsttime ||
|
||||||
|
(((new_swappgsin > swappgsin) || (new_swappgsout > swappgsout))
|
||||||
|
&& cur_time > last_time_swap + 1)) {
|
||||||
|
|
||||||
|
struct kvm_swap swap;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
swapmax = 0;
|
||||||
|
swapused = 0;
|
||||||
|
|
||||||
|
n = kvm_getswapinfo(kvm_data, &swap, 1, 0);
|
||||||
|
if (n >= 0 && swap.ksw_total != 0) {
|
||||||
|
swapmax = swap.ksw_total;
|
||||||
|
swapused = swap.ksw_used;
|
||||||
|
}
|
||||||
|
|
||||||
|
swap_firsttime = 0;
|
||||||
|
last_time_swap = cur_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
swappgsin = new_swappgsin;
|
||||||
|
swappgsout = new_swappgsout;
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("-------------------\n");
|
||||||
|
printf("total:%10d\n", mtotal * pagesize);
|
||||||
|
printf("free :%10d\n", mfree * pagesize);
|
||||||
|
printf("wired:%10d\n", mwired * pagesize);
|
||||||
|
printf("cache:%10d\n", mcached * pagesize);
|
||||||
|
printf("-------------------\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* calc mem/swap usage in percent */
|
||||||
|
mused = mtotal - mfree;
|
||||||
|
if (opts->ignore_wired)
|
||||||
|
mused -= mwired;
|
||||||
|
if (opts->ignore_cached)
|
||||||
|
mused -= mcached;
|
||||||
|
*per_mem = 100 * (double) mused / (double) mtotal;
|
||||||
|
*per_swap = 100 * (double) swapused / (double) swapmax;
|
||||||
|
|
||||||
|
if (*per_mem > 97)
|
||||||
|
*per_mem = 100;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,63 +0,0 @@
|
||||||
/*
|
|
||||||
* mem_freebsd.c - module to get memory/swap usages in percent, for FreeBSD
|
|
||||||
*
|
|
||||||
* Copyright (c) 2001, 2002 Seiichi SATO <ssato@sh.rim.or.jp>
|
|
||||||
*
|
|
||||||
* licensed under the GPL
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include "config.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "mem.h"
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/sysctl.h>
|
|
||||||
#include <vm/vm_param.h>
|
|
||||||
#include <sys/vmmeter.h>
|
|
||||||
|
|
||||||
/* initialize function */
|
|
||||||
void mem_init(void)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* return mem/swap usage in percent 0 to 100 */
|
|
||||||
void mem_getusage(int *per_mem, int *per_swap, const struct mem_options *opts)
|
|
||||||
{
|
|
||||||
|
|
||||||
struct vmtotal vm;
|
|
||||||
size_t size = sizeof(vm);
|
|
||||||
static int mib[] = { CTL_VM, VM_METER };
|
|
||||||
|
|
||||||
/* get mem usage */
|
|
||||||
if (sysctl(mib, 2, &vm, &size, NULL, 0) < 0)
|
|
||||||
bzero(&vm, sizeof(vm));
|
|
||||||
|
|
||||||
/* calc mem usage in percent */
|
|
||||||
/* FIXME: the total usage(t_rm and t_free) is incorrect?? */
|
|
||||||
if (vm.t_rm > 0)
|
|
||||||
*per_mem = 100 * (double) vm.t_rm / (double) (vm.t_rm + vm.t_free);
|
|
||||||
if (*per_mem > 95)
|
|
||||||
*per_mem = 100;
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("t_vm total virtual memory %6lu\n", vm.t_vm);
|
|
||||||
printf("t_rm total real memory in use %6lu\n", vm.t_rm);
|
|
||||||
printf("t_arm active real memory %6lu\n", vm.t_arm);
|
|
||||||
printf("t_rmshr shared real memory %6lu\n", vm.t_rmshr);
|
|
||||||
printf("t_armshr active shared real memory %6lu\n", vm.t_armshr);
|
|
||||||
printf("t_free free memory pages %6lu\n", vm.t_free);
|
|
||||||
printf("--------------------------------------------\n");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* get swap usage */
|
|
||||||
/* not written yet.. can i get swap usage via sysctl? */
|
|
||||||
|
|
||||||
*per_swap = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
Loading…
Reference in a new issue