SUMMARY: Getting free swap space

From: Andrew Torda (torda@igc.ethz.ch)
Date: Mon Oct 14 1991 - 06:00:42 CDT


The original question was how do I get the kernel to tell me the
amount of swap space left on a machine (under sunOs 4.1.1).
I complained that there were missing elements in the vmtotal structure
defined by <sys/vmmeter.h>.

The answer was given by Mike.Sullivan@EBay.Sun.com who said:

> Well, if you want to do what '/usr/etc/pstat -s' does, you can look
> at the 'anoninfo' variable, which is an anoninfo structure in <vm/anon.h>

If anyone is interested in more detail, the following 30 line example
fleshes out Mike's answer. The example must be linked like
cc example.c -lkvm
and run as root or set group kmem or similar to allow reading from the
kernel. Hit <n> now if you are not interested.
-------------------- example.c --------------------
/* Example for getting out swap info, cc -lkvm, run as root or setgid kmem */
#include <fcntl.h>
#include <nlist.h>
#include <stdio.h>
#include <kvm.h>
#include <vm/anon.h>
#define ptok (unsigned)4 /* pages to K */
int main () {
    struct nlist nl[2];
    struct anoninfo a_info;
    kvm_t *kd;
    int result;
    u_int v_max, v_used, v_free;
    static char s_anon[] = "_anoninfo";
    if ((kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "test_prog")) == NULL) {
        fprintf (stderr, "kernel open fail\n"); exit (1);
    }
    nl [0].n_name = s_anon;
    nl [1].n_name = (char *)NULL;
    if (kvm_nlist (kd, nl) != 0) {
        fprintf (stderr, "kvm_nlist failed.\n"); exit (1);
    }
    result = kvm_read (kd, nl[0].n_value, &a_info, sizeof( a_info));
    if (result != sizeof (a_info)) {
        fprintf (stderr, "kvm_read failure.\n"); exit (1);
    }
    v_max = a_info.ani_max *ptok;
    v_used = a_info.ani_resv *ptok;
    v_free = ( v_max - v_used) ;
    printf ( "max %uk, used %uk, free %uk\n", v_max, v_used, v_free);
    kvm_close (kd);
    return 0;
}
-------------------- end example.c --------------------

-- 
Andrew Torda, Computational Chemistry, ETH, Zurich, torda@igc.ethz.ch



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:06:18 CDT